Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem: amount field max value is not rebalancing #1370

Merged
merged 2 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/core/src/transaction/workflow/TxBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('TxBuilder', () => {
const tokenRegistry = new TokenRegistry([tokenData]);

const btcEntry: BitcoinEntry = {
id: '7d395b44-0bac-49b9-98de-47e88dbc5a28-0',
id: '50391c5d-a517-4b7a-9c42-1411e0603d30-0',
address: {
type: 'xpub',
value: 'vpub_common',
Expand All @@ -46,7 +46,7 @@ describe('TxBuilder', () => {
addresses: [],
};
const ethEntry1: EthereumEntry = {
id: '50391c5d-a517-4b7a-9c42-1411e0603d30-0',
id: '50391c5d-a517-4b7a-9c42-1411e0603d30-1',
address: {
type: 'single',
value: '0x0',
Expand All @@ -60,7 +60,7 @@ describe('TxBuilder', () => {
createdAt: new Date(),
};
const ethEntry2: EthereumEntry = {
id: '50391c5d-a517-4b7a-9c42-1411e0603d30-1',
id: '50391c5d-a517-4b7a-9c42-1411e0603d30-2',
address: {
type: 'single',
value: '0x1',
Expand All @@ -74,7 +74,7 @@ describe('TxBuilder', () => {
createdAt: new Date(),
};
const etcEntry: EthereumEntry = {
id: '50391c5d-a517-4b7a-9c42-1411e0603d30-2',
id: '50391c5d-a517-4b7a-9c42-1411e0603d30-3',
address: {
type: 'single',
value: '0x2',
Expand Down
22 changes: 18 additions & 4 deletions packages/core/src/transaction/workflow/TxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,15 @@

if (isErc20ApproveCreateTx(createTx)) {
if (isChanged) {
createTx = this.transformErc20ApproveTx(createTx);
return this.transformErc20ApproveTx(createTx);

Check warning on line 157 in packages/core/src/transaction/workflow/TxBuilder.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/TxBuilder.ts#L157

Added line #L157 was not covered by tests
}

this.mergeEthereumFee(createTx);
}

if (isErc20ConvertCreateTx(createTx)) {
if (isChanged) {
createTx = this.transformErc20ConvertTx(createTx);
return this.transformErc20ConvertTx(createTx);

Check warning on line 165 in packages/core/src/transaction/workflow/TxBuilder.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/TxBuilder.ts#L165

Added line #L165 was not covered by tests
}

this.mergeEthereumFee(createTx);
Expand Down Expand Up @@ -338,6 +338,8 @@
createTx.gasPrice = feeRange.stdMaxGasPrice;
createTx.maxGasPrice = feeRange.stdMaxGasPrice;
createTx.priorityGasPrice = feeRange.stdPriorityGasPrice;

createTx.rebalance();
}
}

Expand Down Expand Up @@ -375,11 +377,17 @@
}

private transformErc20ApproveTx(createTx: CreateErc20ApproveTx): CreateErc20ApproveTx {
const { asset, entry, tokenRegistry } = this;
const { asset, entry, feeRange, tokenRegistry } = this;

Check warning on line 380 in packages/core/src/transaction/workflow/TxBuilder.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/TxBuilder.ts#L380

Added line #L380 was not covered by tests
const { getBalance } = this.dataProvider;

const blockchain = blockchainIdToCode(entry.blockchain);

if (blockchain !== createTx.blockchain && isEthereumFeeRange(feeRange)) {
createTx.gasPrice = feeRange.stdMaxGasPrice;
createTx.maxGasPrice = feeRange.stdMaxGasPrice;
createTx.priorityGasPrice = feeRange.stdPriorityGasPrice;

Check warning on line 388 in packages/core/src/transaction/workflow/TxBuilder.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/TxBuilder.ts#L386-L388

Added lines #L386 - L388 were not covered by tests
}

let tokenAddress = asset;

if (!tokenRegistry.hasAddress(blockchain, tokenAddress)) {
Expand All @@ -399,11 +407,17 @@
}

private transformErc20ConvertTx(createTx: CreateErc20ConvertTx): CreateErc20ConvertTx {
const { asset, entry, tokenRegistry } = this;
const { asset, entry, feeRange, tokenRegistry } = this;

Check warning on line 410 in packages/core/src/transaction/workflow/TxBuilder.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/TxBuilder.ts#L410

Added line #L410 was not covered by tests
const { getBalance } = this.dataProvider;

const blockchain = blockchainIdToCode(entry.blockchain);

if (blockchain !== createTx.blockchain && isEthereumFeeRange(feeRange)) {
createTx.gasPrice = feeRange.stdMaxGasPrice;
createTx.maxGasPrice = feeRange.stdMaxGasPrice;
createTx.priorityGasPrice = feeRange.stdPriorityGasPrice;

Check warning on line 418 in packages/core/src/transaction/workflow/TxBuilder.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/TxBuilder.ts#L416-L418

Added lines #L416 - L418 were not covered by tests
}

createTx.asset = asset;

let tokenAddress = asset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,13 @@
this._target = value;

switch (value) {
case ApproveTarget.MAX_AVAILABLE:
this._amount = this.totalTokenBalance;

break;
case ApproveTarget.INFINITE:
this._amount = this.token.getAmount(INFINITE_ALLOWANCE);

break;
case ApproveTarget.MAX_AVAILABLE:
this._amount = this.totalTokenBalance;

break;
}
}
Expand Down Expand Up @@ -255,11 +255,23 @@
}

rebalance(): void {
// Nothing
const { amount, target, totalTokenBalance, token } = this;

switch (target) {
case ApproveTarget.INFINITE:
this._amount = token.getAmount(INFINITE_ALLOWANCE);

Check warning on line 262 in packages/core/src/transaction/workflow/create-tx/CreateErc20ApproveTx.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateErc20ApproveTx.ts#L262

Added line #L262 was not covered by tests

break;

Check warning on line 264 in packages/core/src/transaction/workflow/create-tx/CreateErc20ApproveTx.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateErc20ApproveTx.ts#L264

Added line #L264 was not covered by tests
case ApproveTarget.MAX_AVAILABLE:
this._amount = totalTokenBalance;

Check warning on line 266 in packages/core/src/transaction/workflow/create-tx/CreateErc20ApproveTx.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateErc20ApproveTx.ts#L266

Added line #L266 was not covered by tests

break;

Check warning on line 268 in packages/core/src/transaction/workflow/create-tx/CreateErc20ApproveTx.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateErc20ApproveTx.ts#L268

Added line #L268 was not covered by tests
default:
this._amount = new TokenAmount(amount, token.getUnits(), token);
}
}

setToken(token: Token, totalBalance: WeiAny, totalTokenBalance: TokenAmount, iep1559 = false): void {
this._amount = new TokenAmount(this.amount, token.getUnits(), token);
this._token = token;

this.totalBalance = totalBalance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,15 @@
setStage,
setTransaction,
}) => {
const entryId = React.useRef<EntryId>();

Check warning on line 94 in packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx#L94

Added line #L94 was not covered by tests
const mounted = React.useRef(true);

React.useEffect(() => {
prepareTransaction({ action, entries, entry });
if (entry.id !== entryId.current) {
entryId.current = entry.id;

Check warning on line 99 in packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx#L99

Added line #L99 was not covered by tests

prepareTransaction({ action, entries, entry });

Check warning on line 101 in packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx#L101

Added line #L101 was not covered by tests
}
}, [action, entries, entry, storedTx, prepareTransaction]);

React.useEffect(() => {
Expand Down Expand Up @@ -138,9 +143,33 @@
walletId = EntryIdOp.of(entryId).extractWalletId();
}

const { entries } = accounts.selectors.findWallet(state, walletId) ?? {};
const tokenRegistry = new TokenRegistry(state.application.tokens);

Check warning on line 146 in packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx#L146

Added line #L146 was not covered by tests

const entries =
accounts.selectors.findWallet(state, walletId)?.entries.filter(({ blockchain, id, receiveDisabled }) => {
let valid = !receiveDisabled;

Check warning on line 150 in packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx#L150

Added line #L150 was not covered by tests

if (action === TxAction.APPROVE || action === TxAction.CONVERT) {
const blockchainCode = blockchainIdToCode(blockchain);

Check warning on line 153 in packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx#L153

Added line #L153 was not covered by tests

switch (action) {

Check warning on line 155 in packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx#L155

Added line #L155 was not covered by tests
case TxAction.APPROVE:
valid &&= tokenRegistry.hasAnyToken(blockchainCode);

Check warning on line 157 in packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx#L157

Added line #L157 was not covered by tests

break;

Check warning on line 159 in packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx#L159

Added line #L159 was not covered by tests
case TxAction.CONVERT:
valid &&= tokenRegistry.hasWrappedToken(blockchainCode);

Check warning on line 161 in packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx#L161

Added line #L161 was not covered by tests

if (entries == null || entries.length === 0) {
break;

Check warning on line 163 in packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx#L163

Added line #L163 was not covered by tests
}

valid &&= accounts.selectors.getBalance(state, id, amountFactory(blockchainCode)(0)).isPositive();

Check warning on line 166 in packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx#L166

Added line #L166 was not covered by tests
}

return valid;

Check warning on line 169 in packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/SetupTransaction.tsx#L169

Added line #L169 was not covered by tests
}) ?? [];

if (entries.length === 0) {
throw new Error('Something went wrong while getting entries from wallet');
}

Expand All @@ -160,8 +189,6 @@

const blockchain = blockchainIdToCode(entry.blockchain);

const tokenRegistry = new TokenRegistry(state.application.tokens);

const getBalance = (entry: WalletEntry, asset: string, ownerAddress?: string): BigAmount => {
if (tokenRegistry.hasAddress(blockchain, asset)) {
const token = tokenRegistry.byAddress(blockchain, asset);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EthereumEntry, isEthereumEntry } from '@emeraldpay/emerald-vault-core';
import { Blockchains, blockchainIdToCode, workflow } from '@emeraldwallet/core';
import { EthereumEntry } from '@emeraldpay/emerald-vault-core';
import { workflow } from '@emeraldwallet/core';
import { FormLabel, FormRow } from '@emeraldwallet/ui';
import * as React from 'react';
import { SelectAsset } from '../../../../../../common/SelectAsset';
Expand All @@ -21,32 +21,13 @@
}

private renderFrom(): React.ReactElement {
const { entries, tokenRegistry } = this.data;
const { getBalance } = this.dataProvider;
const { entry, entries } = this.data;

Check warning on line 24 in packages/react-app/src/transaction/SetupTransaction/flow/blockchain/ethereum/erc20/approve.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/flow/blockchain/ethereum/erc20/approve.tsx#L24

Added line #L24 was not covered by tests
const { setEntry } = this.handler;

const approvingEntries = entries
.filter((entry): entry is EthereumEntry => isEthereumEntry(entry))
.filter((entry) => {
const blockchain = blockchainIdToCode(entry.blockchain);

return (
!entry.receiveDisabled &&
tokenRegistry.hasAnyToken(blockchain) &&
getBalance(entry, Blockchains[blockchain].params.coinTicker).isPositive()
);
});

let { entry } = this.data;

if (!approvingEntries.some(({ id }) => id === entry.id)) {
[entry] = approvingEntries;
}

return (
<FormRow>
<FormLabel>From</FormLabel>
<SelectEntry entries={approvingEntries} selectedEntry={entry} onSelect={setEntry} />
<SelectEntry entries={entries} selectedEntry={entry} onSelect={setEntry} />
</FormRow>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EthereumEntry, isEthereumEntry } from '@emeraldpay/emerald-vault-core';
import { Blockchains, blockchainIdToCode, workflow } from '@emeraldwallet/core';
import { EthereumEntry } from '@emeraldpay/emerald-vault-core';
import { Blockchains, workflow } from '@emeraldwallet/core';

Check warning on line 2 in packages/react-app/src/transaction/SetupTransaction/flow/blockchain/ethereum/erc20/convert.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/flow/blockchain/ethereum/erc20/convert.tsx#L2

Added line #L2 was not covered by tests
import { FormLabel, FormRow } from '@emeraldwallet/ui';
import { ToggleButton, ToggleButtonGroup } from '@material-ui/lab';
import * as React from 'react';
Expand Down Expand Up @@ -41,32 +41,13 @@
}

private renderFrom(): React.ReactElement {
const { entries, ownerAddress, tokenRegistry } = this.data;
const { getBalance } = this.dataProvider;
const { entry, entries, ownerAddress } = this.data;

Check warning on line 44 in packages/react-app/src/transaction/SetupTransaction/flow/blockchain/ethereum/erc20/convert.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/flow/blockchain/ethereum/erc20/convert.tsx#L44

Added line #L44 was not covered by tests
const { setEntry } = this.handler;

const convertEntries = entries
.filter((entry): entry is EthereumEntry => isEthereumEntry(entry))
.filter((entry) => {
const blockchain = blockchainIdToCode(entry.blockchain);

return (
!entry.receiveDisabled &&
tokenRegistry.hasWrappedToken(blockchain) &&
getBalance(entry, Blockchains[blockchain].params.coinTicker).isPositive()
);
});

let { entry } = this.data;

if (!convertEntries.some(({ id }) => id === entry.id)) {
[entry] = convertEntries;
}

return (
<FormRow>
<FormLabel>From</FormLabel>
<SelectEntry entries={convertEntries} ownerAddress={ownerAddress} selectedEntry={entry} onSelect={setEntry} />
<SelectEntry entries={entries} ownerAddress={ownerAddress} selectedEntry={entry} onSelect={setEntry} />
</FormRow>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

return entries
.filter((entry): entry is EthereumEntry => isEthereumEntry(entry))
.filter(({ blockchain, receiveDisabled }) => !receiveDisabled && blockchain === entry.blockchain);
.filter(({ blockchain }) => blockchain === entry.blockchain);

Check warning on line 28 in packages/react-app/src/transaction/SetupTransaction/flow/blockchain/ethereum/recovery.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/flow/blockchain/ethereum/recovery.tsx#L28

Added line #L28 was not covered by tests
}

private renderTo(entries: EthereumEntry[]): React.ReactElement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export abstract class TransferFlow extends CommonFlow {
<FormLabel>From</FormLabel>
<SelectEntry
withAllowances
entries={entries.filter((entry) => !entry.receiveDisabled)}
entries={entries}
ownerAddress={ownerAddress}
selectedEntry={entry}
onSelect={setEntry}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
export const ApproveAmount: React.FC<OwnProps> = ({ createTx, setTransaction }) => {
const styles = useStyles();

const maxAmountUnits = React.useRef(createTx.amount.units);

Check warning on line 31 in packages/react-app/src/transaction/SetupTransaction/flow/components/ApproveAmount.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/flow/components/ApproveAmount.tsx#L31

Added line #L31 was not covered by tests

const [allowInfinite, setAllowInfinite] = React.useState(createTx.target === workflow.ApproveTarget.INFINITE);
const [maxAmount, setMaxAmount] = React.useState<BigNumber | undefined>();

Expand Down Expand Up @@ -55,6 +57,16 @@
setMaxAmount(createTx.amount.getNumberByUnit(createTx.amount.units.top));
};

React.useEffect(() => {
const { units } = createTx.amount;

Check warning on line 61 in packages/react-app/src/transaction/SetupTransaction/flow/components/ApproveAmount.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/flow/components/ApproveAmount.tsx#L60-L61

Added lines #L60 - L61 were not covered by tests

if (createTx.target === workflow.ApproveTarget.MAX_AVAILABLE && !units.equals(maxAmountUnits.current)) {
maxAmountUnits.current = units;

Check warning on line 64 in packages/react-app/src/transaction/SetupTransaction/flow/components/ApproveAmount.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/flow/components/ApproveAmount.tsx#L64

Added line #L64 was not covered by tests

setMaxAmount(createTx.amount.getNumberByUnit(units.top));

Check warning on line 66 in packages/react-app/src/transaction/SetupTransaction/flow/components/ApproveAmount.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/SetupTransaction/flow/components/ApproveAmount.tsx#L66

Added line #L66 was not covered by tests
}
}, [createTx]);

return (
<>
<FormRow>
Expand Down
13 changes: 13 additions & 0 deletions packages/store/src/txstash/handler/blockchain/ethereum/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import { TokenBalanceBelong, accounts, tokens } from '../../../..';
import { getTokens } from '../../../../application/selectors';
import { setAsset, setPreparing, setTransaction } from '../../../actions';
import { getTransaction } from '../../../selectors';
import { EntryHandler } from '../../types';
import { fetchFee } from './fee';

Expand Down Expand Up @@ -61,6 +62,12 @@
}) ?? token.getAmount(0);
}

const tx = getTransaction(state);

Check warning on line 65 in packages/store/src/txstash/handler/blockchain/ethereum/prepare.ts

View check run for this annotation

Codecov / codecov/patch

packages/store/src/txstash/handler/blockchain/ethereum/prepare.ts#L65

Added line #L65 was not covered by tests

if (tx != null && workflow.isErc20ApprovePlainTx(tx)) {
createTx.target = tx.target;

Check warning on line 68 in packages/store/src/txstash/handler/blockchain/ethereum/prepare.ts

View check run for this annotation

Codecov / codecov/patch

packages/store/src/txstash/handler/blockchain/ethereum/prepare.ts#L68

Added line #L68 was not covered by tests
}

storeProvider.dispatch(setAsset(createTx.asset));
storeProvider.dispatch(setTransaction(createTx.dump()));
storeProvider.dispatch(setPreparing(false));
Expand Down Expand Up @@ -93,6 +100,12 @@
}) ?? token.getAmount(0);
}

const tx = getTransaction(state);

Check warning on line 103 in packages/store/src/txstash/handler/blockchain/ethereum/prepare.ts

View check run for this annotation

Codecov / codecov/patch

packages/store/src/txstash/handler/blockchain/ethereum/prepare.ts#L103

Added line #L103 was not covered by tests

if (tx != null && workflow.isErc20ConvertPlainTx(tx)) {
createTx.target = tx.target;

Check warning on line 106 in packages/store/src/txstash/handler/blockchain/ethereum/prepare.ts

View check run for this annotation

Codecov / codecov/patch

packages/store/src/txstash/handler/blockchain/ethereum/prepare.ts#L106

Added line #L106 was not covered by tests
}

storeProvider.dispatch(setAsset(createTx.asset));
storeProvider.dispatch(setTransaction(createTx.dump()));
storeProvider.dispatch(setPreparing(false));
Expand Down
Loading