api.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package arbitrage
  2. import (
  3. "context"
  4. "github.com/ethereum/go-ethereum/accounts/abi"
  5. "github.com/ethereum/go-ethereum/common"
  6. "github.com/ethereum/go-ethereum/common/hexutil"
  7. "github.com/ethereum/go-ethereum/internal/ethapi"
  8. "github.com/ethereum/go-ethereum/rpc"
  9. "math/big"
  10. )
  11. func (h *HistoryArbitrage) callReturnRealResult(a abi.ABI, from string, to string, method string, params ...interface{}) ([]interface{}, error) {
  12. // 准备Call的内容
  13. finalParams := GetFinalParams(params)
  14. callData, err := FinalPack(a, method, finalParams...)
  15. if err != nil {
  16. return nil, err
  17. }
  18. fromAddr := common.HexToAddress(from)
  19. toAddr := common.HexToAddress(to)
  20. blockNrOrHash := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
  21. ctx, cancel := context.WithCancel(context.Background())
  22. defer cancel()
  23. // 调用节点Call
  24. result, err := h.blockChainApi.Call(ctx, ethapi.CallArgs{
  25. From: &fromAddr,
  26. To: &toAddr,
  27. Data: &callData,
  28. }, blockNrOrHash, nil)
  29. if err != nil {
  30. return nil, err
  31. }
  32. // 解析Call的Result,使其可视化
  33. realResult, err := a.Unpack(method, result)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return realResult, nil
  38. }
  39. func (h *HistoryArbitrage) sendTest(e *EasyWallet) {
  40. ctx := context.Background()
  41. blockOrNbr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber)
  42. // 获取nonce
  43. nonce, err := h.txApi.GetTransactionCount(ctx, e.account.Address, blockOrNbr)
  44. if err != nil {
  45. HistoryError("Get transaction count error.", "error", err.Error())
  46. return
  47. }
  48. gasLimit := hexutil.Uint64(uint64(21000))
  49. gasPrice, err := h.ethereumApi.GasPrice(ctx)
  50. if err != nil {
  51. HistoryError("Get gas price error.", "error", err.Error())
  52. return
  53. }
  54. callData := hexutil.Bytes{}
  55. value := hexutil.Big(*big.NewInt(1))
  56. fromAddr := e.account.Address
  57. toAddr := e.account.Address
  58. sendArgs := ethapi.SendTxArgs{
  59. Nonce: nonce,
  60. Gas: &gasLimit,
  61. GasPrice: gasPrice,
  62. Data: &callData,
  63. Value: &value,
  64. From: fromAddr,
  65. To: &toAddr,
  66. }
  67. hash, err := h.accountApi.SignAndSendTransaction(ctx, sendArgs, "")
  68. if err != nil {
  69. HistoryError("Send error.", "error", err.Error())
  70. return
  71. }
  72. HistoryInfo("Send ok.", "hash", hash)
  73. }