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

fix: add guard for draft tx in send validation #26295

Merged
merged 3 commits into from
Aug 1, 2024
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
17 changes: 15 additions & 2 deletions ui/ducks/send/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,10 @@ const slice = createSlice({
const draftTransaction =
state.draftTransactions[state.currentTransactionUUID];

if (!draftTransaction) {
return;
}

// use maxFeePerGas as the multiplier if working with a FEE_MARKET transaction
// otherwise use gasPrice
if (
Expand Down Expand Up @@ -1080,7 +1084,7 @@ const slice = createSlice({
const draftTransaction =
state.draftTransactions[state.currentTransactionUUID];
let amount = '0x0';
if (draftTransaction.sendAsset.type === AssetType.token) {
if (draftTransaction?.sendAsset.type === AssetType.token) {
const decimals = draftTransaction.sendAsset.details?.decimals ?? 0;

const multiplier = Math.pow(10, Number(decimals));
Expand Down Expand Up @@ -1292,7 +1296,7 @@ const slice = createSlice({
state.gasTotalForLayer1 = action.payload;
if (
state.amountMode === AMOUNT_MODES.MAX &&
draftTransaction.sendAsset.type === AssetType.native
draftTransaction?.sendAsset.type === AssetType.native
) {
slice.caseReducers.updateAmountToMax(state);
}
Expand Down Expand Up @@ -1456,6 +1460,10 @@ const slice = createSlice({
const draftTransaction =
state.draftTransactions[state.currentTransactionUUID];

if (!draftTransaction) {
return;
}

const amountValue = new Numeric(draftTransaction.amount.value, 16);

switch (true) {
Expand Down Expand Up @@ -1557,6 +1565,11 @@ const slice = createSlice({
validateGasField: (state) => {
const draftTransaction =
state.draftTransactions[state.currentTransactionUUID];

if (!draftTransaction) {
return;
}

const insufficientFunds = !isBalanceSufficient({
amount:
draftTransaction.sendAsset.type === AssetType.native
Expand Down
62 changes: 62 additions & 0 deletions ui/ducks/send/send.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,28 @@ describe('Send Slice', () => {
expect(draft.amount.value).toStrictEqual('0xadf7');
expect(draft.status).toStrictEqual(SEND_STATUSES.VALID);
});

it('should not error when draft transaction is not defined', () => {
const state = getInitialSendStateWithExistingTxState({
gas: {
gasPrice: '0x1',
maxFeePerGas: '0x2',
gasLimit: GAS_LIMITS.SIMPLE,
},
});

delete state.draftTransactions['test-uuid'];

const action = {
type: 'send/calculateGasTotal',
};

const runAction = () => sendReducer(state, action);

expect(runAction).not.toThrow();
});
});

describe('resetSendState', () => {
it('should set the state back to a blank slate matching the initialState object', () => {
const action = {
Expand Down Expand Up @@ -1076,6 +1097,29 @@ describe('Send Slice', () => {
);
expect(draftTransaction.status).toBe(SEND_STATUSES.INVALID);
});

it('should not throw error when draft transaction does not exist', () => {
const tokenAssetState = getInitialSendStateWithExistingTxState({
amount: {
value: '0x77359400', // 2000000000
},
sendAsset: {
type: AssetType.token,
balance: '0x6fc23ac0', // 1875000000
details: {
decimals: 0,
},
},
});

const action = {
type: 'send/validateAmountField',
};

delete tokenAssetState.draftTransactions['test-uuid'];

expect(() => sendReducer(tokenAssetState, action)).not.toThrow();
});
});

describe('validateGasField', () => {
Expand All @@ -1101,6 +1145,24 @@ describe('Send Slice', () => {
INSUFFICIENT_FUNDS_ERROR,
);
});
it('should not throw error when draft transaction does not exist', () => {
const gasFieldState = getInitialSendStateWithExistingTxState({
account: {
balance: '0x0',
},
gas: {
gasTotal: '0x1319718a5000', // 21000000000000
},
});

delete gasFieldState.draftTransactions['test-uuid'];

const action = {
type: 'send/validateGasField',
};

expect(() => sendReducer(gasFieldState, action)).not.toThrow();
});
});

// TODO: update this
Expand Down