Browse Source

api结构调整

skyfffire 2 years ago
parent
commit
ad560872d8
3 changed files with 71 additions and 82 deletions
  1. 35 2
      arbitrage/api.go
  2. 31 76
      arbitrage/easy_wallet.go
  3. 5 4
      arbitrage/history.go

+ 35 - 2
arbitrage/api.go

@@ -43,8 +43,41 @@ func (h *HistoryArbitrage) callReturnRealResult(a abi.ABI, from string, to strin
 	return realResult, nil
 }
 
-func (h *HistoryArbitrage) newAccount() (common.Address, error) {
-	return h.accountApi.NewAccount("")
+func (h *HistoryArbitrage) SendTest(e *EasyWallet) {
+	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
+	}
+
+	gasLimit := hexutil.Uint64(uint64(21000))
+	gasPrice := hexutil.Big(*big.NewInt(30 * 1e9))
+	callData := hexutil.Bytes{}
+	value := hexutil.Big(*big.NewInt(1))
+	fromAddr := e.account.Address
+	toAddr := e.account.Address
+
+	sendArgs := ethapi.SendTxArgs{
+		Nonce:    nonce,
+		Gas:      &gasLimit,
+		GasPrice: &gasPrice,
+		Data:     &callData,
+		Value:    &value,
+		From:     fromAddr,
+		To:       &toAddr,
+	}
+
+	hash, err := h.accountApi.SignAndSendTransaction(ctx, sendArgs, "")
+	if err != nil {
+		HistoryError("Send error.", "error", err.Error())
+		return
+	}
+
+	HistoryInfo("Send ok.", "hash", hash)
 }
 
 func GetFinalParams(params []interface{}) []interface{} {

+ 31 - 76
arbitrage/easy_wallet.go

@@ -1,91 +1,46 @@
 package arbitrage
 
 import (
-	"context"
 	"github.com/ethereum/go-ethereum/accounts"
-	"github.com/ethereum/go-ethereum/common/hexutil"
-	"github.com/ethereum/go-ethereum/core/types"
-	"github.com/ethereum/go-ethereum/internal/ethapi"
-	"github.com/ethereum/go-ethereum/params"
-	"github.com/ethereum/go-ethereum/rpc"
-	"math/big"
 )
 
 type EasyWallet struct {
-	wallet  accounts.Wallet
-	account accounts.Account
+	wallet  *accounts.Wallet
+	account *accounts.Account
 }
 
-func NewEasyWallet(wallet accounts.Wallet, account accounts.Account) *EasyWallet {
+func NewEasyWallet(wallet *accounts.Wallet, account *accounts.Account) *EasyWallet {
 	return &EasyWallet{
 		wallet:  wallet,
 		account: account,
 	}
 }
 
-func (e *EasyWallet) SendTest(accountApi *ethapi.PrivateAccountAPI, txApi *ethapi.PublicTransactionPoolAPI) {
-	ctx := context.Background()
-	blockOrNbr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber)
-
-	// 获取nonce
-	nonce, err := txApi.GetTransactionCount(ctx, e.account.Address, blockOrNbr)
-	if err != nil {
-		HistoryError("Get transaction count error.", "error", err.Error())
-		return
-	}
-	HistoryInfo("Get transaction count", "addr", e.account.Address, "nonce", nonce)
-
-	gasLimit := hexutil.Uint64(uint64(21000))
-	gasPrice := hexutil.Big(*big.NewInt(30 * 1e9))
-	callData := hexutil.Bytes{}
-	value := hexutil.Big(*big.NewInt(1))
-	fromAddr := e.account.Address
-	toAddr := e.account.Address
-
-	sendArgs := ethapi.SendTxArgs{
-		Nonce:    nonce,
-		Gas:      &gasLimit,
-		GasPrice: &gasPrice,
-		Data:     &callData,
-		Value:    &value,
-		From:     fromAddr,
-		To:       &toAddr,
-	}
-
-	hash, err := accountApi.SignAndSendTransaction(ctx, sendArgs, "")
-	if err != nil {
-		HistoryError("Send error.", "error", err.Error())
-		return
-	}
-
-	HistoryInfo("Send ok.", "hash", hash)
-}
-
-func (e *EasyWallet) OnlySignTest() {
-	chainId := params.CoreChainConfig.ChainID
-	nonce := uint64(0)
-	gasLimit := uint64(21000)
-	gasPrice := big.NewInt(30 * 1e9)
-	callData := []byte{}
-	value := big.NewInt(1)
-	toAddr := e.account.Address
-
-	tx := types.NewTx(&types.LegacyTx{
-		Nonce:    nonce,
-		To:       &toAddr,
-		Value:    value,
-		Gas:      gasLimit,
-		GasPrice: gasPrice,
-		Data:     callData,
-	})
-
-	tx, err := e.wallet.SignTx(e.account, tx, chainId)
-
-	if err != nil {
-		HistoryError("Sign test err", "err", err)
-
-		return
-	}
-
-	HistoryInfo("Sign test", "tx", tx.Hash())
-}
+//func (e *EasyWallet) OnlySignTest() {
+//	chainId := params.CoreChainConfig.ChainID
+//	nonce := uint64(0)
+//	gasLimit := uint64(21000)
+//	gasPrice := big.NewInt(30 * 1e9)
+//	callData := []byte{}
+//	value := big.NewInt(1)
+//	toAddr := e.account.Address
+//
+//	tx := types.NewTx(&types.LegacyTx{
+//		Nonce:    nonce,
+//		To:       &toAddr,
+//		Value:    value,
+//		Gas:      gasLimit,
+//		GasPrice: gasPrice,
+//		Data:     callData,
+//	})
+//
+//	tx, err := e.wallet.SignTx(e.account, tx, chainId)
+//
+//	if err != nil {
+//		HistoryError("Sign test err", "err", err)
+//
+//		return
+//	}
+//
+//	HistoryInfo("Sign test", "tx", tx.Hash())
+//}

+ 5 - 4
arbitrage/history.go

@@ -13,7 +13,7 @@ type HistoryArbitrage struct {
 	// 核心变量
 	stack   *node.Node
 	ctx     *cli.Context
-	wallets []EasyWallet
+	wallets []*EasyWallet
 	// 生命周期控制
 	quit    chan struct{}
 	running bool
@@ -31,7 +31,7 @@ func RegisterHistoryArbitrage(stack *node.Node, ctx *cli.Context, apiBackend eth
 	h := &HistoryArbitrage{
 		stack:   stack,
 		ctx:     ctx,
-		wallets: []EasyWallet{},
+		wallets: []*EasyWallet{},
 
 		running: false,
 		quit:    make(chan struct{}),
@@ -82,7 +82,7 @@ func (h *HistoryArbitrage) setupWallets() {
 		}
 
 		HistoryInfo(fmt.Sprintf("Wallet[%02d]", index), "addr", account.Address)
-		h.wallets = append(h.wallets, EasyWallet{wallet: wallet, account: account})
+		h.wallets = append(h.wallets, NewEasyWallet(&wallet, &account))
 	}
 
 	HistoryInfo("Wallets setup done.", "wallet count", len(h.wallets))
@@ -92,7 +92,8 @@ func (h *HistoryArbitrage) run() {
 	// 配置钱包
 	h.setupWallets()
 
-	h.wallets[0].SendTest(h.accountApi, h.txApi)
+	// 交易发送测试
+	h.SendTest(h.wallets[0])
 
 running:
 	for {