8c2a0f1905
* test: swap flow cypress tests * fix: use default parameter * feat: use Swap Component on TDP * feat: auto nav for TDP tokens * chore: merge * chore: merge * chore: merge * chore: merge * fix: remove extra inputCurrency URL parsing logic * fix: undo last change * fix: pass expected chain id to swap component * fix: search for default tokens on unconnected networks if needed * test: e2e test for l2 token * fix: delete irrelevant tests * fix: address comments * fix: lint error * test: update TDP e2e tests * fix: use pageChainId for filter * fix: rename chainId * fix: typecheck * fix: chainId bug * fix: chainId required fixes * fix: bad merge in e2e test * fix: remove unused test util * fix: remove unnecessary variable * fix: token defaults * fix: address comments * fix: address comments and fix tests * fix: e2e test formatting, remove Maybe<> * fix: remove unused variable * fix: use feature flag for swap component on TDP * fix: back button * feat: copy review screen UI from widgetg * fix: modal padding * feat: add final detail row * fix: remove widget comment * fix: update unit tests * fix: code style consistency * fix: remove padding from AutoColumn * fix: update snapshots * fix: use semantic gaps * fix: more px and gaps * fix: design feedbacks * fix: button radius in summary modal * fix: design nits * feat: update design of summary modal * fix: font weight and vertical spacing * fix: update snapshots * fix: css nits * wip: move approval to summary modal * wip: not working * feat: working * fix: fix flow * feat: simplify states and build new modal UI * feat: todos and differs fix * feat: update tx status modal * feat: split up approve and permit * feat: error state * feat: update success and error states * feat: undo changes to TxConfirmationModal * feat: re-order functions * wip: move approval to summary modal * wip: not working * feat: update permit2 e2e tests * feat: tests passing * fix: swap test * fix: bad merge * wip: move approval to summary modal * wip: not working * feat: PendingModalContent tests * feat: useMaxAmountIn * fix: bad merge * fix: naming * fix: modal flicker when refetching trade * wip: move approval to summary modal * wip: not working * feat: working * fix: fix flow * feat: simplify states and build new modal UI * feat: todos and differs fix * feat: update tx status modal * feat: split up approve and permit * feat: error state * feat: update success and error states * feat: undo changes to TxConfirmationModal * feat: remove step indicators when only one step * feat: move content into PendingModalContent component * fix: lint * chore: merge * fix: update tests for new modal * feat: add l2 chain logo to modal * feat: add unit test * fix: correct modal state when moving between steps * fix: correct modal state when moving between steps * fix: proper error handling of user rejection of swap * feat: update e2e test * fix: typecheck * feat: design updates, state updates * fix: comments * fix: code style improvements * feat: require trade to be defined * fix: remove extra props from ThemedTexts * fix: one more trans * fix: remove unused export * feat: remove undefined checks and other fixes * fix: update test * fix: add missing dollar sign * fix: remove null check and update test * fix: remove max width from detail row value * fix: remove isOpen prop * fix: isopen * feat: refactor approval flow into a hook * fix: custom error type * fix: testid fix * fix: text colors * fix: add comment * fix: tradeMeaningfullyDiffers improvement and prepareFlow fix * fix: address comments * fix: headerContent prop * fix: change tooltip to external link * feat: add comments explaining async state * fix: test updates * fix: nits * fix: reduce nesting * fix: address comments * test: remove line from test for debugging * fix: update tests * fix: address comments * fix: comments * fix: update tests * fix: update tests * fix: more nesting in test * fix: update test * fix: update e2e test * fix: update error test * fix: update content in test * fix: reorganize test code
212 lines
8.0 KiB
TypeScript
212 lines
8.0 KiB
TypeScript
import { MaxUint160, MaxUint256 } from '@uniswap/permit2-sdk'
|
|
|
|
import { DAI, USDC_MAINNET } from '../../src/constants/tokens'
|
|
import { getTestSelector } from '../utils'
|
|
|
|
/** Initiates a swap. */
|
|
function initiateSwap() {
|
|
cy.get('#swap-button').should('not.be.disabled')
|
|
// Completes the swap.
|
|
cy.get('#swap-button').click()
|
|
cy.get(getTestSelector('confirm-swap-button')).click()
|
|
}
|
|
|
|
describe('Permit2', () => {
|
|
// The same tokens & swap-amount combination is used for all permit2 tests.
|
|
const INPUT_TOKEN = DAI
|
|
const OUTPUT_TOKEN = USDC_MAINNET
|
|
const TEST_BALANCE_INCREMENT = 0.01
|
|
|
|
beforeEach(() => {
|
|
// Sets up a swap between INPUT_TOKEN and OUTPUT_TOKEN.
|
|
cy.visit(`/swap/?inputCurrency=${INPUT_TOKEN.address}&outputCurrency=${OUTPUT_TOKEN.address}`, {
|
|
ethereum: 'hardhat',
|
|
})
|
|
cy.get('#swap-currency-input .token-amount-input').type(TEST_BALANCE_INCREMENT.toString())
|
|
})
|
|
|
|
/** Asserts permit2 has a max approval for spend of the input token on-chain. */
|
|
function expectTokenAllowanceForPermit2ToBeMax() {
|
|
// check token approval
|
|
return cy
|
|
.hardhat()
|
|
.then(({ approval, wallet }) => approval.getTokenAllowanceForPermit2({ owner: wallet, token: INPUT_TOKEN }))
|
|
.should('deep.equal', MaxUint256)
|
|
}
|
|
|
|
/** Asserts the universal router has a max permit2 approval for spend of the input token on-chain. */
|
|
function expectPermit2AllowanceForUniversalRouterToBeMax(approvalTime: number) {
|
|
return cy
|
|
.hardhat()
|
|
.then((hardhat) => hardhat.approval.getPermit2Allowance({ owner: hardhat.wallet, token: INPUT_TOKEN }))
|
|
.then((allowance) => {
|
|
cy.wrap(MaxUint160.eq(allowance.amount)).should('eq', true)
|
|
// Asserts that the on-chain expiration is in 30 days, within a tolerance of 40 seconds.
|
|
const expected = Math.floor((approvalTime + 2_592_000_000) / 1000)
|
|
cy.wrap(allowance.expiration).should('be.closeTo', expected, 40)
|
|
})
|
|
}
|
|
|
|
it('swaps when user has already approved token and permit2', () => {
|
|
cy.hardhat().then(({ approval, wallet }) => {
|
|
approval.setTokenAllowanceForPermit2({ owner: wallet, token: INPUT_TOKEN })
|
|
approval.setPermit2Allowance({ owner: wallet, token: INPUT_TOKEN })
|
|
})
|
|
initiateSwap()
|
|
cy.get(getTestSelector('confirmation-close-icon')).click()
|
|
// Verifies that there is a successful swap notification.
|
|
cy.contains('Swapped').should('exist')
|
|
})
|
|
|
|
it('swaps after completing full permit2 approval process', () => {
|
|
initiateSwap()
|
|
cy.contains('Allow trading DAI on Uniswap').should('exist')
|
|
cy.contains('Approved').should('exist')
|
|
|
|
cy.contains('Unlock DAI for swapping').should('exist')
|
|
cy.contains('Confirm Swap').should('exist')
|
|
|
|
cy.then(() => {
|
|
const approvalTime = Date.now()
|
|
|
|
cy.contains('Swapped').should('exist')
|
|
|
|
expectTokenAllowanceForPermit2ToBeMax()
|
|
expectPermit2AllowanceForUniversalRouterToBeMax(approvalTime)
|
|
})
|
|
})
|
|
|
|
it('swaps after handling user rejection of both approval and signature', () => {
|
|
const USER_REJECTION = { code: 4001 }
|
|
cy.hardhat().then((hardhat) => {
|
|
const tokenApprovalStub = cy.stub(hardhat.wallet, 'sendTransaction')
|
|
tokenApprovalStub.rejects(USER_REJECTION) // reject token approval
|
|
const permitApprovalStub = cy.stub(hardhat.provider, 'send')
|
|
permitApprovalStub.withArgs('eth_signTypedData_v4').rejects(USER_REJECTION) // reject permit approval
|
|
permitApprovalStub.callThrough() // allows non-eth_signTypedData_v4 send calls to return non-stubbed values
|
|
|
|
initiateSwap()
|
|
|
|
// tokenApprovalStub should reject here, and the modal should revert to the review state.
|
|
cy.contains('Review Swap').should('be.visible')
|
|
|
|
cy.then(() => {
|
|
// The user is now allowing approval, but the permit2 signature will be rejected by the user (permitApprovalStub).
|
|
tokenApprovalStub.restore() // allow token approval
|
|
})
|
|
|
|
cy.get(getTestSelector('confirm-swap-button')).click()
|
|
cy.contains('Allow trading DAI on Uniswap').should('exist')
|
|
cy.contains('Approved').should('exist')
|
|
|
|
// permitApprovalStub should reject here, and the modal should revert to the review state.
|
|
cy.contains('Review Swap')
|
|
.should('be.visible')
|
|
.then(() => {
|
|
permitApprovalStub.restore() // allow permit approval
|
|
})
|
|
|
|
cy.get(getTestSelector('confirm-swap-button')).click()
|
|
|
|
// The swap should now be able to proceed, as the permit2 signature will be accepted by the user.
|
|
const approvalTime = Date.now()
|
|
|
|
cy.contains('Confirm Swap').should('exist')
|
|
cy.contains('Swapped').should('exist')
|
|
|
|
expectTokenAllowanceForPermit2ToBeMax()
|
|
expectPermit2AllowanceForUniversalRouterToBeMax(approvalTime)
|
|
})
|
|
})
|
|
|
|
it('swaps with existing token approval and missing permit approval', () => {
|
|
cy.hardhat().then(({ approval, wallet, provider }) => {
|
|
approval.setTokenAllowanceForPermit2({ owner: wallet, token: INPUT_TOKEN })
|
|
cy.spy(provider, 'send').as('permitApprovalSpy')
|
|
})
|
|
cy.then(() => initiateSwap())
|
|
cy.then(() => {
|
|
const approvalTime = Date.now()
|
|
|
|
cy.contains('Confirm Swap').should('exist')
|
|
cy.contains('Swapped').should('exist')
|
|
|
|
expectPermit2AllowanceForUniversalRouterToBeMax(approvalTime)
|
|
cy.get('@permitApprovalSpy').should('have.been.calledWith', 'eth_signTypedData_v4')
|
|
})
|
|
})
|
|
|
|
it('swaps with existing permit approval and missing token approval', () => {
|
|
cy.hardhat().then(({ approval, wallet }) => approval.setPermit2Allowance({ owner: wallet, token: INPUT_TOKEN }))
|
|
cy.then(() => {
|
|
initiateSwap()
|
|
})
|
|
cy.then(() => {
|
|
const approvalTime = Date.now()
|
|
|
|
cy.contains('Confirm Swap').should('exist')
|
|
cy.contains('Swapped').should('exist')
|
|
|
|
expectPermit2AllowanceForUniversalRouterToBeMax(approvalTime)
|
|
})
|
|
})
|
|
|
|
it('prompts signature when existing permit approval is expired', () => {
|
|
const expiredAllowance = { expiration: Math.floor((Date.now() - 1) / 1000) }
|
|
|
|
cy.hardhat().then(({ approval, wallet, provider }) => {
|
|
approval.setTokenAllowanceForPermit2({ owner: wallet, token: INPUT_TOKEN })
|
|
approval.setPermit2Allowance({ owner: wallet, token: INPUT_TOKEN }, expiredAllowance)
|
|
cy.spy(provider, 'send').as('permitApprovalSpy')
|
|
})
|
|
cy.then(() => {
|
|
initiateSwap()
|
|
})
|
|
cy.then(() => {
|
|
const approvalTime = Date.now()
|
|
|
|
cy.contains('Confirm Swap').should('exist')
|
|
cy.contains('Swapped').should('exist')
|
|
|
|
expectPermit2AllowanceForUniversalRouterToBeMax(approvalTime)
|
|
cy.get('@permitApprovalSpy').should('have.been.calledWith', 'eth_signTypedData_v4')
|
|
})
|
|
})
|
|
|
|
it('prompts signature when existing permit approval amount is too low', () => {
|
|
const smallAllowance = { amount: 1 }
|
|
|
|
cy.hardhat().then(({ approval, wallet, provider }) => {
|
|
approval.setTokenAllowanceForPermit2({ owner: wallet, token: INPUT_TOKEN })
|
|
approval.setPermit2Allowance({ owner: wallet, token: INPUT_TOKEN }, smallAllowance)
|
|
cy.spy(provider, 'send').as('permitApprovalSpy')
|
|
initiateSwap()
|
|
const approvalTime = Date.now()
|
|
|
|
cy.contains('Confirm Swap').should('exist')
|
|
cy.contains('Swapped').should('exist')
|
|
|
|
expectPermit2AllowanceForUniversalRouterToBeMax(approvalTime)
|
|
cy.get('@permitApprovalSpy').should('have.been.calledWith', 'eth_signTypedData_v4')
|
|
})
|
|
})
|
|
|
|
it('prompts token approval when existing approval amount is too low', () => {
|
|
cy.hardhat()
|
|
.then(({ approval, wallet }) => {
|
|
approval.setPermit2Allowance({ owner: wallet, token: INPUT_TOKEN })
|
|
approval.setTokenAllowanceForPermit2({ owner: wallet, token: INPUT_TOKEN }, 1)
|
|
})
|
|
.then(() => {
|
|
initiateSwap()
|
|
const approvalTime = Date.now()
|
|
cy.contains('Allow trading DAI on Uniswap').should('exist')
|
|
|
|
cy.contains('Confirm Swap').should('exist')
|
|
cy.contains('Swapped').should('exist')
|
|
|
|
expectPermit2AllowanceForUniversalRouterToBeMax(approvalTime)
|
|
})
|
|
})
|
|
})
|