State-Modifying Functions

Để tương tác với deployed smart contract, ta cũng có thể dùng lệnh cast với subcommand send:

cast send 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512 "store(uint256)" 1337 --rpc-url $RPC_URL --private-key $PRIVATE_KEY

Với:

  • send là để publish một transaction có thay đổi state của smart contract
  • "store(uint256)" là signature của hàm mà ta cần gọi.
  • 1337 là đối số truyền vào hàm cần gọi.

Output:

cast send 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512 "store(uint256)" 1337 --account account1 --rpc-url http://127.0.0.1:8545
Enter keystore password:
 
 
blockHash            0x26cc45cba479f724add99ba08a221b88b96aecdb82a090437b0a0997c109cdf8
blockNumber          3
contractAddress
cumulativeGasUsed    43758
effectiveGasPrice    773852900
from                 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
gasUsed              43758
logs                 []
logsBloom            0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
root
status               1 (success)
transactionHash      0x1b006425214fe8254af4313ff21c775f4867bf6faffa64869d6de993c9e5d574
transactionIndex     0
type                 2
blobGasPrice         1
blobGasUsed
to                   0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512

Read-Only Functions

Để gọi một hàm không làm thay đổi state của smart contract (không tạo transaction), ta dùng cast với subcommand call:

cast call 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512 "retrieve()"

Giá trị nhận được có thể là:

0x0000000000000000000000000000000000000000000000000000000000000539

Dùng cast để chuyển về decimal:

cast --to-base 0x0000000000000000000000000000000000000000000000000000000000000539 dec
1337

Function Selector

Dùng lệnh cast kèm theo signature của hàm để tính toán function selector:

cast sig "fund()"

Calldata Decode

Giả sử calldata có giá trị là;

0xca1d209d000000000000000000000000000000000000000000000000016345785d8a0000

Decode sử dụng cast như sau;

cast --calldata-decode "fund(uint256)" 0xca1d209d000000000000000000000000000000000000000000000000016345785d8a0000

Với positional argument đầu tiên là signature của hàm còn positional argument thứ hai là callldata.

Output:

100000000000000000 [1e17]

Tip

Bằng cách này, chúng ta có thể calldata của từng giao dịch (là một việc nên làm) chỉ bằng cách biết function signature và calldata của giao dịch đó.

Resources