Skip to content

Commit

Permalink
0.0.41 - utils.toNano and utils.fromNano now disallow Number as an ar…
Browse files Browse the repository at this point in the history
…gument. Please pass numbers as strings or BN objects to avoid precision errors. Please note that the ethjs-unit library erroneously handles Number argument in toWei/fromWei functions (BN and String is OK).
  • Loading branch information
tolya-yanot committed Jun 3, 2022
1 parent 155bf79 commit 66ebde2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dist/tonweb.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/types/utils/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ export declare function sha256(bytes: Uint8Array): Promise<ArrayBuffer>;
/**
* Converts the specified amount from coins to nanocoins.
*/
export declare function toNano(amount: (number | BN | string)): BN;
export declare function toNano(amount: (BN | string)): BN;
/**
* Converts the specified amount from nanocoins to coins.
*/
export declare function fromNano(amount: (number | BN | string)): string;
export declare function fromNano(amount: (BN | string)): string;
/**
* Converts the specified bytes array to hex string
* using lookup table.
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": "tonweb",
"version": "0.0.40",
"version": "0.0.41",
"description": "TonWeb - JavaScript API for TON blockchain",
"main": "src/index.js",
"types": "dist/types/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const {SubscriptionContract} = require("./contract/subscription/index");
const TransportWebUSB = require("@ledgerhq/hw-transport-webusb").default;
const TransportWebHID = require("@ledgerhq/hw-transport-webhid").default;
const BluetoothTransport = require("@ledgerhq/hw-transport-web-ble").default;
const version = '0.0.40';
const version = '0.0.41';

class TonWeb {
constructor(provider) {
Expand Down
12 changes: 10 additions & 2 deletions src/utils/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,27 @@ function sha256(bytes) {

/**
* from coins to nanocoins
* @param amount {number | BN | string}
* @param amount {BN | string}
* @return {BN}
*/
function toNano(amount) {
if (!BN.isBN(amount) && !(typeof amount === 'string')) {
throw new Error('Please pass numbers as strings or BN objects to avoid precision errors.');
}

return ethunit.toWei(amount, 'gwei');
}

/**
* from nanocoins to coins
* @param amount {number | BN | string}
* @param amount {BN | string}
* @return {string}
*/
function fromNano(amount) {
if (!BN.isBN(amount) && !(typeof amount === 'string')) {
throw new Error('Please pass numbers as strings or BN objects to avoid precision errors.');
}

return ethunit.fromWei(amount, 'gwei');
}

Expand Down

0 comments on commit 66ebde2

Please sign in to comment.