2022-10-11 12:34:32 +00:00
|
|
|
package requests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-10-31 02:20:58 +00:00
|
|
|
"strconv"
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
2023-05-20 20:57:32 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2023-01-13 18:12:18 +00:00
|
|
|
|
2022-10-31 10:46:49 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/devnet/models"
|
2022-10-11 12:34:32 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/rpctest/rpctest"
|
|
|
|
)
|
|
|
|
|
2023-05-20 20:57:32 +00:00
|
|
|
func GetBalance(reqId int, address libcommon.Address, blockNum models.BlockNumber, logger log.Logger) (uint64, error) {
|
|
|
|
reqGen := initialiseRequestGenerator(reqId, logger)
|
2022-10-11 12:34:32 +00:00
|
|
|
var b rpctest.EthBalance
|
|
|
|
|
2022-10-31 10:46:49 +00:00
|
|
|
if res := reqGen.Erigon(models.ETHGetBalance, reqGen.GetBalance(address, blockNum), &b); res.Err != nil {
|
2022-10-11 12:34:32 +00:00
|
|
|
return 0, fmt.Errorf("failed to get balance: %v", res.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bal, err := json.Marshal(b.Balance)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
balStr := string(bal)[3 : len(bal)-1]
|
|
|
|
balance, err := strconv.ParseInt(balStr, 16, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("cannot convert balance to decimal: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return uint64(balance), nil
|
|
|
|
}
|