Skip to content

Commit

Permalink
feature: balance at a specific block number #163 (#175)
Browse files Browse the repository at this point in the history
* feature: balance at a specific block number #163

* updated error message
  • Loading branch information
r-gochain authored Dec 23, 2020
1 parent 80f3f5f commit 64315fc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ COMMANDS:
transaction, tx Transaction details for a tx hash
receipt, rc Transaction receipt for a tx hash
address, addr Account details for a specific address, or the one corresponding to the private key.
balance Get balance for your private key or an address passed in. eg: `balance 0xABC123`
balance Get balance for your private key or an address passed in(you could also use "block" as an optional parameter). eg: `balance 0xABC123`
increasegas Increase gas for a transaction. Useful if a tx is taking too long and you want it to go faster.
replace Replace transaction. If a transaction is still pending, you can attempt to replace it.
contract, c Contract operations
Expand Down
28 changes: 23 additions & 5 deletions cmd/web3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,13 @@ func main() {
EnvVar: pkVarName,
Destination: &privateKey,
Hidden: false},
cli.StringFlag{
Name: "block",
Usage: "Block number",
Hidden: false},
},
Action: func(c *cli.Context) {
GetAddressDetails(ctx, network, c.Args().First(), privateKey, false, "")
GetAddressDetails(ctx, network, c.Args().First(), privateKey, false, "", c.String("block"))
},
},
{
Expand All @@ -195,6 +199,10 @@ func main() {
EnvVar: addrVarName,
Usage: "Contract address",
Hidden: false},
cli.StringFlag{
Name: "block",
Usage: "Block number",
Hidden: false},
},
Action: func(c *cli.Context) {
contractAddress = ""
Expand All @@ -204,7 +212,7 @@ func main() {
fatalExit(errors.New("You must set ERC20 contract address"))
}
}
GetAddressDetails(ctx, network, c.Args().First(), privateKey, true, contractAddress)
GetAddressDetails(ctx, network, c.Args().First(), privateKey, true, contractAddress, c.String("block"))
},
},
{
Expand Down Expand Up @@ -1151,7 +1159,7 @@ func GetBlockDetails(ctx context.Context, network web3.Network, numberOrHash str
if numberOrHash != "" {
blockN, err = web3.ParseBigInt(numberOrHash)
if err != nil {
fatalExit(fmt.Errorf("Block argument must be a number (decimal integer) or hash (hexadecimal with 0x prefix) %q: %v", numberOrHash, err))
fatalExit(fmt.Errorf("Block argument must be a number (decimal integer) %q: %v", numberOrHash, err))
}
}
block, err = client.GetBlockByNumber(ctx, blockN, includeTxs)
Expand Down Expand Up @@ -1292,7 +1300,7 @@ func printInputData(data []byte, format string) {
}

func GetAddressDetails(ctx context.Context, network web3.Network, addrHash, privateKey string, onlyBalance bool,
contractAddress string) {
contractAddress string, blockNumber string) {
if addrHash == "" {
if privateKey == "" {
fatalExit(errors.New("Missing address. Must be specified as only argument, or implied from a private key."))
Expand Down Expand Up @@ -1320,12 +1328,22 @@ func GetAddressDetails(ctx context.Context, network web3.Network, addrHash, priv
return
}

var blockN *big.Int
var err error
// Don't try to parse empty string, which means 'latest'.
if blockNumber != "" {
blockN, err = web3.ParseBigInt(blockNumber)
if err != nil {
fatalExit(fmt.Errorf("Block argument must be a number (decimal integer) %q: %v", blockNumber, err))
}
}

client, err := web3.Dial(network.URL)
if err != nil {
fatalExit(fmt.Errorf("Failed to connect to %q: %v", network.URL, err))
}
defer client.Close()
bal, err := client.GetBalance(ctx, addrHash, nil)
bal, err := client.GetBalance(ctx, addrHash, blockN)
if err != nil {
fatalExit(fmt.Errorf("Cannot get address balance from the network: %v", err))
}
Expand Down

0 comments on commit 64315fc

Please sign in to comment.