Всегда возвращает пустой массив массивов, хотя он показывает успешную транзакцию в сети Shasta - PullRequest
0 голосов
/ 08 апреля 2020

Я пытаюсь вернуть все записи типа struct из моей функции solidity contract, см. Ниже solidity contract фрагмент кода


struct Record {
    uint _id;
    string _countryCode;
    string _state;
    string _postal;
    string _location;
    string _userId;
    string _additionalDetails;
}
Record[] public records;

function getEntries() public view returns (Record[] memory) {
    return records;
}

function setRecord(
    string memory _countryCode,
    string memory _state,
    string memory _postal,
    string memory _location,
    string memory _userId,
    string memory _additionalDetails
) public onlyOwner {
    Record memory newStruct = Record({
        _id: _counter + 1,
        _countryCode: _countryCode,
        _state: _state,
        _postal: _postal,
        _location: _location,
        _userId: _userId,
        _additionalDetails: _additionalDetails
    });
    records.push(newStruct);
    _counter++;
}

Я успешно могу добавить новый запись с использованием tronweb в nodejs ниже в моем NodeJs фрагменте кода


const walletAddress = "";
const privateKey = "";
const contractAddress = "";

tronweb.setAddress(walletAddress);
tronweb.setPrivateKey(privateKey);
const init = async () => {
    let contract = await tronweb.contract().at(contractAddress);
    const getRecords = async () => await contract.getEntries().call();
    const setRecord = async () => await contract.setRecord(

            "USA",
            "LA",
            "12345",
            "Street 1",
            "1324-12345-12456-45642",
            "Testing notes"

    ).send({
        "from": walletAddress
    });
    await setRecord();
    getRecords().then(result => {
        console.log(result); // getting empty array like [ [], [] ]
    });
};
init();

Вот консольное изображение результатов моего nodejs сценария, которое всегда возвращает пустой массив массивов

enter image description here

Вот детали транзакции сети Shasta со статусом CONFIRMED

enter image description here

Кто-нибудь может мне помочь?

1 Ответ

1 голос
/ 12 апреля 2020

В ABI любое возвращаемое значение типа структуры будет интерпретировано как tuple. В вашем случае это тип tuple[].

Библиотека не имеет ни малейшего представления о том, какой у вас tuple размер, каков его тип.

Вы должны проанализировать возвращаемые данные. рукой.

{
  "constant_result": [
    "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000005800000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000002400000000000000000000000000000000000000000000000000000000000000003555341000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024c4100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000053132333435000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000853747265657420310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016313332342d31323334352d31323435362d343536343200000000000000000000000000000000000000000000000000000000000000000000000000000000000d54657374696e67206e6f74657300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000002400000000000000000000000000000000000000000000000000000000000000003555341000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024c4100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000053132333435000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000853747265657420310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016313332342d31323334352d31323435362d343536343200000000000000000000000000000000000000000000000000000000000000000000000000000000000d54657374696e67206e6f74657300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000002400000000000000000000000000000000000000000000000000000000000000003555341000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024c4100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000053132333435000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000853747265657420310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016313332342d31323334352d31323435362d343536343200000000000000000000000000000000000000000000000000000000000000000000000000000000001254657374696e672077697468204a616d65730000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000002400000000000000000000000000000000000000000000000000000000000000003555341000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024c4100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000053132333435000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000853747265657420310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016313332342d31323334352d31323435362d343536343200000000000000000000000000000000000000000000000000000000000000000000000000000000001e54657374696e672077697468204a616d6573206e65772076657273696f6e0000"
  ],
  "result": {
    "code": "SUCCESS",
    "message": "",
    "result": true
  }
}

Чтобы разобрать константу_результата:

  • выровняйте ее по границе 64 символа (256 бит)
  • обычно значения смещений или длин
  • для деталей, вы должны обратиться к ETH ABI do c
0000000000000000000000000000000000000000000000000000000000000020  offset of first parm
0000000000000000000000000000000000000000000000000000000000000004
0000000000000000000000000000000000000000000000000000000000000080  
0000000000000000000000000000000000000000000000000000000000000300
0000000000000000000000000000000000000000000000000000000000000580
0000000000000000000000000000000000000000000000000000000000000800
0000000000000000000000000000000000000000000000000000000000000001  #first
0000000000000000000000000000000000000000000000000000000000000100
0000000000000000000000000000000000000000000000000000000000000140
0000000000000000000000000000000000000000000000000000000000000180
00000000000000000000000000000000000000000000000000000000000001c0
0000000000000000000000000000000000000000000000000000000000000200
0000000000000000000000000000000000000000000000000000000000000001  
0000000000000000000000000000000000000000000000000000000000000240
0000000000000000000000000000000000000000000000000000000000000003  a string with 3 chars
5553410000000000000000000000000000000000000000000000000000000000  "USA"
0000000000000000000000000000000000000000000000000000000000000002  a string with 2 chars
4c41000000000000000000000000000000000000000000000000000000000000  "LA"
0000000000000000000000000000000000000000000000000000000000000005  
3132333435000000000000000000000000000000000000000000000000000000  '12345'
0000000000000000000000000000000000000000000000000000000000000008
5374726565742031000000000000000000000000000000000000000000000000  'Street 1'
0000000000000000000000000000000000000000000000000000000000000016
313332342d31323334352d31323435362d343536343200000000000000000000  '1324-12345-12456-45642'
000000000000000000000000000000000000000000000000000000000000000d
54657374696e67206e6f74657300000000000000000000000000000000000000  'Testing notes'
0000000000000000000000000000000000000000000000000000000000000002  #second
0000000000000000000000000000000000000000000000000000000000000100
0000000000000000000000000000000000000000000000000000000000000140
0000000000000000000000000000000000000000000000000000000000000180
00000000000000000000000000000000000000000000000000000000000001c0
0000000000000000000000000000000000000000000000000000000000000200
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000240
0000000000000000000000000000000000000000000000000000000000000003
5553410000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000002
4c41000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000005
3132333435000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000008
5374726565742031000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000016
313332342d31323334352d31323435362d343536343200000000000000000000
000000000000000000000000000000000000000000000000000000000000000d
54657374696e67206e6f74657300000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000003  #third
0000000000000000000000000000000000000000000000000000000000000100
0000000000000000000000000000000000000000000000000000000000000140
0000000000000000000000000000000000000000000000000000000000000180
00000000000000000000000000000000000000000000000000000000000001c0
0000000000000000000000000000000000000000000000000000000000000200
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000240
0000000000000000000000000000000000000000000000000000000000000003
5553410000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000002
4c41000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000005
3132333435000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000008
5374726565742031000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000016
313332342d31323334352d31323435362d343536343200000000000000000000
0000000000000000000000000000000000000000000000000000000000000012
54657374696e672077697468204a616d65730000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000004   #fourth
0000000000000000000000000000000000000000000000000000000000000100
0000000000000000000000000000000000000000000000000000000000000140
0000000000000000000000000000000000000000000000000000000000000180
00000000000000000000000000000000000000000000000000000000000001c0
0000000000000000000000000000000000000000000000000000000000000200
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000240
0000000000000000000000000000000000000000000000000000000000000003
5553410000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000002
4c41000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000005
3132333435000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000008
5374726565742031000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000016
313332342d31323334352d31323435362d343536343200000000000000000000
000000000000000000000000000000000000000000000000000000000000001e
54657374696e672077697468204a616d6573206e65772076657273696f6e0000
...