Serializing and Deserializing args Array ? #3019
-
hello , is there any way to serializing entire contract call returns and events ? Currently I am using this to deserializing bignumber, JSON_parse(obj) {
const func = (k, v) => {
if (typeof v == "object" && v["type"] === "BigNumber") {
return BigNumber.from(v);
}
//TODO fix args array
return v;
};
return JSON.parse(obj, func);
}, But because of the array used in ethers.js ( @ethersproject/abi/src.ts/coders/array.ts ) is vanilla array , I can't get my struct field back. export function unpack(reader: Reader, coders: Array<Coder>): Result {
let values: any = []; // why it is just a normal array ??? , it should have a special mark (tag ? symbol , like bignumber's type )
// A reader anchored to this base
let baseReader = reader.subReader(0);
... Is there any proper way to do this ? eg: contract.sol struct VeryComplexStruct {
uint256 a;
uint256 b;
uint256 c;
uint256 d;
}
function getStates() public returns (VeryComplexStruct memory) {
...
} goal: Once i get contract state (use a,b fieldName to access , not index) , I need to save them into localStorage , Next time user open the website , Load local cache first , then request latest data . (actually, it is mainly needed in events) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
It just uses normal JavaScript objects when possible; BigNumber has special detection because it’s a class ethers provides. To detect an array, you use the JavaScript |
Beta Was this translation helpful? Give feedback.
It just uses normal JavaScript objects when possible; BigNumber has special detection because it’s a class ethers provides.
To detect an array, you use the JavaScript
Array.isArray(v)
. You will lose any name properties on it, as named parameters are added within the parsing functions.