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

solution: common class for new tx ui #1355

Merged
merged 4 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
71 changes: 67 additions & 4 deletions packages/core/src/transaction/workflow/TxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,30 @@
isEthereum,
} from '../../blockchains';
import { EthereumTransactionType } from '../ethereum';
import { CreateBitcoinTx, CreateErc20ApproveTx, CreateErc20ConvertTx, CreateErc20Tx, CreateEtherTx } from './create-tx';
import {
CreateBitcoinTx,
CreateErc20ApproveTx,
CreateErc20ConvertTx,
CreateErc20RecoveryTx,
CreateErc20Tx,
CreateEtherRecoveryTx,
CreateEtherTx,
} from './create-tx';
import {
AnyCreateTx,
AnyErc20CreateTx,
AnyEtherCreateTx,
AnyEthereumCreateTx,
AnyEthereumRecoveryTx,
fromBitcoinPlainTx,
fromEthereumPlainTx,
isAnyErc20CreateTx,
isErc20ApproveCreateTx,
isErc20ConvertCreateTx,
isErc20CreateTx,
isErc20RecoveryCreateTx,
isEtherCreateTx,
isEtherRecoveryCreateTx,
} from './create-tx/types';
import {
AnyPlainTx,
Expand Down Expand Up @@ -141,23 +152,75 @@
this.mergeEthereumTx(transaction, createTx);
}

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

if (isErc20ConvertCreateTx(createTx)) {
this.mergeEthereumFee(createTx);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L160 was not covered by tests
}

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

this.mergeEthereumFee(createTx);
}

if (isEtherRecoveryCreateTx(createTx) || isErc20RecoveryCreateTx(createTx)) {
if (isChanged) {
return this.convertEthereumRecoveryTx(createTx);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L173 was not covered by tests
}

this.mergeEthereumFee(createTx);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L176 was not covered by tests
}
}
}

return createTx;
}

private convertEthereumRecoveryTx(oldCreateTx: AnyEthereumRecoveryTx): AnyEthereumRecoveryTx {
const { asset, entry, feeRange, tokenRegistry } = this;
const { getBalance } = this.dataProvider;

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/TxBuilder.ts#L185-L186

Added lines #L185 - L186 were not covered by tests

const blockchain = blockchainIdToCode(entry.blockchain);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L188 was not covered by tests

const { coinTicker, eip1559: supportEip1559 = false } = Blockchains[blockchain].params;

const type = supportEip1559 ? oldCreateTx.type : EthereumTransactionType.LEGACY;

let newCreateTx: AnyEthereumRecoveryTx;

if (tokenRegistry.hasAddress(blockchain, asset)) {
newCreateTx = new CreateErc20RecoveryTx(asset, tokenRegistry, blockchain, type);
newCreateTx.totalBalance = getBalance(entry, coinTicker) as WeiAny;
newCreateTx.totalTokenBalance = getBalance(entry, asset, newCreateTx.transferFrom);

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/TxBuilder.ts#L197-L199

Added lines #L197 - L199 were not covered by tests
} else {
newCreateTx = new CreateEtherRecoveryTx(null, blockchain, type);
newCreateTx.totalBalance = getBalance(entry, asset) as WeiAny;

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/TxBuilder.ts#L201-L202

Added lines #L201 - L202 were not covered by tests
}

newCreateTx.from = entry.address?.value;
newCreateTx.to = oldCreateTx.to;

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/TxBuilder.ts#L205-L206

Added lines #L205 - L206 were not covered by tests

if (blockchain === oldCreateTx.blockchain && (oldCreateTx.gasPrice?.isPositive() ?? false)) {
newCreateTx.gasPrice = oldCreateTx.gasPrice;
newCreateTx.maxGasPrice = oldCreateTx.maxGasPrice;
newCreateTx.priorityGasPrice = oldCreateTx.priorityGasPrice;

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/TxBuilder.ts#L209-L211

Added lines #L209 - L211 were not covered by tests
} else if (isEthereumFeeRange(feeRange)) {
newCreateTx.gasPrice = feeRange.stdMaxGasPrice;
newCreateTx.maxGasPrice = feeRange.stdMaxGasPrice;
newCreateTx.priorityGasPrice = feeRange.stdPriorityGasPrice;

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/TxBuilder.ts#L213-L215

Added lines #L213 - L215 were not covered by tests
}

newCreateTx.target = TxTarget.SEND_ALL;
newCreateTx.rebalance();

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/TxBuilder.ts#L218-L219

Added lines #L218 - L219 were not covered by tests

return newCreateTx;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L221 was not covered by tests
}

private convertEthereumTx(oldCreateTx: EthereumBasicCreateTx): EthereumBasicCreateTx {
const { asset, entry, feeRange, ownerAddress, tokenRegistry } = this;
const { getBalance } = this.dataProvider;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { TokenRegistry } from '../../../blockchains';
import { EthereumBasicPlainTx, TxMetaType } from '../types';
import { CreateErc20Tx, fromPlainDetails } from './CreateErc20Tx';

export class CreateErc20RecoveryTx extends CreateErc20Tx {
meta = { type: TxMetaType.ERC20_RECOVERY };

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateErc20RecoveryTx.ts#L6

Added line #L6 was not covered by tests

static fromPlain(details: EthereumBasicPlainTx, tokenRegistry: TokenRegistry): CreateErc20RecoveryTx {
return new CreateErc20RecoveryTx(fromPlainDetails(details, tokenRegistry), tokenRegistry);

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateErc20RecoveryTx.ts#L9

Added line #L9 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { EthereumBasicPlainTx, TxMetaType } from '../types';
import { CreateEtherTx, fromPlainDetails } from './CreateEtherTx';

export class CreateEtherRecoveryTx extends CreateEtherTx {
meta = { type: TxMetaType.ETHER_RECOVERY };

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateEtherRecoveryTx.ts#L5

Added line #L5 was not covered by tests

static fromPlain(details: EthereumBasicPlainTx): CreateEtherRecoveryTx {
return new CreateEtherRecoveryTx(fromPlainDetails(details));

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateEtherRecoveryTx.ts#L8

Added line #L8 was not covered by tests
}
}
5 changes: 5 additions & 0 deletions packages/core/src/transaction/workflow/create-tx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
AnyErc20CreateTx,
AnyEtherCreateTx,
AnyEthereumCreateTx,
AnyEthereumRecoveryTx,
EthereumTx,
fromBitcoinPlainTx,
fromErc20PlainTx,
Expand All @@ -24,9 +25,11 @@ export {
isErc20CancelCreateTx,
isErc20ConvertCreateTx,
isErc20CreateTx,
isErc20RecoveryCreateTx,
isErc20SpeedUpCreateTx,
isEtherCancelCreateTx,
isEtherCreateTx,
isEtherRecoveryCreateTx,
isEtherSpeedUpCreateTx,
} from './types';

Expand All @@ -36,8 +39,10 @@ export { CreateBitcoinCancelTx } from './CreateBitcoinCancelTx';
export { CreateBitcoinSpeedUpTx } from './CreateBitcoinSpeedUpTx';
export { CreateErc20CancelTx } from './CreateErc20CancelTx';
export { CreateErc20ConvertTx, Erc20ConvertTxDetails } from './CreateErc20ConvertTx';
export { CreateErc20RecoveryTx } from './CreateErc20RecoveryTx';
export { CreateErc20SpeedUpTx } from './CreateErc20SpeedUpTx';
export { CreateErc20Tx, Erc20TxDetails } from './CreateErc20Tx';
export { CreateEtherCancelTx } from './CreateEtherCancelTx';
export { CreateEtherRecoveryTx } from './CreateEtherRecoveryTx';
export { CreateEtherSpeedUpTx } from './CreateEtherSpeedUpTx';
export { CreateEtherTx, TxDetails } from './CreateEtherTx';
19 changes: 18 additions & 1 deletion packages/core/src/transaction/workflow/create-tx/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import { CreateErc20ApproveTx } from './CreateErc20ApproveTx';
import { CreateErc20CancelTx } from './CreateErc20CancelTx';
import { CreateErc20ConvertTx } from './CreateErc20ConvertTx';
import { CreateErc20RecoveryTx } from './CreateErc20RecoveryTx';
import { CreateErc20SpeedUpTx } from './CreateErc20SpeedUpTx';
import { CreateErc20Tx } from './CreateErc20Tx';
import { CreateEtherCancelTx } from './CreateEtherCancelTx';
import { CreateEtherRecoveryTx } from './CreateEtherRecoveryTx';
import { CreateEtherSpeedUpTx } from './CreateEtherSpeedUpTx';
import { CreateEtherTx } from './CreateEtherTx';

Expand All @@ -36,7 +38,8 @@
export type AnyEtherCreateTx = CreateEtherTx | CreateEtherCancelTx | CreateEtherSpeedUpTx;
export type AnyErc20CreateTx = CreateErc20Tx | CreateErc20CancelTx | CreateErc20SpeedUpTx;
export type AnyContractCreateTx = AnyErc20CreateTx | CreateErc20ApproveTx | CreateErc20ConvertTx;
export type AnyEthereumCreateTx = AnyEtherCreateTx | AnyContractCreateTx;
export type AnyEthereumRecoveryTx = CreateEtherRecoveryTx | CreateErc20RecoveryTx;
export type AnyEthereumCreateTx = AnyEtherCreateTx | AnyContractCreateTx | AnyEthereumRecoveryTx;
export type AnyCreateTx = AnyBitcoinCreateTx | AnyEthereumCreateTx;

const bitcoinTxMetaTypes: Readonly<TxMetaType[]> = [
Expand All @@ -47,12 +50,14 @@

const etherTxMetaTypes: Readonly<TxMetaType[]> = [
TxMetaType.ETHER_CANCEL,
TxMetaType.ETHER_RECOVERY,
TxMetaType.ETHER_SPEEDUP,
TxMetaType.ETHER_TRANSFER,
];

const erc20TxMetaTypes: Readonly<TxMetaType[]> = [
TxMetaType.ERC20_CANCEL,
TxMetaType.ERC20_RECOVERY,
TxMetaType.ERC20_SPEEDUP,
TxMetaType.ERC20_TRANSFER,
];
Expand Down Expand Up @@ -91,6 +96,10 @@
return createTx.meta.type === TxMetaType.ETHER_CANCEL;
}

export function isEtherRecoveryCreateTx(createTx: AnyCreateTx): createTx is CreateEtherRecoveryTx {
return createTx.meta.type === TxMetaType.ETHER_RECOVERY;
}

export function isEtherSpeedUpCreateTx(createTx: AnyCreateTx): createTx is CreateEtherSpeedUpTx {
return createTx.meta.type === TxMetaType.ETHER_SPEEDUP;
}
Expand Down Expand Up @@ -119,6 +128,10 @@
return createTx.meta.type === TxMetaType.ERC20_CONVERT;
}

export function isErc20RecoveryCreateTx(createTx: AnyCreateTx): createTx is CreateErc20RecoveryTx {
return createTx.meta.type === TxMetaType.ERC20_RECOVERY;
}

export function isErc20SpeedUpCreateTx(createTx: AnyCreateTx): createTx is CreateErc20SpeedUpTx {
return createTx.meta.type === TxMetaType.ERC20_SPEEDUP;
}
Expand All @@ -140,6 +153,8 @@
switch (transaction.meta.type) {
case TxMetaType.ETHER_CANCEL:
return CreateEtherCancelTx.fromPlain(transaction);
case TxMetaType.ETHER_RECOVERY:
return CreateEtherRecoveryTx.fromPlain(transaction);

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/types.ts#L157

Added line #L157 was not covered by tests
case TxMetaType.ETHER_SPEEDUP:
return CreateEtherSpeedUpTx.fromPlain(transaction);
case TxMetaType.ETHER_TRANSFER:
Expand All @@ -157,6 +172,8 @@
return CreateErc20CancelTx.fromPlain(transaction, tokenRegistry);
case TxMetaType.ERC20_CONVERT:
return CreateErc20ConvertTx.fromPlain(transaction, tokenRegistry);
case TxMetaType.ERC20_RECOVERY:
return CreateErc20RecoveryTx.fromPlain(transaction, tokenRegistry);

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/types.ts#L176

Added line #L176 was not covered by tests
case TxMetaType.ERC20_SPEEDUP:
return CreateErc20SpeedUpTx.fromPlain(transaction, tokenRegistry);
case TxMetaType.ERC20_TRANSFER:
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/transaction/workflow/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
AnyErc20CreateTx,
AnyEtherCreateTx,
AnyEthereumCreateTx,
AnyEthereumRecoveryTx,
ApproveTarget,
BitcoinTx,
BitcoinTxDetails,
Expand All @@ -17,9 +18,11 @@
CreateErc20ApproveTx,
CreateErc20CancelTx,
CreateErc20ConvertTx,
CreateErc20RecoveryTx,

Check warning on line 21 in packages/core/src/transaction/workflow/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/index.ts#L21

Added line #L21 was not covered by tests
CreateErc20SpeedUpTx,
CreateErc20Tx,
CreateEtherCancelTx,
CreateEtherRecoveryTx,

Check warning on line 25 in packages/core/src/transaction/workflow/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/index.ts#L25

Added line #L25 was not covered by tests
CreateEtherSpeedUpTx,
CreateEtherTx,
Erc20ApproveTxDetails,
Expand All @@ -43,9 +46,11 @@
isErc20CancelCreateTx,
isErc20ConvertCreateTx,
isErc20CreateTx,
isErc20RecoveryCreateTx,

Check warning on line 49 in packages/core/src/transaction/workflow/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/index.ts#L49

Added line #L49 was not covered by tests
isErc20SpeedUpCreateTx,
isEtherCancelCreateTx,
isEtherCreateTx,
isEtherRecoveryCreateTx,

Check warning on line 53 in packages/core/src/transaction/workflow/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/index.ts#L53

Added line #L53 was not covered by tests
isEtherSpeedUpCreateTx,
} from './create-tx';

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/transaction/workflow/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export enum TxMetaType {
ERC20_APPROVE,
ERC20_CANCEL,
ERC20_CONVERT,
ERC20_RECOVERY,
ERC20_SPEEDUP,
ERC20_TRANSFER,
}
Expand Down
4 changes: 4 additions & 0 deletions packages/react-app/src/transaction/CreateTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
case TxAction.CONVERT:
title = 'Create Convert Transaction';

break;

Check warning on line 53 in packages/react-app/src/transaction/CreateTransaction.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/CreateTransaction.tsx#L53

Added line #L53 was not covered by tests
case TxAction.RECOVERY:
title = 'Create Recovery Transaction';

Check warning on line 55 in packages/react-app/src/transaction/CreateTransaction.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react-app/src/transaction/CreateTransaction.tsx#L55

Added line #L55 was not covered by tests

break;
case TxAction.SPEEDUP:
title = 'Speed Up Transaction';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@
getFiatBalance(asset: string): CurrencyAmount | undefined;
}

interface TxOrigin {
action: TxAction;
entries: WalletEntry[];
entry: WalletEntry;
}

interface DispatchProps {
prepareTransaction(action: TxAction, entry: WalletEntry): void;
prepareTransaction(origin: TxOrigin): void;
setAsset(asset: string): void;
setEntry(entry: WalletEntry, ownerAddress?: string): void;
setStage(stage: CreateTxStage): void;
Expand Down Expand Up @@ -88,8 +94,8 @@
const mounted = React.useRef(true);

React.useEffect(() => {
prepareTransaction(action, entry);
}, [action, entry, storedTx, prepareTransaction]);
prepareTransaction({ action, entries, entry });

Check warning on line 97 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#L97

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

React.useEffect(() => {
return () => {
Expand Down Expand Up @@ -132,7 +138,7 @@
walletId = EntryIdOp.of(entryId).extractWalletId();
}

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

if (entries == null || entries.length === 0) {
throw new Error('Something went wrong while getting entries from wallet');
Expand Down Expand Up @@ -250,8 +256,8 @@
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(dispatch: any, { initialAllowance, storedTx }) => ({
prepareTransaction(action, entry) {
dispatch(txStash.actions.prepareTransaction({ action, entry, initialAllowance, storedTx }));
prepareTransaction({ action, entries, entry }) {
dispatch(txStash.actions.prepareTransaction({ action, entries, entry, initialAllowance, storedTx }));

Check warning on line 260 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#L260

Added line #L260 was not covered by tests
},
setAsset(asset) {
dispatch(txStash.actions.setAsset(asset));
Expand Down
Loading
Loading