Skip to content

Commit

Permalink
add istanbul support for testing environment
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahZinsmeister committed Dec 20, 2019
1 parent c5e9261 commit 2037952
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 622 deletions.
6 changes: 4 additions & 2 deletions contracts/ERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ contract ERC20 is IERC20 {
if (_totalSupply > 0) {
_mint(msg.sender, _totalSupply);
}
uint chainId = 1; // hardcode as 1 until ethereum-waffle support istanbul-specific EVM opcodes
// assembly { chainId := chainid() } // solium-disable-line security/no-inline-assembly
uint chainId;
assembly { // solium-disable-line security/no-inline-assembly
chainId := chainid()
}
DOMAIN_SEPARATOR = keccak256(abi.encode(
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256(bytes(name)),
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
"@types/chai": "^4.2.6",
"@types/mocha": "^5.2.7",
"chai": "^4.2.0",
"ethereum-waffle": "2.1.0",
"ethereum-waffle": "2.3.0-istanbul.0",
"ethereumjs-util": "^6.2.0",
"ethers": "^4.0.40",
"mocha": "^6.2.2",
"prettier": "^1.19.1",
"ts-node": "^8.5.4",
Expand Down
6 changes: 2 additions & 4 deletions test/ERC20.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,8 @@ describe('ERC20', () => {
})

it('transfer:fail', async () => {
await expect(token.transfer(other.address, TOKEN_DETAILS.totalSupply.add(1))).to.be.revertedWith(
'ds-math-sub-underflow'
)
await expect(token.connect(other).transfer(wallet.address, 1)).to.be.revertedWith('ds-math-sub-underflow')
await expect(token.transfer(other.address, TOKEN_DETAILS.totalSupply.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 () => {
Expand Down
28 changes: 16 additions & 12 deletions test/UniswapV2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { AddressZero } from 'ethers/constants'
chai.use(solidity)
const { expect } = chai

const overrides = {
gasLimit: 1000000
}

describe('UniswapV2', () => {
const provider = createMockProvider(path.join(__dirname, '..', 'waffle.json'))
const [wallet] = getWallets(provider)
Expand All @@ -35,7 +39,7 @@ describe('UniswapV2', () => {
await token1.transfer(exchange.address, token1Amount)

const expectedLiquidity = expandTo18Decimals(2)
await expect(exchange.connect(wallet).make())
await expect(exchange.connect(wallet).make(overrides))
.to.emit(exchange, 'Transfer')
.withArgs(AddressZero, wallet.address, expectedLiquidity)
.to.emit(exchange, 'Sync')
Expand All @@ -54,7 +58,7 @@ describe('UniswapV2', () => {
async function addLiquidity(token0Amount: BigNumber, token1Amount: BigNumber) {
await token0.transfer(exchange.address, token0Amount)
await token1.transfer(exchange.address, token1Amount)
await exchange.connect(wallet).make()
await exchange.connect(wallet).make(overrides)
}

it('getInputPrice', async () => {
Expand All @@ -73,11 +77,11 @@ describe('UniswapV2', () => {
for (let testCase of testCases) {
await addLiquidity(testCase[1], testCase[2])
await token0.transfer(exchange.address, testCase[0])
await expect(exchange.connect(wallet).move(token0.address, testCase[3].add(1))).to.be.revertedWith('UniswapV2: K')
await exchange.connect(wallet).move(token0.address, testCase[3])
await expect(exchange.connect(wallet).move(token0.address, testCase[3].add(1), overrides)).to.be.reverted // UniswapV2: K_VIOLATED
await exchange.connect(wallet).move(token0.address, testCase[3], overrides)
const totalSupply = await exchange.totalSupply()
await exchange.connect(wallet).transfer(exchange.address, totalSupply)
await exchange.connect(wallet).made()
await exchange.connect(wallet).made(overrides)
}
})

Expand All @@ -89,7 +93,7 @@ describe('UniswapV2', () => {
const swapAmount = expandTo18Decimals(1)
const expectedOutputAmount = bigNumberify('1662497915624478906')
await token0.transfer(exchange.address, swapAmount)
await expect(exchange.connect(wallet).move(token0.address, expectedOutputAmount))
await expect(exchange.connect(wallet).move(token0.address, expectedOutputAmount, overrides))
.to.emit(exchange, 'Sync')
.withArgs(token0Amount.add(swapAmount), token1Amount.sub(expectedOutputAmount))
.to.emit(exchange, 'Move')
Expand All @@ -113,7 +117,7 @@ describe('UniswapV2', () => {
const swapAmount = expandTo18Decimals(1)
const expectedOutputAmount = bigNumberify('453305446940074565')
await token1.transfer(exchange.address, swapAmount)
await expect(exchange.connect(wallet).move(token1.address, expectedOutputAmount))
await expect(exchange.connect(wallet).move(token1.address, expectedOutputAmount, overrides))
.to.emit(exchange, 'Sync')
.withArgs(token0Amount.sub(expectedOutputAmount), token1Amount.add(swapAmount))
.to.emit(exchange, 'Move')
Expand All @@ -135,12 +139,12 @@ describe('UniswapV2', () => {
await addLiquidity(token0Amount, token1Amount)

// ensure that setting price{0,1}CumulativeLast for the first time doesn't affect our gas math
await exchange.connect(wallet).sync()
await exchange.connect(wallet).sync(overrides)

const swapAmount = expandTo18Decimals(1)
const expectedOutputAmount = bigNumberify('453305446940074565')
await token0.transfer(exchange.address, swapAmount)
const gasCost = await exchange.estimate.move(token0.address, expectedOutputAmount)
const gasCost = await exchange.estimate.move(token0.address, expectedOutputAmount, overrides)
console.log(`Gas required for swap: ${gasCost}`)
})

Expand All @@ -152,7 +156,7 @@ describe('UniswapV2', () => {
const expectedLiquidity = expandTo18Decimals(3)
await exchange.connect(wallet).transfer(exchange.address, expectedLiquidity)
// this test is bugged, it catches the token{0,1} transfers before the lp transfers
await expect(exchange.connect(wallet).made())
await expect(exchange.connect(wallet).made(overrides))
// .to.emit(exchange, 'Transfer')
// .withArgs(exchange.address, AddressZero, expectedLiquidity)
.to.emit(exchange, 'Made')
Expand All @@ -179,13 +183,13 @@ describe('UniswapV2', () => {
expect(await exchange.price0CumulativeLast()).to.eq(0)
expect(await exchange.price1CumulativeLast()).to.eq(0)

await exchange.connect(wallet).sync()
await exchange.connect(wallet).sync(overrides)
expect(await exchange.price0CumulativeLast()).to.eq(bigNumberify(2).pow(112))
expect(await exchange.price1CumulativeLast()).to.eq(bigNumberify(2).pow(112))
expect(await exchange.blockNumberLast()).to.eq(blockNumber + 1)

await mineBlocks(provider, 8)
await exchange.connect(wallet).sync()
await exchange.connect(wallet).sync(overrides)
expect(await exchange.price0CumulativeLast()).to.eq(
bigNumberify(2)
.pow(112)
Expand Down
16 changes: 5 additions & 11 deletions test/UniswapV2Factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ describe('UniswapV2Factory', () => {
await expect(factory.createExchange(...tokens))
.to.emit(factory, 'ExchangeCreated')
.withArgs(TEST_ADDRESSES.token0, TEST_ADDRESSES.token1, create2Address, bigNumberify(1))
await expect(factory.createExchange(...tokens)).to.be.revertedWith('UniswapV2Factory: EXCHANGE_EXISTS')
await expect(factory.createExchange(...tokens.slice().reverse())).to.be.revertedWith(
'UniswapV2Factory: EXCHANGE_EXISTS'
)
await expect(factory.createExchange(...tokens)).to.be.reverted // UniswapV2Factory: EXCHANGE_EXISTS
await expect(factory.createExchange(...tokens.slice().reverse())).to.be.reverted // UniswapV2Factory: EXCHANGE_EXISTS
expect(await factory.getExchange(...tokens)).to.eq(create2Address)
expect(await factory.getExchange(...tokens.slice().reverse())).to.eq(create2Address)
expect(await factory.getTokens(create2Address)).to.deep.eq([TEST_ADDRESSES.token0, TEST_ADDRESSES.token1])
Expand All @@ -79,18 +77,14 @@ describe('UniswapV2Factory', () => {
})

it('setFactoryOwner', async () => {
await expect(factory.connect(other).setFactoryOwner(other.address)).to.be.revertedWith(
'UniswapV2Factory: FORBIDDEN'
)
await expect(factory.connect(other).setFactoryOwner(other.address)).to.be.reverted // UniswapV2Factory: FORBIDDEN
await factory.setFactoryOwner(other.address)
expect(await factory.factoryOwner()).to.eq(other.address)
await expect(factory.setFactoryOwner(wallet.address)).to.be.revertedWith('UniswapV2Factory: FORBIDDEN')
await expect(factory.setFactoryOwner(wallet.address)).to.be.reverted // UniswapV2Factory: FORBIDDEN
})

it('setFeeRecipient', async () => {
await expect(factory.connect(other).setFeeRecipient(other.address)).to.be.revertedWith(
'UniswapV2Factory: FORBIDDEN'
)
await expect(factory.connect(other).setFeeRecipient(other.address)).to.be.reverted // UniswapV2Factory: FORBIDDEN
await factory.setFeeRecipient(wallet.address)
expect(await factory.feeRecipient()).to.eq(wallet.address)
})
Expand Down
2 changes: 1 addition & 1 deletion waffle.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}
},
"ganacheOptions": {
"hardfork": "constantinople",
"hardfork": "istanbul",
"mnemonic": "horn horn horn horn horn horn horn horn horn horn horn horn",
"gasLimit": "0x989680"
}
Expand Down
Loading

0 comments on commit 2037952

Please sign in to comment.