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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 822 additions and 160 deletions

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);
}
}
};

View File

@ -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"
}
}

View File

@ -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 {

View File

@ -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

View File

@ -20,20 +20,17 @@ import { RawSqlResultsToEntityTransformer } from 'typeorm/query-builder/transfor
import { SelectionNode } from 'graphql';
import _ from 'lodash';
import debug from 'debug';
import JSONbig from 'json-bigint';
import { Database as BaseDatabase, QueryOptions, Where, CanonicalBlockHeight } from '../database';
import { BlockProgressInterface } from '../types';
import { cachePrunedEntitiesCount, eventProcessingLoadEntityCacheHitCount, eventProcessingLoadEntityCount, eventProcessingLoadEntityDBQueryDuration } from '../metrics';
import { ServerConfig } from '../config';
import { Block, formatValue, fromEntityValue, getLatestEntityFromEntity, parseEntityValue, resolveEntityFieldConflicts, toEntityValue } from './utils';
import { Block, formatValue, fromEntityValue, getLatestEntityFromEntity, parseEntityValue, resolveEntityFieldConflicts, toEntityValue, JSONbigNative } from './utils';
import { fromStateEntityValues } from './state-utils';
import { ValueKind } from './types';
const log = debug('vulcanize:graph-database');
const JSONbigNative = JSONbig({ useNativeBigInt: true });
export const FILTER_CHANGE_BLOCK = '_change_block';
export const DEFAULT_LIMIT = 100;
@ -962,7 +959,7 @@ export class GraphDatabase {
const contextInstance = await Entity.__new();
const { __newString } = instanceExports;
const contextValuePromises = Object.entries(contextData as Record<string, { type: ValueKind, data: any }>).map(async ([key, { type, data }]) => {
const contextValuePromises = Object.entries((contextData ?? {}) as Record<string, { type: ValueKind, data: any }>).map(async ([key, { type, data }]) => {
const contextKey = await __newString(key);
const value = JSONbigNative.parse(data);

View File

@ -7,6 +7,7 @@ import { DeepPartial, EntityTarget, InsertEvent, ObjectLiteral, Repository, Upda
import { ColumnMetadata } from 'typeorm/metadata/ColumnMetadata';
import assert from 'assert';
import _ from 'lodash';
import JSONbig from 'json-bigint';
import { MappingKey, StorageLayout } from '@cerc-io/solidity-mapper';
@ -14,6 +15,7 @@ import { GraphDecimal } from './graph-decimal';
import { EthereumValueKind, TypeId, TypeNameToValueKind, ValueKind } from './types';
const log = debug('vulcanize:utils');
export const JSONbigNative = JSONbig({ useNativeBigInt: true });
export const INT256_MIN = '-57896044618658097711785492504343953926634992332820282019728792003956564819968';
export const INT256_MAX = '57896044618658097711785492504343953926634992332820282019728792003956564819967';
@ -762,7 +764,7 @@ export const toJSONValue = async (instanceExports: any, value: any): Promise<any
return CustomJSONValue.fromString(stringPtr);
}
if (typeof value === 'number') {
if (typeof value === 'number' || typeof value === 'bigint') {
const stringPtr = await __newString(value.toString());
return CustomJSONValue.fromNumber(stringPtr);
@ -778,7 +780,7 @@ export const jsonFromBytes = async (instanceExports: any, bytesPtr: number): Pro
const byteArray = await ByteArray.wrap(bytesPtr);
const jsonStringPtr = await byteArray.toString();
const json = JSON.parse(__getString(jsonStringPtr));
const json = JSONbigNative.parse(__getString(jsonStringPtr));
const jsonValue = await toJSONValue(instanceExports, json);
return jsonValue;