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

Quote hash getter updates #576

Merged
merged 5 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ File | % Stmts | % Branch |
Helpers.sol | 100 | 50 | 100 | 100 | |
contracts\interfaces\ | 100 | 100 | 100 | 100 | |
IMysoTokenManager.sol | 100 | 100 | 100 | 100 | |
contracts\peer-to-peer\ | 99.72 | 94.74 | 98.72 | 98.75 | |
contracts\peer-to-peer\ | 99.73 | 94.74 | 98.78 | 98.76 | |
AddressRegistry.sol | 100 | 96.74 | 100 | 99.17 | 116 |
BorrowerGateway.sol | 98.57 | 90.91 | 90.91 | 96.97 | 241,317,358 |
DataTypesPeerToPeer.sol | 100 | 100 | 100 | 100 | |
LenderVaultFactory.sol | 100 | 87.5 | 100 | 100 | |
LenderVaultImpl.sol | 100 | 92.86 | 100 | 98.88 | 63,206 |
QuoteHandler.sol | 100 | 98.04 | 100 | 99.34 | 365 |
QuoteHandler.sol | 100 | 98.04 | 100 | 99.35 | 412 |
contracts\peer-to-peer\callbacks\ | 100 | 75 | 88.89 | 96.88 | |
BalancerV2Looping.sol | 100 | 100 | 100 | 100 | |
UniV3Looping.sol | 100 | 100 | 100 | 100 | |
Expand Down Expand Up @@ -245,6 +245,6 @@ File | % Stmts | % Branch |
IFundingPoolImpl.sol | 100 | 100 | 100 | 100 | |
ILoanProposalImpl.sol | 100 | 100 | 100 | 100 | |
---------------------------------------------------------|----------|----------|----------|----------|----------------|
All files | 98.99 | 88.83 | 98.63 | 96.8 | |
All files | 98.99 | 88.83 | 98.65 | 96.81 | |
---------------------------------------------------------|----------|----------|----------|----------|----------------|
```
2 changes: 1 addition & 1 deletion contracts/peer-to-peer/DataTypesPeerToPeer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ library DataTypesPeerToPeer {
uint256 tokenAmount;
}

struct QuoteHashAndValidDeadline {
struct OnChainQuoteInfo {
// hash of on chain quote
bytes32 quoteHash;
// valid until timestamp
Expand Down
4 changes: 4 additions & 0 deletions contracts/peer-to-peer/LenderVaultImpl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,10 @@ contract LenderVaultImpl is
return _loans.length;
}

function totalNumSigners() external view returns (uint256) {
return signers.length;
}

function getTokenBalancesAndLockedAmounts(
address[] calldata tokens
)
Expand Down
41 changes: 24 additions & 17 deletions contracts/peer-to-peer/QuoteHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ contract QuoteHandler is IQuoteHandler {
mapping(address => mapping(bytes32 => bool))
public offChainQuoteIsInvalidated;
mapping(address => mapping(bytes32 => bool)) public isOnChainQuote;
mapping(address => DataTypesPeerToPeer.QuoteHashAndValidDeadline[])
internal quoteHashesAndValidUntilTimestampsPerVault;
mapping(address => DataTypesPeerToPeer.OnChainQuoteInfo[])
internal onChainQuoteHistory;

constructor(address _addressRegistry) {
if (_addressRegistry == address(0)) {
Expand All @@ -43,10 +43,9 @@ contract QuoteHandler is IQuoteHandler {
if (isOnChainQuoteFromVault[onChainQuoteHash]) {
revert Errors.OnChainQuoteAlreadyAdded();
}
// note: in case of a vault re-adding a prior invalidated quote, this does create duplicate entry in array
// but that should be very rare and not worth tracking index with extra storage variable
quoteHashesAndValidUntilTimestampsPerVault[lenderVault].push(
DataTypesPeerToPeer.QuoteHashAndValidDeadline({
// @dev: on-chain quote history is append only
onChainQuoteHistory[lenderVault].push(
DataTypesPeerToPeer.OnChainQuoteInfo({
quoteHash: onChainQuoteHash,
validUntil: onChainQuote.generalQuoteInfo.validUntil
})
Expand Down Expand Up @@ -74,10 +73,9 @@ contract QuoteHandler is IQuoteHandler {
if (!isOnChainQuoteFromVault[oldOnChainQuoteHash]) {
revert Errors.UnknownOnChainQuote();
}
// note: in case of a vault re-adding a prior invalidated quote, this does create duplicate entry in array
// but that should be very rare and not worth tracking index with extra storage variable
quoteHashesAndValidUntilTimestampsPerVault[lenderVault].push(
DataTypesPeerToPeer.QuoteHashAndValidDeadline({
// @dev: on-chain quote history is append only
onChainQuoteHistory[lenderVault].push(
DataTypesPeerToPeer.OnChainQuoteInfo({
quoteHash: newOnChainQuoteHash,
validUntil: newOnChainQuote.generalQuoteInfo.validUntil
})
Expand Down Expand Up @@ -221,14 +219,23 @@ contract QuoteHandler is IQuoteHandler {
);
}

function getQuoteHashesAndValidUntilTimestampsPerVault(
function getOnChainQuoteHistory(
address lenderVault,
uint256 idx
) external view returns (DataTypesPeerToPeer.OnChainQuoteInfo memory) {
return onChainQuoteHistory[lenderVault][idx];
}
jpick713 marked this conversation as resolved.
Show resolved Hide resolved

function getFullOnChainQuoteHistory(
address lenderVault
) external view returns (DataTypesPeerToPeer.OnChainQuoteInfo[] memory) {
return onChainQuoteHistory[lenderVault];
}

function getOnChainQuoteHistoryLength(
address lenderVault
)
external
view
returns (DataTypesPeerToPeer.QuoteHashAndValidDeadline[] memory)
{
return quoteHashesAndValidUntilTimestampsPerVault[lenderVault];
) external view returns (uint256) {
return onChainQuoteHistory[lenderVault].length;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions contracts/peer-to-peer/interfaces/ILenderVaultImpl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,10 @@ interface ILenderVaultImpl {
* @return total number of loans
*/
function totalNumLoans() external view returns (uint256);

/**
* @notice function returns total number of signers
* @return total number of signers
*/
function totalNumSigners() external view returns (uint256);
}
30 changes: 23 additions & 7 deletions contracts/peer-to-peer/interfaces/IQuoteHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,30 @@ interface IQuoteHandler {
) external view returns (bool);

/**
* @notice function returns array of structs containing the on chain quote hash and validUntil timestamp
* @notice function returns element of on-chain history
* @param lenderVault address of vault
* @return array of quote hash and validUntil data for every on chain quote in a vault
* @return element of on-chain quote history
*/
function getQuoteHashesAndValidUntilTimestampsPerVault(
function getOnChainQuoteHistory(
address lenderVault,
uint256 idx
) external view returns (DataTypesPeerToPeer.OnChainQuoteInfo memory);

/**
* @notice function returns array of structs containing the on-chain quote hash and validUntil timestamp
* @param lenderVault address of vault
* @return array of quote hash and validUntil data for on-chain quote history of a vault
*/
function getFullOnChainQuoteHistory(
address lenderVault
) external view returns (DataTypesPeerToPeer.OnChainQuoteInfo[] memory);

/**
* @notice function returns the number of on-chain quotes that were added or updated
* @param lenderVault address of vault
* @return number of on-chain quotes that were added or updated
*/
function getOnChainQuoteHistoryLength(
address lenderVault
)
external
view
returns (DataTypesPeerToPeer.QuoteHashAndValidDeadline[] memory);
) external view returns (uint256);
}
9 changes: 8 additions & 1 deletion test/peer-to-peer/local-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ async function generateOffChainQuote({
expect(recoveredAddr).to.equal(signer.address)

// add signer
const preNumSigners = await lenderVault.totalNumSigners()
await lenderVault.connect(lender).addSigners([signer.address])
const postNumSigners = await lenderVault.totalNumSigners()
expect(postNumSigners.sub(preNumSigners)).to.be.equal(1)

// lender add sig to quote and pass to borrower
offChainQuote.compactSigs = customSignatures.length != 0 ? customSignatures : [compactSig]
Expand Down Expand Up @@ -3343,6 +3346,8 @@ describe('Peer-to-Peer: Local Tests', function () {
salt: ZERO_BYTES32
}

expect(await quoteHandler.getOnChainQuoteHistoryLength(lenderVault.address)).to.equal(0)

await expect(quoteHandler.connect(lender).addOnChainQuote(lenderVault.address, onChainQuote)).to.emit(
quoteHandler,
'OnChainQuoteAdded'
Expand All @@ -3354,9 +3359,11 @@ describe('Peer-to-Peer: Local Tests', function () {
'OnChainQuoteAdded'
)

const quoteHashAndValidUntilArr = await quoteHandler.getQuoteHashesAndValidUntilTimestampsPerVault(lenderVault.address)
const quoteHashAndValidUntilArr = await quoteHandler.getFullOnChainQuoteHistory(lenderVault.address)
const historyLen = await quoteHandler.getOnChainQuoteHistoryLength(lenderVault.address)

expect(quoteHashAndValidUntilArr.length).to.equal(2)
expect(quoteHashAndValidUntilArr.length).to.equal(historyLen)

await expect(
quoteHandler.connect(lender).deleteOnChainQuote(lenderVault.address, quoteHashAndValidUntilArr[0].quoteHash)
Expand Down
17 changes: 10 additions & 7 deletions test/peer-to-peer/mainnet-forked-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ describe('Peer-to-Peer: Forked Mainnet Tests', function () {
'OnChainQuoteAdded'
)

const quoteHashAndValidUntilArr = await quoteHandler.getQuoteHashesAndValidUntilTimestampsPerVault(lenderVault.address)
const quoteHashAndValidUntilArr = await quoteHandler.getFullOnChainQuoteHistory(lenderVault.address)

expect(quoteHashAndValidUntilArr.length).to.equal(2)

Expand Down Expand Up @@ -968,17 +968,15 @@ describe('Peer-to-Peer: Forked Mainnet Tests', function () {

expect(borrowQuoteAddedEvent).to.be.not.undefined

const quoteHashAndValidUntilArrAfterUpdate = await quoteHandler.getQuoteHashesAndValidUntilTimestampsPerVault(
lenderVault.address
)
const quoteHashAndValidUntilArrAfterUpdate = await quoteHandler.getFullOnChainQuoteHistory(lenderVault.address)

expect(quoteHashAndValidUntilArrAfterUpdate.length).to.equal(3)

await quoteHandler
.connect(lender)
.updateOnChainQuote(lenderVault.address, quoteHashAndValidUntilArrAfterUpdate[2].quoteHash, onChainQuote)

expect(await quoteHandler.getQuoteHashesAndValidUntilTimestampsPerVault(lenderVault.address)).to.have.lengthOf(4)
expect(await quoteHandler.getFullOnChainQuoteHistory(lenderVault.address)).to.have.lengthOf(4)

// borrower approves borrower gateway
await weth.connect(borrower).approve(borrowerGateway.address, MAX_UINT256)
Expand Down Expand Up @@ -1898,9 +1896,11 @@ describe('Peer-to-Peer: Forked Mainnet Tests', function () {
'OnChainQuoteAdded'
)

const quoteHashAndValidUntilArr = await quoteHandler.getQuoteHashesAndValidUntilTimestampsPerVault(lenderVault.address)

const quoteHashAndValidUntilArr = await quoteHandler.getFullOnChainQuoteHistory(lenderVault.address)
const onChainQuoteHistoryElem = await quoteHandler.getOnChainQuoteHistory(lenderVault.address, 0)
expect(quoteHashAndValidUntilArr.length).to.equal(1)
expect(quoteHashAndValidUntilArr[0].quoteHash).to.be.equal(onChainQuoteHistoryElem.quoteHash)
expect(quoteHashAndValidUntilArr[0].validUntil).to.be.equal(onChainQuoteHistoryElem.validUntil)

await expect(
quoteHandler.connect(lender).deleteOnChainQuote(borrower.address, quoteHashAndValidUntilArr[0].quoteHash)
Expand Down Expand Up @@ -5175,7 +5175,10 @@ describe('Peer-to-Peer: Forked Mainnet Tests', function () {
// lenderVault owner deposits usdc
await usdc.connect(lender).transfer(lenderVault.address, ONE_USDC.mul(100000))

const preTotalNumSigners = await lenderVault.numSigners()
await lenderVault.connect(lender).addSigners([team.address])
const postTotalNumSigners = await lenderVault.numSigners()
expect(postTotalNumSigners.sub(preTotalNumSigners)).to.be.equal(1)

// deploy chainlinkOracleContract
const usdcEthChainlinkAddr = '0x986b5e1e1755e3c2440e960477f25201b0a8bbd4'
Expand Down