Commit 088f1d9a authored by Zach Pomerantz's avatar Zach Pomerantz Committed by GitHub

test(e2e): use aliases for permit2 test (#6656)

* test(e2e): disable video compression

* refactor: improve popupList impl/test

* test(e2e): log JSON-RPC calls

* fix: retry backoff logic

* test(e2e): wait for hardhat/popup assertions

* fix: remove transactions after expired

* test(e2e): re-enable other swap tests
Co-authored-by: default avatarJordan Frankfurt <jordanwfrankfurt@gmail.com>

* chore: rm console.log

* fix: expire txs after 6h

* refactor: dry trade info

* test(e2e): use default deadline

* test(e2e): use aliases for permit2 test

* test(e2e): automine/off in beforeEach

* test(e2e): rm intermediate screen with no pause

* fix merge

* fix only

---------
Co-authored-by: default avatarJordan Frankfurt <jordanwfrankfurt@gmail.com>
parent 89a7d98b
......@@ -5,212 +5,192 @@ import { getTestSelector } from '../utils'
/** Initiates a swap. */
function initiateSwap() {
// The swap button is re-rendered once enable, so we must wait until the original button is not disabled to re-select the appropriate button.
// The swap button is re-rendered once enabled, so we must wait until the original button is not disabled to re-select the appropriate button.
cy.get('#swap-button').should('not.be.disabled')
// Completes the swap.
cy.get('#swap-button').click()
cy.get(getTestSelector('confirm-swap-button')).click()
cy.contains('Confirm swap').click()
}
describe('Permit2', () => {
// The same tokens & swap-amount combination is used for all permit2 tests.
// The same tokens are 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())
cy.get('#swap-currency-input .token-amount-input').type('0.01')
})
/** Asserts permit2 has a max approval for spend of the input token on-chain. */
function expectTokenAllowanceForPermit2ToBeMax() {
// check token approval
return cy
.hardhat()
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()
function expectPermit2AllowanceForUniversalRouterToBeMax() {
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)
const THIRTY_DAYS_SECONDS = 2_592_000
const expected = Math.floor(Date.now() / 1000 + THIRTY_DAYS_SECONDS)
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')
})
describe('approval process (with intermediate screens)', () => {
// Turn off automine so that intermediate screens are available to assert on.
beforeEach(() => cy.hardhat({ automine: false }))
it('swaps after completing full permit2 approval process', () => {
cy.hardhat().then(({ provider }) => {
cy.spy(provider, 'send').as('permitApprovalSpy')
})
initiateSwap()
cy.contains('Enable spending DAI on Uniswap').should('exist')
cy.contains('Approved').should('exist')
cy.contains('Allow DAI to be used for swapping').should('exist')
cy.contains('Confirm Swap').should('exist')
// Verify token approval
cy.contains('Enable spending DAI on Uniswap')
cy.wait('@eth_sendRawTransaction')
cy.hardhat().then((hardhat) => hardhat.mine())
cy.get(getTestSelector('popups')).contains('Approved')
expectTokenAllowanceForPermit2ToBeMax()
cy.then(() => {
const approvalTime = Date.now()
// Verify permit2 approval
cy.contains('Allow DAI to be used for swapping')
cy.wait('@eth_signTypedData_v4')
cy.wait('@eth_sendRawTransaction')
cy.hardhat().then((hardhat) => hardhat.mine())
cy.contains('Success')
cy.get(getTestSelector('popups')).contains('Swapped')
expectPermit2AllowanceForUniversalRouterToBeMax()
})
cy.contains('Swapped').should('exist')
it('swaps with existing permit approval and missing token approval', () => {
cy.hardhat().then(async (hardhat) => {
await hardhat.approval.setPermit2Allowance({ owner: hardhat.wallet, token: INPUT_TOKEN })
await hardhat.mine()
})
initiateSwap()
// Verify token approval
cy.contains('Enable spending DAI on Uniswap')
cy.wait('@eth_sendRawTransaction')
cy.hardhat().then((hardhat) => hardhat.mine())
cy.get(getTestSelector('popups')).contains('Approved')
expectTokenAllowanceForPermit2ToBeMax()
expectPermit2AllowanceForUniversalRouterToBeMax(approvalTime)
cy.get('@permitApprovalSpy').should('have.been.calledWith', 'eth_signTypedData_v4')
// Verify transaction
cy.wait('@eth_sendRawTransaction')
cy.hardhat().then((hardhat) => hardhat.mine())
cy.contains('Success')
cy.get(getTestSelector('popups')).contains('Swapped')
})
})
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
it('swaps when user has already approved token and permit2', () => {
cy.hardhat().then(({ approval, wallet }) =>
Promise.all([
approval.setTokenAllowanceForPermit2({ owner: wallet, token: INPUT_TOKEN }),
approval.setPermit2Allowance({ owner: wallet, token: INPUT_TOKEN }),
])
)
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
// Verify transaction
cy.contains('Success')
cy.get(getTestSelector('popups')).contains('Swapped')
})
cy.get(getTestSelector('confirm-swap-button')).click()
cy.contains('Enable spending DAI on Uniswap').should('exist')
cy.contains('Approved').should('exist')
it('swaps after handling user rejection of both approval and signature', () => {
const USER_REJECTION = { code: 4001 }
cy.hardhat().then((hardhat) => {
// Reject token approval
const tokenApprovalStub = cy.stub(hardhat.wallet, 'sendTransaction').log(false)
tokenApprovalStub.rejects(USER_REJECTION) // rejects token approval
initiateSwap()
// permitApprovalStub should reject here, and the modal should revert to the review state.
// Verify token approval rejection
cy.wrap(tokenApprovalStub).should('be.calledOnce')
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()
// Allow token approval
cy.then(() => tokenApprovalStub.restore())
cy.contains('Confirm Swap').should('exist')
cy.contains('Swapped').should('exist')
// Reject permit2 approval
const permitApprovalStub = cy.stub(hardhat.provider, 'send').log(false)
permitApprovalStub.withArgs('eth_signTypedData_v4').rejects(USER_REJECTION) // rejects permit approval
permitApprovalStub.callThrough() // allows non-eth_signTypedData_v4 send calls to return non-stubbed values
cy.contains('Confirm swap').click()
// Verify token approval
cy.get(getTestSelector('popups')).contains('Approved')
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()
// Verify permit2 approval rejection
cy.wrap(permitApprovalStub).should('be.calledWith', 'eth_signTypedData_v4')
cy.contains('Review swap')
cy.contains('Confirm Swap').should('exist')
cy.contains('Swapped').should('exist')
// Allow permit2 approval
cy.then(() => permitApprovalStub.restore())
cy.contains('Confirm swap').click()
expectPermit2AllowanceForUniversalRouterToBeMax(approvalTime)
cy.get('@permitApprovalSpy').should('have.been.calledWith', 'eth_signTypedData_v4')
// Verify permit2 approval
cy.contains('Success')
cy.get(getTestSelector('popups')).contains('Swapped')
expectPermit2AllowanceForUniversalRouterToBeMax()
})
})
it('swaps with existing permit approval and missing token approval', () => {
cy.hardhat().then(({ approval, wallet }) => approval.setPermit2Allowance({ owner: wallet, token: INPUT_TOKEN }))
cy.then(() => {
it('prompts token approval when existing approval amount is too low', () => {
cy.hardhat().then(({ approval, wallet }) =>
Promise.all([
approval.setPermit2Allowance({ owner: wallet, token: INPUT_TOKEN }),
approval.setTokenAllowanceForPermit2({ owner: wallet, token: INPUT_TOKEN }, 1),
])
)
initiateSwap()
})
cy.then(() => {
const approvalTime = Date.now()
cy.contains('Confirm Swap').should('exist')
cy.contains('Swapped').should('exist')
expectPermit2AllowanceForUniversalRouterToBeMax(approvalTime)
})
// Verify token approval
cy.get(getTestSelector('popups')).contains('Approved')
expectPermit2AllowanceForUniversalRouterToBeMax()
})
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(() => {
cy.hardhat().then(({ approval, wallet }) =>
Promise.all([
approval.setTokenAllowanceForPermit2({ owner: wallet, token: INPUT_TOKEN }),
approval.setPermit2Allowance({ owner: wallet, token: INPUT_TOKEN }, expiredAllowance),
])
)
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')
})
// Verify permit2 approval
cy.wait('@eth_signTypedData_v4')
cy.contains('Success')
cy.get(getTestSelector('popups')).contains('Swapped')
expectPermit2AllowanceForUniversalRouterToBeMax()
})
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')
cy.hardhat().then(({ approval, wallet }) =>
Promise.all([
approval.setTokenAllowanceForPermit2({ owner: wallet, token: INPUT_TOKEN }),
approval.setPermit2Allowance({ owner: wallet, token: INPUT_TOKEN }, smallAllowance),
])
)
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('Enable spending DAI on Uniswap').should('exist')
cy.contains('Confirm Swap').should('exist')
cy.contains('Swapped').should('exist')
expectPermit2AllowanceForUniversalRouterToBeMax(approvalTime)
})
// Verify permit2 approval
cy.wait('@eth_signTypedData_v4')
cy.contains('Success')
cy.get(getTestSelector('popups')).contains('Swapped')
expectPermit2AllowanceForUniversalRouterToBeMax()
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment