| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package arbitrage
- import (
- "context"
- "github.com/ethereum/go-ethereum/accounts/abi"
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/common/hexutil"
- "github.com/ethereum/go-ethereum/internal/ethapi"
- "github.com/ethereum/go-ethereum/rpc"
- "math/big"
- )
- func (h *HistoryArbitrage) callReturnRealResult(a abi.ABI, from string, to string, method string, params ...interface{}) ([]interface{}, error) {
- // 准备Call的内容
- finalParams := GetFinalParams(params)
- callData, err := FinalPack(a, method, finalParams...)
- if err != nil {
- return nil, err
- }
- fromAddr := common.HexToAddress(from)
- toAddr := common.HexToAddress(to)
- blockNrOrHash := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- // 调用节点Call
- result, err := h.blockChainApi.Call(ctx, ethapi.CallArgs{
- From: &fromAddr,
- To: &toAddr,
- Data: &callData,
- }, blockNrOrHash, nil)
- if err != nil {
- return nil, err
- }
- // 解析Call的Result,使其可视化
- realResult, err := a.Unpack(method, result)
- if err != nil {
- return nil, err
- }
- return realResult, nil
- }
- 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, err := h.ethereumApi.GasPrice(ctx)
- if err != nil {
- HistoryError("Get gas price error.", "error", err.Error())
- return
- }
- 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)
- }
|