Browse Source

利润获取完了,排序完了

skyfffire 2 years ago
parent
commit
3c57167f0f
2 changed files with 48 additions and 18 deletions
  1. 12 12
      arbitrage/history.go
  2. 36 6
      arbitrage/lp_utils.go

+ 12 - 12
arbitrage/history.go

@@ -141,18 +141,18 @@ func (h *HistoryArbitrage) OnTick() {
 	h.finalLpList = FilterLpList(tempLpList)
 	// 生成最终交易路由
 	h.pathList = ParseLpListToPathList(h.finalLpList, MaxLevel)
-	//// 计算利润
-	//PutSimulationToPathList(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))
-	//	}
-	//}
-	//// 发交易
-	//// Gas War
-	//// TODO 获取fee
+	// 计算利润
+	PutSimulationToPathList(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))
+		}
+	}
+	// 发交易
+	// Gas War
+	// TODO 获取fee
 
 	time.Sleep(time.Second)
 }

+ 36 - 6
arbitrage/lp_utils.go

@@ -5,7 +5,6 @@ import (
 	"github.com/ethereum/go-ethereum/log"
 	"github.com/ethereum/go-ethereum/params"
 	"github.com/shopspring/decimal"
-	"math/big"
 )
 
 // FundMovements 池子资金动向
@@ -15,11 +14,11 @@ type FundMovements struct {
 
 	InIndex   uint64
 	InToken   string
-	InReserve *big.Int
+	InReserve decimal.Decimal
 
 	OutIndex   uint64
 	OutToken   string
-	OutReserve *big.Int
+	OutReserve decimal.Decimal
 }
 
 // Path 交易路径
@@ -38,6 +37,7 @@ type Path struct {
 	FmList        []FundMovements
 }
 
+// InsertionSort 不适合大量数据的排序,暂时可以先用着
 func InsertionSort(paths []Path) {
 	for i := 1; i < len(paths); i++ {
 		key := paths[i]
@@ -54,6 +54,16 @@ func InsertionSort(paths []Path) {
 	}
 }
 
+func PutSimulationToPathList(pathList []Path) {
+	for _, path := range pathList {
+		if path.Level == 2 {
+			SimulationLevel2Path(&path)
+		} else if path.Level == 3 {
+			SimulationLevel3Path(&path)
+		}
+	}
+}
+
 func GenerateMapping(lpList []api.V2Lp) map[string][]api.V2Lp {
 	mapping := make(map[string][]api.V2Lp)
 
@@ -135,6 +145,8 @@ func GeneratePathListCore(finalMapping map[string][]api.V2Lp, level int64, maxLe
 				OutToken:   lp.Token1,
 				OutReserve: lp.R1,
 			})
+
+			newPath.LpHashList = append(newPath.LpHashList, lp.Hash)
 		} else if lastFm.OutToken == lp.Token1 {
 			newPath.FmList = append(path.FmList, FundMovements{
 				LpHash: lp.Hash,
@@ -148,6 +160,8 @@ func GeneratePathListCore(finalMapping map[string][]api.V2Lp, level int64, maxLe
 				OutToken:   lp.Token0,
 				OutReserve: lp.R0,
 			})
+
+			newPath.LpHashList = append(newPath.LpHashList, lp.Hash)
 		} else {
 			log.Error("path生成有错误,请重新debug查询。", "lp hash", lp.Hash)
 
@@ -186,6 +200,7 @@ func GeneratePathList(finalLpList []api.V2Lp, finalMapping map[string][]api.V2Lp
 				},
 			},
 		}
+		pathA.LpHashList = append(pathA.LpHashList, lp.Hash)
 		pathListA := GeneratePathListCore(finalMapping, NowLevel+1, maxLevel, pathA)
 		pathB := Path{
 			Level:         NowLevel,
@@ -207,6 +222,7 @@ func GeneratePathList(finalLpList []api.V2Lp, finalMapping map[string][]api.V2Lp
 				},
 			},
 		}
+		pathB.LpHashList = append(pathB.LpHashList, lp.Hash)
 		pathListB := GeneratePathListCore(finalMapping, NowLevel+1, maxLevel, pathB)
 
 		pathList = append(pathList, pathListA...)
@@ -271,12 +287,26 @@ func (h *HistoryArbitrage) GetLpList() []api.V2Lp {
 	return rst.Data
 }
 
-func PutR0R1ToLpList(lpList *[]api.V2Lp, balanceMapping map[string][2]*big.Int) {
-	for i := range *lpList {
-		lp := &((*lpList)[i])
+func PutR0R1ToLpList(lpList []api.V2Lp, balanceMapping map[string][2]decimal.Decimal) {
+	for i := range lpList {
+		lp := &(lpList[i])
 
 		reserves := balanceMapping[lp.Hash]
 		lp.R0 = reserves[0]
 		lp.R1 = reserves[1]
 	}
 }
+
+func FilterLpList(tempLpList []api.V2Lp) []api.V2Lp {
+	var finalLp []api.V2Lp
+
+	for _, lp := range tempLpList {
+		if lp.R0.Equals(decimal.Zero) || lp.R1.Equals(decimal.Zero) {
+			continue
+		}
+
+		finalLp = append(finalLp, lp)
+	}
+
+	return finalLp
+}