Skip to content

Commit

Permalink
solution: add create tx target validation
Browse files Browse the repository at this point in the history
  • Loading branch information
BOOMER74 committed Dec 28, 2023
1 parent a5f9906 commit e2f003e
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ export class CreateBitcoinTx implements BitcoinTx {
this.rebalance();
}

get target(): TxTarget {
return this.tx.target;

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateBitcoinTx.ts#L213

Added line #L213 was not covered by tests
}

set target(target: TxTarget) {
this.tx.target = target;

Expand Down Expand Up @@ -380,6 +384,10 @@ export class CreateBitcoinTx implements BitcoinTx {
return ValidationResult.INSUFFICIENT_FUNDS;
}

if (!this.validateTarget()) {
return ValidationResult.INCORRECT_TARGET_AMOUNT;

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateBitcoinTx.ts#L388

Added line #L388 was not covered by tests
}

return ValidationResult.OK;
}

Expand All @@ -390,4 +398,21 @@ export class CreateBitcoinTx implements BitcoinTx {

return sequence < MAX_SEQUENCE ? sequence + 1 : MAX_SEQUENCE;
}

private validateTarget(): boolean {
const {
totalToSpend,
tx: { amount, target },
} = this;

if (target === TxTarget.SEND_ALL) {
if (amount == null) {
return false;

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateBitcoinTx.ts#L410

Added line #L410 was not covered by tests
}

return totalToSpend.minus(this.getFees()).equals(amount);

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateBitcoinTx.ts#L413

Added line #L413 was not covered by tests
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ export class CreateErc20ApproveTx implements Erc20ApproveTxDetails {
this.totalBalance = totalBalance;
this.totalTokenBalance = totalTokenBalance;
this.type = iep1559 ? EthereumTransactionType.EIP1559 : EthereumTransactionType.LEGACY;

this.rebalance();
}

validate(): ValidationResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ export class CreateErc20ConvertTx implements Erc20ConvertTxDetails {
this.totalBalance = totalBalance;
this.totalTokenBalance = totalTokenBalance;
this.type = iep1559 ? EthereumTransactionType.EIP1559 : EthereumTransactionType.LEGACY;

this.rebalance();

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateErc20ConvertTx.ts#L281

Added line #L281 was not covered by tests
}

validate(): ValidationResult {
Expand All @@ -302,6 +304,28 @@ export class CreateErc20ConvertTx implements Erc20ConvertTxDetails {
}
}

if (!this.validateTarget()) {
return ValidationResult.INCORRECT_TARGET_AMOUNT;

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateErc20ConvertTx.ts#L308

Added line #L308 was not covered by tests
}

return ValidationResult.OK;
}

validateTarget(): boolean {
const { amount, asset, target, token, totalBalance, totalTokenBalance } = this;

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateErc20ConvertTx.ts#L315

Added line #L315 was not covered by tests

if (target === TxTarget.SEND_ALL) {
if (totalBalance == null || totalTokenBalance == null) {
return false;

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateErc20ConvertTx.ts#L319

Added line #L319 was not covered by tests
}

if (asset.toLowerCase() === token.address.toLowerCase()) {
return amount.equals(totalTokenBalance);

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateErc20ConvertTx.ts#L323

Added line #L323 was not covered by tests
} else {
return amount.plus(this.getFees()).equals(totalBalance);

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateErc20ConvertTx.ts#L325

Added line #L325 was not covered by tests
}
}

return true;

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateErc20ConvertTx.ts#L329

Added line #L329 was not covered by tests
}
}
16 changes: 16 additions & 0 deletions packages/core/src/transaction/workflow/create-tx/CreateErc20Tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,25 @@ export class CreateErc20Tx implements Erc20TxDetails, EthereumTx<BigAmount> {
return ValidationResult.INSUFFICIENT_FUNDS;
}

if (!this.validateTarget()) {
return ValidationResult.INCORRECT_TARGET_AMOUNT;

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateErc20Tx.ts#L260

Added line #L260 was not covered by tests
}

return ValidationResult.OK;
}

public validateTarget(): boolean {
if (this.target === TxTarget.SEND_ALL) {
if (this.totalTokenBalance == null) {
return false;

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateErc20Tx.ts#L269

Added line #L269 was not covered by tests
}

return this.getTotal().equals(this.totalTokenBalance);

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateErc20Tx.ts#L272

Added line #L272 was not covered by tests
}

return true;
}

public rebalance(): boolean {
if (this.target === TxTarget.SEND_ALL) {
if (this.totalTokenBalance == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Wei } from '@emeraldpay/bigamount-crypto';
import BigNumber from 'bignumber.js';
import { BlockchainCode } from '../../../blockchains';
import { EthereumPlainTx, TxMetaType, TxTarget, ValidationResult } from '../types';
import { EthereumBasicPlainTx, TxMetaType, TxTarget, ValidationResult } from '../types';
import { CreateEtherTx } from './CreateEtherTx';

describe('CreateEthereumTx', () => {
Expand Down Expand Up @@ -298,7 +298,7 @@ describe('CreateEthereumTx', () => {
});

it('reads from dumps', () => {
const dump: EthereumPlainTx = {
const dump: EthereumBasicPlainTx = {
amount: '999580000000500002/WEI',
asset: 'ETH',
blockchain: BlockchainCode.ETH,
Expand Down Expand Up @@ -326,7 +326,7 @@ describe('CreateEthereumTx', () => {
});

it('reads from dumps, manual tx', () => {
const dump: EthereumPlainTx = {
const dump: EthereumBasicPlainTx = {
amount: '999580000000500002/WEI',
asset: 'ETH',
blockchain: BlockchainCode.ETH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ export class CreateEtherTx implements TxDetails, EthereumTx<WeiAny> {
return ValidationResult.INSUFFICIENT_FUNDS;
}

if (!this.validateTarget()) {
return ValidationResult.INCORRECT_TARGET_AMOUNT;

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/transaction/workflow/create-tx/CreateEtherTx.ts#L209

Added line #L209 was not covered by tests
}

return ValidationResult.OK;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/transaction/workflow/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export enum TxMetaType {
}

export enum ValidationResult {
INSUFFICIENT_FEE_PRICE,
INCORRECT_TARGET_AMOUNT,
INSUFFICIENT_FUNDS,
INSUFFICIENT_TOKEN_FUNDS,
NO_CHANGE_ADDRESS,
Expand Down

0 comments on commit e2f003e

Please sign in to comment.