diff --git a/test/unit/primitiveEngine/effect/remove.ts b/test/unit/primitiveEngine/effect/remove.ts index 4b2498d5..eb11c5c6 100644 --- a/test/unit/primitiveEngine/effect/remove.ts +++ b/test/unit/primitiveEngine/effect/remove.ts @@ -10,8 +10,9 @@ import loadContext from '../../context' const [strike, sigma, time, _] = [parseWei('1000').raw, 0.85 * PERCENTAGE, 1655655140, parseWei('1100').raw] +const delLiquidity = parseWei('1') const empty: BytesLike = constants.HashZero -let pid: string +let poolId: string let posId: string describe('remove', function () { @@ -19,14 +20,26 @@ describe('remove', function () { await loadContext(waffle.provider, ['engineCreate', 'engineDeposit', 'engineAllocate', 'engineRemove'], removeFragment) }) - describe('when removing to internal', function () { + describe('when removing to margin', function () { beforeEach(async function () { - pid = await this.contracts.engine.getPoolId(strike, sigma, time) - posId = await this.contracts.engineRemove.getPosition(pid) + poolId = await this.contracts.engine.getPoolId(strike, sigma, time) + posId = await this.contracts.engineRemove.getPosition(poolId) + }) + + it('updates the margin', async function () { + await this.contracts.engineRemove.removeToMargin(poolId, delLiquidity.raw, empty) + + const res = await this.contracts.engine.reserves(poolId) + const delRisky = delLiquidity.mul(res.reserveRisky).div(res.liquidity) + const delStable = delLiquidity.mul(res.reserveStable).div(res.liquidity) + + const margin = await this.contracts.engine.margins(this.contracts.engineRemove.address) + expect(margin.balanceRisky).to.equal(delRisky.raw) + expect(margin.balanceStable).to.equal(delStable.raw) }) it('updates the position', async function () { - await this.contracts.engineRemove.removeToMargin(pid, parseWei('1').raw, empty) + await this.contracts.engineRemove.removeToMargin(poolId, delLiquidity.raw, empty) expect(await this.contracts.engine.positions(posId)).to.be.deep.eq([ BigNumber.from('0'), @@ -37,92 +50,58 @@ describe('remove', function () { ]) }) - it('updates the margin', async function () { - const delLiquidity = parseWei('1') - - await this.contracts.engineRemove.removeToMargin(pid, delLiquidity.raw, empty) + it('updates the reserves', async function () { + const res = await this.contracts.engine.reserves(poolId) + const delRisky = delLiquidity.mul(res.reserveRisky).div(res.liquidity) + const delStable = delLiquidity.mul(res.reserveStable).div(res.liquidity) - const res = await this.contracts.engine.reserves(pid) + await this.contracts.engineRemove.removeToMargin(poolId, delLiquidity.raw, empty) - const margin = await this.contracts.engine.margins(this.contracts.engineRemove.address) + const updatedRes = await this.contracts.engine.reserves(poolId) + expect(updatedRes.liquidity).to.equal(res.liquidity.sub(delLiquidity.raw)) + expect(updatedRes.reserveRisky).to.equal(res.reserveRisky.sub(delRisky.raw)) + expect(updatedRes.reserveStable).to.equal(res.reserveStable.sub(delStable.raw)) + }) + it('emits the Removed event', async function () { + const res = await this.contracts.engine.reserves(poolId) const delRisky = delLiquidity.mul(res.reserveRisky).div(res.liquidity) const delStable = delLiquidity.mul(res.reserveStable).div(res.liquidity) - expect(margin.balanceRisky).to.equal(delRisky.raw) - - expect(margin.balanceStable).to.equal(delStable.raw) + await expect(this.contracts.engineRemove.removeToMargin(poolId, delLiquidity.raw, empty)) + .to.emit(this.contracts.engine, 'Removed') + .withArgs(this.contracts.engineRemove.address, delRisky.raw, delStable.raw) }) - }) -}) -/* - it('updates the margin', async function () { - await this.contracts.engineRemove.removeToMargin(pid, parseWei('1').raw, empty) - const margin = await this.contracts.engine.margins( - this.contracts.engineRemove.address - ) - - const res = await this.contracts.engine.reserves(pid) - - expect( - margin.balanceRisky, - ).to.equal( - parseWei('1').mul(res.reserveRisky).div(res.liquidity) - ) - - expect( - margin.balanceRisky, - ).to.equal( - parseWei('1').mul(res.reserveStable).div(res.liquidity) - ) + it('reverts if value is 0', async function () { + await expect(this.contracts.engineRemove.removeToMargin(poolId, 0, empty)).to.revertedWith('Cannot be 0') }) - it('updates the reserves', async function () { - const posId = await this.contracts.engineRemove.getPosition(pid) - await this.contracts.engineRemove.removeToMargin(pid, parseWei('1').raw, empty) - - expect(await this.contracts.engine.positions(posId)).to.be.deep.eq([ - BigNumber.from('0'), - BigNumber.from('0'), - BigNumber.from('0'), - parseWei('9').raw, - BigNumber.from('0'), - ]) + it('reverts if required amount is too big', async function () { + await expect(this.contracts.engineRemove.removeToMargin(poolId, parseWei('11').raw, empty)).to.be.reverted }) + }) - // TODO: Finish this test - it('emits the Removed event', async function () { - await expect( - this.contracts.engineRemove.removeToMargin(pid, parseWei('1').raw, empty) - ).to.emit( - this.contracts.engine, - 'Removed', - ) + describe('when removing to external', function () { + beforeEach(async function () { + poolId = await this.contracts.engine.getPoolId(strike, sigma, time) + posId = await this.contracts.engineRemove.getPosition(poolId) }) - it('reverts if deltaL is 0', async function () { - await this.contracts.engineRemove.removeToMargin(pid, 0, empty) - }) - }) + it('transfers the tokens', async function () { + await this.contracts.engineRemove.removeToExternal(poolId, delLiquidity.raw, empty) - describe.skip('when removing to external', function () { - it('updates the position', async function () { - const posId = await this.contracts.engineRemove.getPosition(pid) - await this.contracts.engineRemove.removeToMargin(pid, parseWei('1').raw, empty) + const res = await this.contracts.engine.reserves(poolId) + const delRisky = delLiquidity.mul(res.reserveRisky).div(res.liquidity) + const delStable = delLiquidity.mul(res.reserveStable).div(res.liquidity) - expect(await this.contracts.engine.positions(posId)).to.be.deep.eq([ - BigNumber.from('0'), - BigNumber.from('0'), - BigNumber.from('0'), - parseWei('9').raw, - BigNumber.from('0'), - ]) + expect(await this.contracts.risky.balanceOf(this.contracts.engineRemove.address)).to.equal(delRisky.raw) + + expect(await this.contracts.stable.balanceOf(this.contracts.engineRemove.address)).to.equal(delStable.raw) }) - it('transfers the tokens', async function () { - const posId = await this.contracts.engineRemove.getPosition(pid) - await this.contracts.engineRemove.removeToMargin(pid, parseWei('1').raw, empty) + it('updates the position', async function () { + await this.contracts.engineRemove.removeToExternal(poolId, delLiquidity.raw, empty) expect(await this.contracts.engine.positions(posId)).to.be.deep.eq([ BigNumber.from('0'), @@ -133,45 +112,35 @@ describe('remove', function () { ]) }) + it('updates the reserves', async function () { + const res = await this.contracts.engine.reserves(poolId) + const delRisky = delLiquidity.mul(res.reserveRisky).div(res.liquidity) + const delStable = delLiquidity.mul(res.reserveStable).div(res.liquidity) - }) + await this.contracts.engineRemove.removeToExternal(poolId, delLiquidity.raw, empty) - describe.skip('when the parameters are valid', function () { - it('removes 1 liquidity share and deposits the resultant risky and stable to margin', async function () { - const posId = await this.contracts.engineRemove.getPosition(pid) - await this.contracts.engineRemove.removeToMargin(pid, parseWei('1').raw, empty) - - expect(await this.contracts.engine.positions(posId)).to.be.deep.eq([ - BigNumber.from('0'), - BigNumber.from('0'), - BigNumber.from('0'), - parseWei('9').raw, - BigNumber.from('0'), - ]) + const updatedRes = await this.contracts.engine.reserves(poolId) + expect(updatedRes.liquidity).to.equal(res.liquidity.sub(delLiquidity.raw)) + expect(updatedRes.reserveRisky).to.equal(res.reserveRisky.sub(delRisky.raw)) + expect(updatedRes.reserveStable).to.equal(res.reserveStable.sub(delStable.raw)) }) - it('removes 1 liquidity share and sends the resultant risky and stable to engineDeposit.address', async function () { - const pid = await this.contracts.engine.getPoolId(strike, sigma, time) - const posId = await this.contracts.engineRemove.getPosition(pid) - await this.contracts.engineRemove.removeToExternal(pid, parseWei('1').raw, empty) - expect(await this.contracts.engine.positions(posId)).to.be.deep.eq([ - BigNumber.from('0'), - BigNumber.from('0'), - BigNumber.from('0'), - parseWei('9').raw, - BigNumber.from('0'), - ]) + it('emits the Removed event', async function () { + const res = await this.contracts.engine.reserves(poolId) + const delRisky = delLiquidity.mul(res.reserveRisky).div(res.liquidity) + const delStable = delLiquidity.mul(res.reserveStable).div(res.liquidity) + + await expect(this.contracts.engineRemove.removeToExternal(poolId, delLiquidity.raw, empty)) + .to.emit(this.contracts.engine, 'Removed') + .withArgs(this.contracts.engineRemove.address, delRisky.raw, delStable.raw) }) - it('fails to remove more liquidity to margin than is allocated by the address', async function () { - const poolId = await this.contracts.engine.getPoolId(strike, sigma, time) - await expect(this.contracts.engineRemove.removeToMargin(poolId, parseWei('20').raw, empty)).to.be.reverted + it('reverts if value is 0', async function () { + await expect(this.contracts.engineRemove.removeToExternal(poolId, 0, empty)).to.revertedWith('Cannot be 0') }) - it('fails to remove more liquity to engineRemove.address than is allocated by the address', async function () { - const poolId = await this.contracts.engine.getPoolId(strike, sigma, time) - await expect(this.contracts.engineRemove.removeToExternal(poolId, parseWei('20').raw, empty)).to.be.reverted + it('reverts if required amount is too big', async function () { + await expect(this.contracts.engineRemove.removeToExternal(poolId, parseWei('11').raw, empty)).to.be.reverted }) }) }) -*/