Skip to content

Commit

Permalink
Merge branch 'myso-oracle' of github.com:mysofinance/v2 into myso-oracle
Browse files Browse the repository at this point in the history
  • Loading branch information
jpick713 committed Apr 22, 2024
2 parents d2e3756 + 5bcdc63 commit fd1bffc
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 88 deletions.
88 changes: 0 additions & 88 deletions contracts/tokenManager/MysoTokenManagerArbitrum.sol

This file was deleted.

69 changes: 69 additions & 0 deletions contracts/tokenManager/MysoTokenManagerWithCaps.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity 0.8.19;

import {MysoTokenManager} from "./MysoTokenManager.sol";
import {DataTypesPeerToPeer} from "../peer-to-peer/DataTypesPeerToPeer.sol";

contract MysoTokenManagerWithCaps is MysoTokenManager {
struct CollatCapInfo {
address token;
uint128 maxPledge;
}

mapping(address => uint256) public collatCaps;
mapping(address => uint256) public totalPledged;

event CollatCapsSet(CollatCapInfo[] collatCaps);

constructor(
address _mysoIOOVault,
address _mysoToken,
address _stMysoToken,
uint256 _minMysoWeight,
address _signer,
CollatCapInfo[] memory _collatCaps
)
MysoTokenManager(
_mysoIOOVault,
_mysoToken,
_stMysoToken,
_minMysoWeight,
_signer
)
{
for (uint256 i = 0; i < _collatCaps.length; i++) {
collatCaps[_collatCaps[i].token] = _collatCaps[i].maxPledge;
}
}

function setCollatCaps(CollatCapInfo[] calldata _collatCaps) external {
_checkOwner();
for (uint256 i = 0; i < _collatCaps.length; i++) {
collatCaps[_collatCaps[i].token] = _collatCaps[i].maxPledge;
}
emit CollatCapsSet(_collatCaps);
}

function _isAllowed(
bool isMysoIoo,
DataTypesPeerToPeer.BorrowTransferInstructions
calldata borrowInstructions,
DataTypesPeerToPeer.Loan calldata loan
) internal override returns (bool isAllowed) {
isAllowed = super._isAllowed(isMysoIoo, borrowInstructions, loan);
if (!isAllowed) {
return isAllowed;
}
if (isMysoIoo) {
uint256 _newPledged = totalPledged[loan.collToken] +
loan.initCollAmount;
isAllowed = _newPledged <= collatCaps[loan.collToken];
if (isAllowed) {
totalPledged[loan.collToken] = _newPledged;
}
} else {
isAllowed = true;
}
}
}
75 changes: 75 additions & 0 deletions test/peer-to-peer/local-tests-tokenmanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,5 +393,80 @@ describe('Peer-to-Peer: Local Tests', function () {
expect(preBalTokenManager.sub(postBalTokenManager)).to.be.equal(postBalDeployer.sub(preBalDeployer))
expect(postBalTokenManager).to.be.equal(0)
})

it('Should process caps correctly', async function () {
const {
deployer,
borrower1,
borrower2,
addressRegistry,
borrowerGateway,
quoteHandler,
mysoTokenManager,
iooVault,
usdc,
myt,
stmyt,
iooOnChainQuote
} = await setupTest()

// deploy token manager with pledge caps
const MysoTokenManagerWithCaps = await ethers.getContractFactory('MysoTokenManagerWithCaps')
const collatCaps = [{token: usdc.address, maxPledge: ONE_USDC.mul(10000)}]
const mysoTokenManagerWithCaps = await MysoTokenManagerWithCaps.connect(deployer).deploy(iooVault.address, myt.address, stmyt.address, 0, ethers.constants.AddressZero, collatCaps)
await mysoTokenManagerWithCaps.deployed()

// whitelist new myso token manager
await addressRegistry.connect(deployer).setWhitelistState([mysoTokenManager.address], 0)
await addressRegistry.connect(deployer).setWhitelistState([mysoTokenManagerWithCaps.address], 9)

// borrowers approves gateway
await usdc.connect(borrower1).approve(borrowerGateway.address, MAX_UINT256)
await usdc.connect(borrower2).approve(borrowerGateway.address, MAX_UINT256)

// borrow initiation
const collSendAmount = ONE_USDC.mul(4000)
const expectedProtocolAndVaultTransferFee = 0
const expectedCompartmentTransferFee = 0
const quoteTupleIdx = 0
const callbackAddr = ZERO_ADDRESS
const callbackData = ZERO_BYTES32
const borrowInstructions = {
collSendAmount,
expectedProtocolAndVaultTransferFee,
expectedCompartmentTransferFee,
deadline: MAX_UINT256,
minLoanAmount: 0,
callbackAddr,
callbackData,
mysoTokenManagerData: ZERO_BYTES32
}

// check 2 borrows should work
borrowInstructions.mysoTokenManagerData = ZERO_BYTES32
await borrowerGateway
.connect(borrower2)
.borrowWithOnChainQuote(iooVault.address, borrowInstructions, iooOnChainQuote, quoteTupleIdx)
await borrowerGateway
.connect(borrower2)
.borrowWithOnChainQuote(iooVault.address, borrowInstructions, iooOnChainQuote, quoteTupleIdx)
await expect(borrowerGateway
.connect(borrower2)
.borrowWithOnChainQuote(iooVault.address, borrowInstructions, iooOnChainQuote, quoteTupleIdx)).to.be.revertedWithCustomError(mysoTokenManagerWithCaps, "NotAllowed")

// set cap higher
const newCollatCaps = [{token: usdc.address, maxPledge: ONE_USDC.mul(12000)}]
await mysoTokenManagerWithCaps.setCollatCaps(newCollatCaps)

// now borrow works again
await borrowerGateway
.connect(borrower2)
.borrowWithOnChainQuote(iooVault.address, borrowInstructions, iooOnChainQuote, quoteTupleIdx)

// next one fails again
await expect(borrowerGateway
.connect(borrower2)
.borrowWithOnChainQuote(iooVault.address, borrowInstructions, iooOnChainQuote, quoteTupleIdx)).to.be.revertedWithCustomError(mysoTokenManagerWithCaps, "NotAllowed")
})
})
})

0 comments on commit fd1bffc

Please sign in to comment.