[SCH] foundry cast를 이용하여 on-chain data를 읽어보자

frenchkebab·2023년 5월 14일
0

EIP / Open Source

목록 보기
7/9
post-thumbnail

원래는 ethers.jsgetStorageAt()을 사용하는 방법만 알고 있었는데,
foundrycast를 사용하면 훨씬 간편하게 deploy된 contract의 storage를 읽어올 수 있다.

https://book.getfoundry.sh/reference/cast/cast
foundrybook을 보면 전반적인 사용 방법이 나와 있다.

예시

cast로 storage 값 읽어오기

https://book.getfoundry.sh/reference/cast/cast-storage?highlight=storage#cast-storage
여기를 보면 storage와 관련된 방식이 나와있다.

cast storage <CONTRACT_ADDRESS> <SLOT_NUMBER> --rpc-url <YOUR_ROC_URL>

예시를 들면 이렇게 사용할 수 있다.

cast storage 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 0

뒤에 rpc url을 설정해줘서 원하는 네트워크를 사용할 수도 있다.

tx의 정보 읽어오기

cast tx $TX_HASH

특정 트랜잭션에 대한 정보를 받아올 수 있다.

cast tx $TX_HASH sender

해당 transcation의 sender에 대한 정보를 받아올 수 있다.

getter function 호출하기

function signature 지정하여 getter 함수를 통해 정보를 받아올 수도 있다.

$ cast call <CONTRACT_ADDRESS> "totalSupply()(uint256) --rpc-url <YOUR_URL>"
export CONTRACT=<CONTRACT_ADDRESS>
export TOKEN_ID=<TOKEN_ID>

cast call $CONTRACT "tokenURI(uint256)(sring)" $TOKEN_ID

다른 예시들

https://book.getfoundry.sh/reference/cast/cast
여기 보면 여러가지 예시가 나온다

cast --help

로 여러 명령어를 확인할 수 있다.

1. General한 정보들 받아오기

1) chain id 받아오기

$ cast chain-id --rpc-url https://rpc.ankr.com/bsc
56

: BSC의 chain id는 56이다.

2) 마지막 block number 받아오기

$ cast block-number --rpc-url https://rpc.ankr.com/bsc
28195555

현재 글 기준으로, 마지막으로 validate된 블록 넘버는 28195555이다.

2. transaction/block 정보들 받아오기

1) tx 정보들 읽어오기

0x3f6da406747a55797a7f84173cbb243f4fd929d57326fdcfcf8d7ca55b75fe99

$ cast tx 0x3f6da406747a55797a7f84173cbb243f4fd929d57326fdcfcf8d7ca55b75fe99 --rpc-url https://rpc.ankr.com/bsc

blockHash            0x82af8b2f3896fa7b30a696c1e65555f1f0fa6d8f28a0786fcc46defdef290ebc
blockNumber          25263862
from                 0x8CC1cfFA1Aa4f2117C688e2af56870Cb9dA2482e
gas                  222434
gasPrice             5000000000
hash                 0x3f6da406747a55797a7f84173cbb243f4fd929d57326fdcfcf8d7ca55b75fe99
input                0x88303dbd000000000000000000000000000000000000000000000000000000000000031c00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000010974d
nonce                36
r                    0x13ee6cd5f0583ebd1c245552ad0f991301afb0d882376abf7f1eea262ea52238
s                    0x3cca4db7cddaffb990674eea31e1ccbf0b03b84866221a3ec4e6d7879f5989aa
to                   0x5aF6D33DE2ccEC94efb1bDF8f92Bd58085432d2c
transactionIndex     101
v                    148
value                0

2) 해당 block의 timestamp와 miner 주소 받아오기

$ cast block 25263862 --rpc-url https://rpc.ankr.com/bsc


baseFeePerGas        
difficulty           2
extraData            0xd983010112846765746889676f312e31372e3133856c696e757800005b7663b513d78398c987a176428487e68d1d8723c6a86a7b9e299621cbee670115fd6a961078cd7d397631fea106864d39a46fbe8b5770645569e49ef5cf94e9e4c3b8a800
gasLimit             139997863
gasUsed              16493279
hash                 0x82af8b2f3896fa7b30a696c1e65555f1f0fa6d8f28a0786fcc46defdef290ebc
logsBloom            0x28262e01510c17371a8d204a8c045c86618e53e8947c872a77d510a022141951628a3361c88f140853857100006619e128971096433a308a54998d7e10fc92be6420944ea80d4f5c3b10870d54a23b7cf89a10a1e9560b068a26804dd0bdcf0069f918ad97c322d33d41e91a6086bc133ec0a34dea6c75b479e6505d41b6dc7373afbbf2267910515c9317418f1ac3b1a6258d85583a32383d59da5858c02a25aa8a80645fc80a91028601426b8f24acecee1265604023051c223d5c097f1fda23848c43fd264c9a40a035358ab236443162611f5129649e0ec92dceef54fc5c4fd2095b0ca45409d1d11bcbcf210308e03477db5b519a4a2a09e288f158507b
miner                0x2465176C461AfB316ebc773C61fAEe85A6515DAA
mixHash              0x0000000000000000000000000000000000000000000000000000000000000000
nonce                0x0000000000000000
number               25263862
parentHash           0xc9ca020ef35624a5a50f09f417b9069b3d1031ecb913fc6a47b51c48d3cd3b29
receiptsRoot         0xfb2b2aab5bd47d1d4d8be9438bacba5fb6fe3235e4dc5329a8b7d8ca4a0f9f94
sealFields           []
sha3Uncles           0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347
size                 44150
stateRoot            0x3048ac52f5aad00d728445e217ec9c99a9aa4131f1f3b8b1387a23620a59a2f6
timestamp            1675173198
totalDifficulty      50212610
transactions:        [
	0xc01413a12ba889a4f4afb70659fb45559b05a7105564584574c2e1452d9d7fb5

...
]

3) transaction의 input data와 호출한 contract 확인해보기

cast tx 0x3f6da406747a55797a7f84173cbb243f4fd929d57326fdcfcf8d7ca55b75fe99 --rpc-url https://rpc.ankr.com/bsc input
0x88303dbd000000000000000000000000000000000000000000000000000000000000031c00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000010974d
$ cast tx 0x3f6da406747a55797a7f84173cbb243f4fd929d57326fdcfcf8d7ca55b75fe99 --rpc-url https://rpc.ankr.com/bsc to  
0x5aF6D33DE2ccEC94efb1bDF8f92Bd58085432d2c

3. transaction 분석

1) 해당 transaction의 호출된 함수와 parameter 찾기

$ cast 4byte 88303dbd
buyTickets(uint256,uint32[])

이건 좀 쩌는 것 같다...

2) input data를 decode하기

cast 4byte-decode 0x88303dbd000000000000000000000000000000000000000000000000000000000000031c00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000010974d
1) "buyTickets(uint256,uint32[])"
796
[1087309]

이건 좀 더 쩌는 것 같다 ㄷㄷ;;

88303dbd
000000000000000000000000000000000000000000000000000000000000031c
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000001
000000000000000000000000000000000000000000000000000000000010974d

원래같았으면 https://openchain.xyz/signatures 여기에 selector를 돌려보고,
이렇게 하나하나 끊어서 직접 분석했을거다.

4. Contract의 Storage 분석하기

1) Contract의 bytecode

cast code 0x5aF6D33DE2ccEC94efb1bDF8f92Bd58085432d2c --rpc-url https://rpc.ankr.com/bsc

2) storage slot 0, 1, 2 확인

cast storage 0x5aF6D33DE2ccEC94efb1bDF8f92Bd58085432d2c 0 --rpc-url https://rpc.ankr.com/bsc

cast storage 0x5aF6D33DE2ccEC94efb1bDF8f92Bd58085432d2c 1 --rpc-url https://rpc.ankr.com/bsc

cast storage 0x5aF6D33DE2ccEC94efb1bDF8f92Bd58085432d2c 2 --rpc-url https://rpc.ankr.com/bsc

결론

foundry의 cast의 몇 가지 예시에 대해 정리해 보았다.

etherscan을 보면서 약간 부가 적인 사용은 가능할 것 같지만,
cli 환경이라 테스트 시에는 결국 cheatcode를 더 많이 알아두어야 할 것 같다.

profile
Blockchain Dev Journey

0개의 댓글