Solidity编码简要学习
solidity中函数编码按参数顺序,但是如果是动态类型,相应位置保存的是该动态类型元素的的起始位置。
1、直接使用abi编码
代码片断如下:
function test2(address[] calldata pairs) external view returns(bytes memory) {
uint[] memory pays = new uint[](3);
pays[0] = 12345;
pays[1] = 678910;
pays[2] = 888888888;
bytes memory data = abi.encode(pairs[1],pairs[2],pays);
return data;
}
编码后的结果如下:
0x0000 : 000000000000000000000000696d67a2ab7296527af05ac1eb5d57fac0a5a664
0x0020 : 000000000000000000000000ee095890fef04ad2b53c4ade71cd1b74429990c6
0x0040 : 0000000000000000000000000000000000000000000000000000000000000060
0x0060 : 0000000000000000000000000000000000000000000000000000000000000003
0x0080 : 0000000000000000000000000000000000000000000000000000000000003039
0x00a0 : 00000000000000000000000000000000000000000000000000000000000a5bfe
0x00c0 : 0000000000000000000000000000000000000000000000000000000034fb5e38
从上面的结果可以看出,编码是按顺序编码的
我们调整一下代码,将pays放在两个pair中间
bytes memory data = abi.encode(pairs[1],pays,pairs[2]);
得到的结果如下:
0x0000 : 000000000000000000000000696d67a2ab7296527af05ac1eb5d57fac0a5a664
0x0020 : 0000000000000000000000000000000000000000000000000000000000000060
0x0040 : 000000000000000000000000ee095890fef04ad2b53c4ade71cd1b74429990c6
0x0060 : 0000000000000000000000000000000000000000000000000000000000000003
0x0080 : 0000000000000000000000000000000000000000000000000000000000003039
0x00a0 : 00000000000000000000000000000000000000000000000000000000000a5bfe
0x00c0 : 0000000000000000000000000000000000000000000000000000000034fb5e38
这里可以看到,结果是类似的,编码优先编码值,所以数组就算在两个pair中间,他仍然只是记录了一个首字节的地址,然后先编码两个地址。就算你把数组放在最前面,得到的结果也只有第一行和第二行交换顺序而已。
2、函数参数编码
函数参数编码就是普通编码加上函数选择器。
现在我们稍微增加一点难度,将上面生成的data作为bytes编码到其它函数中,例如存在如下代码:
IPancakeCallee(to).pancakeCall(msg.sender, amountOut0, amountOut1, data);
其中参数为:
-
msg.sender 为0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 , -
amountOut0 为:128888876 -
amountOut1 为:0x12789e2b9a732000 -
data为 0x84800812000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb922660000000000000000000000000000000000000000000000000000000007aeb02c00000000000000000000000000000000000000000000000012789e2b9a732000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000696d67a2ab7296527af05ac1eb5d57fac0a5a664000000000000000000000000ee095890fef04ad2b53c4ade71cd1b74429990c600000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000303900000000000000000000000000000000000000000000000000000000000a5bfe0000000000000000000000000000000000000000000000000000000034fb5e38
全部编码后我们得到如下结果:
0x84800812(函数选择器)
0x0000 : 000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266
0x0020 : 0000000000000000000000000000000000000000000000000000000007aeb02c
0x0040 : 00000000000000000000000000000000000000000000000012789e2b9a732000
0x0060 : 0000000000000000000000000000000000000000000000000000000000000080
0x0080 : 00000000000000000000000000000000000000000000000000000000000000e0
0x00a0 : 000000000000000000000000696d67a2ab7296527af05ac1eb5d57fac0a5a664
0x00c0 : 000000000000000000000000ee095890fef04ad2b53c4ade71cd1b74429990c6
0x00e0 : 0000000000000000000000000000000000000000000000000000000000000060
0x0100 : 0000000000000000000000000000000000000000000000000000000000000003
0x0120 : 0000000000000000000000000000000000000000000000000000000000003039
0x0140 : 00000000000000000000000000000000000000000000000000000000000a5bfe
0x0160 : 0000000000000000000000000000000000000000000000000000000034fb5e38
这里我们可以看到,我们的编码仍然遵守相同的规则变化
- 首先,仍然是
msg.sender 编码,正确无误 - 接下来是 amountOut0 与amountOut1 的编码,正确,
- 接下来呢,是data的位置(在80地址)
- 接下来是80地址,它的内容为data的长度,数值为 e0,大小为7个word ( 7 * 0x20 = 7 * 32 )
- 接下来就是我们第一次的data编码了。
|