|
| 1 | +import { network, ethers, upgrades } from 'hardhat' |
| 2 | +import { Contract, Signer, Wallet, BigNumber } from 'ethers' |
| 3 | +import { expect } from 'chai' |
| 4 | + |
| 5 | +import { |
| 6 | + EIP712_DOMAIN_TYPEHASH, |
| 7 | + EIP2612_PERMIT_TYPEHASH, |
| 8 | + getDomainSeparator, |
| 9 | + signEIP712Permission, |
| 10 | +} from '../utils/signatures' |
| 11 | + |
| 12 | +let accounts: Signer[], |
| 13 | + deployer: Signer, |
| 14 | + deployerAddress: string, |
| 15 | + owner: Wallet, |
| 16 | + ownerAddress: string, |
| 17 | + spender: Wallet, |
| 18 | + spenderAddress: string, |
| 19 | + uFragments: Contract, |
| 20 | + initialSupply: BigNumber |
| 21 | + |
| 22 | +async function setupContracts() { |
| 23 | + // prepare signers |
| 24 | + accounts = await ethers.getSigners() |
| 25 | + deployer = accounts[0] |
| 26 | + deployerAddress = await deployer.getAddress() |
| 27 | + |
| 28 | + owner = Wallet.createRandom() |
| 29 | + ownerAddress = await owner.getAddress() |
| 30 | + |
| 31 | + spender = Wallet.createRandom() |
| 32 | + spenderAddress = await spender.getAddress() |
| 33 | + |
| 34 | + // deploy upgradable token |
| 35 | + const factory = await ethers.getContractFactory('UFragments') |
| 36 | + uFragments = await upgrades.deployProxy(factory, [deployerAddress], { |
| 37 | + initializer: 'initialize(address)', |
| 38 | + }) |
| 39 | + // fetch initial supply |
| 40 | + initialSupply = await uFragments.totalSupply() |
| 41 | +} |
| 42 | + |
| 43 | +// https://eips.ethereum.org/EIPS/eip-2612 |
| 44 | +// Test cases as in: |
| 45 | +// https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/test/drafts/ERC20Permit.test.js |
| 46 | +describe('UFragments:Initialization', () => { |
| 47 | + before('setup UFragments contract', setupContracts) |
| 48 | + |
| 49 | + it('should set the EIP2612 parameters', async function () { |
| 50 | + expect(await uFragments.EIP712_REVISION()).to.eq('1') |
| 51 | + expect(await uFragments.EIP712_DOMAIN()).to.eq(EIP712_DOMAIN_TYPEHASH) |
| 52 | + expect(await uFragments.PERMIT_TYPEHASH()).to.eq(EIP2612_PERMIT_TYPEHASH) |
| 53 | + // with hard-coded parameters |
| 54 | + expect(await uFragments.DOMAIN_SEPARATOR()).to.eq( |
| 55 | + getDomainSeparator( |
| 56 | + await uFragments.EIP712_REVISION(), |
| 57 | + await uFragments.name(), |
| 58 | + uFragments.address, |
| 59 | + network.config.chainId || 1, |
| 60 | + ), |
| 61 | + ) |
| 62 | + }) |
| 63 | + |
| 64 | + it('initial nonce is 0', async function () { |
| 65 | + expect(await uFragments.nonces(deployerAddress)).to.eq('0') |
| 66 | + expect(await uFragments.nonces(ownerAddress)).to.eq('0') |
| 67 | + expect(await uFragments.nonces(spenderAddress)).to.eq('0') |
| 68 | + }) |
| 69 | +}) |
| 70 | + |
| 71 | +// Using the cases specified by: |
| 72 | +// https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/test/drafts/ERC20Permit.test.js |
| 73 | +describe('UFragments:EIP-2612 Permit', () => { |
| 74 | + const MAX_DEADLINE = BigNumber.from(2).pow(256).sub(1) |
| 75 | + |
| 76 | + beforeEach('setup UFragments contract', setupContracts) |
| 77 | + |
| 78 | + describe('permit', function () { |
| 79 | + const signPermission = async ( |
| 80 | + signer: Wallet, |
| 81 | + owner: string, |
| 82 | + spender: string, |
| 83 | + value: number, |
| 84 | + nonce: number, |
| 85 | + deadline: BigNumber, |
| 86 | + ) => { |
| 87 | + return signEIP712Permission( |
| 88 | + await uFragments.EIP712_REVISION(), |
| 89 | + await uFragments.name(), |
| 90 | + uFragments.address, |
| 91 | + network.config.chainId || 1, |
| 92 | + signer, |
| 93 | + owner, |
| 94 | + spender, |
| 95 | + value, |
| 96 | + nonce, |
| 97 | + deadline, |
| 98 | + ) |
| 99 | + } |
| 100 | + |
| 101 | + it('accepts owner signature', async function () { |
| 102 | + const { v, r, s } = await signPermission( |
| 103 | + owner, |
| 104 | + ownerAddress, |
| 105 | + spenderAddress, |
| 106 | + 123, |
| 107 | + 0, |
| 108 | + MAX_DEADLINE, |
| 109 | + ) |
| 110 | + await expect( |
| 111 | + uFragments |
| 112 | + .connect(deployer) |
| 113 | + .permit(ownerAddress, spenderAddress, 123, MAX_DEADLINE, v, r, s), |
| 114 | + ) |
| 115 | + .to.emit(uFragments, 'Approval') |
| 116 | + .withArgs(ownerAddress, spenderAddress, '123') |
| 117 | + expect(await uFragments.nonces(ownerAddress)).to.eq('1') |
| 118 | + expect(await uFragments.allowance(ownerAddress, spenderAddress)).to.eq( |
| 119 | + '123', |
| 120 | + ) |
| 121 | + }) |
| 122 | + |
| 123 | + it('rejects reused signature', async function () { |
| 124 | + const { v, r, s } = await signPermission( |
| 125 | + owner, |
| 126 | + ownerAddress, |
| 127 | + spenderAddress, |
| 128 | + 123, |
| 129 | + 0, |
| 130 | + MAX_DEADLINE, |
| 131 | + ) |
| 132 | + await uFragments |
| 133 | + .connect(deployer) |
| 134 | + .permit(ownerAddress, spenderAddress, 123, MAX_DEADLINE, v, r, s) |
| 135 | + await expect( |
| 136 | + uFragments |
| 137 | + .connect(deployer) |
| 138 | + .permit(ownerAddress, spenderAddress, 123, MAX_DEADLINE, v, r, s), |
| 139 | + ).to.be.reverted |
| 140 | + }) |
| 141 | + |
| 142 | + it('rejects other signature', async function () { |
| 143 | + const { v, r, s } = await signPermission( |
| 144 | + spender, |
| 145 | + ownerAddress, |
| 146 | + spenderAddress, |
| 147 | + 123, |
| 148 | + 0, |
| 149 | + MAX_DEADLINE, |
| 150 | + ) |
| 151 | + await expect( |
| 152 | + uFragments |
| 153 | + .connect(deployer) |
| 154 | + .permit(ownerAddress, spenderAddress, 123, MAX_DEADLINE, v, r, s), |
| 155 | + ).to.be.reverted |
| 156 | + }) |
| 157 | + |
| 158 | + it('rejects expired permit', async function () { |
| 159 | + const currentTs = (await ethers.provider.getBlock('latest')).timestamp |
| 160 | + const olderTs = currentTs - 3600 * 24 * 7 |
| 161 | + const deadline = BigNumber.from(olderTs) |
| 162 | + const { v, r, s } = await signPermission( |
| 163 | + owner, |
| 164 | + ownerAddress, |
| 165 | + spenderAddress, |
| 166 | + 123, |
| 167 | + 0, |
| 168 | + deadline, |
| 169 | + ) |
| 170 | + await expect( |
| 171 | + uFragments |
| 172 | + .connect(deployer) |
| 173 | + .permit(ownerAddress, spenderAddress, 123, deadline, v, r, s), |
| 174 | + ).to.be.reverted |
| 175 | + }) |
| 176 | + }) |
| 177 | +}) |
0 commit comments