rpc.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package main
  2. import (
  3. "context"
  4. "github.com/ethereum/go-ethereum/log"
  5. "github.com/ethereum/go-ethereum/rpc"
  6. "github.com/ethereum/go-ethereum/web3/api"
  7. "math/big"
  8. "strconv"
  9. "time"
  10. )
  11. func PING() (string, error) {
  12. currentTime := time.Now()
  13. timeString := currentTime.Format("2006-01-02 15:04:05.123")
  14. return timeString, nil
  15. }
  16. func main() {
  17. api.PrintTime("GO")
  18. ipcPath := "\\\\.\\pipe\\geth.ipc"
  19. // 使用 IPC 连接到以太坊节点
  20. client, err := rpc.Dial(ipcPath)
  21. if err != nil {
  22. api.PrintTime("无法连接到以太坊节点:", err)
  23. }
  24. // 连接成功,可以使用 client 进行后续操作
  25. // eth_call
  26. var result string
  27. //arg := map[string]interface{}{
  28. // "from": "0x746Fe602eD945378478523D53007B1c9d14037Ff",
  29. // "to": "0x746Fe602eD945378478523D53007B1c9d14037Ff",
  30. // "data": "0x",
  31. // "value": hexutil.Uint64(0),
  32. // "gas": hexutil.Uint64(100000),
  33. // "gasPrice": hexutil.Uint64(100000),
  34. //}
  35. //api.PrintTime("eth_call")
  36. //err = client.CallContext(context.Background(), &result, "eth_call", arg, "pending")
  37. //api.PrintTime(result)
  38. //
  39. //err = client.CallContext(context.Background(), &result, "zdy_getPrvKey", "0x000000FB5e4fbEE939625B0099288bCF51Ed6FA1")
  40. //if err != nil {
  41. // log.Fatal("无法获取信息:", err)
  42. //}
  43. //
  44. //fmt.Println("当前结果:", result)
  45. //err = client.CallContext(context.Background(), &result, "zdy_unLockAllAccount")
  46. //if err != nil {
  47. // log.Fatal("无法获取信息:", err)
  48. //}
  49. //
  50. //fmt.Println("当前结果:", result)
  51. //
  52. //arg = map[string]interface{}{
  53. // "from": "0x000000fb5e4fbee939625b0099288bcf51ed6fa1",
  54. // "to": "0x0000000000000000000000000000000000000000",
  55. // "data": "0x",
  56. // "value": hexutil.Uint64(0),
  57. // "nonce": hexutil.Uint64(38),
  58. // "gas": hexutil.Uint64(100000),
  59. // "gasPrice": hexutil.Uint64(10000000000),
  60. // "chainID": "0x45c",
  61. //}
  62. //err = client.CallContext(context.Background(), &result, "eth_sendTransaction", arg)
  63. //if err != nil {
  64. // log.Fatal("无法获取信息:", err)
  65. //}
  66. //
  67. //fmt.Println("当前结果:", result)
  68. toAddress := "0x0000000000000000000000000000000000000410"
  69. gasPrice := big.NewInt(50000000410)
  70. gasLimit := uint64(30000)
  71. value := big.NewInt(0) // 1 ETH
  72. nonce := uint64(87)
  73. data := ""
  74. // 获取私钥对象
  75. privateKeyHex := "d18f844b8ff00c723592257d57c41b2ebef0b197c8daf4f8a5909ffd18165b8d"
  76. _, rawHash, err := api.SignTransaction(nonce, toAddress, value, gasLimit, gasPrice, data, privateKeyHex)
  77. //api.SpeedTest(PING)
  78. //api.SpeedTest(client.CallContext, context.Background(), &result, "zdy_ping")
  79. //api.PrintTime(result)
  80. err = client.CallContext(context.Background(), &result, "eth_blockNumber")
  81. blockNumber, _ := strconv.ParseInt(result, 0, 64)
  82. api.PrintTime(blockNumber)
  83. for {
  84. err = client.CallContext(context.Background(), &result, "eth_blockNumber")
  85. nowBlockNumber, _ := strconv.ParseInt(result, 0, 64)
  86. if nowBlockNumber > blockNumber {
  87. api.PrintTime(nowBlockNumber)
  88. break
  89. }
  90. }
  91. //
  92. //err = client.CallContext(context.Background(), &result, "eth_sendRawTransaction", rawHash)
  93. //api.PrintTime(result)
  94. //
  95. //gasLimit = uint64(100002)
  96. //
  97. //// 获取私钥对象
  98. //
  99. //_, rawHash, err = api.SignTransaction(nonce, toAddress, value, gasLimit, gasPrice, data, privateKeyHex)
  100. //err = client.CallContext(context.Background(), &result, "zdy_sendRawTransaction", rawHash)
  101. //api.PrintTime(result)
  102. //api.PrintTime("zdy_sendRawTransaction")
  103. //
  104. //client.CallContext(context.Background(), &result, "zdy_sendRawTransaction", rawHash)
  105. //api.PrintTime("zdy_sendRawTransaction", result)
  106. api.PrintTime("eth_newPendingTransactionFilter")
  107. client.CallContext(context.Background(), &result, "eth_newPendingTransactionFilter", rawHash)
  108. api.PrintTime("eth_newPendingTransactionFilter", result)
  109. api.SpeedTest(log.Info, "abc123")
  110. }