Skip to content
This repository was archived by the owner on Oct 26, 2022. It is now read-only.

Liquidate like tiran #32

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
liquidation test
  • Loading branch information
yaronvel committed Oct 25, 2021
commit 71afd770f20b4390a171631fcfcda3389c423bd7
8 changes: 3 additions & 5 deletions contracts/B.Protocol/BAMM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,12 @@ contract BAMM is PriceFormula, BoringOwnable, ERC20 {
address[] calldata users,
uint256[] calldata maxBorrowParts,
address to,
ISwapper swapper,
bool open
ISwapper swapper
) public
returns(uint mimBalanceBefore, uint collateralBalanceBefore, uint mimBalanceAfter, uint collateralBalanceAfter)
{
(mimBalanceBefore, collateralBalanceBefore) = getBalances();
lendingPair.liquidate(users, maxBorrowParts, to, swapper, open);
lendingPair.liquidate(users, maxBorrowParts, address(this), swapper, true);
(mimBalanceAfter, collateralBalanceAfter) = getBalances();

uint callerReward = mimBalanceBefore.sub(mimBalanceAfter).mul(callerFee) / 10000;
Expand All @@ -222,15 +221,14 @@ contract BAMM is PriceFormula, BoringOwnable, ERC20 {
uint256[] calldata maxBorrowParts,
address to,
ISwapper swapper,
bool open,
bool viaBentobox
) public {
// take the mim
receiveToken(mim, extraMim, viaBentobox);

// do the liquidation
(uint mimBalanceBefore, uint collatBalanceBefore, uint mimBalanceAfter, uint collatBalanceAfter) =
liquidate(users, maxBorrowParts, to, swapper, open);
liquidate(users, maxBorrowParts, to, swapper);

if(extraMim <= mimBalanceAfter) {
sendToken(mim, msg.sender, extraMim, viaBentobox);
Expand Down
51 changes: 34 additions & 17 deletions test/BProtocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,11 @@ describe("KashiPair Basic", function () {
const share = await thisObject.bentoBox.balanceOf(token, address)
return await thisObject.bentoBox.toAmount(token, share, false)
}

async function toAmount(thisObject, token, share) {
return await thisObject.bentoBox.toAmount(token, share, false)
}

it("deposit and withdraw only with mim", async function () {
const bamm = this.BAMM
const depositAmonut = getBigNumber(2, 18);
Expand Down Expand Up @@ -761,46 +766,58 @@ describe("KashiPair Basic", function () {

//await this.pairHelper.contract.connect(this.alice).setBProtocolMock(bamm.address)

const price = getBigNumber(11, 27);

await this.pairHelper.run((cmd) => [
cmd.as(this.bob).approveAsset(getBigNumber(310, 8)),
cmd.as(this.bob).depositAsset(getBigNumber(290, 8)),
cmd.approveCollateral(getBigNumber(100)),
cmd.depositCollateral(getBigNumber(100)),
cmd.borrow(sansBorrowFee(getBigNumber(75, 8))),
cmd.accrue(),
cmd.do(this.oracle.set, "11000000000000000000000000000"),
cmd.do(this.oracle.set, price.toString()),
cmd.updateExchangeRate(),
cmd.do(this.bentoBox.connect(this.bob).deposit, this.b.address, this.bob.address, this.bob.address, getBigNumber(20, 8), 0),
cmd.do(this.pairHelper.contract.connect(this.bob).removeAsset, this.bob.address, getBigNumber(50, 8)),
])

//console.log("Try to liquiate")
//console.log(await this.bentoBox.balanceOf(this.b.address, this.bob.address), await this.bentoBox.balanceOf(this.a.address, this.bob.address))
/*
await this.pairHelper.contract
.connect(this.bob)
.liquidate([this.alice.address], [getBigNumber(20, 8)], this.bob.address, "0x0000000000000000000000000000000000000000", true)
*/

//console.log("deposit started")
await bamm.setParams(20, 0, 100)

// deposit
const depositAmonut = await this.b.balanceOf(this.bob.address);

await this.b.connect(this.bob).approve(bamm.address, depositAmonut);
await bamm.connect(this.bob).deposit(depositAmonut, false);
/*
console.log(depositAmonut.toString())

console.log("deposit ended")
console.log(await this.bentoBox.balanceOf(this.b.address, bamm.address), await this.bentoBox.balanceOf(this.b.address, this.bob.address))
const liquidationShare = await this.pairHelper.contract.userBorrowPart(this.alice.address) //getBigNumber(20, 18);
const liquidationAmount = await toAmount(this, this.b.address, liquidationShare)

const bammMimBalBefore = await getBentoBoxBalance(this, this.b.address, bamm.address)
const bammColBalBefore = await getBentoBoxBalance(this, this.a.address, bamm.address)
const pairMimBalBefore = await getBentoBoxBalance(this, this.b.address, this.pairHelper.contract.address)
const nullAddr = "0x0000000000000000000000000000000000000000"
const rewardAddress = "0x0000000000000000000000000000000000000007"
await bamm.connect(this.bob).liquidate([this.alice.address], [liquidationShare], rewardAddress, nullAddr)
const bammMimBalAfter = await getBentoBoxBalance(this, this.b.address, bamm.address)
const bammColBalAfter = await getBentoBoxBalance(this, this.a.address, bamm.address)
const pairMimBalAfter = await getBentoBoxBalance(this, this.b.address, this.pairHelper.contract.address)

const deltaMimBamm = bammMimBalBefore.sub(bammMimBalAfter)
const deltaMimPair = pairMimBalAfter.sub(pairMimBalBefore)

const rewardBalance = await getBentoBoxBalance(this, this.b.address, rewardAddress)

console.log(await this.pairHelper.contract.userBorrowPart(this.alice.address))*/
await bamm.connect(this.bob).liquidate([this.alice.address], [getBigNumber(20, 8)], this.bob.address, "0x0000000000000000000000000000000000000000", true)
expect(deltaMimBamm.sub(rewardBalance)).to.be.equal(deltaMimPair)
expect(deltaMimPair.div(100)).to.be.equal(rewardBalance)

const deltaCol = bammColBalAfter.sub(bammColBalBefore)
const deltaMimWithPermium = deltaMimPair.mul(112).div(100)

const roundingFactor = getBigNumber(1, 11);
expect(deltaMimWithPermium.mul(price).div(getBigNumber(1,18)).div(roundingFactor)).to.be.equal(deltaCol.div(roundingFactor))
})

// liquidate normal test - TODO

})

describe("Liquidate", function () {
Expand Down