Pārlūkot izejas kodu

利润计算没有问题?了

skyfffire 2 gadi atpakaļ
vecāks
revīzija
232077b526

+ 7 - 2
arbitrage/calc_utils.go

@@ -68,9 +68,11 @@ func SimulationLevel2Path(path *Path) {
 	partB := partB0.Sub(B0.Mul(B1))
 
 	// best input number
-	I := partB.Div(partX).Truncate(0)
+	amountIn := partB.Div(partX).Truncate(0)
+	if amountIn.LessThan(decimal.Zero) {
+		return
+	}
 
-	amountIn := I
 	amountOut0 := CalcOutByIn(amountIn, B0, S0, fee0)
 	amountOut1 := CalcOutByIn(amountOut0, B1, S1, fee1)
 	profit := amountOut1.Sub(amountIn)
@@ -152,6 +154,9 @@ func SimulationLevel3Path(path *Path) {
 	partA1 := B0.Mul(B1).Mul(B2)
 	// ((B0 * B1 * B2 * F0 * F1 * F2 * S0 * S1 * S2) ** 0.5 - B0 * B1 * B2) / F0 * F1 * F2 * S0 * S1 + B2 * F0 * F1 * S0 + B1 * B2 * F0
 	amountIn := partA0.Sub(partA1).Div(partX).Truncate(0)
+	if amountIn.LessThan(decimal.Zero) {
+		return
+	}
 	//HistoryInfo(fmt.Sprintf("amountIn=%v", amountIn))
 
 	decimalNumber2 := decimal.NewFromInt(2)

+ 9 - 3
arbitrage/calc_utils_test.go

@@ -1,7 +1,6 @@
 package arbitrage
 
 import (
-	"fmt"
 	"github.com/shopspring/decimal"
 	"math/big"
 	"testing"
@@ -45,8 +44,15 @@ func TestSimulationLevel2Path(t *testing.T) {
 	}
 
 	SimulationLevel2Path(&path2)
+	t.Logf("%+v\n", path2.Sim.ToJsonString())
 
-	HistoryInfo(fmt.Sprintf("%+v", path2.Sim.ToJsonString()))
+	// path数组装填测试
+	path2Copy := path2
+	pathList := []Path{path2, path2Copy}
+	PutSimulationToPathList(pathList)
+	for _, path := range pathList {
+		t.Logf("level=%+v, Sim=%+v, LoanLp=%+v", path.Level, path.Sim.ToJsonString(), path.LoanLp)
+	}
 }
 
 func TestSimulationLevel3Path(t *testing.T) {
@@ -102,5 +108,5 @@ func TestSimulationLevel3Path(t *testing.T) {
 
 	SimulationLevel3Path(&path3)
 
-	HistoryInfo(fmt.Sprintf("%+v", path3.Sim.ToJsonString()))
+	t.Logf("%+v\n", path3.Sim.ToJsonString())
 }

+ 1 - 1
arbitrage/config.go

@@ -11,7 +11,7 @@ var V2ToolsContract = Contract{
 /* lp 相关 */
 
 // MaxLpLength 从数据库取出的最大lp数量
-var MaxLpLength = uint64(2000)
+var MaxLpLength = uint64(5000)
 
 // MaxLevel 最大path等级
 var MaxLevel = int64(3)

+ 4 - 2
arbitrage/history.go

@@ -157,18 +157,20 @@ func (h *HistoryArbitrage) OnTick() {
 	h.pathList = ParseLpListToPathList(h.finalLpList, MaxLevel)
 	// 计算利润
 	PutSimulationToPathList(h.pathList)
+	// 排除交易中profit不满足的path
+	h.pathList = GetFinalPathList(h.pathList)
 	// 根据利润排序
 	InsertionSort(h.pathList)
 	for _, path := range h.pathList {
 		if path.Profit.GreaterThan(decimal.NewFromInt(0)) {
-			HistoryInfo(fmt.Sprintf("level=%+v, Sim=%+v, LoanLp=%+v", path.Level, path.Sim.ToJsonString(), path.LoanLp))
+			HistoryInfo(fmt.Sprintf("level=%+v, Sim=%+v, lpList=%+v", path.Level, path.Sim.ToJsonString(), path.LpHashList))
 		}
 	}
 	// 发交易
 	// Gas War
 	// TODO 获取fee
 
-	time.Sleep(time.Second)
+	time.Sleep(100 * time.Second)
 }
 
 func (h *HistoryArbitrage) Stop() error {

+ 14 - 1
arbitrage/lp_utils.go

@@ -55,13 +55,26 @@ func InsertionSort(paths []Path) {
 }
 
 func PutSimulationToPathList(pathList []Path) {
-	for _, path := range pathList {
+	for index, path := range pathList {
 		if path.Level == 2 {
 			SimulationLevel2Path(&path)
 		} else if path.Level == 3 {
 			SimulationLevel3Path(&path)
 		}
+
+		pathList[index] = path
+	}
+}
+
+func GetFinalPathList(pathList []Path) []Path {
+	var finalPathList []Path
+	for _, path := range pathList {
+		if path.Profit.GreaterThan(decimal.Zero) {
+			finalPathList = append(finalPathList, path)
+		}
 	}
+
+	return finalPathList
 }
 
 func GenerateMapping(lpList []api.V2Lp) map[string][]api.V2Lp {