Skip to content

Commit 64315fc

Browse files
authored
feature: balance at a specific block number #163 (#175)
* feature: balance at a specific block number #163 * updated error message
1 parent 80f3f5f commit 64315fc

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ COMMANDS:
2929
transaction, tx Transaction details for a tx hash
3030
receipt, rc Transaction receipt for a tx hash
3131
address, addr Account details for a specific address, or the one corresponding to the private key.
32-
balance Get balance for your private key or an address passed in. eg: `balance 0xABC123`
32+
balance Get balance for your private key or an address passed in(you could also use "block" as an optional parameter). eg: `balance 0xABC123`
3333
increasegas Increase gas for a transaction. Useful if a tx is taking too long and you want it to go faster.
3434
replace Replace transaction. If a transaction is still pending, you can attempt to replace it.
3535
contract, c Contract operations

cmd/web3/main.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,13 @@ func main() {
171171
EnvVar: pkVarName,
172172
Destination: &privateKey,
173173
Hidden: false},
174+
cli.StringFlag{
175+
Name: "block",
176+
Usage: "Block number",
177+
Hidden: false},
174178
},
175179
Action: func(c *cli.Context) {
176-
GetAddressDetails(ctx, network, c.Args().First(), privateKey, false, "")
180+
GetAddressDetails(ctx, network, c.Args().First(), privateKey, false, "", c.String("block"))
177181
},
178182
},
179183
{
@@ -195,6 +199,10 @@ func main() {
195199
EnvVar: addrVarName,
196200
Usage: "Contract address",
197201
Hidden: false},
202+
cli.StringFlag{
203+
Name: "block",
204+
Usage: "Block number",
205+
Hidden: false},
198206
},
199207
Action: func(c *cli.Context) {
200208
contractAddress = ""
@@ -204,7 +212,7 @@ func main() {
204212
fatalExit(errors.New("You must set ERC20 contract address"))
205213
}
206214
}
207-
GetAddressDetails(ctx, network, c.Args().First(), privateKey, true, contractAddress)
215+
GetAddressDetails(ctx, network, c.Args().First(), privateKey, true, contractAddress, c.String("block"))
208216
},
209217
},
210218
{
@@ -1151,7 +1159,7 @@ func GetBlockDetails(ctx context.Context, network web3.Network, numberOrHash str
11511159
if numberOrHash != "" {
11521160
blockN, err = web3.ParseBigInt(numberOrHash)
11531161
if err != nil {
1154-
fatalExit(fmt.Errorf("Block argument must be a number (decimal integer) or hash (hexadecimal with 0x prefix) %q: %v", numberOrHash, err))
1162+
fatalExit(fmt.Errorf("Block argument must be a number (decimal integer) %q: %v", numberOrHash, err))
11551163
}
11561164
}
11571165
block, err = client.GetBlockByNumber(ctx, blockN, includeTxs)
@@ -1292,7 +1300,7 @@ func printInputData(data []byte, format string) {
12921300
}
12931301

12941302
func GetAddressDetails(ctx context.Context, network web3.Network, addrHash, privateKey string, onlyBalance bool,
1295-
contractAddress string) {
1303+
contractAddress string, blockNumber string) {
12961304
if addrHash == "" {
12971305
if privateKey == "" {
12981306
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
13201328
return
13211329
}
13221330

1331+
var blockN *big.Int
1332+
var err error
1333+
// Don't try to parse empty string, which means 'latest'.
1334+
if blockNumber != "" {
1335+
blockN, err = web3.ParseBigInt(blockNumber)
1336+
if err != nil {
1337+
fatalExit(fmt.Errorf("Block argument must be a number (decimal integer) %q: %v", blockNumber, err))
1338+
}
1339+
}
1340+
13231341
client, err := web3.Dial(network.URL)
13241342
if err != nil {
13251343
fatalExit(fmt.Errorf("Failed to connect to %q: %v", network.URL, err))
13261344
}
13271345
defer client.Close()
1328-
bal, err := client.GetBalance(ctx, addrHash, nil)
1346+
bal, err := client.GetBalance(ctx, addrHash, blockN)
13291347
if err != nil {
13301348
fatalExit(fmt.Errorf("Cannot get address balance from the network: %v", err))
13311349
}

0 commit comments

Comments
 (0)