Skip to content

Commit

Permalink
Merge branch 'develop' into hm/migrate-snap-permissions-to-caveats
Browse files Browse the repository at this point in the history
  • Loading branch information
hmalik88 committed Mar 7, 2023
2 parents 875088d + 03732af commit d89877e
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 69 deletions.
9 changes: 8 additions & 1 deletion app/scripts/controllers/metametrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,14 @@ export default class MetaMetricsController {
currency: fragment.currency,
environmentType: fragment.environmentType,
actionId: fragment.actionId,
uniqueIdentifier: fragment.uniqueIdentifier,
// We append success or failure to the unique-identifier so that the
// messageId can still be idempotent, but so that it differs from the
// initial event fired. The initial event was preventing new events from
// making it to mixpanel because they were using the same unique ID as
// the events processed in other parts of the fragment lifecycle.
uniqueIdentifier: fragment.uniqueIdentifier
? `${fragment.uniqueIdentifier}-${abandoned ? 'failure' : 'success'}`
: undefined,
});
const { fragments } = this.store.getState();
delete fragments[id];
Expand Down
3 changes: 2 additions & 1 deletion app/scripts/controllers/metametrics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const SAMPLE_NON_PERSISTED_EVENT = {
category: 'Unit Test',
successEvent: 'sample non-persisted event success',
failureEvent: 'sample non-persisted event failure',
uniqueIdentifier: 'sample-non-persisted-event',
properties: {
test: true,
},
Expand Down Expand Up @@ -175,7 +176,7 @@ describe('MetaMetricsController', function () {
...DEFAULT_EVENT_PROPERTIES,
test: true,
},
messageId: Utils.generateRandomId(),
messageId: 'sample-non-persisted-event-failure',
timestamp: new Date(),
});
const metaMetricsController = getMetaMetricsController();
Expand Down
8 changes: 4 additions & 4 deletions coverage-targets.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
// subset of files to check against these targets.
module.exports = {
global: {
lines: 64.33,
branches: 52.98,
statements: 63.59,
functions: 56.59,
lines: 64,
branches: 52.5,
statements: 63.1,
functions: 56.1,
},
transforms: {
branches: 100,
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/mock-e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,10 @@ async function setupMocking(server, testSpecificMock) {
};
});

// It disables loading of token icons, e.g. this URL: https://static.metaswap.codefi.network/api/v1/tokenIcons/1337/0x0000000000000000000000000000000000000000.png
// It disables loading of token icons, e.g. this URL: https://static.metafi.codefi.network/api/v1/tokenIcons/1337/0x0000000000000000000000000000000000000000.png
await server
.forGet(
/^https:\/\/static\.metaswap\.codefi\.network\/api\/v1\/tokenIcons\/1337\/.*\.png/u,
/^https:\/\/static\.metafi\.codefi\.network\/api\/v1\/tokenIcons\/1337\/.*\.png/u,
)
.thenCallback(() => {
return {
Expand Down
131 changes: 117 additions & 14 deletions test/e2e/swaps/shared.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { strict: assert } = require('assert');
const FixtureBuilder = require('../fixture-builder');
const { veryLargeDelayMs } = require('../helpers');
const { regularDelayMs, veryLargeDelayMs } = require('../helpers');

const ganacheOptions = {
accounts: [
Expand All @@ -16,32 +17,27 @@ const withFixturesOptions = {
ganacheOptions,
};

const loadSwaps = async (driver) => {
const loadExtension = async (driver) => {
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);
await driver.clickElement(
'.wallet-overview__buttons .icon-button:nth-child(3)',
);
};

const buildQuote = async (driver, options) => {
await driver.clickElement(
'.wallet-overview__buttons .icon-button:nth-child(3)',
);
await driver.fill('input[placeholder*="0"]', options.amount);
await driver.delay(veryLargeDelayMs); // Need an extra delay after typing an amount.
await driver.clickElement(
'[class="dropdown-search-list__closed-primary-label dropdown-search-list__select-default"]',
await driver.waitForSelector(
'[class="dropdown-input-pair dropdown-input-pair__to"]',
);
await driver.wait(async () => {
const tokens = await driver.findElements('.searchable-item-list__item');
return tokens.length > 1;
});
await driver.clickElement('.searchable-item-list__labels');
await driver.clickElement('.dropdown-input-pair__to');
await driver.clickElement('input[data-testid="search-list-items"]');
await driver.fill(
'input[data-testid="search-list-items"]',
options.swapTo || options.swapToContractAddress,
);
await driver.delay(veryLargeDelayMs); // Need an extra delay after typing an amount.
if (options.swapTo) {
await driver.wait(async () => {
const tokenNames = await driver.findElements(
Expand All @@ -63,8 +59,115 @@ const buildQuote = async (driver, options) => {
await driver.clickElement('.searchable-item-list__primary-label');
};

const reviewQuote = async (driver, options) => {
await driver.clickElement({ text: 'Review swap', tag: 'button' });
await driver.waitForSelector({ text: 'Swap', tag: 'button' });
await driver.waitForSelector({
css: '[class*="box--align-items-center"]',
text: 'Estimated gas fee',
});
const sourceValue = await driver.waitForSelector(
'.main-quote-summary__source-row-value',
);
assert.equal(
await sourceValue.getText(),
options.amount,
'Error: Quote has wrong amount',
);
const sourceSymbol = await driver.waitForSelector(
'.main-quote-summary__source-row-symbol',
);
assert.equal(
await sourceSymbol.getText(),
options.swapFrom,
'Error: SwapFrom has wrong symbol',
);
const swapToSymbol = await driver.waitForSelector(
'.main-quote-summary__destination-row',
);
assert.equal(
await swapToSymbol.getText(),
options.swapTo,
'Error: SwapTo has wrong symbol',
);
await driver.waitForSelector(
'[class="exchange-rate-display main-quote-summary__exchange-rate-display"]',
);
await driver.waitForSelector('[class="fee-card__info-tooltip-container"]');
await driver.waitForSelector({
css: '[class="countdown-timer__time"]',
text: '0:23',
});
};

const waitForTransactionToComplete = async (driver, tokenName) => {
const sucessfulTransactionMessage = await driver.waitForSelector(
{
css: '[class="awaiting-swap__header"]',
text: 'Transaction complete',
},
{ timeout: 30000 },
);
assert.equal(
await sucessfulTransactionMessage.getText(),
'Transaction complete',
'Incorrect transaction message',
);
const sucessfulTransactionToken = await driver.waitForSelector({
css: '[class="awaiting-swap__amount-and-symbol"]',
text: tokenName,
});
assert.equal(
await sucessfulTransactionToken.getText(),
tokenName,
'Incorrect token name',
);
await driver.clickElement({ text: 'Close', tag: 'button' });
await driver.waitForSelector('[data-testid="home__asset-tab"]');
};

const checkActivityTransaction = async (driver, options) => {
await driver.clickElement('[data-testid="home__activity-tab"]');
const itemsText = await driver.findElements('.list-item__title');
assert.equal(
await itemsText[options.index].getText(),
`Swap ${options.swapFrom} to ${options.swapTo}`,
'Title is incorrect',
);
const amountValues = await driver.findElements(
'.transaction-list-item__primary-currency',
);
assert.equal(
await amountValues[options.index].getText(),
`-${options.amount} ${options.swapFrom}`,
'Transaction amount is incorrect',
);
await itemsText[options.index].click();
await driver.delay(regularDelayMs);
const txStatus = await driver.findElement(
'.transaction-list-item-details__tx-status >div > div:last-child',
);
assert.equal(
await txStatus.getText(),
`Confirmed`,
`Transaction status is not 'Confirmed'`,
);
const txAmount = await driver.findElement(
'.transaction-breakdown__value--amount',
);
assert.equal(
await txAmount.getText(),
`-${options.amount} ${options.swapFrom}`,
'Transaction breakdown is incorrect',
);
await driver.clickElement('[data-testid="popover-close"]');
};

module.exports = {
withFixturesOptions,
loadSwaps,
loadExtension,
buildQuote,
reviewQuote,
waitForTransactionToComplete,
checkActivityTransaction,
};
109 changes: 66 additions & 43 deletions test/e2e/swaps/swap-eth.spec.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,87 @@
const { strict: assert } = require('assert');

const { withFixtures } = require('../helpers');
const { withFixturesOptions, loadSwaps, buildQuote } = require('./shared');
const {
withFixturesOptions,
loadExtension,
buildQuote,
reviewQuote,
waitForTransactionToComplete,
checkActivityTransaction,
} = require('./shared');

describe('Swap Eth for another Token', function () {
it('Completes a Swap between Eth and Matic', async function () {
it('Completes second Swaps while first swap is processing', async function () {
withFixturesOptions.ganacheOptions.blockTime = 10;

await withFixtures(
{
...withFixturesOptions,
failOnConsoleError: false,
title: this.test.title,
},
async ({ driver }) => {
await loadSwaps(driver);
await loadExtension(driver);
await buildQuote(driver, {
amount: 2,
swapTo: 'DAI',
amount: 0.001,
swapTo: 'USDC',
});
await driver.clickElement({ text: 'Review swap', tag: 'button' });
await driver.waitForSelector('[class*="box--align-items-center"]');
const estimatedEth = await driver.waitForSelector({
css: '[class*="box--align-items-center"]',
text: 'Estimated gas fee',
await reviewQuote(driver, {
amount: '0.001',
swapFrom: 'TESTETH',
swapTo: 'USDC',
});
assert.equal(await estimatedEth.getText(), 'Estimated gas fee');
await driver.waitForSelector(
'[class="exchange-rate-display main-quote-summary__exchange-rate-display"]',
);
await driver.waitForSelector(
'[class="fee-card__info-tooltip-container"]',
);
await driver.waitForSelector({
css: '[class="countdown-timer__time"]',
text: '0:24',
await driver.clickElement({ text: 'Swap', tag: 'button' });
await driver.clickElement({ text: 'View in activity', tag: 'button' });
await buildQuote(driver, {
amount: 0.003,
swapTo: 'DAI',
});
await reviewQuote(driver, {
amount: '0.003',
swapFrom: 'TESTETH',
swapTo: 'DAI',
});
await driver.clickElement({ text: 'Swap', tag: 'button' });
const sucessfulTransactionMessage = await driver.waitForSelector({
css: '[class="awaiting-swap__header"]',
text: 'Transaction complete',
await waitForTransactionToComplete(driver, 'DAI');
await checkActivityTransaction(driver, {
index: 0,
amount: '0.003',
swapFrom: 'TESTETH',
swapTo: 'DAI',
});
assert.equal(
await sucessfulTransactionMessage.getText(),
'Transaction complete',
);
const sucessfulTransactionToken = await driver.waitForSelector({
css: '[class="awaiting-swap__amount-and-symbol"]',
text: 'DAI',
await checkActivityTransaction(driver, {
index: 1,
amount: '0.001',
swapFrom: 'TESTETH',
swapTo: 'USDC',
});
},
);
});
it('Completes a Swap between Eth and Dai', async function () {
await withFixtures(
{
...withFixturesOptions,
title: this.test.title,
},
async ({ driver }) => {
await loadExtension(driver);
await buildQuote(driver, {
amount: 2,
swapTo: 'DAI',
});
assert.equal(await sucessfulTransactionToken.getText(), 'DAI');
await driver.clickElement({ text: 'Close', tag: 'button' });
await driver.clickElement('[data-testid="home__activity-tab"]');
const swaptotal = await driver.waitForSelector({
css: '[class="transaction-list-item__primary-currency"]',
text: '-2 TESTETH',
await reviewQuote(driver, {
amount: '2',
swapFrom: 'TESTETH',
swapTo: 'DAI',
});
assert.equal(await swaptotal.getText(), '-2 TESTETH');
const swaptotaltext = await driver.waitForSelector({
css: '[class="list-item__title"]',
text: 'Swap TESTETH to DAI',
await driver.clickElement({ text: 'Swap', tag: 'button' });
await waitForTransactionToComplete(driver, 'DAI');
await checkActivityTransaction(driver, {
index: 0,
amount: '2',
swapFrom: 'TESTETH',
swapTo: 'DAI',
});
assert.equal(await swaptotaltext.getText(), 'Swap TESTETH to DAI');
},
);
});
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/swaps/swaps-notifications.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { strict: assert } = require('assert');

const { withFixtures } = require('../helpers');

const { withFixturesOptions, loadSwaps, buildQuote } = require('./shared');
const { withFixturesOptions, loadExtension, buildQuote } = require('./shared');

describe('Swaps - notifications', function () {
async function mockTradesApiPriceSlippageError(mockServer) {
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('Swaps - notifications', function () {
title: this.test.title,
},
async ({ driver }) => {
await loadSwaps(driver);
await loadExtension(driver);
await buildQuote(driver, {
amount: 2,
swapTo: 'INUINU',
Expand Down Expand Up @@ -99,7 +99,7 @@ describe('Swaps - notifications', function () {
title: this.test.title,
},
async ({ driver }) => {
await loadSwaps(driver);
await loadExtension(driver);
await buildQuote(driver, {
amount: 50,
swapTo: 'USDC',
Expand Down Expand Up @@ -134,7 +134,7 @@ describe('Swaps - notifications', function () {
title: this.test.title,
},
async ({ driver }) => {
await loadSwaps(driver);
await loadExtension(driver);
await buildQuote(driver, {
amount: 2,
swapToContractAddress: '0x72c9Fb7ED19D3ce51cea5C56B3e023cd918baaDf',
Expand Down

0 comments on commit d89877e

Please sign in to comment.