Skip to content

Commit

Permalink
Merge pull request #611 from mysofinance/myso-token-manager-sig
Browse files Browse the repository at this point in the history
simplified cap handling for myso token manager and added test
  • Loading branch information
jpick713 authored Apr 16, 2024
2 parents 8c3e15e + 9a32576 commit 5bcdc63
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 5bcdc63

Please sign in to comment.