Skip to content
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
6 changes: 6 additions & 0 deletions app/_locales/en/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions app/_locales/en_GB/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions app/scripts/controllers/app-state-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,32 @@ describe('AppStateController', () => {
});
});
});

describe('setEnforcedSimulationsSlippage', () => {
it('updates the enforcedSimulationsSlippage state', async () => {
await withController(({ controller }) => {
controller.setEnforcedSimulationsSlippage(23);
expect(controller.state.enforcedSimulationsSlippage).toBe(23);
});
});
});

describe('setEnforcedSimulationsSlippageForTransaction', () => {
it('updates the enforcedSimulationsSlippageForTransactions state', async () => {
await withController(({ controller }) => {
controller.setEnforcedSimulationsSlippageForTransaction(
TRANSACTION_ID_MOCK,
25,
);

expect(
controller.state.enforcedSimulationsSlippageForTransactions,
).toStrictEqual({
[TRANSACTION_ID_MOCK]: 25,
});
});
});
});
});

type WithControllerOptions = {
Expand Down
27 changes: 27 additions & 0 deletions app/scripts/controllers/app-state-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export type AppStateControllerState = {
lastUpdatedAt: number | null;
enableEnforcedSimulations: boolean;
enableEnforcedSimulationsForTransactions: Record<string, boolean>;
enforcedSimulationsSlippage: number;
enforcedSimulationsSlippageForTransactions: Record<string, number>;
};

const controllerName = 'AppStateController';
Expand Down Expand Up @@ -213,6 +215,8 @@ const getDefaultAppStateControllerState = (): AppStateControllerState => ({
lastUpdatedAt: null,
enableEnforcedSimulations: true,
enableEnforcedSimulationsForTransactions: {},
enforcedSimulationsSlippage: 10,
enforcedSimulationsSlippageForTransactions: {},
...getInitialStateOverrides(),
});

Expand Down Expand Up @@ -398,6 +402,14 @@ const controllerMetadata = {
persist: false,
anonymous: true,
},
enforcedSimulationsSlippage: {
persist: true,
anonymous: true,
},
enforcedSimulationsSlippageForTransactions: {
persist: false,
anonymous: true,
},
};

export class AppStateController extends BaseController<
Expand Down Expand Up @@ -1148,4 +1160,19 @@ export class AppStateController extends BaseController<
state.enableEnforcedSimulationsForTransactions[transactionId] = enabled;
});
}

setEnforcedSimulationsSlippage(value: number): void {
this.update((state) => {
state.enforcedSimulationsSlippage = value;
});
}

setEnforcedSimulationsSlippageForTransaction(
transactionId: string,
value: number,
): void {
this.update((state) => {
state.enforcedSimulationsSlippageForTransactions[transactionId] = value;
});
}
}
151 changes: 142 additions & 9 deletions app/scripts/lib/transaction/containers/enforced-simulations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import { cloneDeep } from 'lodash';
import { DELEGATOR_CONTRACTS } from '@metamask/delegation-deployments';
import { Hex, remove0x } from '@metamask/utils';
import { DelegationControllerSignDelegationAction } from '@metamask/delegation-controller';
import { toHex } from '@metamask/controller-utils';
import { TransactionControllerInitMessenger } from '../../../controller-init/messengers/transaction-controller-messenger';
import {
AppStateControllerGetStateAction,
AppStateControllerState,
} from '../../../controllers/app-state-controller';
import { enforceSimulations } from './enforced-simulations';

const TOKEN_MOCK = '0x4567890abcdef1234567890abcdef1234567890a' as Hex;
Expand All @@ -35,12 +40,20 @@ const TX_PARAMS_MOCK: TransactionParams = {
};

const TRANSACTION_META_MOCK: TransactionMeta = {
txParams: {},
chainId: CHAIN_ID_MOCK,
id: '123-456',
simulationData: SIMULATION_DATA_MOCK,
txParams: TX_PARAMS_MOCK,
} as TransactionMeta;

describe('Enforced Simulations Utils', () => {
let messenger: TransactionControllerInitMessenger;
let options: Parameters<typeof enforceSimulations>[0];
let simulationData: SimulationData;

const getAppStateMock: jest.MockedFn<
AppStateControllerGetStateAction['handler']
> = jest.fn();

const signDelegationMock: jest.MockedFn<
DelegationControllerSignDelegationAction['handler']
Expand All @@ -50,29 +63,43 @@ describe('Enforced Simulations Utils', () => {
jest.resetAllMocks();

const baseMessenger = new Messenger<
DelegationControllerSignDelegationAction,
| AppStateControllerGetStateAction
| DelegationControllerSignDelegationAction,
never
>();

baseMessenger.registerActionHandler(
'AppStateController:getState',
getAppStateMock,
);

baseMessenger.registerActionHandler(
'DelegationController:signDelegation',
signDelegationMock,
);

messenger = baseMessenger.getRestricted({
name: 'TransactionControllerInitMessenger',
allowedActions: ['DelegationController:signDelegation'],
allowedActions: [
'AppStateController:getState',
'DelegationController:signDelegation',
],
allowedEvents: [],
});

getAppStateMock.mockReturnValue({
enforcedSimulationsSlippage: 10,
enforcedSimulationsSlippageForTransactions: {},
} as AppStateControllerState);

signDelegationMock.mockResolvedValue(DELEGATION_SIGNATURE_MOCK);

options = {
chainId: CHAIN_ID_MOCK,
messenger,
simulationData: cloneDeep(SIMULATION_DATA_MOCK),
txParams: TX_PARAMS_MOCK,
transactionMeta: cloneDeep(TRANSACTION_META_MOCK),
};

simulationData = options.transactionMeta.simulationData as SimulationData;
});

describe('enforceSimulations', () => {
Expand Down Expand Up @@ -123,7 +150,7 @@ describe('Enforced Simulations Utils', () => {
});

it('includes erc20 token balance change caveat', async () => {
options.simulationData.tokenBalanceChanges = [
simulationData.tokenBalanceChanges = [
{
...BALANCE_CHANGE_MOCK,
address: TOKEN_MOCK,
Expand All @@ -146,7 +173,7 @@ describe('Enforced Simulations Utils', () => {
});

it('includes erc721 token balance change caveat', async () => {
options.simulationData.tokenBalanceChanges = [
simulationData.tokenBalanceChanges = [
{
...BALANCE_CHANGE_MOCK,
address: TOKEN_MOCK,
Expand All @@ -169,7 +196,7 @@ describe('Enforced Simulations Utils', () => {
});

it('includes erc1155 token balance change caveat', async () => {
options.simulationData.tokenBalanceChanges = [
simulationData.tokenBalanceChanges = [
{
...BALANCE_CHANGE_MOCK,
address: TOKEN_MOCK,
Expand Down Expand Up @@ -210,5 +237,111 @@ describe('Enforced Simulations Utils', () => {
),
);
});

describe('applies slippage', () => {
it('if decrease', async () => {
simulationData.tokenBalanceChanges = [
{
...BALANCE_CHANGE_MOCK,
difference: toHex(100000),
address: TOKEN_MOCK,
standard: SimulationTokenStandard.erc20,
},
];

getAppStateMock.mockReturnValue({
enforcedSimulationsSlippage: 23,
enforcedSimulationsSlippageForTransactions: {},
} as AppStateControllerState);

const { updateTransaction } = await enforceSimulations(options);

const newTransaction = cloneDeep(TRANSACTION_META_MOCK);
updateTransaction?.(newTransaction);

expect(newTransaction.txParams.data).toStrictEqual(
expect.stringContaining(remove0x(toHex(123000)).toLowerCase()),
);
});

it('if increase', async () => {
simulationData.tokenBalanceChanges = [
{
...BALANCE_CHANGE_MOCK,
isDecrease: false,
difference: toHex(100000),
address: TOKEN_MOCK,
standard: SimulationTokenStandard.erc20,
},
];

getAppStateMock.mockReturnValue({
enforcedSimulationsSlippage: 23,
enforcedSimulationsSlippageForTransactions: {},
} as AppStateControllerState);

const { updateTransaction } = await enforceSimulations(options);

const newTransaction = cloneDeep(TRANSACTION_META_MOCK);
updateTransaction?.(newTransaction);

expect(newTransaction.txParams.data).toStrictEqual(
expect.stringContaining(remove0x(toHex(77000)).toLowerCase()),
);
});

it('if overridden', async () => {
simulationData.tokenBalanceChanges = [
{
...BALANCE_CHANGE_MOCK,
isDecrease: false,
difference: toHex(100000),
address: TOKEN_MOCK,
standard: SimulationTokenStandard.erc20,
},
];

getAppStateMock.mockReturnValue({
enforcedSimulationsSlippage: 10,
enforcedSimulationsSlippageForTransactions: {
[TRANSACTION_META_MOCK.id]: 15,
},
} as AppStateControllerState);

const { updateTransaction } = await enforceSimulations(options);

const newTransaction = cloneDeep(TRANSACTION_META_MOCK);
updateTransaction?.(newTransaction);

expect(newTransaction.txParams.data).toStrictEqual(
expect.stringContaining(remove0x(toHex(85000)).toLowerCase()),
);
});

// @ts-expect-error Wrong `it` type
it.each([
SimulationTokenStandard.erc721,
SimulationTokenStandard.erc1155,
])('unless token is %s', async (standard: SimulationTokenStandard) => {
simulationData.tokenBalanceChanges = [
{
...BALANCE_CHANGE_MOCK,
isDecrease: false,
difference: toHex(100000),
address: TOKEN_MOCK,
standard,
},
];

const { updateTransaction } = await enforceSimulations(options);

const newTransaction = cloneDeep(TRANSACTION_META_MOCK);
updateTransaction?.(newTransaction);

expect(newTransaction.txParams.data).not.toStrictEqual(
expect.stringContaining(remove0x(toHex(90000)).toLowerCase()),
);
});
});
});
});
Loading
Loading