66 "github.com/bcdevtools/devd/v2/cmd/utils"
77 "github.com/ethereum/go-ethereum"
88 "github.com/ethereum/go-ethereum/common"
9+ "github.com/ethereum/go-ethereum/ethclient"
910 "github.com/spf13/cobra"
1011 "math/big"
1112)
@@ -23,18 +24,21 @@ func GetQueryBalanceCommand() *cobra.Command {
2324 return
2425 }
2526
26- //fetchErc20ModuleAndVfbc := cmd.Flags().Changed(flagErc20)
27-
2827 ethClient8545 , _ := mustGetEthClient (cmd , false )
29- var bz []byte
28+ var restApiEndpoint string
29+
30+ fetchErc20ModuleAndVfbc := cmd .Flags ().Changed (flagErc20 )
31+ if fetchErc20ModuleAndVfbc {
32+ restApiEndpoint = mustGetRest (cmd )
33+ }
3034
3135 contextHeight := readContextHeightFromFlag (cmd )
3236
3337 accountAddr := evmAddrs [0 ]
3438 fmt .Println ("Account" , accountAddr )
3539
3640 printRow := func (colType , colContract , colSymbol , colBalance , colRaw , colDecimals , colHigh , colLow , extra string ) {
37- fmt .Printf ("%-6s | %42s | %10s | %28s | %27s | %8s | %9s | %18s | %-1s\n " , colType , colContract , colSymbol , colBalance , colRaw , colDecimals , colHigh , colLow , extra )
41+ fmt .Printf ("%-7s | %42s | %10s | %28s | %27s | %8s | %9s | %18s | %-1s\n " , colType , colContract , colSymbol , colBalance , colRaw , colDecimals , colHigh , colLow , extra )
3842 }
3943
4044 printRow ("Type" , "Contract" , "Symbol" , "Balance" , "Raw" , "Decimals" , "High" , "Low" , "Extra" )
@@ -50,55 +54,94 @@ func GetQueryBalanceCommand() *cobra.Command {
5054 for i := 1 ; i < len (evmAddrs ); i ++ {
5155 contractAddr := evmAddrs [i ]
5256
53- bz , err = ethClient8545 .CallContract (context .Background (), ethereum.CallMsg {
54- To : & contractAddr ,
55- Data : []byte {0x95 , 0xd8 , 0x9b , 0x41 }, // symbol()
56- }, contextHeight )
57- if err != nil {
58- utils .PrintlnStdErr ("ERR: failed to get symbol for contract" , contractAddr , ":" , err )
59- continue
60- }
61-
62- contractSymbol , err := utils .AbiDecodeString (bz )
57+ tokenBalance , tokenBalanceDisplay , contractSymbol , contractDecimals , balancePartHigh , balancePartLow , err := fetchBalanceForErc20Contract (contractAddr , contextHeight , ethClient8545 , accountAddr , "contract" )
6358 if err != nil {
64- utils .PrintlnStdErr ("ERR: failed to decode symbol for contract" , contractAddr , ":" , err )
6559 continue
6660 }
6761
68- bz , err = ethClient8545 .CallContract (context .Background (), ethereum.CallMsg {
69- To : & contractAddr ,
70- Data : []byte {0x31 , 0x3c , 0xe5 , 0x67 }, // decimals()
71- }, contextHeight )
72- if err != nil {
73- utils .PrintlnStdErr ("ERR: failed to get decimals for contract" , contractAddr , ":" , err )
74- continue
75- }
76-
77- contractDecimals := new (big.Int ).SetBytes (bz )
62+ printRow ("Input" , contractAddr .String (), contractSymbol , tokenBalanceDisplay , tokenBalance .String (), contractDecimals .String (), balancePartHigh .String (), balancePartLow .String (), "" )
63+ }
7864
79- var tokenBalance * big.Int
80- bz , err = ethClient8545 .CallContract (context .Background (), ethereum.CallMsg {
81- To : & contractAddr ,
82- Data : append ([]byte {0x70 , 0xa0 , 0x82 , 0x31 }, common .BytesToHash (accountAddr .Bytes ()).Bytes ()... ), // balanceOf(address)
83- }, contextHeight )
65+ if fetchErc20ModuleAndVfbc && restApiEndpoint != "" {
66+ erc20TokenPairs , err := fetchErc20ModuleTokenPairsFromRest (restApiEndpoint )
8467 if err != nil {
85- utils .PrintlnStdErr ("ERR: failed to get contract token" , contractAddr , "balance for" , accountAddr , ":" , err )
86- continue
68+ utils .PrintlnStdErr ("ERR:" , err )
69+ return
70+ } else {
71+ for _ , erc20TokenPair := range erc20TokenPairs {
72+ contractAddr := common .HexToAddress (erc20TokenPair .Erc20Address )
73+
74+ tokenBalance , tokenBalanceDisplay , contractSymbol , contractDecimals , balancePartHigh , balancePartLow , err := fetchBalanceForErc20Contract (contractAddr , contextHeight , ethClient8545 , accountAddr , "contract" )
75+ if err != nil {
76+ continue
77+ }
78+
79+ if tokenBalance .Sign () == 0 {
80+ continue
81+ }
82+
83+ printRow ("x/erc20" , contractAddr .String (), contractSymbol , tokenBalanceDisplay , tokenBalance .String (), contractDecimals .String (), balancePartHigh .String (), balancePartLow .String (), erc20TokenPair .Denom )
84+ }
8785 }
88-
89- tokenBalance = new (big.Int ).SetBytes (bz )
90-
91- display , high , low , err := utils .ConvertNumberIntoDisplayWithExponent (tokenBalance , int (contractDecimals .Int64 ()))
92- utils .ExitOnErr (err , "failed to convert number into display with exponent" )
93-
94- printRow ("Input" , contractAddr .String (), contractSymbol , display , tokenBalance .String (), contractDecimals .String (), high .String (), low .String (), "" )
9586 }
9687 },
9788 }
9889
9990 cmd .Flags ().String (flagRpc , "" , flagEvmRpcDesc )
91+ cmd .Flags ().String (flagRest , "" , flagCosmosRestDesc )
10092 cmd .Flags ().Int64 (flagHeight , 0 , "query balance at specific height" )
101- cmd .Flags ().String (flagErc20 , "" , "query balance of ERC-20 contracts of `x/erc20` module and virtual frontier bank contracts" )
93+ cmd .Flags ().Bool (flagErc20 , false , "query balance of ERC-20 contracts of `x/erc20` module and virtual frontier bank contracts" )
10294
10395 return cmd
10496}
97+
98+ func fetchBalanceForErc20Contract (contractAddr common.Address , contextHeight * big.Int , ethClient8545 * ethclient.Client , accountAddr common.Address , contractType string ) (
99+ tokenBalance * big.Int , tokenBalanceDisplay , contractSymbol string ,
100+ contractDecimals , balancePartHigh , balancePartLow * big.Int ,
101+ err error ,
102+ ) {
103+ bz , err := ethClient8545 .CallContract (context .Background (), ethereum.CallMsg {
104+ To : & contractAddr ,
105+ Data : []byte {0x95 , 0xd8 , 0x9b , 0x41 }, // symbol()
106+ }, contextHeight )
107+ if err != nil {
108+ utils .PrintlnStdErr ("ERR: failed to get symbol for" , contractType , contractAddr , ":" , err )
109+ return
110+ }
111+
112+ contractSymbol , err = utils .AbiDecodeString (bz )
113+ if err != nil {
114+ utils .PrintlnStdErr ("ERR: failed to decode symbol for" , contractType , contractAddr , ":" , err )
115+ return
116+ }
117+
118+ bz , err = ethClient8545 .CallContract (context .Background (), ethereum.CallMsg {
119+ To : & contractAddr ,
120+ Data : []byte {0x31 , 0x3c , 0xe5 , 0x67 }, // decimals()
121+ }, contextHeight )
122+ if err != nil {
123+ utils .PrintlnStdErr ("ERR: failed to get decimals for" , contractType , contractAddr , ":" , err )
124+ return
125+ }
126+
127+ contractDecimals = new (big.Int ).SetBytes (bz )
128+
129+ bz , err = ethClient8545 .CallContract (context .Background (), ethereum.CallMsg {
130+ To : & contractAddr ,
131+ Data : append ([]byte {0x70 , 0xa0 , 0x82 , 0x31 }, common .BytesToHash (accountAddr .Bytes ()).Bytes ()... ), // balanceOf(address)
132+ }, contextHeight )
133+ if err != nil {
134+ utils .PrintlnStdErr ("ERR: failed to get" , contractType , "token" , contractAddr , "balance for" , accountAddr , ":" , err )
135+ return
136+ }
137+
138+ tokenBalance = new (big.Int ).SetBytes (bz )
139+
140+ tokenBalanceDisplay , balancePartHigh , balancePartLow , err = utils .ConvertNumberIntoDisplayWithExponent (tokenBalance , int (contractDecimals .Int64 ()))
141+ if err != nil {
142+ utils .PrintlnStdErr ("ERR: failed to convert number" , tokenBalance .String (), "decimals" , contractDecimals .String (), "into display with exponent for" , contractType , "token balance:" , err )
143+ return
144+ }
145+
146+ return
147+ }
0 commit comments