Implement BigInt times and dividedBy APIs (#30)

* Add script for building example subgraph

* Implement BigInt times and dividedBy API
This commit is contained in:
nikugogoi 2021-10-08 11:10:49 +05:30 committed by nabarun
parent ca01fa788d
commit 1ce07bbb6e
4 changed files with 73 additions and 16 deletions

View File

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

View File

@ -224,11 +224,11 @@ export const instantiate = async (filePath: string): Promise<loader.ResultObject
return bigInt;
},
'bigInt.plus': async (x: number, y: number) => {
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<loader.ResultObject
return sumBigInt;
},
'bigInt.minus': async (x: number, y: number) => {
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');

View File

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

View File

@ -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', []);