Skip to content

Commit

Permalink
added borrows and examined price along curve
Browse files Browse the repository at this point in the history
  • Loading branch information
jpick713 committed Apr 9, 2024
1 parent b29d0c3 commit 48a513a
Showing 1 changed file with 215 additions and 47 deletions.
262 changes: 215 additions & 47 deletions test/peer-to-peer/mainnet-myso-oracle-forked-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const ONE_WSTETH = ethers.BigNumber.from(10).pow(18)
const MAX_UINT128 = ethers.BigNumber.from(2).pow(128).sub(1)
const MAX_UINT256 = ethers.BigNumber.from(2).pow(256).sub(1)
const ONE_HOUR = ethers.BigNumber.from(60 * 60)
const ONE_DAY = ethers.BigNumber.from(60 * 60 * 24)
const ZERO_ADDR = '0x0000000000000000000000000000000000000000'
const ZERO_BYTES32 = ethers.utils.formatBytes32String('')

Expand Down Expand Up @@ -131,7 +132,7 @@ describe('Peer-to-Peer: Myso Recent Forked Mainnet Tests', function () {
const WETH_ADDRESS = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'
const weth = await ethers.getContractAt('IWETH', WETH_ADDRESS)
await ethers.provider.send('hardhat_setBalance', [borrower.address, '0x204FCE5E3E25026110000000'])
await weth.connect(borrower).deposit({ value: ONE_WETH.mul(1) })
await weth.connect(borrower).deposit({ value: ONE_WETH.mul(1000) })

//prepare wstEth balances
const WSTETH_ADDRESS = '0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0'
Expand Down Expand Up @@ -217,9 +218,9 @@ describe('Peer-to-Peer: Myso Recent Forked Mainnet Tests', function () {
[rethToEthChainlinkAddr, cbethToEthChainlinkAddr, usdcToEthChainlinkAddr, usdtToEthChainlinkAddr],
team.address,
testnetTokenManager.address,
62000000,
ethers.BigNumber.from('800000000000000000'),
1770,
55000000,
ethers.BigNumber.from('660000000000000000'),
1650,
1000
)
await mysoOracle.deployed()
Expand Down Expand Up @@ -291,76 +292,243 @@ describe('Peer-to-Peer: Myso Recent Forked Mainnet Tests', function () {
console.log(ethers.utils.formatUnits(methCollMysoLoanPrice, 18))
console.log(ethers.utils.formatUnits(usdcCollMysoLoanPrice, 18))
console.log(ethers.utils.formatUnits(usdtCollMysoLoanPrice, 18))
//console.log(ethers.utils.formatUnits(daiCollMysoLoanPrice, 18))
console.log(ethers.utils.formatUnits(mysoCollWethLoanPrice, 18))
console.log(ethers.utils.formatUnits(mysoCollWstEthLoanPrice, 18))
console.log(ethers.utils.formatUnits(mysoCollUsdcLoanPrice, 6))
console.log(ethers.utils.formatUnits(mysoCollUsdtLoanPrice, 6))
//console.log(ethers.utils.formatUnits(mysoCollDaiLoanPrice, 18))
}

await mysoOracle.connect(team).setMysoPriceParams(70000000, ethers.BigNumber.from('800000000000000000'), 1770, 1000)
const newMysoPriceData = await mysoOracle.mysoPriceParams()
expect(newMysoPriceData[0]).to.equal(70000000)
expect(newMysoPriceData[1]).to.equal(ethers.BigNumber.from('800000000000000000'))
expect(newMysoPriceData[2]).to.equal(1770)
expect(newMysoPriceData[3]).to.equal(1000)

await mysoOracle.connect(team).setMysoPriceParams(55000000, ethers.BigNumber.from('660000000000000000'), 1650, 1000)

const initialTotalMysoLoanAmount = await testnetTokenManager.totalMysoLoanAmount()
expect(initialTotalMysoLoanAmount).to.equal(0)

await dai.connect(team).transfer(lenderVault.address, ONE_MYSO.mul(20000000))

await addressRegistry.connect(team).setWhitelistState([mysoOracle.address], 2)

// lenderVault owner gives quote
const blocknum = await ethers.provider.getBlockNumber()
const timestamp = (await ethers.provider.getBlock(blocknum)).timestamp
let quoteTuples = [
{
loanPerCollUnitOrLtv: BASE,
interestRatePctInBase: 0,
upfrontFeePctInBase: 0,
tenor: ONE_DAY.mul(180)
}
]
let onChainQuote = {
generalQuoteInfo: {
collToken: weth.address,
loanToken: myso,
oracleAddr: mysoOracle.address,
minLoan: ONE_MYSO.mul(1000),
maxLoan: MAX_UINT256,
validUntil: timestamp + 600,
earliestRepayTenor: 0,
borrowerCompartmentImplementation: ZERO_ADDR,
isSingleUse: false,
whitelistAddr: ZERO_ADDR,
isWhitelistAddrSingleBorrower: false
},
quoteTuples: quoteTuples,
salt: ZERO_BYTES32
}
await addressRegistry.connect(team).setWhitelistState([weth.address, myso], 1)

await expect(quoteHandler.connect(lender).addOnChainQuote(lenderVault.address, onChainQuote)).to.emit(
quoteHandler,
'OnChainQuoteAdded'
)

// borrower approves borrower gateway
await weth.connect(borrower).approve(borrowerGateway.address, MAX_UINT256)

// borrow with on chain quote
const collSendAmount = ONE_WETH.mul(50)
const expectedProtocolAndVaultTransferFee = 0
const expectedCompartmentTransferFee = 0
const quoteTupleIdx = 0
const callbackAddr = ZERO_ADDR
const callbackData = ZERO_BYTES32

const borrowInstructions = {
collSendAmount,
expectedProtocolAndVaultTransferFee,
expectedCompartmentTransferFee,
deadline: MAX_UINT256,
minLoanAmount: 0,
callbackAddr,
callbackData,
mysoTokenManagerData: ZERO_BYTES32
}

const borrowWithOnChainQuoteTransaction = await borrowerGateway
.connect(borrower)
.borrowWithOnChainQuote(lenderVault.address, borrowInstructions, onChainQuote, quoteTupleIdx)

const borrowWithOnChainQuoteReceipt = await borrowWithOnChainQuoteTransaction.wait()

const borrowEvent = borrowWithOnChainQuoteReceipt.events?.find(x => {
return x.event === 'Borrowed'
})

const repayAmountFirst = borrowEvent?.args?.loan?.['initRepayAmount']
const initCollAmountFirst = borrowEvent?.args?.loan?.['initCollAmount']

const mysoLoanAmountFirstBorrow = await testnetTokenManager.totalMysoLoanAmount()

expect(repayAmountFirst).to.equal(mysoLoanAmountFirstBorrow)
expect(initCollAmountFirst).to.equal(collSendAmount)

console.log('mysoLoanAmountFirstBorrow', ethers.utils.formatUnits(mysoLoanAmountFirstBorrow, 18))

const wethCollMysoLoanPostFirstBorrowPrice = await mysoOracle.getPrice(weth.address, myso)
const mysoCollUsdcLoanPostFirstBorrowPrice = await mysoOracle.getPrice(myso, usdc)
const usdcCollMysoLoanPostFirstBorrowPrice = await mysoOracle.getPrice(usdc, myso)
expect(wethCollMysoLoanPostFirstBorrowPrice).to.be.lt(wethCollMysoLoanPrice)
expect(usdcCollMysoLoanPostFirstBorrowPrice).to.be.lt(usdcCollMysoLoanPrice)
if (showLogs) {
console.log(
'usdcCollMysoLoanPostFirstBorrowPrice',
ethers.utils.formatUnits(usdcCollMysoLoanPostFirstBorrowPrice, 18)
)
console.log('usdcCollMysoLoanPrice', ethers.utils.formatUnits(usdcCollMysoLoanPrice, 18))
console.log(
'mysoCollUsdcLoanPostFirstBorrowPrice',
ethers.utils.formatUnits(mysoCollUsdcLoanPostFirstBorrowPrice, 6)
)
console.log('mysoCollUsdcLoanPrice', ethers.utils.formatUnits(mysoCollUsdcLoanPrice, 6))
}

const wethCollMysoLoanPostPrice = await mysoOracle.getPrice(weth.address, myso)
const wstEthCollMysoLoanPostPrice = await mysoOracle.getPrice(wsteth.address, myso)
const rethCollMysoLoanPostPrice = await mysoOracle.getPrice(reth, myso)
const cbethCollMysoLoanPostPrice = await mysoOracle.getPrice(cbeth, myso)
const rplCollMysoLoanPostPrice = await mysoOracle.getPrice(rpl, myso)
const methCollMysoLoanPostPrice = await mysoOracle.getPrice(meth, myso)
const usdcCollMysoLoanPostPrice = await mysoOracle.getPrice(usdc, myso)
const ankrethCollMysoLoanPostPrice = await mysoOracle.getPrice(ankreth, myso)
const mysoCollWethLoanPostPrice = await mysoOracle.getPrice(myso, weth.address)
const mysoCollWstEthLoanPostPrice = await mysoOracle.getPrice(myso, wsteth.address)
const mysoCollUsdcLoanPostPrice = await mysoOracle.getPrice(myso, usdc)
const mysoCollUsdtLoanPostPrice = await mysoOracle.getPrice(myso, usdt)
//const mysoCollDaiLoanPostPrice = await mysoOracle.getPrice(myso, dai)
const secondBorrowTransaction = await borrowerGateway
.connect(borrower)
.borrowWithOnChainQuote(lenderVault.address, borrowInstructions, onChainQuote, quoteTupleIdx)

const secondBorrowReceipt = await secondBorrowTransaction.wait()

const secondBorrowEvent = secondBorrowReceipt.events?.find(x => {
return x.event === 'Borrowed'
})

const repayAmountSecond = secondBorrowEvent?.args?.loan?.['initRepayAmount']

const mysoLoanAmountSecondBorrow = await testnetTokenManager.totalMysoLoanAmount()

expect(repayAmountSecond).to.equal(mysoLoanAmountSecondBorrow.sub(mysoLoanAmountFirstBorrow))

console.log('mysoLoanAmountSecondBorrow', ethers.utils.formatUnits(mysoLoanAmountSecondBorrow, 18))

const mysoCollUsdcLoanPostSecondBorrowPrice = await mysoOracle.getPrice(myso, usdc)
const usdcCollMysoLoanPostSecondBorrowPrice = await mysoOracle.getPrice(usdc, myso)
if (showLogs) {
console.log(
'wethCollMysoLoanPostPrice',
Math.round(1000000 * Number(ethers.utils.formatUnits(wethCollMysoLoanPostPrice, 18).slice(0, 8))) / 1000000
'usdcCollMysoLoanPostSecondBorrowPrice',
ethers.utils.formatUnits(usdcCollMysoLoanPostSecondBorrowPrice, 18)
)
console.log(
'usdcCollMysoLoanPostFirstBorrowPrice',
ethers.utils.formatUnits(usdcCollMysoLoanPostFirstBorrowPrice, 18)
)
console.log(
'wstEthCollMysoLoanPostPrice',
Math.round(1000000 * Number(ethers.utils.formatUnits(wstEthCollMysoLoanPostPrice, 18).slice(0, 8))) / 1000000
'mysoCollUsdcLoanPostSecondBorrowPrice',
ethers.utils.formatUnits(mysoCollUsdcLoanPostSecondBorrowPrice, 6)
)
console.log(
'rethCollMysoLoanPostPrice',
Math.round(1000000 * Number(ethers.utils.formatUnits(rethCollMysoLoanPostPrice, 18).slice(0, 8))) / 1000000
'mysoCollUsdcLoanPostFirstBorrowPrice',
ethers.utils.formatUnits(mysoCollUsdcLoanPostFirstBorrowPrice, 6)
)
}

const largeBorrowInstructions = {
...borrowInstructions,
collSendAmount: ONE_WETH.mul(250)
}

const thirdBorrowTransaction = await borrowerGateway
.connect(borrower)
.borrowWithOnChainQuote(lenderVault.address, largeBorrowInstructions, onChainQuote, quoteTupleIdx)

const thirdBorrowReceipt = await thirdBorrowTransaction.wait()

const thirdBorrowEvent = thirdBorrowReceipt.events?.find(x => {
return x.event === 'Borrowed'
})

const repayAmountThird = thirdBorrowEvent?.args?.loan?.['initRepayAmount']

const mysoLoanAmountThirdBorrow = await testnetTokenManager.totalMysoLoanAmount()

expect(repayAmountThird).to.equal(mysoLoanAmountThirdBorrow.sub(mysoLoanAmountSecondBorrow))

console.log('mysoLoanAmountThirdBorrow', ethers.utils.formatUnits(mysoLoanAmountThirdBorrow, 18))

const mysoCollUsdcLoanPostThirdBorrowPrice = await mysoOracle.getPrice(myso, usdc)

if (showLogs) {
console.log(
'mysoCollUsdcLoanPostThirdBorrowPrice',
ethers.utils.formatUnits(mysoCollUsdcLoanPostThirdBorrowPrice, 6)
)
console.log(
'mysoCollUsdcLoanPostSecondBorrowPrice',
ethers.utils.formatUnits(mysoCollUsdcLoanPostSecondBorrowPrice, 6)
)
}

const fourthBorrowTransaction = await borrowerGateway
.connect(borrower)
.borrowWithOnChainQuote(lenderVault.address, largeBorrowInstructions, onChainQuote, quoteTupleIdx)

const fourthBorrowReceipt = await fourthBorrowTransaction.wait()

const fourthBorrowEvent = fourthBorrowReceipt.events?.find(x => {
return x.event === 'Borrowed'
})

const repayAmountFourth = fourthBorrowEvent?.args?.loan?.['initRepayAmount']

const mysoLoanAmountFourthBorrow = await testnetTokenManager.totalMysoLoanAmount()

expect(repayAmountFourth).to.equal(mysoLoanAmountFourthBorrow.sub(mysoLoanAmountThirdBorrow))

console.log('mysoLoanAmountFourthBorrow', ethers.utils.formatUnits(mysoLoanAmountFourthBorrow, 18))

const mysoCollUsdcLoanPostFourthBorrowPrice = await mysoOracle.getPrice(myso, usdc)
const wethCollMysoLoanPostFourthBorrowPrice = await mysoOracle.getPrice(weth.address, myso)
const wstEthCollMysoLoanPostFourthBorrowPrice = await mysoOracle.getPrice(wsteth.address, myso)
const rplCollMysoLoanPostFourthBorrowPrice = await mysoOracle.getPrice(rpl, myso)
const methCollMysoLoanPostFourthBorrowPrice = await mysoOracle.getPrice(meth, myso)

if (showLogs) {
console.log(
'cbEthCollMysoLoanPostPrice',
Math.round(1000000 * Number(ethers.utils.formatUnits(cbethCollMysoLoanPostPrice, 18).slice(0, 8))) / 1000000
'mysoCollUsdcLoanPostFourthBorrowPrice',
ethers.utils.formatUnits(mysoCollUsdcLoanPostFourthBorrowPrice, 6)
)
console.log(
'rplCollMysoLoanPostPrice',
Math.round(1000000 * Number(ethers.utils.formatUnits(rplCollMysoLoanPostPrice, 18).slice(0, 8))) / 1000000
'wethCollMysoLoanPostFourthBorrowPrice',
ethers.utils.formatUnits(wethCollMysoLoanPostFourthBorrowPrice, 18)
)
console.log(
'methCollMysoLoanPostPrice',
Math.round(1000000 * Number(ethers.utils.formatUnits(methCollMysoLoanPostPrice, 18).slice(0, 8))) / 1000000
'wstEthCollMysoLoanPostFourthBorrowPrice',
ethers.utils.formatUnits(wstEthCollMysoLoanPostFourthBorrowPrice, 18)
)
console.log(
'usdcCollMysoLoanPostPrice',
Math.round(1000000 * Number(ethers.utils.formatUnits(usdcCollMysoLoanPostPrice, 18).slice(0, 8))) / 1000000
'rplCollMysoLoanPostFourthBorrowPrice',
ethers.utils.formatUnits(rplCollMysoLoanPostFourthBorrowPrice, 18)
)
console.log(
'ankrethCollMysoLoanPostPrice',
Math.round(1000000 * Number(ethers.utils.formatUnits(ankrethCollMysoLoanPostPrice, 18).slice(0, 8))) / 1000000
'methCollMysoLoanPostFourthBorrowPrice',
ethers.utils.formatUnits(methCollMysoLoanPostFourthBorrowPrice, 18)
)
console.log(ethers.utils.formatUnits(wethCollMysoLoanPostPrice, 18))
console.log(ethers.utils.formatUnits(wstEthCollMysoLoanPostPrice, 18))
console.log(ethers.utils.formatUnits(rethCollMysoLoanPostPrice, 18))
console.log(ethers.utils.formatUnits(cbethCollMysoLoanPostPrice, 18))
console.log(ethers.utils.formatUnits(rplCollMysoLoanPostPrice, 18))
console.log(ethers.utils.formatUnits(methCollMysoLoanPostPrice, 18))
console.log(ethers.utils.formatUnits(usdcCollMysoLoanPostPrice, 18))
console.log(ethers.utils.formatUnits(mysoCollWethLoanPostPrice, 18))
console.log(ethers.utils.formatUnits(mysoCollWstEthLoanPostPrice, 18))
console.log(ethers.utils.formatUnits(mysoCollUsdcLoanPostPrice, 6))
console.log(ethers.utils.formatUnits(mysoCollUsdtLoanPostPrice, 6))
//console.log(ethers.utils.formatUnits(mysoCollDaiLoanPostPrice, 18))
}
})
})
Expand Down

0 comments on commit 48a513a

Please sign in to comment.