Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bank borrow bug #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions contracts/Bank.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,6 @@ contract Bank is BankStorage, AccessControlEnumerable, Initializable {
reserve.oracleContract = oracleContract;
}

/*Modifiers*/
modifier onlyOwner() {
require(_owner == msg.sender, "IS NOT OWNER");
_;
}

/*Functions*/
/**
* @dev Returns the owner of the bank
*/
function owner() public view returns (address) {
return _owner;
}

/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
* NOTE: Override this to add changing the
*/
function transferOwnership(address newOwner) public onlyOwner {
_owner = newOwner;
}

/**
* @dev This function sets the fundamental parameters for the bank
* and assigns the first admin
Expand Down Expand Up @@ -245,7 +222,6 @@ contract Bank is BankStorage, AccessControlEnumerable, Initializable {
function vaultDeposit(uint256 amount) external {
require(amount > 0, "Amount is zero !!");
vaults[msg.sender].collateralAmount += amount;
reserve.collateralBalance += amount;
IERC20(collateral.tokenAddress).safeTransferFrom(
msg.sender,
address(this),
Expand All @@ -268,7 +244,6 @@ contract Bank is BankStorage, AccessControlEnumerable, Initializable {
reserve.collateralizationRatio) * 100;
maxBorrow *= debt.priceGranularity;
maxBorrow /= collateral.priceGranularity;
maxBorrow -= vaults[msg.sender].debtAmount;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the source of the error, subtracting the debt amount was not needed. This was missed since there was no test for the scenario that causes this to be a problem

vaults[msg.sender].debtAmount +=
amount +
((amount * reserve.originationFee) / 10000);
Expand Down
8 changes: 3 additions & 5 deletions migrations/3_tellor_contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = async function (deployer, network, accounts) {
let period = 86400;
let ricusdRequestId = 77;
let usdcusdRequestId = 78;
let initialPriceRic = 900000;
let initialPriceRic = 1200000;
let initialPriceUsdc = 1000000;
let priceGranularity = 1000000;
let ricAddress = "0x263026e7e53dbfdce5ae55ade22493f828922965";
Expand All @@ -33,11 +33,9 @@ module.exports = async function (deployer, network, accounts) {
await deployer.deploy(BankFactory, bank.address);
let bankFactory = await BankFactory.deployed();

console.log("Bank owner", await bank.owner())
console.log("Deployed");
// TRB/DAI
let clone1 = await bankFactory.createBank("🚰 REX Bank v1.0", interestRate, originationFee, collateralizationRatio, liquidationPenalty, period, tellorOracleAddress);
console.log("createBank")
let clone1 = await bankFactory.createBank("🚿 REX Bank v1.1", interestRate, originationFee, collateralizationRatio, liquidationPenalty, period, tellorOracleAddress);
console.log("Created Bank")
let bankClone1 = await Bank.at(clone1.logs[0].args.newBankAddress);
await bankClone1.setCollateral(ricAddress, ricusdRequestId, priceGranularity, initialPriceRic);
await bankClone1.setDebt(usdcxAddress, usdcusdRequestId, priceGranularity, initialPriceUsdc);
Expand Down
20 changes: 10 additions & 10 deletions test/bank.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,30 +229,30 @@ contract("Bank", function (_accounts) {
});

it('should allow the user to borrow', async function () {
await this.dt.approve(this.bank.address, this.depositAmount);
await this.bank.reserveDeposit(this.depositAmount);
await this.dt.approve(this.bank.address, this.depositAmount.mul(new BN(2)));
await this.bank.reserveDeposit(this.depositAmount.mul(new BN(2)));
await this.ct.approve(this.bank.address, this.depositAmount, { from: _accounts[1] });
await this.bank.vaultDeposit(this.depositAmount, { from: _accounts[1] });
await this.bank.vaultBorrow(this.borrowAmount, { from: _accounts[1] });
await this.ct.approve(this.bank.address, this.depositAmount, { from: _accounts[1] });
await this.bank.vaultDeposit(this.depositAmount, { from: _accounts[1] });
await this.bank.vaultBorrow(this.smallBorrowAmount, { from: _accounts[1] });
await time.increase(60 * 60 * 24 * 2 + 10);
await this.bank.vaultBorrow(this.smallBorrowAmount, { from: _accounts[1] });
//await this.bank.vaultBorrow(this.smallBorrowAmount, {from: _accounts[1]});
const collateralAmount = await this.bank.getVaultCollateralAmount({ from: _accounts[1] });
const debtAmount = await this.bank.getVaultDebtAmount({ from: _accounts[1] });
expect(collateralAmount).to.be.bignumber.equal(this.depositAmount);
const debtAmount = await this.bank.getVaultRepayAmount({ from: _accounts[1] });
expect(collateralAmount).to.be.bignumber.equal(this.depositAmount.mul(new BN(2)));
// Calculate borrowed amount, use pays origination fee on 2 borrows
var s_amount = new BN(this.smallBorrowAmount);
var s_amount = (new BN(this.smallBorrowAmount)).add(new BN(this.borrowAmount));
var b_amount = s_amount.add(s_amount.mul(new BN(ORIGINATION_FEE)).div(new BN(10000)));
var f_b_amount = b_amount.add(b_amount.mul(new BN(INTEREST_RATE)).div(new BN(10000)).div(new BN(365)));
f_b_amount = f_b_amount.add(b_amount.mul(new BN(INTEREST_RATE)).div(new BN(10000)).div(new BN(365)));
f_b_amount = f_b_amount.add(s_amount.mul(new BN(ORIGINATION_FEE)).div(new BN(10000)));
f_b_amount = f_b_amount.add(s_amount);
expect(debtAmount).to.be.bignumber.equal(f_b_amount.toString());

const collateralBalance = await this.ct.balanceOf(this.bank.address);
const debtBalance = await this.dt.balanceOf(this.bank.address);
expect(collateralBalance).to.be.bignumber.equal(this.depositAmount);
expect(debtBalance).to.be.bignumber.equal(ether(new BN(60)));
expect(collateralBalance).to.be.bignumber.equal((this.depositAmount.mul(new BN(2))));
expect(debtBalance).to.be.bignumber.equal((this.depositAmount.mul(new BN(2))).sub(s_amount));
});

it('should not allow the user to borrow above collateralization ratio', async function () {
Expand Down