Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get ERC20 token balance by GetStorageRanges on Snap Protocol #29838

Open
suhoangson opened this issue May 25, 2024 · 0 comments
Open

Get ERC20 token balance by GetStorageRanges on Snap Protocol #29838

suhoangson opened this issue May 25, 2024 · 0 comments

Comments

@suhoangson
Copy link

Hi Everyone,
I want to use GetStorageRanges in Snap Protocol https://github.com/ethereum/devp2p/blob/master/caps/snap.md#getstorageranges-0x02 for get balance of ERC20 token from Peer (i do not want to use RCP).
Based on my understanding, balance of ERC20 token is stored in storage slots
I check with USDT token: 0xdAC17F958D2ee523a2206206994597C13D831ec7
My account holder: 0xC0fcFAd6a5db86F566102B8a7115823068a93c50
Current my USDT balance on Metamask is: 7.45433 USDT
I checked on this tool: https://github.com/vocdoni/storage-proofs-eth-go/blob/master/examples/verify-top-50-erc20/main.go with USDT token and my account above
=>The result: my USDT balance was stored in slot 2, slot hash is: 0xc83ac632ddd978600e673c3064377a289fc7b6beba2050e1c1b0c5905a89fb9e

Based on above result, my code is:

......
func GetMapSlot(holder common.Address, position int) [32]byte {
	return crypto.Keccak256Hash(
		common.LeftPadBytes(holder[:], 32),
		common.LeftPadBytes(big.NewInt(int64(position)).Bytes(), 32),
	)
}
func main(){
                token_addr:=common.HexToAddress("0xdAC17F958D2ee523a2206206994597C13D831ec7") //USDT 
		token_hash:=crypto.Keccak256Hash(token_addr[:])
		fmt.Println("1. USDT Token hash: ",token_hash)

		account_addr:=common.HexToAddress("0xC0fcFAd6a5db86F566102B8a7115823068a93c50") //holder address
		start_slot_hash:=GetMapSlot(account_addr,2)
		fmt.Println("2. Start slot hash: ",common.Hash(start_slot_hash).Hex())
		end_slot_hash:=start_slot_hash
		
		//StateRoot: just pick a latest block on etherscan.io
		root_hash:=common.HexToHash("0xe87f3c92bbeadc3963c747fd039e390d4f3e0f0f9e6491302b96d20b284db0a0") 
               
               /* 
                    .....
                   create a peer connection as this example:  https://github.com/ethereum/go-ethereum/blob/master/cmd/devp2p/internal/ethtest/conn.go
               */ 
 	       // write request
	       req := &snap.GetStorageRangesPacket{
		      ID:               uint64(rand.Int63()),
		      Root:           root_hash,
		      Accounts:    []common.Hash{token_hash},
	 	      Origin:         start_slot_hash[:],
		      Limit:           end_slot_hash[:],
		      Bytes:          4000,
	       }
	       msg, err := conn.snapRequest(snap.GetStorageRangesMsg, req)
	       if err != nil {
		      return
	       }
	       res, ok := msg.(*snap.StorageRangesPacket)
	       if !ok {
		      return
	       }
               for _, slots := range res.Slots {
                      for j := 0; j < len(slots); j++ {
                            slot_hash:=slots[j].Hash
                            balance:=new(big.Int).SetBytes(slots[j].Body)
	                    fmt.Println("3. Slot hash from Peer: ",slot_hash)
	                    fmt.Println("4. Balance from Peer: ",balance)
                      }
               }
}

After i excuted above code, the result is:

1. USDT Token hash:              0xab14d68802a763f7db875346d03fbf86f137de55814b191c069e721f47474733
2. Start slot hash:             0xc83ac632ddd978600e673c3064377a289fc7b6beba2050e1c1b0c5905a89fb9e 
3. Slot hash from Peer:          0xc83ac7360fc812328443907c5af92c510beeec16180b33b60783bada4d36ecf5 
4. Balance from Peer:            18642526367207907463194928586398753164376467531168130810352671025274013872029695;

Slot hash (3) return from Peer is difference with (2) and blance (4) from Peer also difference with my current USDT balance. What is wrong in my code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant