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"
|
|
|
|
"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"
|
2021-11-23 16:44:46 +00:00
|
|
|
)
|
|
|
|
|
2021-11-26 16:05:16 +00:00
|
|
|
func parseResponse(resp interface{}) string {
|
|
|
|
result, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(result)
|
|
|
|
}
|
|
|
|
|
2021-11-23 16:44:46 +00:00
|
|
|
func GetBalance(address common.Address, blockNum string) {
|
|
|
|
reqGen := initialiseRequestGenerator()
|
|
|
|
var b rpctest.EthBalance
|
|
|
|
|
|
|
|
res := reqGen.Erigon("eth_getBalance", reqGen.getBalance(address, blockNum), &b)
|
|
|
|
if res.Err != nil {
|
|
|
|
fmt.Printf("Error getting balance: %v\n", res.Err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-26 16:05:16 +00:00
|
|
|
fmt.Printf("Balance retrieved: %v\n", parseResponse(b))
|
2021-11-23 16:44:46 +00:00
|
|
|
}
|
|
|
|
|
2021-11-26 16:05:16 +00:00
|
|
|
func SendTx(signedTx *types.Transaction) {
|
|
|
|
reqGen := initialiseRequestGenerator()
|
|
|
|
var b rpctest.EthSendRawTransaction
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := (*signedTx).MarshalBinary(&buf)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error trying to marshal binary: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := reqGen.Erigon("eth_sendRawTransaction", reqGen.sendRawTransaction(buf.Bytes()), &b)
|
|
|
|
if res.Err != nil {
|
|
|
|
fmt.Printf("Error sending transaction: %v\n", res.Err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Submitted transaction successfully: %v\n", parseResponse(b))
|
2021-11-23 16:44:46 +00:00
|
|
|
}
|
|
|
|
|
2021-11-26 16:05:16 +00:00
|
|
|
func TxpoolContent() {
|
|
|
|
reqGen := initialiseRequestGenerator()
|
|
|
|
var b rpctest.EthTxPool
|
|
|
|
|
|
|
|
res := reqGen.Erigon("txpool_content", reqGen.txpoolContent(), &b)
|
|
|
|
if res.Err != nil {
|
|
|
|
fmt.Printf("Error fetching txpool: %v\n", res.Err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Txpool content: %v\n", parseResponse(b))
|
2021-11-23 16:44:46 +00:00
|
|
|
}
|