Solidity
如何為solidity函式呼叫編碼數組
我想了解如何在 Solidity 中對以下數組進行編碼。
動態數組
例如,以下內容:
function getDynamicUintArray() public pure returns (uint[] memory) { uint[] memory array = new uint[](3); array[0] = 1; array[1] = 2; array[2] = 3; return array; }
遵循文件中的正式規範:我們將按照以下步驟進行:
enc(array) = enc(len(array)) enc(array[0]) enc(array[1]) enc([array[2])
這將導致以下結果:
0000000000000000000000000000000000000000000000000000000000000003 0000000000000000000000000000000000000000000000000000000000000001 0000000000000000000000000000000000000000000000000000000000000002 0000000000000000000000000000000000000000000000000000000000000003
問題是當我為部署在Rinkeby上的合約執行此操作時,並執行以下 RPC 呼叫:
curl <rinkeby_endpoint> -X POST --data '{"jsonrpc":"2.0", "method":"eth_call", "params":[{"from": "<send_address>", "to": "0xaD2bbEeF0A1FCa287d7a1d6cFa925E8C79cC7aC0", "data": "0x421fc531"}, "latest"], "id":4}'
我得到以下結果:
{"jsonrpc":"2.0","id":4,"result":"0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003"}
0000000000000000000000000000000000000000000000000000000000000020
開頭有一個額外的。究竟是什麼額外的?結構數組
此外,在編碼結構時。下面的例子:
struct Foo { string id; string name; }
當我執行以下功能時:
function getMultipleFoo() public pure returns (Foo[] memory) { Foo[3] multipleFoo; Foo memory _foo = Foo("id", "name"); multipleFoo[0] = _foo; multipleFoo[1] = _foo; multipleFoo[2] = _foo; return multipleFoo; }
我應該收到類似的東西:
0000000000000000000000000000000000000000000000000000000000000060 == offset of first struct 0000000000000000000000000000000000000000000000000000000000000120 == offset of second struct 00000000000000000000000000000000000000000000000000000000000001e0 == offset of third struct 0000000000000000000000000000000000000000000000000000000000000040 == offset of first struct string 0000000000000000000000000000000000000000000000000000000000000080 == offset of second struct string 0000000000000000000000000000000000000000000000000000000000000002 == first string length 6964000000000000000000000000000000000000000000000000000000000000 == "id" hex representation 0000000000000000000000000000000000000000000000000000000000000004 == second string length 6e616d6500000000000000000000000000000000000000000000000000000000 == "name" hex representation 0000000000000000000000000000000000000000000000000000000000000040 == offset of first struct string 0000000000000000000000000000000000000000000000000000000000000080 == offset of second struct string 0000000000000000000000000000000000000000000000000000000000000002 == first string length 6964000000000000000000000000000000000000000000000000000000000000 == "id" hex representation 0000000000000000000000000000000000000000000000000000000000000004 == second string length 6e616d6500000000000000000000000000000000000000000000000000000000 == "name" hex representation 0000000000000000000000000000000000000000000000000000000000000040 == offset of first struct string 0000000000000000000000000000000000000000000000000000000000000080 == offset of second struct string 0000000000000000000000000000000000000000000000000000000000000002 == first string length 6964000000000000000000000000000000000000000000000000000000000000 == "id" hex representation 0000000000000000000000000000000000000000000000000000000000000004 == second string length 6e616d6500000000000000000000000000000000000000000000000000000000 == "name" hex representation
但是,當我獲得該函式呼叫的返回值時,我會
0000000000000000000000000000000000000000000000000000000000000020
像第一種情況一樣在開始時獲得一個額外的值。
如果您作為參數傳遞或從函式接收非規範類型的值,例如字元串或數組,則其描述的偏移量將在相應參數或返回值的位置傳遞。
因此,對於兩個 bytese32 數組,它將是 (array1
$$ 2 $$, 數組2$$ 3 $$):
000 0000000000000000000000000000000000000000000000000000000000000040 020 00000000000000000000000000000000000000000000000000000000000000a0 040 0000000000000000000000000000000000000000000000000000000000000002 060 0000000000000000000000000000000000000000000000000000000000000101 080 0000000000000000000000000000000000000000000000000000000000000102 0a0 0000000000000000000000000000000000000000000000000000000000000003 0c0 0000000000000000000000000000000000000000000000000000000000000201 0e0 0000000000000000000000000000000000000000000000000000000000000202 100 0000000000000000000000000000000000000000000000000000000000000203
不幸的是,我無法再找到描述此內容的文檔,因此附上我使用 ROC API 的說明的連結(這是我在 habr.com 上的文章的翻譯) https://github.com/oldmadjackal/人群/blob/master/Doc/Wiki/Habr-Ethereum-RPC_API.docx