2021-11-23 16:44:46 +00:00
|
|
|
package requests
|
|
|
|
|
|
|
|
import (
|
2021-11-26 16:05:16 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2021-11-23 16:44:46 +00:00
|
|
|
"fmt"
|
2022-03-17 20:16:02 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-11-23 16:44:46 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/rpctest/rpctest"
|
|
|
|
"github.com/ledgerwatch/erigon/common"
|
2021-11-26 16:05:16 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
2022-03-17 20:16:02 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2021-11-23 16:44:46 +00:00
|
|
|
)
|
|
|
|
|
2022-03-17 20:16:02 +00:00
|
|
|
func post(client *http.Client, url, request string, response interface{}) error {
|
2022-03-18 11:57:23 +00:00
|
|
|
fmt.Printf("Request: %+v\n", request)
|
2022-03-17 20:16:02 +00:00
|
|
|
start := time.Now()
|
|
|
|
r, err := client.Post(url, "application/json", strings.NewReader(request))
|
2021-11-26 16:05:16 +00:00
|
|
|
if err != nil {
|
2022-03-17 20:16:02 +00:00
|
|
|
return fmt.Errorf("client failed to make post request: %v", err)
|
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
if r.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("status %s", r.Status)
|
2021-11-26 16:05:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 20:16:02 +00:00
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
err = decoder.Decode(response)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to decode response: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Got in", "time", time.Since(start).Seconds())
|
|
|
|
return nil
|
2021-11-26 16:05:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 20:16:02 +00:00
|
|
|
func GetBalance(reqId int, address common.Address, blockNum string) error {
|
2022-01-14 13:08:41 +00:00
|
|
|
reqGen := initialiseRequestGenerator(reqId)
|
2021-11-23 16:44:46 +00:00
|
|
|
var b rpctest.EthBalance
|
|
|
|
|
2022-03-17 20:16:02 +00:00
|
|
|
if res := reqGen.Erigon("eth_getBalance", reqGen.getBalance(address, blockNum), &b); res.Err != nil {
|
|
|
|
return fmt.Errorf("failed to get balance: %v", res.Err)
|
2021-11-23 16:44:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 20:16:02 +00:00
|
|
|
s, err := parseResponse(b)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing resonse: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Balance retrieved: %v\n", s)
|
|
|
|
return nil
|
2021-11-23 16:44:46 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 03:23:40 +00:00
|
|
|
func SendTx(reqId int, signedTx *types.Transaction) (*common.Hash, error) {
|
2022-01-14 13:08:41 +00:00
|
|
|
reqGen := initialiseRequestGenerator(reqId)
|
2021-11-26 16:05:16 +00:00
|
|
|
var b rpctest.EthSendRawTransaction
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2022-03-17 20:16:02 +00:00
|
|
|
if err := (*signedTx).MarshalBinary(&buf); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to marshal binary: %v", err)
|
2021-11-26 16:05:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 20:16:02 +00:00
|
|
|
if res := reqGen.Erigon("eth_sendRawTransaction", reqGen.sendRawTransaction(buf.Bytes()), &b); res.Err != nil {
|
|
|
|
return nil, fmt.Errorf("could not make request to eth_sendRawTransaction: %v", res.Err)
|
2021-11-26 16:05:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 20:16:02 +00:00
|
|
|
s, err := parseResponse(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing resonse: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Submitted transaction successfully: %v\n", s)
|
2022-02-11 03:23:40 +00:00
|
|
|
return &b.TxnHash, nil
|
2021-11-23 16:44:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 20:16:02 +00:00
|
|
|
func TxpoolContent(reqId int) error {
|
2022-01-14 13:08:41 +00:00
|
|
|
reqGen := initialiseRequestGenerator(reqId)
|
2021-11-26 16:05:16 +00:00
|
|
|
var b rpctest.EthTxPool
|
|
|
|
|
2022-03-17 20:16:02 +00:00
|
|
|
if res := reqGen.Erigon("txpool_content", reqGen.txpoolContent(), &b); res.Err != nil {
|
|
|
|
return fmt.Errorf("failed to fetch txpool content: %v", res.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := parseResponse(b)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing resonse: %v", err)
|
2021-11-26 16:05:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 20:16:02 +00:00
|
|
|
fmt.Printf("Txpool content: %v\n", s)
|
|
|
|
return nil
|
2021-11-23 16:44:46 +00:00
|
|
|
}
|
2022-01-14 13:08:41 +00:00
|
|
|
|
2022-03-17 20:16:02 +00:00
|
|
|
func ParityList(reqId int, account common.Address, quantity int, offset []byte, blockNum string) error {
|
2022-01-14 13:08:41 +00:00
|
|
|
reqGen := initialiseRequestGenerator(reqId)
|
|
|
|
var b rpctest.ParityListStorageKeysResult
|
|
|
|
|
2022-03-17 20:16:02 +00:00
|
|
|
if res := reqGen.Erigon("parity_listStorageKeys", reqGen.parityStorageKeyListContent(account, quantity, offset, blockNum), &b); res.Err != nil {
|
|
|
|
return fmt.Errorf("failed to fetch storage keys: %v", res.Err)
|
2022-01-14 13:08:41 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 20:16:02 +00:00
|
|
|
s, err := parseResponse(b)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing resonse: %v", err)
|
|
|
|
}
|
2022-01-14 13:08:41 +00:00
|
|
|
|
2022-03-17 20:16:02 +00:00
|
|
|
fmt.Printf("Storage keys: %v\n", s)
|
|
|
|
return nil
|
2022-01-14 13:08:41 +00:00
|
|
|
}
|
2022-03-14 13:59:51 +00:00
|
|
|
|
|
|
|
func GetLogs(reqId int, fromBlock, toBlock uint64, address common.Address) error {
|
|
|
|
reqGen := initialiseRequestGenerator(reqId)
|
2022-03-16 22:21:05 +00:00
|
|
|
var b rpctest.EthGetLogs
|
2022-03-14 13:59:51 +00:00
|
|
|
|
2022-03-17 20:16:02 +00:00
|
|
|
if res := reqGen.Erigon("eth_getLogs", reqGen.getLogs(fromBlock, toBlock, address), &b); res.Err != nil {
|
2022-03-18 11:57:23 +00:00
|
|
|
return fmt.Errorf("error fetching logs: %v\n", res.Err)
|
2022-03-14 13:59:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 20:16:02 +00:00
|
|
|
s, err := parseResponse(b)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing resonse: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Logs: %v\n", s)
|
2022-03-14 13:59:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-03-18 11:57:23 +00:00
|
|
|
|
2022-03-23 14:26:33 +00:00
|
|
|
func GetTransactionCountCmd(reqId int, address common.Address, blockNum string) error {
|
2022-03-18 11:57:23 +00:00
|
|
|
reqGen := initialiseRequestGenerator(reqId)
|
|
|
|
var b rpctest.EthGetTransactionCount
|
|
|
|
|
|
|
|
if res := reqGen.Erigon("eth_getTransactionCount", reqGen.getTransactionCount(address, blockNum), &b); res.Err != nil {
|
|
|
|
return fmt.Errorf("error getting transaction count: %v\n", res.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.Error != nil {
|
|
|
|
return fmt.Errorf("error populating response object: %v", b.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := parseResponse(b)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing resonse: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Nonce: %v\n", s)
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-23 14:26:33 +00:00
|
|
|
|
|
|
|
func GetTransactionCount(reqId int, address common.Address, blockNum string) (rpctest.EthGetTransactionCount, error) {
|
|
|
|
reqGen := initialiseRequestGenerator(reqId)
|
|
|
|
var b rpctest.EthGetTransactionCount
|
|
|
|
|
|
|
|
if res := reqGen.Erigon("eth_getTransactionCount", reqGen.getTransactionCount(address, blockNum), &b); res.Err != nil {
|
|
|
|
return b, fmt.Errorf("error getting transaction count: %v\n", res.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.Error != nil {
|
|
|
|
return b, fmt.Errorf("error populating response object: %v", b.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|