mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-07-28 11:02:07 +00:00
Implement BigInt times and dividedBy APIs (#30)
* Add script for building example subgraph * Implement BigInt times and dividedBy API
This commit is contained in:
parent
ca01fa788d
commit
1ce07bbb6e
@ -21,11 +21,12 @@
|
|||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"lint": "eslint .",
|
"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: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:release": "asc assembly/index.ts --lib ./node_modules --exportRuntime --target release --runPasses asyncify",
|
||||||
"asbuild": "yarn asbuild:debug && yarn asbuild:release",
|
"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": {
|
"dependencies": {
|
||||||
"assemblyscript": "https://github.com/vulcanize/assemblyscript.git#ng-integrate-asyncify"
|
"assemblyscript": "https://github.com/vulcanize/assemblyscript.git#ng-integrate-asyncify"
|
||||||
|
@ -224,11 +224,11 @@ export const instantiate = async (filePath: string): Promise<loader.ResultObject
|
|||||||
return bigInt;
|
return bigInt;
|
||||||
},
|
},
|
||||||
'bigInt.plus': async (x: number, y: number) => {
|
'bigInt.plus': async (x: number, y: number) => {
|
||||||
const xBigIntArray = __getArray(x);
|
const xBigInt = await BigInt.wrap(x);
|
||||||
const xBigNumber = BigNumber.from(xBigIntArray);
|
const xBigNumber = BigNumber.from(__getString(await xBigInt.toString()));
|
||||||
|
|
||||||
const yBigIntArray = __getArray(y);
|
const yBigInt = await BigInt.wrap(y);
|
||||||
const yBigNumber = BigNumber.from(yBigIntArray);
|
const yBigNumber = BigNumber.from(__getString(await yBigInt.toString()));
|
||||||
|
|
||||||
const sum = xBigNumber.add(yBigNumber);
|
const sum = xBigNumber.add(yBigNumber);
|
||||||
const ptr = await __newString(sum.toString());
|
const ptr = await __newString(sum.toString());
|
||||||
@ -237,23 +237,43 @@ export const instantiate = async (filePath: string): Promise<loader.ResultObject
|
|||||||
return sumBigInt;
|
return sumBigInt;
|
||||||
},
|
},
|
||||||
'bigInt.minus': async (x: number, y: number) => {
|
'bigInt.minus': async (x: number, y: number) => {
|
||||||
const xBigIntArray = __getArray(x);
|
const xBigInt = await BigInt.wrap(x);
|
||||||
const xBigNumber = BigNumber.from(xBigIntArray);
|
const xBigNumber = BigNumber.from(__getString(await xBigInt.toString()));
|
||||||
|
|
||||||
const yBigIntArray = __getArray(y);
|
const yBigInt = await BigInt.wrap(y);
|
||||||
const yBigNumber = BigNumber.from(yBigIntArray);
|
const yBigNumber = BigNumber.from(__getString(await yBigInt.toString()));
|
||||||
|
|
||||||
const diff = xBigNumber.sub(yBigNumber);
|
const diff = xBigNumber.sub(yBigNumber);
|
||||||
const ptr = await __newString(diff.toString());
|
const ptr = await __newString(diff.toString());
|
||||||
const sumBigInt = BigInt.fromString(ptr);
|
const diffBigInt = BigInt.fromString(ptr);
|
||||||
|
|
||||||
return sumBigInt;
|
return diffBigInt;
|
||||||
},
|
},
|
||||||
'bigInt.dividedBy': () => {
|
'bigInt.times': async (x: number, y: number) => {
|
||||||
console.log('bigInt.dividedBy');
|
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': () => {
|
'bigInt.dividedBy': async (x: number, y: number) => {
|
||||||
console.log('bigInt.times');
|
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': () => {
|
'bigInt.dividedByDecimal': () => {
|
||||||
console.log('bigInt.dividedByDecimal');
|
console.log('bigInt.dividedByDecimal');
|
||||||
|
@ -44,6 +44,20 @@ describe('numbers wasm tests', () => {
|
|||||||
expect(__getString(ptr)).to.equal('100');
|
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', () => {
|
xit('should execute bigDecimal dividedBy API', () => {
|
||||||
const { testBigDecimalDividedBy, __getString } = exports;
|
const { testBigDecimalDividedBy, __getString } = exports;
|
||||||
|
|
||||||
|
@ -139,6 +139,28 @@ export function testBigIntMinus (): string {
|
|||||||
return res.toString();
|
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 {
|
export function testBigIntFromString (): string {
|
||||||
log.debug('In test bigInt.fromString', []);
|
log.debug('In test bigInt.fromString', []);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user