Skip to content

Commit

Permalink
v10.4.4: fix parsing scientific numbers e.g. 1.9e-17
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNe0x1 committed Dec 27, 2024
1 parent 1bf8603 commit 5668a27
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 46 deletions.
2 changes: 1 addition & 1 deletion dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link rel="icon" type="image/png" href="https://depay.com/favicon.png"/>
<title>Development</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://cdn.ethers.io/lib/ethers-5.7.umd.min.js" type="application/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ethers/5.7.1/ethers.umd.min.js" type="application/javascript"></script>
<script crossorigin src="https://unpkg.com/@depay/solana-web3.js@1"></script>
<script crossorigin src="https://unpkg.com/@depay/web3-blockchains@9"></script>
<script crossorigin src="https://unpkg.com/@depay/web3-client@10"></script>
Expand Down
18 changes: 12 additions & 6 deletions dist/esm/index.evm.js
Original file line number Diff line number Diff line change
Expand Up @@ -900,9 +900,19 @@ class Token {
}

async BigNumber(amount) {
let decimals = await this.decimals();
const decimals = await this.decimals();
if(typeof(amount) != 'string') {
amount = amount.toString();
}
if(amount.match('e')) {
amount = parseFloat(amount).toFixed(decimals).toString();
}
const decimalsMatched = amount.match(/\.(\d+)/);
if(decimalsMatched && decimalsMatched[1] && decimalsMatched[1].length > decimals) {
amount = parseFloat(amount).toFixed(decimals).toString();
}
return ethers.utils.parseUnits(
Token.safeAmount({ amount: parseFloat(amount), decimals }).toString(),
amount,
decimals
)
}
Expand All @@ -925,10 +935,6 @@ Token.readable = async ({ amount, blockchain, address }) => {
return token.readable(amount)
};

Token.safeAmount = ({ amount, decimals }) => {
return parseFloat(amount.toFixed(decimals))
};


Token.ethereum = {
DEFAULT: ERC20,
Expand Down
18 changes: 12 additions & 6 deletions dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1181,9 +1181,19 @@ class Token {
}

async BigNumber(amount) {
let decimals = await this.decimals();
const decimals = await this.decimals();
if(typeof(amount) != 'string') {
amount = amount.toString();
}
if(amount.match('e')) {
amount = parseFloat(amount).toFixed(decimals).toString();
}
const decimalsMatched = amount.match(/\.(\d+)/);
if(decimalsMatched && decimalsMatched[1] && decimalsMatched[1].length > decimals) {
amount = parseFloat(amount).toFixed(decimals).toString();
}
return ethers.utils.parseUnits(
Token.safeAmount({ amount: parseFloat(amount), decimals }).toString(),
amount,
decimals
)
}
Expand All @@ -1206,10 +1216,6 @@ Token.readable = async ({ amount, blockchain, address }) => {
return token.readable(amount)
};

Token.safeAmount = ({ amount, decimals }) => {
return parseFloat(amount.toFixed(decimals))
};


Token.ethereum = {
DEFAULT: ERC20,
Expand Down
18 changes: 12 additions & 6 deletions dist/esm/index.solana.js
Original file line number Diff line number Diff line change
Expand Up @@ -1198,9 +1198,19 @@ class Token {
}

async BigNumber(amount) {
let decimals = await this.decimals();
const decimals = await this.decimals();
if(typeof(amount) != 'string') {
amount = amount.toString();
}
if(amount.match('e')) {
amount = parseFloat(amount).toFixed(decimals).toString();
}
const decimalsMatched = amount.match(/\.(\d+)/);
if(decimalsMatched && decimalsMatched[1] && decimalsMatched[1].length > decimals) {
amount = parseFloat(amount).toFixed(decimals).toString();
}
return ethers.utils.parseUnits(
Token.safeAmount({ amount: parseFloat(amount), decimals }).toString(),
amount,
decimals
)
}
Expand All @@ -1223,10 +1233,6 @@ Token.readable = async ({ amount, blockchain, address }) => {
return token.readable(amount)
};

Token.safeAmount = ({ amount, decimals }) => {
return parseFloat(amount.toFixed(decimals))
};


Token.solana = {
MINT_LAYOUT,
Expand Down
18 changes: 12 additions & 6 deletions dist/umd/index.evm.js
Original file line number Diff line number Diff line change
Expand Up @@ -906,9 +906,19 @@
}

async BigNumber(amount) {
let decimals = await this.decimals();
const decimals = await this.decimals();
if(typeof(amount) != 'string') {
amount = amount.toString();
}
if(amount.match('e')) {
amount = parseFloat(amount).toFixed(decimals).toString();
}
const decimalsMatched = amount.match(/\.(\d+)/);
if(decimalsMatched && decimalsMatched[1] && decimalsMatched[1].length > decimals) {
amount = parseFloat(amount).toFixed(decimals).toString();
}
return ethers.ethers.utils.parseUnits(
Token.safeAmount({ amount: parseFloat(amount), decimals }).toString(),
amount,
decimals
)
}
Expand All @@ -931,10 +941,6 @@
return token.readable(amount)
};

Token.safeAmount = ({ amount, decimals }) => {
return parseFloat(amount.toFixed(decimals))
};


Token.ethereum = {
DEFAULT: ERC20,
Expand Down
18 changes: 12 additions & 6 deletions dist/umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1186,9 +1186,19 @@
}

async BigNumber(amount) {
let decimals = await this.decimals();
const decimals = await this.decimals();
if(typeof(amount) != 'string') {
amount = amount.toString();
}
if(amount.match('e')) {
amount = parseFloat(amount).toFixed(decimals).toString();
}
const decimalsMatched = amount.match(/\.(\d+)/);
if(decimalsMatched && decimalsMatched[1] && decimalsMatched[1].length > decimals) {
amount = parseFloat(amount).toFixed(decimals).toString();
}
return ethers.ethers.utils.parseUnits(
Token.safeAmount({ amount: parseFloat(amount), decimals }).toString(),
amount,
decimals
)
}
Expand All @@ -1211,10 +1221,6 @@
return token.readable(amount)
};

Token.safeAmount = ({ amount, decimals }) => {
return parseFloat(amount.toFixed(decimals))
};


Token.ethereum = {
DEFAULT: ERC20,
Expand Down
18 changes: 12 additions & 6 deletions dist/umd/index.solana.js
Original file line number Diff line number Diff line change
Expand Up @@ -1204,9 +1204,19 @@
}

async BigNumber(amount) {
let decimals = await this.decimals();
const decimals = await this.decimals();
if(typeof(amount) != 'string') {
amount = amount.toString();
}
if(amount.match('e')) {
amount = parseFloat(amount).toFixed(decimals).toString();
}
const decimalsMatched = amount.match(/\.(\d+)/);
if(decimalsMatched && decimalsMatched[1] && decimalsMatched[1].length > decimals) {
amount = parseFloat(amount).toFixed(decimals).toString();
}
return ethers.ethers.utils.parseUnits(
Token.safeAmount({ amount: parseFloat(amount), decimals }).toString(),
amount,
decimals
)
}
Expand All @@ -1229,10 +1239,6 @@
return token.readable(amount)
};

Token.safeAmount = ({ amount, decimals }) => {
return parseFloat(amount.toFixed(decimals))
};


Token.solana = {
MINT_LAYOUT,
Expand Down
2 changes: 1 addition & 1 deletion package.evm.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@depay/web3-tokens-evm",
"moduleName": "Web3Tokens",
"version": "10.4.3",
"version": "10.4.4",
"description": "JavaScript library providing basic functionalities to interact with web3 tokens.",
"main": "dist/umd/index.evm.js",
"module": "dist/esm/index.evm.js",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@depay/web3-tokens",
"moduleName": "Web3Tokens",
"version": "10.4.3",
"version": "10.4.4",
"description": "JavaScript library providing basic functionalities to interact with web3 tokens.",
"main": "dist/umd/index.js",
"module": "dist/esm/index.js",
Expand Down
2 changes: 1 addition & 1 deletion package.solana.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@depay/web3-tokens-solana",
"moduleName": "Web3Tokens",
"version": "10.4.3",
"version": "10.4.4",
"description": "JavaScript library providing basic functionalities to interact with web3 tokens.",
"main": "dist/umd/index.solana.js",
"module": "dist/esm/index.solana.js",
Expand Down
18 changes: 12 additions & 6 deletions src/Token.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,19 @@ class Token {
}

async BigNumber(amount) {
let decimals = await this.decimals()
const decimals = await this.decimals()
if(typeof(amount) != 'string') {
amount = amount.toString()
}
if(amount.match('e')) {
amount = parseFloat(amount).toFixed(decimals).toString()
}
const decimalsMatched = amount.match(/\.(\d+)/)
if(decimalsMatched && decimalsMatched[1] && decimalsMatched[1].length > decimals) {
amount = parseFloat(amount).toFixed(decimals).toString()
}
return ethers.utils.parseUnits(
Token.safeAmount({ amount: parseFloat(amount), decimals }).toString(),
amount,
decimals
)
}
Expand All @@ -236,10 +246,6 @@ Token.readable = async ({ amount, blockchain, address }) => {
return token.readable(amount)
}

Token.safeAmount = ({ amount, decimals }) => {
return parseFloat(amount.toFixed(decimals))
}

/*#if _EVM
Token.ethereum = {
Expand Down
19 changes: 19 additions & 0 deletions tests/units/platforms/evm/BigNumber.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,25 @@ describe('BigNumber', () => {

expect(amountBN.toString()).toEqual('19126903977196')
})

it('fixes syntific numbers when parsing', async ()=> {
let tokenCallMock = mock({
provider,
blockchain,
request: {
to: '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb',
api: Token[blockchain].DEFAULT,
method: 'decimals',
return: 18
}
})

let token = new Token({ blockchain, address: '0xa0bed124a09ac2bd941b10349d8d224fe3c955eb' })

let amountBN = await token.BigNumber(1.9e-10)

expect(amountBN.toString()).toEqual('190000000')
})
})
})
})

0 comments on commit 5668a27

Please sign in to comment.