mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-02-08 19:12:49 +00:00
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:
parent
729d862aac
commit
ffd1d267d8
@ -767,20 +767,18 @@ export const instantiate = async (
|
|||||||
return JSONResult.__new(null);
|
return JSONResult.__new(null);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// TODO: Number methods do not work as 64bit values are not supported in js.
|
'json.toI64': (decimal: number) => {
|
||||||
// Tried solution in https://github.com/AssemblyScript/assemblyscript/issues/117#issuecomment-531556954
|
|
||||||
'json.toI64': async (decimal: number) => {
|
|
||||||
return BigInt(__getString(decimal));
|
return BigInt(__getString(decimal));
|
||||||
},
|
},
|
||||||
'json.toU64': async (decimal: number) => {
|
'json.toU64': (decimal: number) => {
|
||||||
return BigInt(__getString(decimal));
|
return BigInt(__getString(decimal));
|
||||||
},
|
},
|
||||||
'json.toF64': async (decimal: number) => {
|
'json.toF64': (decimal: number) => {
|
||||||
return BigInt(__getString(decimal));
|
return Number(__getString(decimal));
|
||||||
},
|
},
|
||||||
// TODO: Debug toBigInt not working.
|
|
||||||
'json.toBigInt': async (decimal: number) => {
|
'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"
|
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 example1"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@graphprotocol/graph-ts": "npm:@cerc-io/graph-ts@0.22.1-watcher-ts-0.1.0",
|
"@graphprotocol/graph-ts": "npm:@cerc-io/graph-ts@0.27.0-watcher-ts-0.1.3",
|
||||||
"@cerc-io/graph-cli": "0.22.4-watcher-ts-0.1.2"
|
"@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')!;
|
const numberValue = jsonData.toObject().get('numberValue')!;
|
||||||
assert(numberValue.kind === JSONValueKind.NUMBER, 'JSON value is not a number');
|
assert(numberValue.kind === JSONValueKind.NUMBER, 'JSON value is not a number');
|
||||||
|
|
||||||
// TODO: Debug json toI64 failing test case.
|
const i64Value = numberValue.toI64();
|
||||||
// const i64Value = numberValue.toI64();
|
const expectedI64: i64 = 123;
|
||||||
// assert(i64Value == 123, 'values are not equal');
|
assert(i64Value === expectedI64, 'i64 values are not equal');
|
||||||
|
|
||||||
// TODO: Debug json toBigInt failing test case.
|
const u64Value = numberValue.toU64();
|
||||||
// const bigIntValue = numberValue.toBigInt();
|
const expectedU64: u64 = 123;
|
||||||
// assert(bigIntValue.toString() == '123', 'values are not equal');
|
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 {
|
export function testJsonTryFromBytes (): void {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
specVersion: 0.0.2
|
specVersion: 0.0.4
|
||||||
schema:
|
schema:
|
||||||
file: ./schema.graphql
|
file: ./schema.graphql
|
||||||
dataSources:
|
dataSources:
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -20,20 +20,17 @@ import { RawSqlResultsToEntityTransformer } from 'typeorm/query-builder/transfor
|
|||||||
import { SelectionNode } from 'graphql';
|
import { SelectionNode } from 'graphql';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import debug from 'debug';
|
import debug from 'debug';
|
||||||
import JSONbig from 'json-bigint';
|
|
||||||
|
|
||||||
import { Database as BaseDatabase, QueryOptions, Where, CanonicalBlockHeight } from '../database';
|
import { Database as BaseDatabase, QueryOptions, Where, CanonicalBlockHeight } from '../database';
|
||||||
import { BlockProgressInterface } from '../types';
|
import { BlockProgressInterface } from '../types';
|
||||||
import { cachePrunedEntitiesCount, eventProcessingLoadEntityCacheHitCount, eventProcessingLoadEntityCount, eventProcessingLoadEntityDBQueryDuration } from '../metrics';
|
import { cachePrunedEntitiesCount, eventProcessingLoadEntityCacheHitCount, eventProcessingLoadEntityCount, eventProcessingLoadEntityDBQueryDuration } from '../metrics';
|
||||||
import { ServerConfig } from '../config';
|
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 { fromStateEntityValues } from './state-utils';
|
||||||
import { ValueKind } from './types';
|
import { ValueKind } from './types';
|
||||||
|
|
||||||
const log = debug('vulcanize:graph-database');
|
const log = debug('vulcanize:graph-database');
|
||||||
|
|
||||||
const JSONbigNative = JSONbig({ useNativeBigInt: true });
|
|
||||||
|
|
||||||
export const FILTER_CHANGE_BLOCK = '_change_block';
|
export const FILTER_CHANGE_BLOCK = '_change_block';
|
||||||
|
|
||||||
export const DEFAULT_LIMIT = 100;
|
export const DEFAULT_LIMIT = 100;
|
||||||
@ -962,7 +959,7 @@ export class GraphDatabase {
|
|||||||
const contextInstance = await Entity.__new();
|
const contextInstance = await Entity.__new();
|
||||||
|
|
||||||
const { __newString } = instanceExports;
|
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 contextKey = await __newString(key);
|
||||||
|
|
||||||
const value = JSONbigNative.parse(data);
|
const value = JSONbigNative.parse(data);
|
||||||
|
@ -7,6 +7,7 @@ import { DeepPartial, EntityTarget, InsertEvent, ObjectLiteral, Repository, Upda
|
|||||||
import { ColumnMetadata } from 'typeorm/metadata/ColumnMetadata';
|
import { ColumnMetadata } from 'typeorm/metadata/ColumnMetadata';
|
||||||
import assert from 'assert';
|
import assert from 'assert';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
import JSONbig from 'json-bigint';
|
||||||
|
|
||||||
import { MappingKey, StorageLayout } from '@cerc-io/solidity-mapper';
|
import { MappingKey, StorageLayout } from '@cerc-io/solidity-mapper';
|
||||||
|
|
||||||
@ -14,6 +15,7 @@ import { GraphDecimal } from './graph-decimal';
|
|||||||
import { EthereumValueKind, TypeId, TypeNameToValueKind, ValueKind } from './types';
|
import { EthereumValueKind, TypeId, TypeNameToValueKind, ValueKind } from './types';
|
||||||
|
|
||||||
const log = debug('vulcanize:utils');
|
const log = debug('vulcanize:utils');
|
||||||
|
export const JSONbigNative = JSONbig({ useNativeBigInt: true });
|
||||||
|
|
||||||
export const INT256_MIN = '-57896044618658097711785492504343953926634992332820282019728792003956564819968';
|
export const INT256_MIN = '-57896044618658097711785492504343953926634992332820282019728792003956564819968';
|
||||||
export const INT256_MAX = '57896044618658097711785492504343953926634992332820282019728792003956564819967';
|
export const INT256_MAX = '57896044618658097711785492504343953926634992332820282019728792003956564819967';
|
||||||
@ -762,7 +764,7 @@ export const toJSONValue = async (instanceExports: any, value: any): Promise<any
|
|||||||
return CustomJSONValue.fromString(stringPtr);
|
return CustomJSONValue.fromString(stringPtr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof value === 'number') {
|
if (typeof value === 'number' || typeof value === 'bigint') {
|
||||||
const stringPtr = await __newString(value.toString());
|
const stringPtr = await __newString(value.toString());
|
||||||
|
|
||||||
return CustomJSONValue.fromNumber(stringPtr);
|
return CustomJSONValue.fromNumber(stringPtr);
|
||||||
@ -778,7 +780,7 @@ export const jsonFromBytes = async (instanceExports: any, bytesPtr: number): Pro
|
|||||||
|
|
||||||
const byteArray = await ByteArray.wrap(bytesPtr);
|
const byteArray = await ByteArray.wrap(bytesPtr);
|
||||||
const jsonStringPtr = await byteArray.toString();
|
const jsonStringPtr = await byteArray.toString();
|
||||||
const json = JSON.parse(__getString(jsonStringPtr));
|
const json = JSONbigNative.parse(__getString(jsonStringPtr));
|
||||||
const jsonValue = await toJSONValue(instanceExports, json);
|
const jsonValue = await toJSONValue(instanceExports, json);
|
||||||
|
|
||||||
return jsonValue;
|
return jsonValue;
|
||||||
|
Loading…
Reference in New Issue
Block a user