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