瀏覽代碼

使用decimal实现了best input number的计算,准备测试

skyfffire 2 年之前
父節點
當前提交
73727da3da
共有 4 個文件被更改,包括 72 次插入0 次删除
  1. 64 0
      arbitrage/calc_utils.go
  2. 5 0
      arbitrage/history.go
  3. 1 0
      go.mod
  4. 2 0
      go.sum

+ 64 - 0
arbitrage/calc_utils.go

@@ -0,0 +1,64 @@
+package arbitrage
+
+import (
+	"fmt"
+	"github.com/shopspring/decimal"
+)
+
+func SimulationLevel2Path(path Path) {
+	if path.Level != 2 {
+		HistoryError(fmt.Sprintf("Level error. Need %v, but got %v.", 2, path.Level))
+
+		return
+	}
+
+	fm0 := path.FmList[0]
+	fm1 := path.FmList[1]
+
+	// fee0, fee1
+	fee0 := decimal.NewFromInt(fm0.Fee)
+	fee1 := decimal.NewFromInt(fm1.Fee)
+
+	decimalNumber1 := decimal.NewFromInt(1)
+	decimalNumber1e6 := decimal.NewFromInt(1).Pow(decimal.NewFromInt(6))
+
+	// F0 = 1 - fee0/1e6
+	F0 := decimalNumber1.Sub(fee0.Div(decimalNumber1e6))
+	// F1 = 1 - fee1/1e6
+	F1 := decimalNumber1.Sub(fee1.Div(decimalNumber1e6))
+
+	// B0, S0
+	B0, _ := decimal.NewFromString(fm0.InReserve.String())
+	S0, _ := decimal.NewFromString(fm0.OutReserve.String())
+	// B1, S1
+	B1, _ := decimal.NewFromString(fm1.InReserve.String())
+	S1, _ := decimal.NewFromString(fm1.OutReserve.String())
+
+	// F0 * F1 * S0
+	part00 := F0.Mul(F1.Mul(S0))
+	// B1 * F0
+	part01 := B1.Mul(F0)
+	// F0 * F1 * S0 + B1 * F0
+	part0 := part00.Add(part01)
+
+	// if F0*F1*S0 + B1*F0 == 0
+	if part0.IntPart() == 0 {
+		HistoryError(fmt.Sprintf("Is error part0. F0=%v, F1=%v, S0=%v, B1=%v, F0=%v.", F0, F1, S0, B1, F0))
+
+		return
+	}
+
+	// B0 * B1
+	part10 := B0.Mul(B1)
+	// F0 * F1
+	part11 := F0.Mul(F1)
+	// S0 * S1
+	part12 := S0.Mul(S1)
+	// B0 * B1 * F0 * F1 * S0 * S1 ** 0.5
+	part1 := part10.Mul(part11.Mul(part12)).Pow(decimal.NewFromFloat(0.5)).Sub(part10)
+
+	// best input number
+	I := part1.Div(part0)
+
+	HistoryInfo(fmt.Sprintf("part0=%v, part1=%v, I=%v.", part0, part1, I))
+}

+ 5 - 0
arbitrage/history.go

@@ -136,6 +136,11 @@ func (h *HistoryArbitrage) OnTick() {
 	PutR0R1ToLpList(&h.finalLpList, h.finalLpBalanceMapping)
 	// 生成最终交易路由
 	h.pathList = ParseLpListToPathList(h.finalLpList, MaxLevel)
+	// 计算利润
+	// 根据利润排序
+	// 发交易
+	// Gas War
+	// TODO 获取fee
 
 	time.Sleep(time.Second)
 }

+ 1 - 0
go.mod

@@ -90,6 +90,7 @@ require (
 	github.com/opentracing/opentracing-go v1.1.0 // indirect
 	github.com/pkg/errors v0.9.1 // indirect
 	github.com/pmezard/go-difflib v1.0.0 // indirect
+	github.com/shopspring/decimal v1.3.1 // indirect
 	github.com/tklauser/go-sysconf v0.3.5 // indirect
 	github.com/tklauser/numcpus v0.2.2 // indirect
 	golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect

+ 2 - 0
go.sum

@@ -381,6 +381,8 @@ github.com/segmentio/kafka-go v0.2.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfP
 github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
 github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible h1:Bn1aCHHRnjv4Bl16T8rcaFjYSrGrIZvpiGO6P3Q4GpU=
 github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
+github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
+github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
 github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
 github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
 github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=