Skip to content

Commit

Permalink
Implement host API for big decimal equals (#476)
Browse files Browse the repository at this point in the history
* Implement host api for big decimal equals

* Add test cases for big decimal equals

* Use boolean type conversion

---------

Co-authored-by: neeraj <neeraj.rtly@gmail.com>
  • Loading branch information
nikugogoi and neerajvijay1997 authored Nov 17, 2023
1 parent 7c8f84b commit e54f4c9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
16 changes: 16 additions & 0 deletions packages/graph-node/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,23 @@ export const instantiate = async (

return powResultBigDecimal;
},
'bigDecimal.equals': async (x: number, y: number) => {
// Create decimal x string.
const xBigDecimal = await BigDecimal.wrap(x);
const xStringPtr = await xBigDecimal.toString();
const xDecimalString = __getString(xStringPtr);
const xDecimal = new GraphDecimal(xDecimalString);

// Create decimal y string.
const yBigDecimal = await BigDecimal.wrap(y);
const yStringPtr = await yBigDecimal.toString();
const yDecimalString = __getString(yStringPtr);

// Perform the decimal equal operation.
const isEqual = xDecimal.equals(yDecimalString);

return isEqual;
},
'bigInt.fromString': async (s: number) => {
const string = __getString(s);

Expand Down
18 changes: 18 additions & 0 deletions packages/graph-node/src/numbers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,4 +638,22 @@ describe('numbers wasm tests', () => {
expect(__getString(ptr)).to.equal(expected);
});
});

describe('should execute bigDecimal equals API', () => {
let testBigDecimalEquals: any, __newString: any;

before(() => {
({ testBigDecimalEquals, __newString } = exports);
});

it('should check given bigDecimals are equal', async () => {
const ptr = await testBigDecimalEquals(await __newString('231543212.2132354'), await __newString('231543212.2132354'));
expect(Boolean(ptr)).to.equal(true);
});

it('should check given bigDecimals are not equal', async () => {
const ptr = await testBigDecimalEquals(await __newString('231543212.2132354'), await __newString('54652.65645'));
expect(Boolean(ptr)).to.equal(false);
});
});
});
11 changes: 10 additions & 1 deletion packages/graph-node/test/subgraph/example1/src/mapping.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import { Address, log, BigInt, BigDecimal, ByteArray, dataSource, ethereum, Bytes, crypto, json, JSONValueKind } from '@graphprotocol/graph-ts';
import { Address, log, BigInt, BigDecimal, bigDecimal, ByteArray, dataSource, ethereum, Bytes, crypto, json, JSONValueKind } from '@graphprotocol/graph-ts';

import {
Example1,
Expand Down Expand Up @@ -299,6 +299,15 @@ export function testBigDecimalTimes (value1: string, value2: string): string {
return res.toString();
}

export function testBigDecimalEquals (value1: string, value2: string): boolean {
log.debug('In test bigDecimal.equals', []);

const bigDecimal1 = bigDecimal.fromString(value1);
const bigDecimal2 = bigDecimal.fromString(value2);

return bigDecimal1.equals(bigDecimal2);
}

export function testBigIntPlus (value1: string, value2: string): string {
log.debug('In test bigInt.plus', []);

Expand Down
7 changes: 7 additions & 0 deletions packages/util/src/graph/graph-decimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ export class GraphDecimal {
return this.value.cmp(param);
}

equals (n: Decimal.Value | GraphDecimal): boolean {
this._checkOutOfRange(this);
const param = this._checkOutOfRange(n);

return this.value.equals(param);
}

/**
* Function to check and throw an error if a given value has exponent out of the specified range (MIN_EXP to MAX_EXP).
* @param n A Decimal value to check the range for.
Expand Down

0 comments on commit e54f4c9

Please sign in to comment.