diff --git a/README.md b/README.md index 95d7d47..970f7b6 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cmd/web3/main.go b/cmd/web3/main.go index bfd7d46..ca84b1f 100644 --- a/cmd/web3/main.go +++ b/cmd/web3/main.go @@ -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")) }, }, { @@ -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 = "" @@ -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")) }, }, { @@ -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) @@ -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.")) @@ -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)) }