Skip to content
Snippets Groups Projects
Unverified Commit 63525e7f authored by Mateusz Tyszczak's avatar Mateusz Tyszczak :scroll:
Browse files

Add xxxCoins methods deprecating old methods

parent d1d1d2ce
No related branches found
No related tags found
1 merge request!208Add satoshis for assets
......@@ -337,7 +337,7 @@ test.describe('Wax object interface formatters tests', () => {
delegatees: [ "gtg", "null" ],
from: "initminer",
rc: {
amount: "4127361273000000",
amount: "4127361273",
nai: "@@000000037",
precision: 6,
}
......
......@@ -565,7 +565,7 @@ test.describe('Wax object interface chain tests', () => {
test('Should be able to get hive asset with JS Double-precision floating-point format', async ({ waxTest }) => {
const retVal = await waxTest(async({ chain }) => {
return chain.hive(100.3).amount;
return chain.hiveCoins(100.3).amount;
});
expect(retVal).toBe("100300");
......@@ -573,18 +573,26 @@ test.describe('Wax object interface chain tests', () => {
test('Should be able to get hbd asset with JS Double-precision floating-point format', async ({ waxTest }) => {
const retVal = await waxTest(async({ chain }) => {
return chain.hbd(100.3).amount;
return chain.hbdCoins(100.34567).amount;
});
expect(retVal).toBe("100300");
expect(retVal).toBe("100345");
});
test('Should be able to get vests asset with JS Double-precision floating-point format', async ({ waxTest }) => {
const retVal = await waxTest(async({ chain }) => {
return chain.vests(100.3).amount;
return chain.vestsCoins(100).amount;
});
expect(retVal).toBe("100000000");
});
test('Should be able to get vests asset with JS Double-precision floating-point format near max safe integer (with fractional part)', async ({ waxTest }) => {
const retVal = await waxTest(async({ chain }) => {
return chain.vestsCoins(9007199254740.543).amount;
});
expect(retVal).toBe("100300000");
expect(retVal).toBe("9007199254740543000");
});
test('Should be able to get hive asset with large number value', async ({ waxTest }) => {
......
......@@ -55,16 +55,38 @@ export class WaxBaseApi implements IWaxBaseInterface {
return props;
}
public hive(amount: number): NaiAsset {
return this.hiveSatoshis((amount * (10**this.ASSETS.HIVE.precision)).toFixed(0));
private naiAssetToLong(amount: number, precision: number): Long {
let satoshisValue = Long.fromNumber(amount).multiply(10 ** precision);
const [ , frac ] = amount.toString().split('.') as [string, string | undefined];
if (frac)
satoshisValue = satoshisValue.add(frac.substring(0, precision) + '0'.repeat(Math.max(0, precision - frac.length)));
return satoshisValue;
}
public hiveCoins(amount: number): NaiAsset {
return this.hiveSatoshis(this.naiAssetToLong(amount, this.ASSETS.HIVE.precision));
}
public hbdCoins(amount: number): NaiAsset {
return this.hbdSatoshis(this.naiAssetToLong(amount, this.ASSETS.HBD.precision));
}
public vestsCoins(amount: number): NaiAsset {
return this.vestsSatoshis(this.naiAssetToLong(amount, this.ASSETS.VESTS.precision));
}
public hive(amount: number | string | BigInt | Long): NaiAsset {
return this.hiveSatoshis(amount);
}
public hbd(amount: number): NaiAsset {
return this.hbdSatoshis((amount * (10**this.ASSETS.HBD.precision)).toFixed(0));
public hbd(amount: number | string | BigInt | Long): NaiAsset {
return this.hbdSatoshis(amount);
}
public vests(amount: number): NaiAsset {
return this.vestsSatoshis((amount * (10**this.ASSETS.VESTS.precision)).toFixed(0));
public vests(amount: number | string | BigInt | Long): NaiAsset {
return this.vestsSatoshis(amount);
}
public hiveSatoshis(amount: number | string | BigInt | Long): NaiAsset {
......
......@@ -498,7 +498,7 @@ export interface IWaxBaseInterface {
* @param {number} amount amount of HIVE
* @returns {NaiAsset} HIVE in nai form
*/
hive(amount: number): NaiAsset;
hiveCoins(amount: number): NaiAsset;
/**
* Retrieves HBD in nai form with given amount
......@@ -512,7 +512,7 @@ export interface IWaxBaseInterface {
* @param {number} amount amount of HBD
* @returns {NaiAsset} HBD in nai form
*/
hbd(amount: number): NaiAsset;
hbdCoins(amount: number): NaiAsset;
/**
* Retrieves VESTS in nai form with given amount
......@@ -526,7 +526,7 @@ export interface IWaxBaseInterface {
* @param {number} amount amount of VESTS
* @returns {NaiAsset} VESTS in nai form
*/
vests(amount: number): NaiAsset;
vestsCoins(amount: number): NaiAsset;
/**
* Retrieves HIVE in nai form with given amount
......@@ -570,6 +570,54 @@ export interface IWaxBaseInterface {
*/
vestsSatoshis(amount: number | string | BigInt | Long): NaiAsset;
/**
* Retrieves HIVE in nai form with given amount
*
* Note: This function only accepts integer values.
* If you want to pass fractional number values, use {@link hiveCoins} instead.
* This function copies the input value to the output `amount` property without any conversion - adds just a `nai` id.
*
* @example Input: `10000`, `"10000000000000000"`, `BigInt("10000000000000000")`
*
* @param {number | string | BigInt | Long} amount amount of HIVE
* @returns {NaiAsset} HIVE in nai form
*
* @deprecated Use {@link hiveSatoshis} or {@link hiveCoins} instead
*/
hive(amount: number | string | BigInt | Long): NaiAsset;
/**
* Retrieves HBD in nai form with given amount
*
* Note: This function only accepts integer values.
* If you want to pass fractional number values, use {@link hbdCoins} instead.
* This function copies the input value to the output `amount` property without any conversion - adds just a `nai` id.
*
* @example Input: `10000`, `"10000000000000000"`, `BigInt("10000000000000000")`
*
* @param {number | string | BigInt | Long} amount amount of HBD
* @returns {NaiAsset} HBD in nai form
*
* @deprecated Use {@link hbdSatoshis} or {@link hbdCoins} instead
*/
hbd(amount: number | string | BigInt | Long): NaiAsset;
/**
* Retrieves VESTS in nai form with given amount
*
* Note: This function only accepts integer values.
* If you want to pass fractional number values, use {@link vestsCoins} instead.
* This function copies the input value to the output `amount` property without any conversion - adds just a `nai` id.
*
* @example Input: `10000`, `"10000000000000000"`, `BigInt("10000000000000000")`
*
* @param {number | string | BigInt | Long} amount amount of VESTS
* @returns {NaiAsset} VESTS in nai form
*
* @deprecated Use {@link vestsSatoshis} or {@link vestsCoins} instead
*/
vests(amount: number | string | BigInt | Long): NaiAsset;
/**
* Converts VESTS to HP in nai form
* @param {NaiAsset} vests VESTS asset
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment