@@ -15,6 +15,43 @@ rust solana
1515## Notes
1616
1717<!-- Content_START -->
18+ # 2025-08-20
19+
20+ ABI (Application Binary Interface,应用二进制接口)是与以太坊智能合约交互的标准。数据基于他们的类型编码;并且由于编码后不包含类型信息,解码时需要注明它们的类型。
21+
22+ 函数参数编码为字节码
23+
24+ 接受一个合约地址和一段字节码, 调用该合约并传递字节码,如果调用成功则不做任何操作,否则抛出异常。
25+
26+ function test(address _ contract, bytes calldata data) external {
27+ (bool ok, ) = _ contract.call(data);
28+ require(ok, "call failed");
29+ }
30+ 接受一个地址和一个整数,返回一个ABI编码后的字节数组, 该字节数组包含了一个名为"transfer"的函数签名,以及传递给该函数的地址和整数参数。
31+
32+ function encodeWithSignature(
33+ address to,
34+ uint amount
35+ ) external pure returns (bytes memory) {
36+ // 拼写错误未被检查 - "transfer(address, uint)"
37+ return abi.encodeWithSignature("transfer(address,uint256)", to, amount);
38+ }
39+ 该字节数组使用 IERC20.transfer.selector 作为函数选择器,将 to 地址和 amount 数量编码为参数。 这个函数的目的是编码一个 ERC20 转账操作的 ABI。
40+
41+ function encodeWithSelector(
42+ address to,
43+ uint amount
44+ ) external pure returns (bytes memory) {
45+ // 类型错误未被检查 - (IERC20.transfer.selector, true, amount)
46+ return abi.encodeWithSelector(IERC20.transfer.selector, to, amount);
47+ }
48+ 函数的目的是返回一个 bytes 类型的值,这个值是通过调用 abi.encodeCall 函数来生成的。 abi.encodeCall 函数将一个函数调用打包成一个字节数组,可以用于在以太坊上进行外部函数调用。
49+
50+ function encodeCall(address to, uint amount) external pure returns (bytes memory) {
51+ // 拼写错误和类型错误将无法编译
52+ return abi.encodeCall(IERC20.transfer, (to, amount));
53+ }
54+
1855# 2025-08-19
1956
2057定义了一组通用接口,使得钱包、交易所、DApp 等可以统一地与代币交互。
0 commit comments