clean up tests

This commit is contained in:
Noah Zinsmeister
2020-01-24 13:56:46 -05:00
parent d4661b1ae7
commit 3e228282d3
4 changed files with 52 additions and 64 deletions

View File

@ -14,7 +14,7 @@ chai.use(solidity)
const TOTAL_SUPPLY = expandTo18Decimals(10000)
const TEST_AMOUNT = expandTo18Decimals(10)
describe('UniswapV2ERC20 via GenericERC20', () => {
describe('UniswapV2ERC20', () => {
const provider = new MockProvider({
hardfork: 'istanbul',
mnemonic: 'horn horn horn horn horn horn horn horn horn horn horn horn',
@ -55,6 +55,13 @@ describe('UniswapV2ERC20 via GenericERC20', () => {
)
})
it('approve', async () => {
await expect(token.approve(other.address, TEST_AMOUNT))
.to.emit(token, 'Approval')
.withArgs(wallet.address, other.address, TEST_AMOUNT)
expect(await token.allowance(wallet.address, other.address)).to.eq(TEST_AMOUNT)
})
it('transfer', async () => {
await expect(token.transfer(other.address, TEST_AMOUNT))
.to.emit(token, 'Transfer')
@ -63,11 +70,9 @@ describe('UniswapV2ERC20 via GenericERC20', () => {
expect(await token.balanceOf(other.address)).to.eq(TEST_AMOUNT)
})
it('approve', async () => {
await expect(token.approve(other.address, TEST_AMOUNT))
.to.emit(token, 'Approval')
.withArgs(wallet.address, other.address, TEST_AMOUNT)
expect(await token.allowance(wallet.address, other.address)).to.eq(TEST_AMOUNT)
it('transfer:fail', async () => {
await expect(token.transfer(other.address, TOTAL_SUPPLY.add(1))).to.be.reverted // ds-math-sub-underflow
await expect(token.connect(other).transfer(wallet.address, 1)).to.be.reverted // ds-math-sub-underflow
})
it('transferFrom', async () => {
@ -90,27 +95,22 @@ describe('UniswapV2ERC20 via GenericERC20', () => {
expect(await token.balanceOf(other.address)).to.eq(TEST_AMOUNT)
})
it('transfer:fail', async () => {
await expect(token.transfer(other.address, TOTAL_SUPPLY.add(1))).to.be.reverted // ds-math-sub-underflow
await expect(token.connect(other).transfer(wallet.address, 1)).to.be.reverted // ds-math-sub-underflow
})
it('permit', async () => {
const nonce = await token.nonces(wallet.address)
const expiration = MaxUint256
const deadline = MaxUint256
const digest = await getApprovalDigest(
token,
{ owner: wallet.address, spender: other.address, value: TEST_AMOUNT },
nonce,
expiration
deadline
)
const { v, r, s } = ecsign(Buffer.from(digest.slice(2), 'hex'), Buffer.from(wallet.privateKey.slice(2), 'hex'))
await expect(token.permit(wallet.address, other.address, TEST_AMOUNT, expiration, v, hexlify(r), hexlify(s)))
await expect(token.permit(wallet.address, other.address, TEST_AMOUNT, deadline, v, hexlify(r), hexlify(s)))
.to.emit(token, 'Approval')
.withArgs(wallet.address, other.address, TEST_AMOUNT)
expect(await token.nonces(wallet.address)).to.eq(bigNumberify(1))
expect(await token.allowance(wallet.address, other.address)).to.eq(TEST_AMOUNT)
expect(await token.nonces(wallet.address)).to.eq(bigNumberify(1))
})
})

View File

@ -78,7 +78,7 @@ describe('UniswapV2Exchange', () => {
for (let testCase of testCases) {
await addLiquidity(testCase[1], testCase[2])
await token0.transfer(exchange.address, testCase[0])
await expect(exchange.swap(token0.address, testCase[3].add(1), wallet.address, overrides)).to.be.reverted // UniswapV2: K_VIOLATED
await expect(exchange.swap(token0.address, testCase[3].add(1), wallet.address, overrides)).to.be.reverted // UniswapV2: K
await exchange.swap(token0.address, testCase[3], wallet.address, overrides)
const totalSupply = await exchange.totalSupply()
await exchange.transfer(exchange.address, totalSupply)
@ -160,14 +160,13 @@ describe('UniswapV2Exchange', () => {
const expectedLiquidity = expandTo18Decimals(3)
await exchange.transfer(exchange.address, expectedLiquidity)
// this test is bugged, it catches the token{0,1} transfers before the lp transfers
await expect(exchange.burn(wallet.address, overrides))
// .to.emit(exchange, 'Transfer')
// .withArgs(exchange.address, AddressZero, expectedLiquidity)
.to.emit(exchange, 'Burn')
.withArgs(wallet.address, token0Amount, token1Amount, wallet.address)
.to.emit(exchange, 'Transfer')
.withArgs(exchange.address, AddressZero, expectedLiquidity)
.to.emit(exchange, 'Sync')
.withArgs(0, 0)
.to.emit(exchange, 'Burn')
.withArgs(wallet.address, token0Amount, token1Amount, wallet.address)
expect(await exchange.balanceOf(wallet.address)).to.eq(0)
expect(await exchange.totalSupply()).to.eq(0)

View File

@ -11,10 +11,10 @@ import UniswapV2Exchange from '../build/UniswapV2Exchange.json'
chai.use(solidity)
const TEST_ADDRESSES = {
token0: '0x1000000000000000000000000000000000000000',
token1: '0x2000000000000000000000000000000000000000'
}
const TEST_ADDRESSES: [string, string] = [
'0x1000000000000000000000000000000000000000',
'0x2000000000000000000000000000000000000000'
]
describe('UniswapV2Factory', () => {
const provider = new MockProvider({
@ -31,66 +31,55 @@ describe('UniswapV2Factory', () => {
factory = fixture.factory
})
it('feeToSetter, feeTo, exchangesCount', async () => {
expect(await factory.feeToSetter()).to.eq(wallet.address)
it('feeTo, feeToSetter, allExchanges, allExchangesLength', async () => {
expect(await factory.feeTo()).to.eq(AddressZero)
expect(await factory.exchangesCount()).to.eq(0)
expect(await factory.feeToSetter()).to.eq(wallet.address)
expect(await factory.allExchangesLength()).to.eq(0)
})
it('sortTokens', async () => {
expect(await factory.sortTokens(TEST_ADDRESSES.token0, TEST_ADDRESSES.token1)).to.deep.eq([
TEST_ADDRESSES.token0,
TEST_ADDRESSES.token1
])
expect(await factory.sortTokens(TEST_ADDRESSES.token1, TEST_ADDRESSES.token0)).to.deep.eq([
TEST_ADDRESSES.token0,
TEST_ADDRESSES.token1
])
})
async function createExchange(tokens: string[]) {
async function createExchange(tokens: [string, string]) {
const bytecode = `0x${UniswapV2Exchange.evm.bytecode.object}`
const create2Address = getCreate2Address(factory.address, TEST_ADDRESSES.token0, TEST_ADDRESSES.token1, bytecode)
const create2Address = getCreate2Address(factory.address, tokens, bytecode)
await expect(factory.createExchange(...tokens))
.to.emit(factory, 'ExchangeCreated')
.withArgs(TEST_ADDRESSES.token0, TEST_ADDRESSES.token1, create2Address, bigNumberify(1))
.withArgs(TEST_ADDRESSES[0], TEST_ADDRESSES[1], create2Address, bigNumberify(1))
await expect(factory.createExchange(...tokens)).to.be.reverted // UniswapV2Factory: EXCHANGE_EXISTS
await expect(factory.createExchange(...tokens.slice().reverse())).to.be.reverted // UniswapV2Factory: EXCHANGE_EXISTS
await expect(factory.createExchange(...tokens)).to.be.reverted // UniswapV2: EXCHANGE_EXISTS
await expect(factory.createExchange(...tokens.slice().reverse())).to.be.reverted // UniswapV2: EXCHANGE_EXISTS
expect(await factory.getExchange(...tokens)).to.eq(create2Address)
expect(await factory.getExchange(...tokens.slice().reverse())).to.eq(create2Address)
expect(await factory.exchanges(0)).to.eq(create2Address)
expect(await factory.exchangesCount()).to.eq(1)
expect(await factory.allExchanges(0)).to.eq(create2Address)
expect(await factory.allExchangesLength()).to.eq(1)
const exchange = new Contract(create2Address, JSON.stringify(UniswapV2Exchange.abi), provider)
expect(await exchange.factory()).to.eq(factory.address)
expect(await exchange.token0()).to.eq(TEST_ADDRESSES.token0)
expect(await exchange.token1()).to.eq(TEST_ADDRESSES.token1)
expect(await exchange.token0()).to.eq(TEST_ADDRESSES[0])
expect(await exchange.token1()).to.eq(TEST_ADDRESSES[1])
}
it('createExchange', async () => {
await createExchange([TEST_ADDRESSES.token0, TEST_ADDRESSES.token1])
await createExchange(TEST_ADDRESSES)
})
it('createExchange:reverse', async () => {
await createExchange([TEST_ADDRESSES.token1, TEST_ADDRESSES.token0])
await createExchange(TEST_ADDRESSES.slice().reverse() as [string, string])
})
it('createExchange:gas', async () => {
const gasCost = await factory.estimate.createExchange(TEST_ADDRESSES.token0, TEST_ADDRESSES.token1)
const gasCost = await factory.estimate.createExchange(...TEST_ADDRESSES)
console.log(`Gas required for createExchange: ${gasCost}`)
})
it('setFeeToSetter', async () => {
await expect(factory.connect(other).setFeeToSetter(other.address)).to.be.reverted // UniswapV2Factory: FORBIDDEN
await factory.setFeeToSetter(other.address)
expect(await factory.feeToSetter()).to.eq(other.address)
await expect(factory.setFeeToSetter(wallet.address)).to.be.reverted // UniswapV2Factory: FORBIDDEN
})
it('setFeeTo', async () => {
await expect(factory.connect(other).setFeeTo(other.address)).to.be.reverted // UniswapV2Factory: FORBIDDEN
await expect(factory.connect(other).setFeeTo(other.address)).to.be.reverted // 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.reverted // UniswapV2: FORBIDDEN
await factory.setFeeToSetter(other.address)
expect(await factory.feeToSetter()).to.eq(other.address)
await expect(factory.setFeeToSetter(wallet.address)).to.be.reverted // UniswapV2: FORBIDDEN
})
})

View File

@ -35,14 +35,14 @@ function getDomainSeparator(name: string, tokenAddress: string) {
export function getCreate2Address(
factoryAddress: string,
token0Address: string,
token1Address: string,
[tokenA, tokenB]: [string, string],
bytecode: string
): string {
const [token0, token1] = tokenA < tokenB ? [tokenA, tokenB] : [tokenB, tokenA]
const create2Inputs = [
'0xff',
factoryAddress,
keccak256(solidityPack(['address', 'address'], [token0Address, token1Address])),
keccak256(solidityPack(['address', 'address'], [token0, token1])),
keccak256(bytecode)
]
const sanitizedInputs = `0x${create2Inputs.map(i => i.slice(2)).join('')}`
@ -57,7 +57,7 @@ export async function getApprovalDigest(
value: BigNumber
},
nonce: BigNumber,
expiration: BigNumber
deadline: BigNumber
): Promise<string> {
const name = await token.name()
const DOMAIN_SEPARATOR = getDomainSeparator(name, token.address)
@ -71,7 +71,7 @@ export async function getApprovalDigest(
keccak256(
defaultAbiCoder.encode(
['bytes32', 'address', 'address', 'uint256', 'uint256', 'uint256'],
[PERMIT_TYPEHASH, approve.owner, approve.spender, approve.value, nonce, expiration]
[PERMIT_TYPEHASH, approve.owner, approve.spender, approve.value, nonce, deadline]
)
)
]