Implement BigInt times and dividedBy APIs (#30)

* Add script for building example subgraph

* Implement BigInt times and dividedBy API
This commit is contained in:
2021-12-28 16:08:05 +05:30
committed by nabarun
parent ca01fa788d
commit 1ce07bbb6e
4 changed files with 73 additions and 16 deletions
+34 -14
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');
+14
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;