2022-01-14 13:08:41 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-03-17 20:16:02 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/devnettest/services"
|
2022-01-14 13:08:41 +00:00
|
|
|
|
|
|
|
"github.com/ledgerwatch/erigon/cmd/devnettest/requests"
|
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-03-31 12:40:09 +00:00
|
|
|
accountAddr string
|
|
|
|
blockNum string
|
2022-01-14 13:08:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2022-03-31 12:40:09 +00:00
|
|
|
getBalanceCmd.Flags().StringVar(&accountAddr, "addr", "", "String address to check")
|
2022-01-14 13:08:41 +00:00
|
|
|
getBalanceCmd.MarkFlagRequired("addr")
|
|
|
|
getBalanceCmd.Flags().StringVar(&blockNum, "block-num", "latest", "String denoting block number")
|
|
|
|
rootCmd.AddCommand(getBalanceCmd)
|
2022-03-18 11:57:23 +00:00
|
|
|
|
2022-03-31 12:40:09 +00:00
|
|
|
getTransactionCountCmd.Flags().StringVar(&accountAddr, "addr", "", "String address to check")
|
2022-03-18 11:57:23 +00:00
|
|
|
getTransactionCountCmd.MarkFlagRequired("addr")
|
|
|
|
getTransactionCountCmd.Flags().StringVar(&blockNum, "block-num", "latest", "String denoting block number")
|
|
|
|
rootCmd.AddCommand(getTransactionCountCmd)
|
2022-01-14 13:08:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var getBalanceCmd = &cobra.Command{
|
|
|
|
Use: "get-balance",
|
|
|
|
Short: "Checks balance by address",
|
|
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
|
|
|
switch blockNum {
|
|
|
|
case "pending", "latest", "earliest":
|
|
|
|
default:
|
2022-03-17 20:16:02 +00:00
|
|
|
return fmt.Errorf("block number must be 'pending', 'latest' or 'earliest'")
|
2022-01-14 13:08:41 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if clearDev {
|
2022-03-17 20:16:02 +00:00
|
|
|
defer services.ClearDevDB()
|
2022-01-14 13:08:41 +00:00
|
|
|
}
|
2022-03-31 12:40:09 +00:00
|
|
|
if !common.IsHexAddress(accountAddr) {
|
|
|
|
fmt.Printf("address: %v, is not a valid hex address\n", accountAddr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
address := common.HexToAddress(accountAddr)
|
2022-03-18 11:57:23 +00:00
|
|
|
if err := requests.GetBalance(reqId, address, blockNum); err != nil {
|
|
|
|
fmt.Printf("could not get balance: %v\n", err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var getTransactionCountCmd = &cobra.Command{
|
|
|
|
Use: "get-transaction-count",
|
|
|
|
Short: "Gets the total number of transactions sent out by an account, the nonce",
|
|
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
|
|
|
switch blockNum {
|
|
|
|
case "pending", "latest", "earliest":
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("block number must be 'pending', 'latest' or 'earliest'")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if clearDev {
|
|
|
|
defer services.ClearDevDB()
|
|
|
|
}
|
2022-03-31 12:40:09 +00:00
|
|
|
if !common.IsHexAddress(accountAddr) {
|
|
|
|
fmt.Printf("address: %v, is not a valid hex address\n", accountAddr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
address := common.HexToAddress(accountAddr)
|
2022-03-23 14:26:33 +00:00
|
|
|
if err := requests.GetTransactionCountCmd(reqId, address, blockNum); err != nil {
|
2022-03-18 11:57:23 +00:00
|
|
|
fmt.Printf("could not get transaction count: %v\n", err)
|
2022-03-17 20:16:02 +00:00
|
|
|
}
|
2022-01-14 13:08:41 +00:00
|
|
|
},
|
|
|
|
}
|