diff --git a/packages/graph-node/package.json b/packages/graph-node/package.json index ae0b1dc5..b26d6973 100644 --- a/packages/graph-node/package.json +++ b/packages/graph-node/package.json @@ -21,11 +21,12 @@ }, "scripts": { "lint": "eslint .", - "build": "tsc", + "build": "yarn asbuild:debug && yarn build:example", "asbuild:debug": "asc assembly/index.ts --lib ./node_modules --exportRuntime --target debug --runPasses asyncify", "asbuild:release": "asc assembly/index.ts --lib ./node_modules --exportRuntime --target release --runPasses asyncify", "asbuild": "yarn asbuild:debug && yarn asbuild:release", - "test": "yarn asbuild:debug && mocha src/**/*.test.ts" + "test": "yarn build && mocha src/**/*.test.ts", + "build:example": "cd test/subgraph/example1 && yarn && yarn build" }, "dependencies": { "assemblyscript": "https://github.com/vulcanize/assemblyscript.git#ng-integrate-asyncify" diff --git a/packages/graph-node/src/index.ts b/packages/graph-node/src/index.ts index 1e465fb6..8084a3ff 100644 --- a/packages/graph-node/src/index.ts +++ b/packages/graph-node/src/index.ts @@ -224,11 +224,11 @@ export const instantiate = async (filePath: string): Promise { - const xBigIntArray = __getArray(x); - const xBigNumber = BigNumber.from(xBigIntArray); + const xBigInt = await BigInt.wrap(x); + const xBigNumber = BigNumber.from(__getString(await xBigInt.toString())); - const yBigIntArray = __getArray(y); - const yBigNumber = BigNumber.from(yBigIntArray); + const yBigInt = await BigInt.wrap(y); + const yBigNumber = BigNumber.from(__getString(await yBigInt.toString())); const sum = xBigNumber.add(yBigNumber); const ptr = await __newString(sum.toString()); @@ -237,23 +237,43 @@ export const instantiate = async (filePath: string): Promise { - const xBigIntArray = __getArray(x); - const xBigNumber = BigNumber.from(xBigIntArray); + const xBigInt = await BigInt.wrap(x); + const xBigNumber = BigNumber.from(__getString(await xBigInt.toString())); - const yBigIntArray = __getArray(y); - const yBigNumber = BigNumber.from(yBigIntArray); + const yBigInt = await BigInt.wrap(y); + const yBigNumber = BigNumber.from(__getString(await yBigInt.toString())); const diff = xBigNumber.sub(yBigNumber); const ptr = await __newString(diff.toString()); - const sumBigInt = BigInt.fromString(ptr); + const diffBigInt = BigInt.fromString(ptr); - return sumBigInt; + return diffBigInt; }, - 'bigInt.dividedBy': () => { - console.log('bigInt.dividedBy'); + 'bigInt.times': async (x: number, y: number) => { + const xBigInt = await BigInt.wrap(x); + const xBigNumber = BigNumber.from(__getString(await xBigInt.toString())); + + const yBigInt = await BigInt.wrap(y); + const yBigNumber = BigNumber.from(__getString(await yBigInt.toString())); + + const product = xBigNumber.mul(yBigNumber); + const ptr = await __newString(product.toString()); + const productBigInt = BigInt.fromString(ptr); + + return productBigInt; }, - 'bigInt.times': () => { - console.log('bigInt.times'); + 'bigInt.dividedBy': async (x: number, y: number) => { + const xBigInt = await BigInt.wrap(x); + const xBigNumber = BigNumber.from(__getString(await xBigInt.toString())); + + const yBigInt = await BigInt.wrap(y); + const yBigNumber = BigNumber.from(__getString(await yBigInt.toString())); + + const quotient = xBigNumber.div(yBigNumber); + const ptr = await __newString(quotient.toString()); + const quotientBigInt = BigInt.fromString(ptr); + + return quotientBigInt; }, 'bigInt.dividedByDecimal': () => { console.log('bigInt.dividedByDecimal'); diff --git a/packages/graph-node/src/numbers.test.ts b/packages/graph-node/src/numbers.test.ts index 23f748a2..66eb2714 100644 --- a/packages/graph-node/src/numbers.test.ts +++ b/packages/graph-node/src/numbers.test.ts @@ -44,6 +44,20 @@ describe('numbers wasm tests', () => { expect(__getString(ptr)).to.equal('100'); }); + it('should execute bigInt times API', async () => { + const { testBigIntTimes, __getString } = exports; + + const ptr = await testBigIntTimes(); + expect(__getString(ptr)).to.equal('1000'); + }); + + it('should execute bigInt dividedBy API', async () => { + const { testBigIntDividedBy, __getString } = exports; + + const ptr = await testBigIntDividedBy(); + expect(__getString(ptr)).to.equal('100'); + }); + xit('should execute bigDecimal dividedBy API', () => { const { testBigDecimalDividedBy, __getString } = exports; diff --git a/packages/graph-node/test/subgraph/example1/src/mapping.ts b/packages/graph-node/test/subgraph/example1/src/mapping.ts index f51f91f8..5b30ce62 100644 --- a/packages/graph-node/test/subgraph/example1/src/mapping.ts +++ b/packages/graph-node/test/subgraph/example1/src/mapping.ts @@ -139,6 +139,28 @@ export function testBigIntMinus (): string { return res.toString(); } +export function testBigIntTimes (): string { + log.debug('In test bigInt.times', []); + + const bigInt1 = BigInt.fromString('100'); + const bigInt2 = BigInt.fromString('10'); + + const res = bigInt1 * bigInt2; + log.debug('bigInt.times result: {}', [res.toString()]); + return res.toString(); +} + +export function testBigIntDividedBy (): string { + log.debug('In test bigInt.dividedBy', []); + + const bigInt1 = BigInt.fromString('1000'); + const bigInt2 = BigInt.fromString('10'); + + const res = bigInt1 / bigInt2; + log.debug('bigInt.dividedBy result: {}', [res.toString()]); + return res.toString(); +} + export function testBigIntFromString (): string { log.debug('In test bigInt.fromString', []);