Skip to content

Commit 3aa04b2

Browse files
Fabrice BascoulergueSegfault
Fabrice Bascoulergue
and
Segfault
authored
Add support for ulum base denom (#9)
* CI/CD pipes improvement, added contributing file * Move types declaration dependencies in production from dev dependencies * Update auto generated docs * 0.3.4 * Add support for ulum base denom * 0.3.5 Co-authored-by: Segfault <[email protected]>
1 parent fd2c6ed commit 3aa04b2

File tree

10 files changed

+116
-5
lines changed

10 files changed

+116
-5
lines changed

docs/lib/modules/lumconstants.md

+19
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
- [LumBech32PrefixValAddr](lumconstants.md#lumbech32prefixvaladdr)
1717
- [LumBech32PrefixValPub](lumconstants.md#lumbech32prefixvalpub)
1818
- [LumDenom](lumconstants.md#lumdenom)
19+
- [LumExponent](lumconstants.md#lumexponent)
1920
- [LumWalletSigningVersion](lumconstants.md#lumwalletsigningversion)
21+
- [MicroLumDenom](lumconstants.md#microlumdenom)
2022
- [PrivateKeyLength](lumconstants.md#privatekeylength)
2123

2224
### Functions
@@ -93,6 +95,15 @@ Lum Coin denomination
9395

9496
___
9597

98+
### LumExponent
99+
100+
`Const` **LumExponent**: *6*= 6
101+
102+
Lum Exponent
103+
1 lum = 10^6 ulum
104+
105+
___
106+
96107
### LumWalletSigningVersion
97108

98109
`Const` **LumWalletSigningVersion**: *1*= '1'
@@ -101,6 +112,14 @@ Signing version of the SDK
101112

102113
___
103114

115+
### MicroLumDenom
116+
117+
`Const` **MicroLumDenom**: *ulum*= 'ulum'
118+
119+
Micro Lum Coin denomination
120+
121+
___
122+
104123
### PrivateKeyLength
105124

106125
`Const` **PrivateKeyLength**: *32*= 32

docs/lib/modules/lumutils.md

+23
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
- [broadcastTxCommitSuccess](lumutils.md#broadcasttxcommitsuccess)
1212
- [broadcastTxSyncSuccess](lumutils.md#broadcasttxsyncsuccess)
13+
- [convertUnit](lumutils.md#convertunit)
1314
- [generateAuthInfoBytes](lumutils.md#generateauthinfobytes)
1415
- [generateKeyStore](lumutils.md#generatekeystore)
1516
- [generateMnemonic](lumutils.md#generatemnemonic)
@@ -78,6 +79,28 @@ Name | Type |
7879

7980
___
8081

82+
### convertUnit
83+
84+
`Const`**convertUnit**(`coin`: [*Coin*](../interfaces/lumtypes.coin.md), `toDenom`: *string*): *string*
85+
86+
Converts the Coin amount into the destination denom.
87+
This method does not do any actual math and only "move" the floating precision of the amoun in order to avoid any
88+
possible floating point precision issue.
89+
It does nothing if src denom = dst denom.
90+
91+
#### Parameters:
92+
93+
Name | Type | Description |
94+
:------ | :------ | :------ |
95+
`coin` | [*Coin*](../interfaces/lumtypes.coin.md) | Coin to convert into toDenom |
96+
`toDenom` | *string* | destination denom to convert into |
97+
98+
**Returns:** *string*
99+
100+
the amount converted
101+
102+
___
103+
81104
### generateAuthInfoBytes
82105

83106
`Const`**generateAuthInfoBytes**(`publicKey`: *Uint8Array*, `fee`: [*Fee*](../interfaces/lumtypes.fee.md), `sequence`: *number*, `signMode`: SignMode): *Uint8Array*

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lum-network/sdk-javascript",
3-
"version": "0.3.4",
3+
"version": "0.3.5",
44
"license": "Apache-2.0",
55
"description": "Javascript SDK library for NodeJS and Web browsers to interact with the Lum Network.",
66
"homepage": "https://github.com/lum-network/sdk-javascript#readme",

src/constants/index.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
/**
2+
* Lum Exponent
3+
* 1 lum = 10^6 ulum
4+
*/
5+
export const LumExponent = 6;
6+
17
/**
28
* Lum Coin denomination
39
*/
410
export const LumDenom = 'lum';
511

12+
/**
13+
* Micro Lum Coin denomination
14+
*/
15+
export const MicroLumDenom = 'ulum';
16+
617
/**
718
* Lum Network Bech32 prefix of an account's address
819
*/

src/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export * from './transactions';
66
export * from './broadcast';
77
export * from './search';
88
export * from './txlogs';
9+
export * from './units';

src/utils/units.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { LumTypes, LumConstants } from '..';
2+
3+
/**
4+
* Converts the Coin amount into the destination denom.
5+
* This method does not do any actual math and only "move" the floating precision of the amoun in order to avoid any
6+
* possible floating point precision issue.
7+
* It does nothing if src denom = dst denom.
8+
*
9+
* @param coin Coin to convert into toDenom
10+
* @param toDenom destination denom to convert into
11+
* @returns the amount converted
12+
*/
13+
export const convertUnit = (coin: LumTypes.Coin, toDenom: string): string => {
14+
const parts = coin.amount.split('.');
15+
if (parts.length > 2) {
16+
throw new Error('More than one separator found');
17+
}
18+
19+
if (coin.denom.startsWith('u') && coin.denom.endsWith(toDenom)) {
20+
// from micro to base
21+
if (parts.length !== 1) {
22+
throw new Error('Micro units cannot have floating precision');
23+
}
24+
let res = parts[0];
25+
for (let i = res.length; res.length <= LumConstants.LumExponent; i++) {
26+
res = '0' + res;
27+
}
28+
const floatIdx = res.length - LumConstants.LumExponent;
29+
return (res.substring(0, floatIdx) + '.' + res.substring(floatIdx)).replace(/0+$/, '');
30+
} else if (toDenom.startsWith('u') && toDenom.endsWith(coin.denom)) {
31+
// form base to micro
32+
if (parts.length === 2 && parts[1].length > LumConstants.LumExponent) {
33+
throw new Error(`Floating precision cannot exceed ${LumConstants.LumExponent} digits`);
34+
}
35+
let res = parts[0] + (parts[1] || '');
36+
for (let i = parts.length === 2 ? parts[1].length : 0; i < LumConstants.LumExponent; i++) {
37+
res += '0';
38+
}
39+
return res.replace(/^0+/, '');
40+
}
41+
return coin.amount;
42+
};

tests/client.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('LumClient', () => {
4040
const supplies = await clt.queryClient.bank.unverified.totalSupply();
4141
expect(supplies).toBeTruthy();
4242
expect(supplies.length).toBeGreaterThan(0);
43-
const lumSupply = supplies.filter((c) => c.denom === LumConstants.LumDenom)[0];
43+
const lumSupply = supplies.filter((c) => c.denom === LumConstants.MicroLumDenom)[0];
4444
expect(lumSupply).toBeTruthy();
4545
expect(parseFloat(lumSupply.amount)).toBeGreaterThan(0);
4646
});
@@ -80,7 +80,7 @@ describe('LumClient', () => {
8080
const balances = await clt.getAllBalancesUnverified(account.address);
8181
expect(balances).toBeTruthy();
8282
expect(balances.length).toBeGreaterThan(0);
83-
const lumBalance = balances.filter((b) => b.denom === LumConstants.LumDenom)[0];
83+
const lumBalance = balances.filter((b) => b.denom === LumConstants.MicroLumDenom)[0];
8484
expect(lumBalance).toBeTruthy();
8585
expect(parseFloat(lumBalance.amount)).toBeGreaterThan(0);
8686
});

tests/faucet.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('Faucet', () => {
1717
// WIP
1818
// const mintMsg = LumMessages.BuildMsgMintAndSend(w1.getAddress(), new Date());
1919
// const fee = {
20-
// amount: [{ denom: LumConstants.LumDenom, amount: '0' }],
20+
// amount: [{ denom: LumConstants.MicroLumDenom, amount: '0' }],
2121
// gas: '100000',
2222
// };
2323
// const chainId = await clt.getChainId();

tests/ledger.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('Ledger', () => {
2727
// const chainId = await clt.getChainId();
2828
// const sendMsg = LumMessages.BuildMsgSend(w1.getAddress(), 'lum1lsagfzrm4gz28he4wunt63sts5xzmczwjttsr9', [{ denom: 'lum', amount: '3' }]);
2929
// const fee = {
30-
// amount: [{ denom: LumConstants.LumDenom, amount: '1' }],
30+
// amount: [{ denom: LumConstants.MicroLumDenom, amount: '1' }],
3131
// gas: '100000',
3232
// };
3333
// const doc = {

tests/utils.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { LumConstants, LumUtils } from '../src';
2+
3+
describe('Utils', () => {
4+
it('Unit conversion should output consistent results', () => {
5+
expect(LumUtils.convertUnit({ denom: LumConstants.LumDenom, amount: '23.456789' }, LumConstants.LumDenom)).toEqual('23.456789');
6+
expect(LumUtils.convertUnit({ denom: LumConstants.LumDenom, amount: '23.456789' }, LumConstants.MicroLumDenom)).toEqual('23456789');
7+
expect(LumUtils.convertUnit({ denom: LumConstants.LumDenom, amount: '23456789' }, LumConstants.MicroLumDenom)).toEqual('23456789000000');
8+
expect(LumUtils.convertUnit({ denom: LumConstants.MicroLumDenom, amount: '123456789000' }, LumConstants.LumDenom)).toEqual('123456.789');
9+
expect(LumUtils.convertUnit({ denom: LumConstants.MicroLumDenom, amount: '123456789111' }, LumConstants.LumDenom)).toEqual('123456.789111');
10+
expect(LumUtils.convertUnit({ denom: LumConstants.MicroLumDenom, amount: '123456' }, LumConstants.LumDenom)).toEqual('0.123456');
11+
expect(LumUtils.convertUnit({ denom: LumConstants.MicroLumDenom, amount: '23456' }, LumConstants.LumDenom)).toEqual('0.023456');
12+
expect(LumUtils.convertUnit({ denom: LumConstants.LumDenom, amount: '0.000001' }, LumConstants.MicroLumDenom)).toEqual('1');
13+
expect(LumUtils.convertUnit({ denom: LumConstants.MicroLumDenom, amount: '1' }, LumConstants.LumDenom)).toEqual('0.000001');
14+
});
15+
});

0 commit comments

Comments
 (0)