fix stale cache bug from SLOAD optimization

This commit is contained in:
Noah Zinsmeister
2020-01-27 18:13:43 -05:00
parent 0b37035f04
commit 452b531205
2 changed files with 52 additions and 10 deletions

View File

@ -95,15 +95,15 @@ contract UniswapV2Exchange is IUniswapV2Exchange, UniswapV2ERC20 {
// mint liquidity
function mint(address to) external lock returns (uint liquidity) {
uint _totalSupply = totalSupply; // gas savings
uint112 _reserve0 = reserve0; // gas savings
uint112 _reserve1 = reserve1; // gas savings
uint112 _reserve0 = reserve0; // gas savings
uint112 _reserve1 = reserve1; // gas savings
uint balance0 = IERC20(token0).balanceOf(address(this));
uint balance1 = IERC20(token1).balanceOf(address(this));
uint amount0 = balance0.sub(_reserve0);
uint amount1 = balance1.sub(_reserve1);
bool feeOn = _mintFee(_reserve0, _reserve1);
uint _totalSupply = totalSupply; // gas savings
liquidity = _totalSupply == 0
? Math.sqrt(amount0.mul(amount1))
: Math.min(amount0.mul(_totalSupply) / _reserve0, amount1.mul(_totalSupply) / _reserve1);
@ -117,16 +117,16 @@ contract UniswapV2Exchange is IUniswapV2Exchange, UniswapV2ERC20 {
// burn liquidity
function burn(address to) external lock returns (uint amount0, uint amount1) {
uint _totalSupply = totalSupply; // gas savings
uint112 _reserve0 = reserve0; // gas savings
uint112 _reserve1 = reserve1; // gas savings
address _token0 = token0; // gas savings
address _token1 = token1; // gas savings
uint112 _reserve0 = reserve0; // gas savings
uint112 _reserve1 = reserve1; // gas savings
address _token0 = token0; // gas savings
address _token1 = token1; // gas savings
uint balance0 = IERC20(_token0).balanceOf(address(this));
uint balance1 = IERC20(_token1).balanceOf(address(this));
uint liquidity = balanceOf[address(this)];
bool feeOn = _mintFee(_reserve0, _reserve1);
uint _totalSupply = totalSupply; // gas savings
amount0 = liquidity.mul(balance0) / _totalSupply; // use balances instead of reserves to address force sends
amount1 = liquidity.mul(balance1) / _totalSupply; // use balances instead of reserves to address force sends
require(amount0 > 0 && amount1 > 0, 'UniswapV2: INSUFFICIENT_LIQUIDITY_BURNED');

View File

@ -19,14 +19,16 @@ describe('UniswapV2Exchange', () => {
mnemonic: 'horn horn horn horn horn horn horn horn horn horn horn horn',
gasLimit: 9999999
})
const [wallet] = provider.getWallets()
const [wallet, other] = provider.getWallets()
const loadFixture = createFixtureLoader(provider, [wallet])
let factory: Contract
let token0: Contract
let token1: Contract
let exchange: Contract
beforeEach(async () => {
const fixture = await loadFixture(exchangeFixture)
factory = fixture.factory
token0 = fixture.token0
token1 = fixture.token1
exchange = fixture.exchange
@ -78,7 +80,9 @@ 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
await expect(exchange.swap(token0.address, testCase[3].add(1), wallet.address, overrides)).to.be.revertedWith(
'UniswapV2: K'
)
await exchange.swap(token0.address, testCase[3], wallet.address, overrides)
const totalSupply = await exchange.totalSupply()
await exchange.transfer(exchange.address, totalSupply)
@ -207,4 +211,42 @@ describe('UniswapV2Exchange', () => {
)
expect((await exchange.getReserves())[2]).to.eq(blockTimestamp + 10)
})
it('feeTo:off', async () => {
const token0Amount = expandTo18Decimals(1000)
const token1Amount = expandTo18Decimals(1000)
await addLiquidity(token0Amount, token1Amount)
const swapAmount = expandTo18Decimals(1)
const expectedOutputAmount = bigNumberify('996006981039903216')
await token1.transfer(exchange.address, swapAmount)
await exchange.swap(token1.address, expectedOutputAmount, wallet.address, overrides)
const expectedLiquidity = expandTo18Decimals(1000)
await exchange.transfer(exchange.address, expectedLiquidity)
await exchange.burn(wallet.address, overrides)
expect(await exchange.totalSupply()).to.eq(0)
})
it('feeTo:on', async () => {
await factory.setFeeTo(other.address)
const token0Amount = expandTo18Decimals(1000)
const token1Amount = expandTo18Decimals(1000)
await addLiquidity(token0Amount, token1Amount)
const swapAmount = expandTo18Decimals(1)
const expectedOutputAmount = bigNumberify('996006981039903216')
await token1.transfer(exchange.address, swapAmount)
await exchange.swap(token1.address, expectedOutputAmount, wallet.address, overrides)
const expectedLiquidity = expandTo18Decimals(1000)
await exchange.transfer(exchange.address, expectedLiquidity)
await exchange.burn(wallet.address, overrides)
expect(await exchange.totalSupply()).to.eq('299700614071741')
expect(await exchange.balanceOf(other.address)).to.eq('299700614071741')
expect(await token0.balanceOf(exchange.address)).to.eq('299402020436935')
expect(await token1.balanceOf(exchange.address)).to.eq('300000224775562')
})
})