Skip to content

Commit

Permalink
fix(suite): EVM available balance
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasklim committed Apr 29, 2024
1 parent 32838d7 commit b87ea38
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 16 deletions.
19 changes: 13 additions & 6 deletions packages/blockchain-link-utils/src/blockbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,13 +357,20 @@ export const transformAccountInfo = (payload: BlockbookAccountInfo): AccountInfo
const descriptor = payload.address;
const addresses = transformAddresses(payload.tokens);
const tokens = transformTokenInfo(payload.tokens);

const unconfirmedBalance = new BigNumber(payload.unconfirmedBalance);
// reduce or increase availableBalance
const availableBalance =
!unconfirmedBalance.isNaN() && !unconfirmedBalance.isZero()
? unconfirmedBalance.plus(payload.balance).toString()
: payload.balance;

let availableBalance = payload.balance;
if (!unconfirmedBalance.isNaN()) {
if (!isEVM) {
availableBalance = unconfirmedBalance.plus(payload.balance).toString();
} else if (isEVM && unconfirmedBalance.lt(0)) {
// if unconfirmed balance is positive it means that address has pending receive transaction
// this address cannot use this balance yet so it should not be visible
// however, for negative it has to be applied, otherwise it would not be possible to e.g. bump fee
// (when tx is pending the balance is still on the account)
availableBalance = unconfirmedBalance.plus(payload.balance).toString();
}
}
const empty =
payload.txs === 0 &&
payload.unconfirmedTxs === 0 &&
Expand Down
126 changes: 116 additions & 10 deletions packages/blockchain-link/tests/unit/fixtures/getAccountInfo-blockbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ const fixtures: {
},
},
{
description: 'ETH (Ropsten) smart contract',
description: 'ETH staking pools',
params: {
descriptor: '0xFc6B5d6af8A13258f7CbD0D39E11b35e01a32F93',
},
Expand All @@ -495,28 +495,134 @@ const fixtures: {
method: 'getAccountInfo',
response: {
data: {
nonce: '100',
address: '0xFc6B5d6af8A13258f7CbD0D39E11b35e01a32F93',
stakingPools: [
{
name: 'Klimactivist Pool',
},
],
},
},
},
],
response: {
descriptor: '0xFc6B5d6af8A13258f7CbD0D39E11b35e01a32F93',
empty: false,
history: {},
misc: {
nonce: '100',
stakingPools: [
{
name: 'Klimactivist Pool',
},
],
addressAliases: undefined,
contractInfo: undefined,
},
},
},
{
description: 'ETH send receive tx',
params: {
descriptor: '0xFc6B5d6af8A13258f7CbD0D39E11b35e01a32F93',
},
serverFixtures: [
{
method: 'getAccountInfo',
response: {
data: {
nonce: '100',
address: '0xFc6B5d6af8A13258f7CbD0D39E11b35e01a32F93',
balance: '100',
unconfirmedBalance: '-1',
},
},
},
],
response: {
descriptor: '0xFc6B5d6af8A13258f7CbD0D39E11b35e01a32F93',
empty: false,
balance: '100',
availableBalance: '99',
history: {},
misc: {
nonce: '100',
stakingPools: undefined,
addressAliases: undefined,
contractInfo: undefined,
},
},
},
{
description: 'ETH pending receive tx',
params: {
descriptor: '0xFc6B5d6af8A13258f7CbD0D39E11b35e01a32F93',
},
serverFixtures: [
{
method: 'getAccountInfo',
response: {
data: {
nonce: '100',
address: '0xFc6B5d6af8A13258f7CbD0D39E11b35e01a32F93',
balance: '100',
unconfirmedBalance: '1',
},
},
},
],
response: {
descriptor: '0xFc6B5d6af8A13258f7CbD0D39E11b35e01a32F93',
empty: false,
balance: '100',
availableBalance: '100',
history: {},
misc: {
nonce: '100',
stakingPools: undefined,
addressAliases: undefined,
contractInfo: undefined,
},
},
},
{
description: 'ETH smart contract',
params: {
descriptor: '0x3c205C8B3e02421Da82064646788c82f7bd753B9',
},
serverFixtures: [
{
method: 'getAccountInfo',
response: {
data: {
nonce: '100',
address: '0x3c205C8B3e02421Da82064646788c82f7bd753B9',
contractInfo: {
contract: '0xFc6B5d6af8A13258f7CbD0D39E11b35e01a32F93',
name: 'Grzegorz Brzęczyszczykiewicz',
symbol: 'GRZBRZ',
decimals: 3,
type: 'ERC20',
contract: '0x3c205C8B3e02421Da82064646788c82f7bd753B9',
name: 'PureFi Token',
symbol: 'UFI',
decimals: 18,
},
},
},
},
],
response: {
descriptor: '0xFc6B5d6af8A13258f7CbD0D39E11b35e01a32F93',
descriptor: '0x3c205C8B3e02421Da82064646788c82f7bd753B9',
empty: false,
history: {},
misc: {
nonce: '100',
stakingPools: undefined,
addressAliases: undefined,
contractInfo: {
type: 'ERC20',
contract: '0xFc6B5d6af8A13258f7CbD0D39E11b35e01a32F93',
name: 'Grzegorz Brzęczyszczykiewicz',
symbol: 'GRZBRZ',
decimals: 3,
contract: '0x3c205C8B3e02421Da82064646788c82f7bd753B9',
name: 'PureFi Token',
symbol: 'UFI',
decimals: 18,
},
},
},
Expand Down
3 changes: 3 additions & 0 deletions packages/suite/src/hooks/wallet/useRbfForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ const useRbfState = ({ selectedAccount, rbfParams, chainedTxs }: UseRbfProps) =>
// override Account data
const rbfAccount = {
...account,
// on EVM, when send tx is pending, balance has not been changed yet
availableBalance:
account.networkType === 'ethereum' ? account.balance : account.availableBalance,
utxo: rbfParams.utxo.concat(availableUtxo),
// make sure that the exact same change output will be picked by @trezor/connect > hd-wallet during the tx compose process
// fallback to default if change address is not present
Expand Down

0 comments on commit b87ea38

Please sign in to comment.