|
1 | 1 | import { expect } from 'chai'
|
2 | 2 | import { AddressZero } from 'ethers/constants'
|
| 3 | +import { BigNumber, Arrayish, HDNode, SigningKey, keccak256, Signature } from 'ethers/utils' |
| 4 | +import { eip712 } from '@graphprotocol/common-ts/dist/attestations' |
3 | 5 |
|
4 | 6 | import { GraphToken } from '../build/typechain/contracts/GraphToken'
|
5 | 7 |
|
6 | 8 | import * as deployment from './lib/deployment'
|
7 |
| -import { provider, toGRT } from './lib/testHelpers' |
| 9 | +import { getChainID, provider, toBN, toGRT } from './lib/testHelpers' |
| 10 | + |
| 11 | +const MAX_UINT256 = toBN('2') |
| 12 | + .pow('256') |
| 13 | + .sub(1) |
| 14 | + |
| 15 | +const PERMIT_TYPE_HASH = eip712.typeHash( |
| 16 | + 'Permit(address owner,address spender,uint256 nonce,uint256 expiry,bool allowed)', |
| 17 | +) |
| 18 | +const SALT = '0x51f3d585afe6dfeb2af01bba0889a36c1db03beec88c6a4d0c53817069026afa' |
| 19 | + |
| 20 | +interface Permit { |
| 21 | + owner: string |
| 22 | + spender: string |
| 23 | + nonce: BigNumber |
| 24 | + expiry: BigNumber |
| 25 | + allowed: boolean |
| 26 | +} |
| 27 | + |
| 28 | +function hashEncodePermit(permit: Permit) { |
| 29 | + return eip712.hashStruct( |
| 30 | + PERMIT_TYPE_HASH, |
| 31 | + ['address', 'address', 'uint256', 'uint256', 'bool'], |
| 32 | + [permit.owner, permit.spender, permit.nonce, permit.expiry, permit.allowed], |
| 33 | + ) |
| 34 | +} |
| 35 | + |
| 36 | +function signPermit( |
| 37 | + signer: Arrayish | HDNode.HDNode, |
| 38 | + chainId: number, |
| 39 | + contractAddress: string, |
| 40 | + permit: Permit, |
| 41 | +): Signature { |
| 42 | + const domainSeparator = eip712.domainSeparator({ |
| 43 | + name: 'Graph Token', |
| 44 | + version: '0', |
| 45 | + chainId, |
| 46 | + verifyingContract: contractAddress, |
| 47 | + salt: SALT, |
| 48 | + }) |
| 49 | + const hashEncodedPermit = hashEncodePermit(permit) |
| 50 | + const message = eip712.encode(domainSeparator, hashEncodedPermit) |
| 51 | + const messageHash = keccak256(message) |
| 52 | + const signingKey = new SigningKey(signer) |
| 53 | + return signingKey.signDigest(messageHash) |
| 54 | +} |
8 | 55 |
|
9 | 56 | describe('GraphToken', () => {
|
10 |
| - const [me, governor] = provider().getWallets() |
| 57 | + const [me, other, governor] = provider().getWallets() |
11 | 58 |
|
12 | 59 | let grt: GraphToken
|
13 | 60 |
|
| 61 | + async function permitOK(): Promise<Permit> { |
| 62 | + const nonce = await grt.nonces(me.address) |
| 63 | + return { |
| 64 | + owner: me.address, |
| 65 | + spender: other.address, |
| 66 | + nonce: nonce, |
| 67 | + expiry: toBN('0'), |
| 68 | + allowed: true, |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + async function permitExpired(): Promise<Permit> { |
| 73 | + const permit = await permitOK() |
| 74 | + permit.expiry = toBN('1') |
| 75 | + return permit |
| 76 | + } |
| 77 | + |
| 78 | + async function permitDeny(): Promise<Permit> { |
| 79 | + const permit = await permitOK() |
| 80 | + permit.allowed = false |
| 81 | + return permit |
| 82 | + } |
| 83 | + |
| 84 | + async function createPermitTransaction(permit: Permit, signer: string) { |
| 85 | + const chainID = (await getChainID()) as number |
| 86 | + const signature: Signature = signPermit(signer, chainID, grt.address, permit) |
| 87 | + |
| 88 | + return grt.permit( |
| 89 | + permit.owner, |
| 90 | + permit.spender, |
| 91 | + permit.nonce, |
| 92 | + permit.expiry, |
| 93 | + permit.allowed, |
| 94 | + signature.v, |
| 95 | + signature.r, |
| 96 | + signature.s, |
| 97 | + ) |
| 98 | + } |
| 99 | + |
14 | 100 | beforeEach(async function() {
|
15 | 101 | // Deploy graph token
|
16 | 102 | grt = await deployment.deployGRT(governor.address, me)
|
| 103 | + |
| 104 | + // Mint some tokens |
| 105 | + const tokens = toGRT('100') |
| 106 | + await grt.connect(governor).mint(me.address, tokens) |
| 107 | + }) |
| 108 | + |
| 109 | + describe('permit', function() { |
| 110 | + it('should permit max token allowance', async function() { |
| 111 | + // Allow to transfer tokens |
| 112 | + const permit = await permitOK() |
| 113 | + const tx = createPermitTransaction(permit, me.privateKey) |
| 114 | + await expect(tx) |
| 115 | + .to.emit(grt, 'Approval') |
| 116 | + .withArgs(permit.owner, permit.spender, MAX_UINT256) |
| 117 | + |
| 118 | + // Allowance updated |
| 119 | + const allowance = await grt.allowance(me.address, other.address) |
| 120 | + expect(allowance).to.be.eq(MAX_UINT256) |
| 121 | + |
| 122 | + // Transfer tokens should work |
| 123 | + const tokens = toGRT('100') |
| 124 | + await grt.connect(other).transferFrom(me.address, other.address, tokens) |
| 125 | + }) |
| 126 | + |
| 127 | + it('reject use two permits with same nonce', async function() { |
| 128 | + // Allow to transfer tokens |
| 129 | + const permit = await permitOK() |
| 130 | + await createPermitTransaction(permit, me.privateKey) |
| 131 | + |
| 132 | + // Try to re-use the permit |
| 133 | + const tx = createPermitTransaction(permit, me.privateKey) |
| 134 | + await expect(tx).to.revertedWith('GRT: invalid nonce') |
| 135 | + }) |
| 136 | + |
| 137 | + it('reject use expired permit', async function() { |
| 138 | + const permit = await permitExpired() |
| 139 | + const tx = createPermitTransaction(permit, me.privateKey) |
| 140 | + await expect(tx).to.revertedWith('GRT: permit expired') |
| 141 | + }) |
| 142 | + |
| 143 | + it('reject permit if holder address does not match', async function() { |
| 144 | + const permit = await permitOK() |
| 145 | + const tx = createPermitTransaction(permit, other.privateKey) |
| 146 | + await expect(tx).to.revertedWith('GRT: invalid permit') |
| 147 | + }) |
| 148 | + |
| 149 | + it('should deny transfer from if permit was denied', async function() { |
| 150 | + // Allow to transfer tokens |
| 151 | + const permit1 = await permitOK() |
| 152 | + await createPermitTransaction(permit1, me.privateKey) |
| 153 | + |
| 154 | + // Deny transfer tokens |
| 155 | + const permit2 = await permitDeny() |
| 156 | + await createPermitTransaction(permit2, me.privateKey) |
| 157 | + |
| 158 | + // Allowance updated |
| 159 | + const allowance = await grt.allowance(me.address, other.address) |
| 160 | + expect(allowance).to.be.eq(toBN('0')) |
| 161 | + |
| 162 | + // Try to transfer without permit should fail |
| 163 | + const tokens = toGRT('100') |
| 164 | + const tx = grt.connect(other).transferFrom(me.address, other.address, tokens) |
| 165 | + await expect(tx).to.revertedWith('ERC20: transfer amount exceeds allowance') |
| 166 | + }) |
17 | 167 | })
|
18 | 168 |
|
19 | 169 | describe('mint', function() {
|
20 |
| - context('> when is not minter', function() { |
21 |
| - describe('addMinter()', function() { |
22 |
| - it('reject add a new minter if not allowed', async function() { |
23 |
| - const tx = grt.connect(me).addMinter(me.address) |
24 |
| - await expect(tx).to.be.revertedWith('Only Governor can call') |
25 |
| - }) |
| 170 | + describe('addMinter()', function() { |
| 171 | + it('reject add a new minter if not allowed', async function() { |
| 172 | + const tx = grt.connect(me).addMinter(me.address) |
| 173 | + await expect(tx).to.be.revertedWith('Only Governor can call') |
| 174 | + }) |
26 | 175 |
|
27 |
| - it('should add a new minter', async function() { |
28 |
| - expect(await grt.isMinter(me.address)).to.be.eq(false) |
29 |
| - const tx = grt.connect(governor).addMinter(me.address) |
30 |
| - await expect(tx) |
31 |
| - .to.emit(grt, 'MinterAdded') |
32 |
| - .withArgs(me.address) |
33 |
| - expect(await grt.isMinter(me.address)).to.be.eq(true) |
34 |
| - }) |
| 176 | + it('should add a new minter', async function() { |
| 177 | + expect(await grt.isMinter(me.address)).to.be.eq(false) |
| 178 | + const tx = grt.connect(governor).addMinter(me.address) |
| 179 | + await expect(tx) |
| 180 | + .to.emit(grt, 'MinterAdded') |
| 181 | + .withArgs(me.address) |
| 182 | + expect(await grt.isMinter(me.address)).to.be.eq(true) |
35 | 183 | })
|
| 184 | + }) |
36 | 185 |
|
37 |
| - describe('mint()', async function() { |
38 |
| - it('reject mint if not minter', async function() { |
39 |
| - const tx = grt.connect(me).mint(me.address, toGRT('100')) |
40 |
| - await expect(tx).to.be.revertedWith('Only minter can call') |
41 |
| - }) |
| 186 | + describe('mint()', async function() { |
| 187 | + it('reject mint if not minter', async function() { |
| 188 | + const tx = grt.connect(me).mint(me.address, toGRT('100')) |
| 189 | + await expect(tx).to.be.revertedWith('Only minter can call') |
42 | 190 | })
|
43 | 191 | })
|
44 | 192 |
|
|
0 commit comments