Quellcode durchsuchen

使用结构体发送交易,最简可以只写from和to

skyfffire vor 2 Jahren
Ursprung
Commit
0ef68e9bc1
2 geänderte Dateien mit 52 neuen und 23 gelöschten Zeilen
  1. 48 22
      arbitrage/api.go
  2. 4 1
      arbitrage/history.go

+ 48 - 22
arbitrage/api.go

@@ -43,36 +43,62 @@ func (h *HistoryArbitrage) callReturnRealResult(a abi.ABI, from string, to strin
 	return realResult, nil
 }
 
-func (h *HistoryArbitrage) sendTest(e *EasyWallet) {
+type ArbTx struct {
+	Nonce    int64
+	Gas      int64
+	GasPrice int64
+	Data     hexutil.Bytes
+	Value    big.Int
+	From     common.Address
+	To       common.Address
+}
+
+func (h *HistoryArbitrage) sendTransaction(arbTx *ArbTx) {
 	ctx := context.Background()
 	blockOrNbr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber)
 
-	// 获取nonce
-	nonce, err := h.txApi.GetTransactionCount(ctx, e.account.Address, blockOrNbr)
-	if err != nil {
-		HistoryError("Get transaction count error.", "error", err.Error())
-		return
+	// nonce
+	finalNonce := hexutil.Uint64(arbTx.Nonce)
+	if arbTx.Nonce == 0 {
+		nonce, err := h.txApi.GetTransactionCount(ctx, arbTx.From, blockOrNbr)
+		if err != nil {
+			HistoryError("Get transaction count error.", "error", err.Error())
+			return
+		}
+
+		finalNonce = *nonce
 	}
 
-	gasLimit := hexutil.Uint64(uint64(21000))
-	gasPrice, err := h.ethereumApi.GasPrice(ctx)
-	if err != nil {
-		HistoryError("Get gas price error.", "error", err.Error())
-		return
+	// gasLimit
+	finalGasLimit := hexutil.Uint64(arbTx.Gas)
+	if arbTx.Gas == 0 {
+		finalGasLimit = hexutil.Uint64(21000)
 	}
-	callData := hexutil.Bytes{}
-	value := hexutil.Big(*big.NewInt(1))
-	fromAddr := e.account.Address
-	toAddr := e.account.Address
 
+	// gasPrice
+	finalGasPrice := hexutil.Big(*big.NewInt(arbTx.GasPrice))
+	if arbTx.GasPrice == 0 {
+		gasPrice, err := h.ethereumApi.GasPrice(ctx)
+		if err != nil {
+			HistoryError("Get gas price error.", "error", err.Error())
+			return
+		}
+
+		finalGasPrice = *gasPrice
+	}
+
+	// value
+	finalValue := hexutil.Big(arbTx.Value)
+
+	// final tx
 	sendArgs := ethapi.SendTxArgs{
-		Nonce:    nonce,
-		Gas:      &gasLimit,
-		GasPrice: gasPrice,
-		Data:     &callData,
-		Value:    &value,
-		From:     fromAddr,
-		To:       &toAddr,
+		Nonce:    &finalNonce,
+		Gas:      &finalGasLimit,
+		GasPrice: &finalGasPrice,
+		Data:     &arbTx.Data,
+		Value:    &finalValue,
+		From:     arbTx.From,
+		To:       &arbTx.To,
 	}
 
 	hash, err := h.accountApi.SignAndSendTransaction(ctx, sendArgs, "")

+ 4 - 1
arbitrage/history.go

@@ -93,7 +93,10 @@ func (h *HistoryArbitrage) run() {
 	h.setupWallets()
 
 	// 交易发送测试
-	h.sendTest(h.wallets[0])
+	h.sendTransaction(&ArbTx{
+		From: h.wallets[0].account.Address,
+		To:   h.wallets[0].account.Address,
+	})
 
 running:
 	for {