eosjs icon indicating copy to clipboard operation
eosjs copied to clipboard

Deserialize Table_rows

Open regisBafutwabo opened this issue 4 years ago • 1 comments

Is there a way to deserialize the table_rows returned data that are serialized since they are not actions? (preferably through eosjs)

here below is the screenshot of what I was trying to accomplish:

  • on the left is the request
  • and on the right is the returned data
Screen Shot 2020-04-13 at 3 00 24 PM

regisBafutwabo avatar Apr 13 '20 06:04 regisBafutwabo

function deserializeTypeData(contract: Contract, account: string, structName: string, data: string | Uint8Array, textEncoder: TextEncoder, textDecoder: TextDecoder): any {
    const type = contract.types.get(structName);
    if (typeof data === "string") {
        data = hexToUint8Array(data);
    }
    if(!type) {
        throw new Error(`Unknown type ${structName} in contract ${account}`);
    }
    const buffer = new SerialBuffer({textDecoder, textEncoder});
    buffer.pushArray(data);
    return type.deserialize(buffer);
}

This works but you should know the struct name from contract code (ex. for eosio rammarket in eosio.system/exchange_state.hpp the struct name is exchange_state)

emperorhan avatar Apr 13 '20 06:04 emperorhan