Browse Source

所有前置变量都已经封装完毕,可以进入利润计算流程了

skyfffire 2 years ago
parent
commit
5cd944f55c
3 changed files with 108 additions and 49 deletions
  1. 4 0
      arbitrage/api/base_structs.go
  2. 16 29
      arbitrage/history.go
  3. 88 20
      arbitrage/lp_utils.go

+ 4 - 0
arbitrage/api/base_structs.go

@@ -1,5 +1,7 @@
 package api
 
+import "math/big"
+
 type Auth struct {
 	AuthStr   string `json:"auth"`
 	Timestamp string `json:"timestamp"`
@@ -48,6 +50,8 @@ type V2Lp struct {
 	//Other           string `json:"other"`
 	//AppendTimestamp int64  `json:"appendTimestamp"`
 	//UpdateTimestamp int64  `json:"updateTimestamp"`
+	R0 *big.Int
+	R1 *big.Int
 }
 
 type V2LpListByChainIdAndPaginateResult struct {

+ 16 - 29
arbitrage/history.go

@@ -6,9 +6,8 @@ import (
 	"github.com/ethereum/go-ethereum/arbitrage/api"
 	"github.com/ethereum/go-ethereum/internal/ethapi"
 	"github.com/ethereum/go-ethereum/node"
-	"github.com/ethereum/go-ethereum/params"
 	"gopkg.in/urfave/cli.v1"
-	"reflect"
+	"math/big"
 	"time"
 )
 
@@ -28,7 +27,11 @@ type HistoryArbitrage struct {
 	txApi         *ethapi.PublicTransactionPoolAPI
 	javaApi       *api.JavaApi
 	// 程序逻辑
-	lpList []api.V2Lp
+	lpList                []api.V2Lp
+	finalLpList           []api.V2Lp
+	finalLpHashList       []string
+	finalLpBalanceMapping map[string][2]*big.Int
+	pathList              []Path
 }
 
 func RegisterHistoryArbitrage(stack *node.Node, ctx *cli.Context, apiBackend ethapi.Backend) {
@@ -122,33 +125,17 @@ running:
 	}
 }
 
-func (h *HistoryArbitrage) GetLpList() []api.V2Lp {
-	rst, err := h.javaApi.V2LpListByChainIdAndPaginate(api.V2LpListByChainIdAndPaginateRequest{
-		ChainId:    params.CoreChainConfig.ChainID.Uint64(),
-		PageNumber: 1,
-		PageSize:   MaxLpLength,
-		AuthObj:    api.GenerateAuth(),
-	})
-
-	if err != nil {
-		HistoryError("Get lp list error.", err.Error())
-		return nil
-	}
-
-	if !rst.State {
-		HistoryError("Get lp list java error.", err.Error())
-		return nil
-	}
-
-	return rst.Data
-}
-
 func (h *HistoryArbitrage) OnTick() {
-	lpHashList := ParseLpListToLpHashList(h.lpList)
-
-	HistoryInfo("Before")
-	result := h.getPairSBalance(lpHashList)
-	HistoryInfo("After", reflect.TypeOf(result).String())
+	// 生成最终lp
+	h.finalLpList = ParseLpToFinalLp(h.lpList)
+	// 获取纯hash list
+	h.finalLpHashList = ParseLpListToLpHashList(h.finalLpList)
+	// 获取余额
+	h.finalLpBalanceMapping = h.getPairSBalance(h.finalLpHashList)
+	// 将获取到的余额映射到LP里面
+	PutR0R1ToLpList(&h.finalLpList, h.finalLpBalanceMapping)
+	// 生成最终交易路由
+	h.pathList = ParseLpListToPathList(h.finalLpList, MaxLevel)
 
 	time.Sleep(time.Second)
 }

+ 88 - 20
arbitrage/lp_utils.go

@@ -3,22 +3,29 @@ package arbitrage
 import (
 	"github.com/ethereum/go-ethereum/arbitrage/api"
 	"github.com/ethereum/go-ethereum/log"
+	"github.com/ethereum/go-ethereum/params"
+	"math/big"
 )
 
 // FundMovements 池子资金动向
 type FundMovements struct {
-	LpHash   string
-	Fee      int64
-	InIndex  uint64
-	InToken  string
-	OutIndex uint64
-	OutToken string
+	LpHash string
+	Fee    int64
+
+	InIndex   uint64
+	InToken   string
+	InReserve *big.Int
+
+	OutIndex   uint64
+	OutToken   string
+	OutReserve *big.Int
 }
 
 // Path 交易路径
 type Path struct {
 	LoanTokenHash string
 	Level         int64
+	Profit        int64
 	FmList        []FundMovements
 }
 
@@ -90,21 +97,29 @@ func GeneratePathListCore(finalMapping map[string][]api.V2Lp, level int64, maxLe
 		// 3.2 构造FundMovements,必须要
 		if lastFm.OutToken == lp.Token0 {
 			newPath.FmList = append(path.FmList, FundMovements{
-				LpHash:   lp.Hash,
-				Fee:      lp.Fee,
-				InIndex:  0,
-				InToken:  lp.Token0,
-				OutIndex: 1,
-				OutToken: lp.Token1,
+				LpHash: lp.Hash,
+				Fee:    lp.Fee,
+
+				InIndex:   0,
+				InToken:   lp.Token0,
+				InReserve: lp.R0,
+
+				OutIndex:   1,
+				OutToken:   lp.Token1,
+				OutReserve: lp.R1,
 			})
 		} else if lastFm.OutToken == lp.Token1 {
 			newPath.FmList = append(path.FmList, FundMovements{
-				LpHash:   lp.Hash,
-				Fee:      lp.Fee,
-				InIndex:  1,
-				InToken:  lp.Token1,
-				OutIndex: 0,
-				OutToken: lp.Token0,
+				LpHash: lp.Hash,
+				Fee:    lp.Fee,
+
+				InIndex:   1,
+				InToken:   lp.Token1,
+				InReserve: lp.R1,
+
+				OutIndex:   0,
+				OutToken:   lp.Token0,
+				OutReserve: lp.R0,
 			})
 		} else {
 			log.Error("path生成有错误,请重新debug查询。", "lp hash", lp.Hash)
@@ -128,7 +143,18 @@ func GeneratePathList(finalLpList []api.V2Lp, finalMapping map[string][]api.V2Lp
 			LoanTokenHash: lp.Token1,
 			Level:         NowLevel,
 			FmList: []FundMovements{
-				{LpHash: lp.Hash, Fee: lp.Fee, InIndex: 0, InToken: lp.Token0, OutIndex: 1, OutToken: lp.Token1},
+				{
+					LpHash: lp.Hash,
+					Fee:    lp.Fee,
+
+					InIndex:    0,
+					InToken:    lp.Token0,
+					OutReserve: lp.R0,
+
+					OutIndex:  1,
+					OutToken:  lp.Token1,
+					InReserve: lp.R1,
+				},
 			},
 		}
 		pathListA := GeneratePathListCore(finalMapping, NowLevel+1, maxLevel, pathA)
@@ -136,7 +162,18 @@ func GeneratePathList(finalLpList []api.V2Lp, finalMapping map[string][]api.V2Lp
 			LoanTokenHash: lp.Token0,
 			Level:         NowLevel,
 			FmList: []FundMovements{
-				{LpHash: lp.Hash, Fee: lp.Fee, InIndex: 1, InToken: lp.Token1, OutIndex: 0, OutToken: lp.Token0},
+				{
+					LpHash: lp.Hash,
+					Fee:    lp.Fee,
+
+					InIndex:   1,
+					InToken:   lp.Token1,
+					InReserve: lp.R1,
+
+					OutIndex:   0,
+					OutToken:   lp.Token0,
+					OutReserve: lp.R0,
+				},
 			},
 		}
 		pathListB := GeneratePathListCore(finalMapping, NowLevel+1, maxLevel, pathB)
@@ -181,3 +218,34 @@ func ParseLpListToLpHashList(lpList []api.V2Lp) []string {
 
 	return rst
 }
+
+func (h *HistoryArbitrage) GetLpList() []api.V2Lp {
+	rst, err := h.javaApi.V2LpListByChainIdAndPaginate(api.V2LpListByChainIdAndPaginateRequest{
+		ChainId:    params.CoreChainConfig.ChainID.Uint64(),
+		PageNumber: 1,
+		PageSize:   MaxLpLength,
+		AuthObj:    api.GenerateAuth(),
+	})
+
+	if err != nil {
+		HistoryError("Get lp list error.", err.Error())
+		return nil
+	}
+
+	if !rst.State {
+		HistoryError("Get lp list java error.", err.Error())
+		return nil
+	}
+
+	return rst.Data
+}
+
+func PutR0R1ToLpList(lpList *[]api.V2Lp, balanceMapping map[string][2]*big.Int) {
+	for i := range *lpList {
+		lp := &((*lpList)[i])
+
+		reserves := balanceMapping[lp.Hash]
+		lp.R0 = reserves[0]
+		lp.R1 = reserves[1]
+	}
+}