Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: units utils #1277

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion __tests__/utils/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
import * as starkCurve from '@scure/starknet';
import { constants, ec, hash, num, stark } from '../../src';

import { constants, ec, hash, num, stark, units } from '../../src';

const { IS_BROWSER } = constants;

test('units', () => {
expect(units(1n, 'fri')).toEqual('0.000000000000000001');
expect(units(1000n, 'fri')).toEqual('0.000000000000001');
expect(units(123123123n, 'fri')).toEqual('0.000000000123123123');
expect(units('123123123', 'fri')).toEqual('0.000000000123123123');
expect(units(10n ** 18n, 'fri')).toEqual('1');
expect(units(30n * 10n ** 16n, 'fri')).toEqual('0.3');
expect(units(30n * 10n ** 22n, 'fri')).toEqual('300000');
expect(units('0x40ff', 'fri')).toEqual('0.000000000000016639');

expect(units('0.3', 'strk')).toEqual('300000000000000000');
expect(units('1', 'strk')).toEqual('1000000000000000000');
expect(units('1000', 'strk')).toEqual('1000000000000000000000');
expect(units('123123123.123', 'strk')).toEqual('123123123123000000000000000');
expect(units('0x40ff', 'strk')).toEqual('16639000000000000000000');

const toTest = ['0.333', '123123123.123', '1000.1', '123123123.123123', '0.0000003'];
toTest.forEach((element) => {
expect(units(units(element, 'strk'), 'fri')).toEqual(element);
});
});

test('isNode', () => {
expect(IS_BROWSER).toBe(false);
});
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export * from './utils/calldata';
export * from './utils/calldata/enum';
export * from './utils/contract';
export * from './utils/transactionReceipt';
export * from './utils/units';
export * as wallet from './wallet/connect';

/**
Expand Down
37 changes: 37 additions & 0 deletions src/utils/units.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { isHex } from './num';

/**
* Convert strk to fri or fri to strk
* @example
* ```typescript
* units(1000n, 'fri') // '0.000000000000001' strk
* units('1', 'strk') // '1000000000000000000' fri
* ```
*/
export function units(amount: string | bigint, simbol: 'fri' | 'strk' = 'fri') {
if (simbol === 'strk') {
let numStr = '';
if (typeof amount === 'bigint') numStr = amount.toString();
else if (typeof amount === 'string') {
if (isHex(amount)) {
numStr = BigInt(amount).toString();
} else {
numStr = amount;
}
}

const [integer, decimal = '0'] = numStr.split('.');
const pdec = decimal.padEnd(18, '0');
return `${integer}${pdec}`.replace(/\b0+/g, '');
}

const bis = BigInt(amount).toString();
let strk;
if (bis.length <= 18) {
strk = `0.${bis.padStart(18, '0')}`;
} else {
strk = `${bis.slice(0, bis.length - 18)}.${bis.slice(bis.length - 18)}`;
}

return strk.replace(/(\.[0-9]*[1-9])0+$|\.0*$/, '$1');
}