Skip to content

Commit e77d669

Browse files
Rename function & add comments
1 parent 73bbc16 commit e77d669

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

packages/core/src/jpyc.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getContract, GetContractReturnType, Hash, WalletClient } from 'viem';
44
import { IJPYC, JPYC_V2_ABI } from './interfaces';
55
import {
66
Address,
7-
addDecimals,
7+
restoreDecimals,
88
Bytes32,
99
InvalidAddressError,
1010
InvalidTransactionError,
@@ -45,25 +45,25 @@ export class JPYC implements IJPYC {
4545
async minterAllowance(params: { minter: Address }): Promise<number> {
4646
const resp = await this.contract.read.minterAllowance([params.minter]);
4747

48-
return addDecimals(Uint256.from(resp as string));
48+
return restoreDecimals(Uint256.from(resp as string));
4949
}
5050

5151
async totalSupply(): Promise<number> {
5252
const resp = await this.contract.read.totalSupply();
5353

54-
return addDecimals(Uint256.from(resp as string));
54+
return restoreDecimals(Uint256.from(resp as string));
5555
}
5656

5757
async balanceOf(params: { account: Address }): Promise<number> {
5858
const resp = await this.contract.read.balanceOf([params.account]);
5959

60-
return addDecimals(Uint256.from(resp as string));
60+
return restoreDecimals(Uint256.from(resp as string));
6161
}
6262

6363
async allowance(params: { owner: Address; spender: Address }): Promise<number> {
6464
const resp = await this.contract.read.allowance([params.owner, params.spender]);
6565

66-
return addDecimals(Uint256.from(resp as string));
66+
return restoreDecimals(Uint256.from(resp as string));
6767
}
6868

6969
async nonces(params: { owner: Address }): Promise<Uint256> {

packages/core/src/utils/math.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Uint256 } from 'soltypes';
22

3-
import { toUint256, removeDecimals, addDecimals } from './math';
3+
import { toUint256, removeDecimals, restoreDecimals } from './math';
44

55
describe('Unit tests of toUint256()', () => {
66
test('converts bigint to uint256 value', () => {
@@ -24,9 +24,9 @@ describe('Unit tests of removeDecimals()', () => {
2424
});
2525
});
2626

27-
describe('Unit tests of addDecimals()', () => {
28-
test('adds decimal places to bigint', () => {
29-
const res = addDecimals(Uint256.from('100'));
27+
describe('Unit tests of restoreDecimals()', () => {
28+
test('restores decimal places to bigint', () => {
29+
const res = restoreDecimals(Uint256.from('100'));
3030
expect(typeof res).toBe('number');
3131
expect(res).toStrictEqual(0.0000000000000001);
3232
});

packages/core/src/utils/math.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,35 @@ import { Uint256 } from 'soltypes';
33
const TOKEN_DECIMALS = 18;
44
const DECIMALS_QUANTIFIER = 10 ** TOKEN_DECIMALS;
55

6+
/**
7+
* Converts bigint to uint256
8+
* @param value bigint value
9+
* @returns value as uint256
10+
*/
611
export const toUint256 = (value: bigint): Uint256 => {
712
return Uint256.from(value.toString());
813
};
914

15+
/**
16+
* Removes decimal places to make sure that only integer values live on-chain.
17+
* e.g.)
18+
* 0.01 -> 10,000,000,000,000,000
19+
* 100 -> 100,000,000,000,000,000,000
20+
* @param value integer or decimal value
21+
* @returns value as uint256
22+
*/
1023
export const removeDecimals = (value: number): Uint256 => {
1124
return toUint256(BigInt(value * DECIMALS_QUANTIFIER));
1225
};
1326

14-
export const addDecimals = (value: Uint256): number => {
27+
/**
28+
* Restores decimal places (mainly for display purpose).
29+
* e.g.)
30+
* 10,000,000,000,000,000 -> 0.01
31+
* 100,000,000,000,000,000,000 -> 100
32+
* @param value uint256 value
33+
* @returns value as number (i.e., integer or decimal)
34+
*/
35+
export const restoreDecimals = (value: Uint256): number => {
1536
return Number(value.toString()) / DECIMALS_QUANTIFIER;
1637
};

0 commit comments

Comments
 (0)