Skip to content

Commit

Permalink
Implement remaining json host APIs (#487)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
prathamesh0 authored Nov 22, 2023
1 parent 729d862 commit ffd1d26
Show file tree
Hide file tree
Showing 7 changed files with 822 additions and 160 deletions.
14 changes: 6 additions & 8 deletions packages/graph-node/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions packages/graph-node/test/subgraph/example1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
20 changes: 14 additions & 6 deletions packages/graph-node/test/subgraph/example1/src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/graph-node/test/subgraph/example1/subgraph.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
specVersion: 0.0.2
specVersion: 0.0.4
schema:
file: ./schema.graphql
dataSources:
Expand Down
Loading

0 comments on commit ffd1d26

Please sign in to comment.