Implement remaining json host APIs (#487)

* Instantiate a new wasm string to convert JSON value to BigInt

* Implement remaining json host APIs

* Upgrade graph-ts and graph-cli in test example subgraph

* Handle null context for dataSource context host API

* Use JSONBig for parsing JSON strings
This commit is contained in:
prathamesh0
2023-11-22 18:03:21 +05:30
committed by GitHub
parent 729d862aac
commit ffd1d267d8
7 changed files with 822 additions and 160 deletions
+6 -8
View File
@@ -767,20 +767,18 @@ export const instantiate = async (
return JSONResult.__new(null);
}
},
// TODO: Number methods do not work as 64bit values are not supported in js.
// Tried solution in https://github.com/AssemblyScript/assemblyscript/issues/117#issuecomment-531556954
'json.toI64': async (decimal: number) => {
'json.toI64': (decimal: number) => {
return BigInt(__getString(decimal));
},
'json.toU64': async (decimal: number) => {
'json.toU64': (decimal: number) => {
return BigInt(__getString(decimal));
},
'json.toF64': async (decimal: number) => {
return BigInt(__getString(decimal));
'json.toF64': (decimal: number) => {
return Number(__getString(decimal));
},
// TODO: Debug toBigInt not working.
'json.toBigInt': async (decimal: number) => {
return ASBigInt.fromString(decimal);
const ptr = await __newString(__getString(decimal));
return ASBigInt.fromString(ptr);
}
}
};
@@ -10,7 +10,7 @@
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 example1"
},
"dependencies": {
"@graphprotocol/graph-ts": "npm:@cerc-io/graph-ts@0.22.1-watcher-ts-0.1.0",
"@cerc-io/graph-cli": "0.22.4-watcher-ts-0.1.2"
"@graphprotocol/graph-ts": "npm:@cerc-io/graph-ts@0.27.0-watcher-ts-0.1.3",
"@cerc-io/graph-cli": "0.32.0-watcher-ts-0.1.3"
}
}
@@ -577,13 +577,21 @@ export function testJsonFromBytes (): void {
const numberValue = jsonData.toObject().get('numberValue')!;
assert(numberValue.kind === JSONValueKind.NUMBER, 'JSON value is not a number');
// TODO: Debug json toI64 failing test case.
// const i64Value = numberValue.toI64();
// assert(i64Value == 123, 'values are not equal');
const i64Value = numberValue.toI64();
const expectedI64: i64 = 123;
assert(i64Value === expectedI64, 'i64 values are not equal');
// TODO: Debug json toBigInt failing test case.
// const bigIntValue = numberValue.toBigInt();
// assert(bigIntValue.toString() == '123', 'values are not equal');
const u64Value = numberValue.toU64();
const expectedU64: u64 = 123;
assert(u64Value === expectedU64, 'u64 values are not equal');
const f64Value = numberValue.toF64();
const expectedF64: f64 = 123;
assert(f64Value === expectedF64, 'f64 values are not equal');
const bigIntValue = numberValue.toBigInt();
const expectedBigInt = BigInt.fromString('123');
assert(bigIntValue.equals(expectedBigInt), 'BigInt values are not equal');
}
export function testJsonTryFromBytes (): void {
@@ -1,4 +1,4 @@
specVersion: 0.0.2
specVersion: 0.0.4
schema:
file: ./schema.graphql
dataSources:
File diff suppressed because it is too large Load Diff