Skip to content

Commit 26037e9

Browse files
authored
feat: units utils (#1277)
1 parent 66a16ae commit 26037e9

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

__tests__/utils/utils.test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
11
import * as starkCurve from '@scure/starknet';
2-
import { constants, ec, hash, num, stark } from '../../src';
2+
3+
import { constants, ec, hash, num, stark, units } from '../../src';
34

45
const { IS_BROWSER } = constants;
56

7+
test('units', () => {
8+
expect(units(1n, 'fri')).toEqual('0.000000000000000001');
9+
expect(units(1000n, 'fri')).toEqual('0.000000000000001');
10+
expect(units(123123123n, 'fri')).toEqual('0.000000000123123123');
11+
expect(units('123123123', 'fri')).toEqual('0.000000000123123123');
12+
expect(units(10n ** 18n, 'fri')).toEqual('1');
13+
expect(units(30n * 10n ** 16n, 'fri')).toEqual('0.3');
14+
expect(units(30n * 10n ** 22n, 'fri')).toEqual('300000');
15+
expect(units('0x40ff', 'fri')).toEqual('0.000000000000016639');
16+
17+
expect(units('0.3', 'strk')).toEqual('300000000000000000');
18+
expect(units('1', 'strk')).toEqual('1000000000000000000');
19+
expect(units('1000', 'strk')).toEqual('1000000000000000000000');
20+
expect(units('123123123.123', 'strk')).toEqual('123123123123000000000000000');
21+
expect(units('0x40ff', 'strk')).toEqual('16639000000000000000000');
22+
23+
const toTest = ['0.333', '123123123.123', '1000.1', '123123123.123123', '0.0000003'];
24+
toTest.forEach((element) => {
25+
expect(units(units(element, 'strk'), 'fri')).toEqual(element);
26+
});
27+
});
28+
629
test('isNode', () => {
730
expect(IS_BROWSER).toBe(false);
831
});

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export * from './utils/calldata';
4545
export * from './utils/calldata/enum';
4646
export * from './utils/contract';
4747
export * from './utils/transactionReceipt';
48+
export * from './utils/units';
4849
export * as wallet from './wallet/connect';
4950

5051
/**

src/utils/units.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { isHex } from './num';
2+
3+
/**
4+
* Convert strk to fri or fri to strk
5+
* @example
6+
* ```typescript
7+
* units(1000n, 'fri') // '0.000000000000001' strk
8+
* units('1', 'strk') // '1000000000000000000' fri
9+
* ```
10+
*/
11+
export function units(amount: string | bigint, simbol: 'fri' | 'strk' = 'fri') {
12+
if (simbol === 'strk') {
13+
let numStr = '';
14+
if (typeof amount === 'bigint') numStr = amount.toString();
15+
else if (typeof amount === 'string') {
16+
if (isHex(amount)) {
17+
numStr = BigInt(amount).toString();
18+
} else {
19+
numStr = amount;
20+
}
21+
}
22+
23+
const [integer, decimal = '0'] = numStr.split('.');
24+
const pdec = decimal.padEnd(18, '0');
25+
return `${integer}${pdec}`.replace(/\b0+/g, '');
26+
}
27+
28+
const bis = BigInt(amount).toString();
29+
let strk;
30+
if (bis.length <= 18) {
31+
strk = `0.${bis.padStart(18, '0')}`;
32+
} else {
33+
strk = `${bis.slice(0, bis.length - 18)}.${bis.slice(bis.length - 18)}`;
34+
}
35+
36+
return strk.replace(/(\.[0-9]*[1-9])0+$|\.0*$/, '$1');
37+
}

0 commit comments

Comments
 (0)