2023-06-22 00:02:14 +03:00
|
|
|
import { BigNumber } from '@ethersproject/bignumber'
|
2023-05-06 00:52:22 +03:00
|
|
|
import { MaxUint160, MaxUint256 } from '@uniswap/permit2-sdk'
|
2023-06-22 00:02:14 +03:00
|
|
|
import { CurrencyAmount, Token } from '@uniswap/sdk-core'
|
2023-05-06 00:52:22 +03:00
|
|
|
|
2023-06-22 00:02:14 +03:00
|
|
|
import { DAI, USDC_MAINNET, USDT } from '../../src/constants/tokens'
|
2023-05-06 00:52:22 +03:00
|
|
|
import { getTestSelector } from '../utils'
|
|
|
|
|
2023-05-25 23:49:27 +03:00
|
|
|
/** Initiates a swap. */
|
|
|
|
function initiateSwap() {
|
2023-06-08 03:55:27 +03:00
|
|
|
// 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.
|
2023-05-26 00:56:37 +03:00
|
|
|
cy.get('#swap-button').should('not.be.disabled')
|
2023-05-06 00:52:22 +03:00
|
|
|
// Completes the swap.
|
|
|
|
cy.get('#swap-button').click()
|
2023-06-08 03:55:27 +03:00
|
|
|
cy.contains('Confirm swap').click()
|
2023-05-06 00:52:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('Permit2', () => {
|
2023-06-22 00:02:14 +03:00
|
|
|
function setupInputs(inputToken: Token, outputToken: Token) {
|
|
|
|
// Sets up a swap between inputToken and outputToken.
|
|
|
|
cy.visit(`/swap/?inputCurrency=${inputToken.address}&outputCurrency=${outputToken.address}`, {
|
2023-05-06 00:52:22 +03:00
|
|
|
ethereum: 'hardhat',
|
|
|
|
})
|
2023-06-08 03:55:27 +03:00
|
|
|
cy.get('#swap-currency-input .token-amount-input').type('0.01')
|
2023-06-22 00:02:14 +03:00
|
|
|
}
|
2023-05-06 00:52:22 +03:00
|
|
|
|
|
|
|
/** Asserts permit2 has a max approval for spend of the input token on-chain. */
|
2023-06-22 00:02:14 +03:00
|
|
|
function expectTokenAllowanceForPermit2ToBeMax(inputToken: Token) {
|
2023-05-06 00:52:22 +03:00
|
|
|
// check token approval
|
2023-06-08 03:55:27 +03:00
|
|
|
cy.hardhat()
|
2023-06-22 00:02:14 +03:00
|
|
|
.then(({ approval, wallet }) => approval.getTokenAllowanceForPermit2({ owner: wallet, token: inputToken }))
|
2023-05-06 00:52:22 +03:00
|
|
|
.should('deep.equal', MaxUint256)
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Asserts the universal router has a max permit2 approval for spend of the input token on-chain. */
|
2023-06-22 00:02:14 +03:00
|
|
|
function expectPermit2AllowanceForUniversalRouterToBeMax(inputToken: Token) {
|
2023-06-08 03:55:27 +03:00
|
|
|
cy.hardhat()
|
2023-06-22 00:02:14 +03:00
|
|
|
.then((hardhat) => hardhat.approval.getPermit2Allowance({ owner: hardhat.wallet, token: inputToken }))
|
2023-05-06 00:52:22 +03:00
|
|
|
.then((allowance) => {
|
|
|
|
cy.wrap(MaxUint160.eq(allowance.amount)).should('eq', true)
|
2023-05-15 20:58:05 +03:00
|
|
|
// Asserts that the on-chain expiration is in 30 days, within a tolerance of 40 seconds.
|
2023-06-08 03:55:27 +03:00
|
|
|
const THIRTY_DAYS_SECONDS = 2_592_000
|
|
|
|
const expected = Math.floor(Date.now() / 1000 + THIRTY_DAYS_SECONDS)
|
2023-05-15 20:58:05 +03:00
|
|
|
cy.wrap(allowance.expiration).should('be.closeTo', expected, 40)
|
2023-05-06 00:52:22 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-08 03:55:27 +03:00
|
|
|
describe('approval process (with intermediate screens)', () => {
|
|
|
|
// Turn off automine so that intermediate screens are available to assert on.
|
|
|
|
beforeEach(() => cy.hardhat({ automine: false }))
|
2023-05-06 00:52:22 +03:00
|
|
|
|
2023-06-08 03:55:27 +03:00
|
|
|
it('swaps after completing full permit2 approval process', () => {
|
2023-06-22 00:02:14 +03:00
|
|
|
setupInputs(DAI, USDC_MAINNET)
|
2023-06-08 03:55:27 +03:00
|
|
|
initiateSwap()
|
2023-05-06 00:52:22 +03:00
|
|
|
|
2023-06-08 23:44:47 +03:00
|
|
|
// verify that the modal retains its state when the window loses focus
|
|
|
|
cy.window().trigger('blur')
|
|
|
|
|
2023-06-08 03:55:27 +03:00
|
|
|
// 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')
|
2023-06-22 00:02:14 +03:00
|
|
|
expectTokenAllowanceForPermit2ToBeMax(DAI)
|
2023-05-06 00:52:22 +03:00
|
|
|
|
2023-06-08 03:55:27 +03:00
|
|
|
// 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')
|
2023-06-22 00:02:14 +03:00
|
|
|
expectPermit2AllowanceForUniversalRouterToBeMax(DAI)
|
2023-06-08 03:55:27 +03:00
|
|
|
})
|
2023-05-06 00:52:22 +03:00
|
|
|
|
2023-06-08 03:55:27 +03:00
|
|
|
it('swaps with existing permit approval and missing token approval', () => {
|
2023-06-22 00:02:14 +03:00
|
|
|
setupInputs(DAI, USDC_MAINNET)
|
2023-06-08 03:55:27 +03:00
|
|
|
cy.hardhat().then(async (hardhat) => {
|
2023-06-22 00:02:14 +03:00
|
|
|
await hardhat.approval.setPermit2Allowance({ owner: hardhat.wallet, token: DAI })
|
2023-06-08 03:55:27 +03:00
|
|
|
await hardhat.mine()
|
|
|
|
})
|
|
|
|
initiateSwap()
|
2023-05-25 23:49:27 +03:00
|
|
|
|
2023-06-08 03:55:27 +03:00
|
|
|
// 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')
|
2023-06-22 00:02:14 +03:00
|
|
|
expectTokenAllowanceForPermit2ToBeMax(DAI)
|
|
|
|
|
|
|
|
// Verify transaction
|
|
|
|
cy.wait('@eth_sendRawTransaction')
|
|
|
|
cy.hardhat().then((hardhat) => hardhat.mine())
|
|
|
|
cy.contains('Success')
|
|
|
|
cy.get(getTestSelector('popups')).contains('Swapped')
|
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On mainnet, you have to revoke USDT approval before increasing it.
|
|
|
|
* From the token contract:
|
|
|
|
* To change the approve amount you first have to reduce the addresses`
|
|
|
|
* allowance to zero by calling `approve(_spender, 0)` if it is not
|
|
|
|
* already 0 to mitigate the race condition described here:
|
|
|
|
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
|
|
|
|
*/
|
|
|
|
it('swaps USDT with existing permit, and existing but insufficient token approval', () => {
|
|
|
|
cy.hardhat().then(async (hardhat) => {
|
|
|
|
await hardhat.fund(hardhat.wallet, CurrencyAmount.fromRawAmount(USDT, 2e6))
|
|
|
|
await hardhat.mine()
|
|
|
|
await hardhat.approval.setTokenAllowanceForPermit2({ owner: hardhat.wallet, token: USDT }, 1e6)
|
|
|
|
await hardhat.mine()
|
|
|
|
await hardhat.approval.setPermit2Allowance({ owner: hardhat.wallet, token: USDT })
|
|
|
|
await hardhat.mine()
|
|
|
|
})
|
|
|
|
setupInputs(USDT, USDC_MAINNET)
|
|
|
|
cy.get('#swap-currency-input .token-amount-input').clear().type('2')
|
|
|
|
initiateSwap()
|
|
|
|
|
|
|
|
// Verify allowance revocation
|
|
|
|
cy.contains('Reset USDT')
|
|
|
|
cy.wait('@eth_sendRawTransaction')
|
|
|
|
cy.hardhat().then((hardhat) => hardhat.mine())
|
|
|
|
cy.hardhat()
|
|
|
|
.then(({ approval, wallet }) => approval.getTokenAllowanceForPermit2({ owner: wallet, token: USDT }))
|
|
|
|
.should('deep.equal', BigNumber.from(0))
|
|
|
|
|
|
|
|
// Verify token approval
|
|
|
|
cy.contains('Enable spending USDT on Uniswap')
|
|
|
|
cy.wait('@eth_sendRawTransaction')
|
|
|
|
cy.hardhat().then((hardhat) => hardhat.mine())
|
|
|
|
cy.get(getTestSelector('popups')).contains('Approved')
|
|
|
|
expectTokenAllowanceForPermit2ToBeMax(USDT)
|
2023-06-08 03:55:27 +03:00
|
|
|
|
|
|
|
// Verify transaction
|
|
|
|
cy.wait('@eth_sendRawTransaction')
|
|
|
|
cy.hardhat().then((hardhat) => hardhat.mine())
|
|
|
|
cy.contains('Success')
|
|
|
|
cy.get(getTestSelector('popups')).contains('Swapped')
|
2023-05-25 23:49:27 +03:00
|
|
|
})
|
2023-05-06 00:52:22 +03:00
|
|
|
})
|
|
|
|
|
2023-06-08 03:55:27 +03:00
|
|
|
it('swaps when user has already approved token and permit2', () => {
|
|
|
|
cy.hardhat().then(({ approval, wallet }) =>
|
|
|
|
Promise.all([
|
2023-06-22 00:02:14 +03:00
|
|
|
approval.setTokenAllowanceForPermit2({ owner: wallet, token: DAI }),
|
|
|
|
approval.setPermit2Allowance({ owner: wallet, token: DAI }),
|
2023-06-08 03:55:27 +03:00
|
|
|
])
|
|
|
|
)
|
2023-06-22 00:02:14 +03:00
|
|
|
setupInputs(DAI, USDC_MAINNET)
|
2023-06-08 03:55:27 +03:00
|
|
|
initiateSwap()
|
|
|
|
|
|
|
|
// Verify transaction
|
|
|
|
cy.contains('Success')
|
|
|
|
cy.get(getTestSelector('popups')).contains('Swapped')
|
|
|
|
})
|
|
|
|
|
2023-05-25 23:49:27 +03:00
|
|
|
it('swaps after handling user rejection of both approval and signature', () => {
|
2023-06-22 00:02:14 +03:00
|
|
|
setupInputs(DAI, USDC_MAINNET)
|
2023-05-06 00:52:22 +03:00
|
|
|
const USER_REJECTION = { code: 4001 }
|
|
|
|
cy.hardhat().then((hardhat) => {
|
2023-06-08 03:55:27 +03:00
|
|
|
// Reject token approval
|
|
|
|
const tokenApprovalStub = cy.stub(hardhat.wallet, 'sendTransaction').log(false)
|
|
|
|
tokenApprovalStub.rejects(USER_REJECTION) // rejects token approval
|
2023-05-25 23:49:27 +03:00
|
|
|
initiateSwap()
|
2023-05-06 00:52:22 +03:00
|
|
|
|
2023-06-08 03:55:27 +03:00
|
|
|
// Verify token approval rejection
|
|
|
|
cy.wrap(tokenApprovalStub).should('be.calledOnce')
|
2023-05-26 01:57:13 +03:00
|
|
|
cy.contains('Review swap')
|
2023-05-25 23:49:27 +03:00
|
|
|
|
2023-06-08 03:55:27 +03:00
|
|
|
// Allow token approval
|
|
|
|
cy.then(() => tokenApprovalStub.restore())
|
2023-05-25 23:49:27 +03:00
|
|
|
|
2023-06-08 03:55:27 +03:00
|
|
|
// 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()
|
2023-05-25 23:49:27 +03:00
|
|
|
|
2023-06-08 03:55:27 +03:00
|
|
|
// Verify token approval
|
|
|
|
cy.get(getTestSelector('popups')).contains('Approved')
|
2023-06-22 00:02:14 +03:00
|
|
|
expectTokenAllowanceForPermit2ToBeMax(DAI)
|
2023-05-06 00:52:22 +03:00
|
|
|
|
2023-06-08 03:55:27 +03:00
|
|
|
// Verify permit2 approval rejection
|
|
|
|
cy.wrap(permitApprovalStub).should('be.calledWith', 'eth_signTypedData_v4')
|
|
|
|
cy.contains('Review swap')
|
2023-05-06 00:52:22 +03:00
|
|
|
|
2023-06-08 03:55:27 +03:00
|
|
|
// Allow permit2 approval
|
|
|
|
cy.then(() => permitApprovalStub.restore())
|
|
|
|
cy.contains('Confirm swap').click()
|
2023-05-06 00:52:22 +03:00
|
|
|
|
2023-06-08 03:55:27 +03:00
|
|
|
// Verify permit2 approval
|
|
|
|
cy.contains('Success')
|
|
|
|
cy.get(getTestSelector('popups')).contains('Swapped')
|
2023-06-22 00:02:14 +03:00
|
|
|
expectPermit2AllowanceForUniversalRouterToBeMax(DAI)
|
2023-05-25 23:49:27 +03:00
|
|
|
})
|
2023-05-06 00:52:22 +03:00
|
|
|
})
|
|
|
|
|
2023-06-08 03:55:27 +03:00
|
|
|
it('prompts token approval when existing approval amount is too low', () => {
|
2023-06-22 00:02:14 +03:00
|
|
|
setupInputs(DAI, USDC_MAINNET)
|
2023-06-08 03:55:27 +03:00
|
|
|
cy.hardhat().then(({ approval, wallet }) =>
|
|
|
|
Promise.all([
|
2023-06-22 00:02:14 +03:00
|
|
|
approval.setPermit2Allowance({ owner: wallet, token: DAI }),
|
|
|
|
approval.setTokenAllowanceForPermit2({ owner: wallet, token: DAI }, 1),
|
2023-06-08 03:55:27 +03:00
|
|
|
])
|
|
|
|
)
|
|
|
|
initiateSwap()
|
2023-05-06 00:52:22 +03:00
|
|
|
|
2023-06-08 03:55:27 +03:00
|
|
|
// Verify token approval
|
|
|
|
cy.get(getTestSelector('popups')).contains('Approved')
|
2023-06-22 00:02:14 +03:00
|
|
|
expectPermit2AllowanceForUniversalRouterToBeMax(DAI)
|
2023-05-06 00:52:22 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('prompts signature when existing permit approval is expired', () => {
|
2023-06-22 00:02:14 +03:00
|
|
|
setupInputs(DAI, USDC_MAINNET)
|
2023-05-06 00:52:22 +03:00
|
|
|
const expiredAllowance = { expiration: Math.floor((Date.now() - 1) / 1000) }
|
2023-06-08 03:55:27 +03:00
|
|
|
cy.hardhat().then(({ approval, wallet }) =>
|
|
|
|
Promise.all([
|
2023-06-22 00:02:14 +03:00
|
|
|
approval.setTokenAllowanceForPermit2({ owner: wallet, token: DAI }),
|
|
|
|
approval.setPermit2Allowance({ owner: wallet, token: DAI }, expiredAllowance),
|
2023-06-08 03:55:27 +03:00
|
|
|
])
|
|
|
|
)
|
|
|
|
initiateSwap()
|
2023-05-06 00:52:22 +03:00
|
|
|
|
2023-06-08 03:55:27 +03:00
|
|
|
// Verify permit2 approval
|
|
|
|
cy.wait('@eth_signTypedData_v4')
|
|
|
|
cy.contains('Success')
|
|
|
|
cy.get(getTestSelector('popups')).contains('Swapped')
|
2023-06-22 00:02:14 +03:00
|
|
|
expectPermit2AllowanceForUniversalRouterToBeMax(DAI)
|
2023-05-06 00:52:22 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('prompts signature when existing permit approval amount is too low', () => {
|
2023-06-22 00:02:14 +03:00
|
|
|
setupInputs(DAI, USDC_MAINNET)
|
2023-05-06 00:52:22 +03:00
|
|
|
const smallAllowance = { amount: 1 }
|
2023-06-08 03:55:27 +03:00
|
|
|
cy.hardhat().then(({ approval, wallet }) =>
|
|
|
|
Promise.all([
|
2023-06-22 00:02:14 +03:00
|
|
|
approval.setTokenAllowanceForPermit2({ owner: wallet, token: DAI }),
|
|
|
|
approval.setPermit2Allowance({ owner: wallet, token: DAI }, smallAllowance),
|
2023-06-08 03:55:27 +03:00
|
|
|
])
|
|
|
|
)
|
|
|
|
initiateSwap()
|
2023-05-06 00:52:22 +03:00
|
|
|
|
2023-06-08 03:55:27 +03:00
|
|
|
// Verify permit2 approval
|
|
|
|
cy.wait('@eth_signTypedData_v4')
|
|
|
|
cy.contains('Success')
|
|
|
|
cy.get(getTestSelector('popups')).contains('Swapped')
|
2023-06-22 00:02:14 +03:00
|
|
|
expectPermit2AllowanceForUniversalRouterToBeMax(DAI)
|
2023-05-06 00:52:22 +03:00
|
|
|
})
|
|
|
|
})
|