forked from William21332/v2-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUniswapV2Factory.spec.ts
86 lines (71 loc) · 3.27 KB
/
UniswapV2Factory.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import chai, { expect } from 'chai'
import { Contract } from 'ethers'
import { AddressZero } from 'ethers/constants'
import { bigNumberify } from 'ethers/utils'
import { solidity, MockProvider, createFixtureLoader } from 'ethereum-waffle'
import { getCreate2Address } from './shared/utilities'
import { factoryFixture } from './shared/fixtures'
import UniswapV2Pair from '../build/UniswapV2Pair.json'
chai.use(solidity)
const TEST_ADDRESSES: [string, string] = [
'0x1000000000000000000000000000000000000000',
'0x2000000000000000000000000000000000000000'
]
describe('UniswapV2Factory', () => {
const provider = new MockProvider({
hardfork: 'istanbul',
mnemonic: 'horn horn horn horn horn horn horn horn horn horn horn horn',
gasLimit: 9999999
})
const [wallet, other] = provider.getWallets()
const loadFixture = createFixtureLoader(provider, [wallet, other])
let factory: Contract
beforeEach(async () => {
const fixture = await loadFixture(factoryFixture)
factory = fixture.factory
})
it('feeTo, feeToSetter, allPairsLength', async () => {
expect(await factory.feeTo()).to.eq(AddressZero)
expect(await factory.feeToSetter()).to.eq(wallet.address)
expect(await factory.allPairsLength()).to.eq(0)
})
async function createPair(tokens: [string, string]) {
const bytecode = `0x${UniswapV2Pair.evm.bytecode.object}`
const create2Address = getCreate2Address(factory.address, tokens, bytecode)
await expect(factory.createPair(...tokens))
.to.emit(factory, 'PairCreated')
.withArgs(TEST_ADDRESSES[0], TEST_ADDRESSES[1], create2Address, bigNumberify(1))
await expect(factory.createPair(...tokens)).to.be.reverted // UniswapV2: PAIR_EXISTS
await expect(factory.createPair(...tokens.slice().reverse())).to.be.reverted // UniswapV2: PAIR_EXISTS
expect(await factory.getPair(...tokens)).to.eq(create2Address)
expect(await factory.getPair(...tokens.slice().reverse())).to.eq(create2Address)
expect(await factory.allPairs(0)).to.eq(create2Address)
expect(await factory.allPairsLength()).to.eq(1)
const pair = new Contract(create2Address, JSON.stringify(UniswapV2Pair.abi), provider)
expect(await pair.factory()).to.eq(factory.address)
expect(await pair.token0()).to.eq(TEST_ADDRESSES[0])
expect(await pair.token1()).to.eq(TEST_ADDRESSES[1])
}
it('createPair', async () => {
await createPair(TEST_ADDRESSES)
})
it('createPair:reverse', async () => {
await createPair(TEST_ADDRESSES.slice().reverse() as [string, string])
})
it('createPair:gas', async () => {
const tx = await factory.createPair(...TEST_ADDRESSES)
const receipt = await tx.wait()
expect(receipt.gasUsed).to.eq(2512920)
})
it('setFeeTo', async () => {
await expect(factory.connect(other).setFeeTo(other.address)).to.be.revertedWith('UniswapV2: FORBIDDEN')
await factory.setFeeTo(wallet.address)
expect(await factory.feeTo()).to.eq(wallet.address)
})
it('setFeeToSetter', async () => {
await expect(factory.connect(other).setFeeToSetter(other.address)).to.be.revertedWith('UniswapV2: FORBIDDEN')
await factory.setFeeToSetter(other.address)
expect(await factory.feeToSetter()).to.eq(other.address)
await expect(factory.setFeeToSetter(wallet.address)).to.be.revertedWith('UniswapV2: FORBIDDEN')
})
})