Skip to content

Commit 232e463

Browse files
committed
enter market in import + dedicated flash loan free function when no debt
1 parent 54484d7 commit 232e463

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

Diff for: contracts/bprotocol/Import.sol

+15
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ contract Import is Exponential {
6060
for(uint i = 0 ; i < cTokenCollateral.length ; i++) {
6161
ICToken cColl = ICToken(cTokenCollateral[i]);
6262
uint collBalance = cColl.balanceOf(account);
63+
bComptroller.enterMarketOnAvatar(IAvatar(avatar), bComptroller.c2b(address(cColl)));
6364
IAvatar(avatar).collectCToken(address(cColl), account, collBalance);
6465
}
6566
}
@@ -78,6 +79,20 @@ contract Import is Exponential {
7879
}
7980
}
8081

82+
// this import function should be called if accounts that don't have debt
83+
// this is safe only if the owner of the compound account is an EOA
84+
function importCollateral(address[] calldata cTokenCollateral) external {
85+
address account = tx.origin;
86+
address avatar = registry.getAvatar(account);
87+
88+
// redeem all non ETH collateral from Compound and deposit it in B
89+
_transferCollateral(
90+
cTokenCollateral,
91+
account,
92+
avatar
93+
);
94+
}
95+
8196
// this is safe only if the owner of the compound account is an EOA
8297
function importAccount(
8398
address[] calldata cTokenCollateral,

Diff for: test/TestImport.spec.ts

+53-1
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,26 @@ contract("BErc20", async (accounts) => {
263263
expect(await bETH.balanceOf(a.user1)).to.be.bignumber.equal(cETHBalance);
264264
expect(await bBAT.balanceOf(a.user1)).to.be.bignumber.equal(cBATBalance);
265265

266+
const avatar1 = await bProtocol.registry.getAvatar.call(a.user1);
267+
const assetsIn = await comptroller.getAssetsIn(avatar1);
268+
269+
expect(assetsIn.includes(cZRX.address)).to.be.equal(true);
270+
expect(assetsIn.includes(cETH.address)).to.be.equal(true);
271+
expect(assetsIn.includes(cBAT.address)).to.be.equal(true);
272+
266273
if (checkDebtAfter) {
267274
console.log("checking debt after");
268275
expect(await bBAT.borrowBalanceCurrent.call(a.user1)).to.be.bignumber.equal(HUNDRED_BAT);
269276
expect(await bUSDT.borrowBalanceCurrent.call(a.user1)).to.be.bignumber.equal(
270277
FIVE_HUNDRED_USDT,
271278
);
272-
} else console.log("skipping checking debt after");
279+
280+
expect(assetsIn.length).to.be.equal(4);
281+
expect(assetsIn.includes(cUSDT.address)).to.be.equal(true);
282+
} else {
283+
expect(assetsIn.length).to.be.equal(3);
284+
console.log("skipping checking debt after");
285+
}
273286

274287
console.log("checking delegate was revoked");
275288
const avatar = await bProtocol.registry.getAvatar.call(a.user1);
@@ -387,6 +400,45 @@ contract("BErc20", async (accounts) => {
387400
checkDebtAfter = false;
388401
});
389402

403+
it("user should import without fees and with no debt without flashloan", async () => {
404+
// repay the bat and usdt debt
405+
await BAT.approve(cBAT.address, HUNDRED_BAT, { from: a.user1 });
406+
await cBAT.repayBorrow(HUNDRED_BAT, { from: a.user1 });
407+
await USDT.approve(cUSDT.address, HUNDRED_BAT, { from: a.user1 });
408+
await cUSDT.repayBorrow(FIVE_HUNDRED_USDT, { from: a.user1 });
409+
410+
// make sure borrow balance was reset
411+
expect(await cBAT.borrowBalanceCurrent.call(a.user1)).to.be.bignumber.equal(ZERO);
412+
expect(await cUSDT.borrowBalanceCurrent.call(a.user1)).to.be.bignumber.equal(ZERO);
413+
414+
const avatar1 = await bProtocol.registry.getAvatar.call(a.user1);
415+
await cZRX.approve(avatar1, new BN(2).pow(new BN(255)), { from: a.user1 });
416+
await cETH.approve(avatar1, new BN(2).pow(new BN(255)), { from: a.user1 });
417+
await cBAT.approve(avatar1, new BN(2).pow(new BN(255)), { from: a.user1 });
418+
419+
// call flash import
420+
const data = await bImport.contract.methods
421+
.importCollateral(
422+
[cZRX.address, cETH.address, cBAT.address]
423+
)
424+
.encodeABI();
425+
426+
const tx = await bProtocol.registry.delegateAndExecuteOnce(
427+
bImport.address,
428+
bImport.address,
429+
data,
430+
{ from: a.user1 },
431+
);
432+
console.log(tx.receipt.gasUsed);
433+
434+
// check balance
435+
expect(await bETH.borrowBalanceCurrent.call(a.user1)).to.be.bignumber.equal(ZERO);
436+
expect(await bBAT.borrowBalanceCurrent.call(a.user1)).to.be.bignumber.equal(ZERO);
437+
expect(await bUSDT.borrowBalanceCurrent.call(a.user1)).to.be.bignumber.equal(ZERO);
438+
439+
checkDebtAfter = false;
440+
});
441+
390442
it("user should import with fees", async () => {
391443
const avatar1 = await bProtocol.registry.getAvatar.call(a.user1);
392444
await cZRX.approve(avatar1, new BN(2).pow(new BN(255)), { from: a.user1 });

0 commit comments

Comments
 (0)