Skip to content
Merged
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
19 changes: 19 additions & 0 deletions docs/lib/modules/lumconstants.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
- [LumBech32PrefixValAddr](lumconstants.md#lumbech32prefixvaladdr)
- [LumBech32PrefixValPub](lumconstants.md#lumbech32prefixvalpub)
- [LumDenom](lumconstants.md#lumdenom)
- [LumExponent](lumconstants.md#lumexponent)
- [LumWalletSigningVersion](lumconstants.md#lumwalletsigningversion)
- [MicroLumDenom](lumconstants.md#microlumdenom)
- [PrivateKeyLength](lumconstants.md#privatekeylength)

### Functions
Expand Down Expand Up @@ -93,6 +95,15 @@ Lum Coin denomination

___

### LumExponent

• `Const` **LumExponent**: *6*= 6

Lum Exponent
1 lum = 10^6 ulum

___

### LumWalletSigningVersion

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

___

### MicroLumDenom

• `Const` **MicroLumDenom**: *ulum*= 'ulum'

Micro Lum Coin denomination

___

### PrivateKeyLength

• `Const` **PrivateKeyLength**: *32*= 32
Expand Down
23 changes: 23 additions & 0 deletions docs/lib/modules/lumutils.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

- [broadcastTxCommitSuccess](lumutils.md#broadcasttxcommitsuccess)
- [broadcastTxSyncSuccess](lumutils.md#broadcasttxsyncsuccess)
- [convertUnit](lumutils.md#convertunit)
- [generateAuthInfoBytes](lumutils.md#generateauthinfobytes)
- [generateKeyStore](lumutils.md#generatekeystore)
- [generateMnemonic](lumutils.md#generatemnemonic)
Expand Down Expand Up @@ -78,6 +79,28 @@ Name | Type |

___

### convertUnit

▸ `Const`**convertUnit**(`coin`: [*Coin*](../interfaces/lumtypes.coin.md), `toDenom`: *string*): *string*

Converts the Coin amount into the destination denom.
This method does not do any actual math and only "move" the floating precision of the amoun in order to avoid any
possible floating point precision issue.
It does nothing if src denom = dst denom.

#### Parameters:

Name | Type | Description |
:------ | :------ | :------ |
`coin` | [*Coin*](../interfaces/lumtypes.coin.md) | Coin to convert into toDenom |
`toDenom` | *string* | destination denom to convert into |

**Returns:** *string*

the amount converted

___

### generateAuthInfoBytes

▸ `Const`**generateAuthInfoBytes**(`publicKey`: *Uint8Array*, `fee`: [*Fee*](../interfaces/lumtypes.fee.md), `sequence`: *number*, `signMode`: SignMode): *Uint8Array*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lum-network/sdk-javascript",
"version": "0.3.4",
"version": "0.3.5",
"license": "Apache-2.0",
"description": "Javascript SDK library for NodeJS and Web browsers to interact with the Lum Network.",
"homepage": "https://github.com/lum-network/sdk-javascript#readme",
Expand Down
11 changes: 11 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
/**
* Lum Exponent
* 1 lum = 10^6 ulum
*/
export const LumExponent = 6;

/**
* Lum Coin denomination
*/
export const LumDenom = 'lum';

/**
* Micro Lum Coin denomination
*/
export const MicroLumDenom = 'ulum';

/**
* Lum Network Bech32 prefix of an account's address
*/
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './transactions';
export * from './broadcast';
export * from './search';
export * from './txlogs';
export * from './units';
42 changes: 42 additions & 0 deletions src/utils/units.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { LumTypes, LumConstants } from '..';

/**
* Converts the Coin amount into the destination denom.
* This method does not do any actual math and only "move" the floating precision of the amoun in order to avoid any
* possible floating point precision issue.
* It does nothing if src denom = dst denom.
*
* @param coin Coin to convert into toDenom
* @param toDenom destination denom to convert into
* @returns the amount converted
*/
export const convertUnit = (coin: LumTypes.Coin, toDenom: string): string => {
const parts = coin.amount.split('.');
if (parts.length > 2) {
throw new Error('More than one separator found');
}

if (coin.denom.startsWith('u') && coin.denom.endsWith(toDenom)) {
// from micro to base
if (parts.length !== 1) {
throw new Error('Micro units cannot have floating precision');
}
let res = parts[0];
for (let i = res.length; res.length <= LumConstants.LumExponent; i++) {
res = '0' + res;
}
const floatIdx = res.length - LumConstants.LumExponent;
return (res.substring(0, floatIdx) + '.' + res.substring(floatIdx)).replace(/0+$/, '');
} else if (toDenom.startsWith('u') && toDenom.endsWith(coin.denom)) {
// form base to micro
if (parts.length === 2 && parts[1].length > LumConstants.LumExponent) {
throw new Error(`Floating precision cannot exceed ${LumConstants.LumExponent} digits`);
}
let res = parts[0] + (parts[1] || '');
for (let i = parts.length === 2 ? parts[1].length : 0; i < LumConstants.LumExponent; i++) {
res += '0';
}
return res.replace(/^0+/, '');
}
return coin.amount;
};
4 changes: 2 additions & 2 deletions tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('LumClient', () => {
const supplies = await clt.queryClient.bank.unverified.totalSupply();
expect(supplies).toBeTruthy();
expect(supplies.length).toBeGreaterThan(0);
const lumSupply = supplies.filter((c) => c.denom === LumConstants.LumDenom)[0];
const lumSupply = supplies.filter((c) => c.denom === LumConstants.MicroLumDenom)[0];
expect(lumSupply).toBeTruthy();
expect(parseFloat(lumSupply.amount)).toBeGreaterThan(0);
});
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('LumClient', () => {
const balances = await clt.getAllBalancesUnverified(account.address);
expect(balances).toBeTruthy();
expect(balances.length).toBeGreaterThan(0);
const lumBalance = balances.filter((b) => b.denom === LumConstants.LumDenom)[0];
const lumBalance = balances.filter((b) => b.denom === LumConstants.MicroLumDenom)[0];
expect(lumBalance).toBeTruthy();
expect(parseFloat(lumBalance.amount)).toBeGreaterThan(0);
});
Expand Down
2 changes: 1 addition & 1 deletion tests/faucet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Faucet', () => {
// WIP
// const mintMsg = LumMessages.BuildMsgMintAndSend(w1.getAddress(), new Date());
// const fee = {
// amount: [{ denom: LumConstants.LumDenom, amount: '0' }],
// amount: [{ denom: LumConstants.MicroLumDenom, amount: '0' }],
// gas: '100000',
// };
// const chainId = await clt.getChainId();
Expand Down
2 changes: 1 addition & 1 deletion tests/ledger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('Ledger', () => {
// const chainId = await clt.getChainId();
// const sendMsg = LumMessages.BuildMsgSend(w1.getAddress(), 'lum1lsagfzrm4gz28he4wunt63sts5xzmczwjttsr9', [{ denom: 'lum', amount: '3' }]);
// const fee = {
// amount: [{ denom: LumConstants.LumDenom, amount: '1' }],
// amount: [{ denom: LumConstants.MicroLumDenom, amount: '1' }],
// gas: '100000',
// };
// const doc = {
Expand Down
15 changes: 15 additions & 0 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { LumConstants, LumUtils } from '../src';

describe('Utils', () => {
it('Unit conversion should output consistent results', () => {
expect(LumUtils.convertUnit({ denom: LumConstants.LumDenom, amount: '23.456789' }, LumConstants.LumDenom)).toEqual('23.456789');
expect(LumUtils.convertUnit({ denom: LumConstants.LumDenom, amount: '23.456789' }, LumConstants.MicroLumDenom)).toEqual('23456789');
expect(LumUtils.convertUnit({ denom: LumConstants.LumDenom, amount: '23456789' }, LumConstants.MicroLumDenom)).toEqual('23456789000000');
expect(LumUtils.convertUnit({ denom: LumConstants.MicroLumDenom, amount: '123456789000' }, LumConstants.LumDenom)).toEqual('123456.789');
expect(LumUtils.convertUnit({ denom: LumConstants.MicroLumDenom, amount: '123456789111' }, LumConstants.LumDenom)).toEqual('123456.789111');
expect(LumUtils.convertUnit({ denom: LumConstants.MicroLumDenom, amount: '123456' }, LumConstants.LumDenom)).toEqual('0.123456');
expect(LumUtils.convertUnit({ denom: LumConstants.MicroLumDenom, amount: '23456' }, LumConstants.LumDenom)).toEqual('0.023456');
expect(LumUtils.convertUnit({ denom: LumConstants.LumDenom, amount: '0.000001' }, LumConstants.MicroLumDenom)).toEqual('1');
expect(LumUtils.convertUnit({ denom: LumConstants.MicroLumDenom, amount: '1' }, LumConstants.LumDenom)).toEqual('0.000001');
});
});