From f0b32529b7e2eeb774873655b0e195a44dba3bba Mon Sep 17 00:00:00 2001 From: POA <33550681+poaman@users.noreply.github.com> Date: Tue, 24 Nov 2020 11:58:55 +0300 Subject: [PATCH] Fix working with big numbers in Staking DApp --- CHANGELOG.md | 1 + .../assets/js/pages/stakes/become_candidate.js | 4 ++-- .../assets/js/pages/stakes/make_stake.js | 2 +- .../assets/js/pages/stakes/move_stake.js | 2 +- .../assets/js/pages/stakes/withdraw_stake.js | 4 ++-- .../lib/block_scout_web/views/stakes_helpers.ex | 17 ++++++++++++----- 6 files changed, 19 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88baa7433a..eb8d8373c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - [#3462](https://github.com/poanetwork/blockscout/pull/3462) - Display price for bridged tokens ### Fixes +- [#3479](https://github.com/poanetwork/blockscout/pull/3479) - Fix working with big numbers in Staking DApp - [#3477](https://github.com/poanetwork/blockscout/pull/3477) - Contracts interaction: fix broken call of GnosisProxy contract methods with parameters - [#3477](https://github.com/poanetwork/blockscout/pull/3477) - Contracts interaction: fix broken call of fallback function - [#3476](https://github.com/poanetwork/blockscout/pull/3476) - Fix contract verification of precompiled contracts diff --git a/apps/block_scout_web/assets/js/pages/stakes/become_candidate.js b/apps/block_scout_web/assets/js/pages/stakes/become_candidate.js index 898b250746..7e544dc32f 100644 --- a/apps/block_scout_web/assets/js/pages/stakes/become_candidate.js +++ b/apps/block_scout_web/assets/js/pages/stakes/become_candidate.js @@ -95,8 +95,8 @@ async function becomeCandidate ($modal, store, msg) { lockModal($modal) - console.log(`Call addPool(${stake.toString()}, ${miningAddress})`) - makeContractCall(stakingContract.methods.addPool(stake.toString(), miningAddress), store) + console.log(`Call addPool(${stake.toFixed()}, ${miningAddress})`) + makeContractCall(stakingContract.methods.addPool(stake.toFixed(), miningAddress), store) } catch (err) { openErrorModal('Error', err.message) } diff --git a/apps/block_scout_web/assets/js/pages/stakes/make_stake.js b/apps/block_scout_web/assets/js/pages/stakes/make_stake.js index 6b64404baa..c4b3919f86 100644 --- a/apps/block_scout_web/assets/js/pages/stakes/make_stake.js +++ b/apps/block_scout_web/assets/js/pages/stakes/make_stake.js @@ -73,7 +73,7 @@ async function makeStake ($modal, address, store, msg) { return } - makeContractCall(stakingContract.methods.stake(address, stake.toString()), store) + makeContractCall(stakingContract.methods.stake(address, stake.toFixed()), store) } function isDelegatorStakeValid (value, store, msg, address) { diff --git a/apps/block_scout_web/assets/js/pages/stakes/move_stake.js b/apps/block_scout_web/assets/js/pages/stakes/move_stake.js index 112bed01c5..6cb7e53cab 100644 --- a/apps/block_scout_web/assets/js/pages/stakes/move_stake.js +++ b/apps/block_scout_web/assets/js/pages/stakes/move_stake.js @@ -66,7 +66,7 @@ function moveStake ($modal, fromAddress, store, msg) { const stake = new BigNumber($modal.find('[move-amount]').val().replace(',', '.').trim()).shiftedBy(decimals).integerValue() const toAddress = $modal.find('[pool-select]').val() - makeContractCall(stakingContract.methods.moveStake(fromAddress, toAddress, stake.toString()), store) + makeContractCall(stakingContract.methods.moveStake(fromAddress, toAddress, stake.toFixed()), store) } function isMoveAmountValid (value, store, msg) { diff --git a/apps/block_scout_web/assets/js/pages/stakes/withdraw_stake.js b/apps/block_scout_web/assets/js/pages/stakes/withdraw_stake.js index c2e2215917..778aca8314 100644 --- a/apps/block_scout_web/assets/js/pages/stakes/withdraw_stake.js +++ b/apps/block_scout_web/assets/js/pages/stakes/withdraw_stake.js @@ -69,7 +69,7 @@ function withdrawStake ($modal, address, store, msg) { const decimals = store.getState().tokenDecimals const amount = new BigNumber($modal.find('[amount]').val().replace(',', '.').trim()).shiftedBy(decimals).integerValue() - makeContractCall(stakingContract.methods.withdraw(address, amount.toString()), store) + makeContractCall(stakingContract.methods.withdraw(address, amount.toFixed()), store) } function orderWithdraw ($modal, address, store, msg) { @@ -85,7 +85,7 @@ function orderWithdraw ($modal, address, store, msg) { return false } - makeContractCall(stakingContract.methods.orderWithdraw(address, amount.toString()), store) + makeContractCall(stakingContract.methods.orderWithdraw(address, amount.toFixed()), store) } function isAmountValid (value, store, msg) { diff --git a/apps/block_scout_web/lib/block_scout_web/views/stakes_helpers.ex b/apps/block_scout_web/lib/block_scout_web/views/stakes_helpers.ex index 2528f5a75a..7b271c4eda 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/stakes_helpers.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/stakes_helpers.ex @@ -41,12 +41,19 @@ defmodule BlockScoutWeb.StakesHelpers do def list_title(:active), do: Gettext.dgettext(BlockScoutWeb.Gettext, "default", "Active Pools") def list_title(:inactive), do: Gettext.dgettext(BlockScoutWeb.Gettext, "default", "Inactive Pools") - def from_wei(%Decimal{} = amount, %Token{} = token) do + def from_wei(%Decimal{} = amount, %Token{} = token, to_string \\ true) do decimals = if token.decimals, do: Decimal.to_integer(token.decimals), else: 0 - amount.sign - |> Decimal.new(amount.coef, amount.exp - decimals) - |> Decimal.normalize() + result = + amount.sign + |> Decimal.new(amount.coef, amount.exp - decimals) + |> Decimal.normalize() + + if to_string do + Decimal.to_string(result, :normal) + else + result + end end def format_token_amount(amount, token, options \\ []) @@ -65,7 +72,7 @@ defmodule BlockScoutWeb.StakesHelpers do ellipsize = Keyword.get(options, :ellipsize, true) no_tooltip = Keyword.get(options, :no_tooltip, false) - reduced = from_wei(amount, token) + reduced = from_wei(amount, token, false) if digits >= -reduced.exp or not ellipsize do "#{Number.to_string!(reduced, fractional_digits: min(digits, -reduced.exp))}#{symbol}"