Skip to content

Commit

Permalink
Merge pull request #576 from mysofinance/quote-hash-getter-updates
Browse files Browse the repository at this point in the history
Quote hash getter updates
  • Loading branch information
jpick713 authored Jul 21, 2023
2 parents ddbb0df + 102b2ef commit 5cd4ed6
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 96 deletions.
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 | |
---------------------------------------------------------|----------|----------|----------|----------|----------------|
```
7 changes: 7 additions & 0 deletions contracts/peer-to-peer/DataTypesPeerToPeer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ library DataTypesPeerToPeer {
uint256 tokenAmount;
}

struct OnChainQuoteInfo {
// hash of on chain quote
bytes32 quoteHash;
// valid until timestamp
uint256 validUntil;
}

enum WhitelistState {
// not whitelisted
NOT_WHITELISTED,
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
45 changes: 41 additions & 4 deletions contracts/peer-to-peer/QuoteHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ contract QuoteHandler is IQuoteHandler {
mapping(address => mapping(bytes32 => bool))
public offChainQuoteIsInvalidated;
mapping(address => mapping(bytes32 => bool)) public isOnChainQuote;
mapping(address => DataTypesPeerToPeer.OnChainQuoteInfo[])
internal onChainQuoteHistory;

constructor(address _addressRegistry) {
if (_addressRegistry == address(0)) {
Expand All @@ -41,13 +43,20 @@ contract QuoteHandler is IQuoteHandler {
if (isOnChainQuoteFromVault[onChainQuoteHash]) {
revert Errors.OnChainQuoteAlreadyAdded();
}
// @dev: on-chain quote history is append only
onChainQuoteHistory[lenderVault].push(
DataTypesPeerToPeer.OnChainQuoteInfo({
quoteHash: onChainQuoteHash,
validUntil: onChainQuote.generalQuoteInfo.validUntil
})
);
isOnChainQuoteFromVault[onChainQuoteHash] = true;
emit OnChainQuoteAdded(lenderVault, onChainQuote, onChainQuoteHash);
}

function updateOnChainQuote(
address lenderVault,
DataTypesPeerToPeer.OnChainQuote calldata oldOnChainQuote,
bytes32 oldOnChainQuoteHash,
DataTypesPeerToPeer.OnChainQuote calldata newOnChainQuote
) external {
_checkIsRegisteredVaultAndSenderIsOwner(lenderVault);
Expand All @@ -56,7 +65,6 @@ contract QuoteHandler is IQuoteHandler {
}
mapping(bytes32 => bool)
storage isOnChainQuoteFromVault = isOnChainQuote[lenderVault];
bytes32 oldOnChainQuoteHash = _hashOnChainQuote(oldOnChainQuote);
bytes32 newOnChainQuoteHash = _hashOnChainQuote(newOnChainQuote);
// this check will catch the case where the old quote is the same as the new quote
if (isOnChainQuoteFromVault[newOnChainQuoteHash]) {
Expand All @@ -65,6 +73,13 @@ contract QuoteHandler is IQuoteHandler {
if (!isOnChainQuoteFromVault[oldOnChainQuoteHash]) {
revert Errors.UnknownOnChainQuote();
}
// @dev: on-chain quote history is append only
onChainQuoteHistory[lenderVault].push(
DataTypesPeerToPeer.OnChainQuoteInfo({
quoteHash: newOnChainQuoteHash,
validUntil: newOnChainQuote.generalQuoteInfo.validUntil
})
);
isOnChainQuoteFromVault[oldOnChainQuoteHash] = false;
emit OnChainQuoteDeleted(lenderVault, oldOnChainQuoteHash);

Expand All @@ -78,12 +93,11 @@ contract QuoteHandler is IQuoteHandler {

function deleteOnChainQuote(
address lenderVault,
DataTypesPeerToPeer.OnChainQuote calldata onChainQuote
bytes32 onChainQuoteHash
) external {
_checkIsRegisteredVaultAndSenderIsOwner(lenderVault);
mapping(bytes32 => bool)
storage isOnChainQuoteFromVault = isOnChainQuote[lenderVault];
bytes32 onChainQuoteHash = _hashOnChainQuote(onChainQuote);
if (!isOnChainQuoteFromVault[onChainQuoteHash]) {
revert Errors.UnknownOnChainQuote();
}
Expand Down Expand Up @@ -205,6 +219,29 @@ contract QuoteHandler is IQuoteHandler {
);
}

function getOnChainQuoteHistory(
address lenderVault,
uint256 idx
) external view returns (DataTypesPeerToPeer.OnChainQuoteInfo memory) {
if (idx < onChainQuoteHistory[lenderVault].length) {
return onChainQuoteHistory[lenderVault][idx];
} else {
revert Errors.InvalidArrayIndex();
}
}

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

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

/**
* @dev The passed signatures must be sorted such that recovered addresses are increasing.
*/
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);
}
36 changes: 32 additions & 4 deletions contracts/peer-to-peer/interfaces/IQuoteHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,24 @@ interface IQuoteHandler {
* @notice function updates on chain quote
* @dev function can only be called by vault owner
* @param lenderVault address of the vault updating quote
* @param oldOnChainQuote data for the old onChain quote (See notes in DataTypesPeerToPeer.sol)
* @param oldOnChainQuoteHash quote hash for the old onChain quote marked for deletion
* @param newOnChainQuote data for the new onChain quote (See notes in DataTypesPeerToPeer.sol)
*/
function updateOnChainQuote(
address lenderVault,
DataTypesPeerToPeer.OnChainQuote calldata oldOnChainQuote,
bytes32 oldOnChainQuoteHash,
DataTypesPeerToPeer.OnChainQuote calldata newOnChainQuote
) external;

/**
* @notice function deletes on chain quote
* @dev function can only be called by vault owner
* @param lenderVault address of the vault deleting
* @param onChainQuote data for the onChain quote marked for deletion (See notes in DataTypesPeerToPeer.sol)
* @param onChainQuoteHash quote hash for the onChain quote marked for deletion
*/
function deleteOnChainQuote(
address lenderVault,
DataTypesPeerToPeer.OnChainQuote calldata onChainQuote
bytes32 onChainQuoteHash
) external;

/**
Expand Down Expand Up @@ -163,4 +163,32 @@ interface IQuoteHandler {
address lenderVault,
bytes32 hashToCheck
) external view returns (bool);

/**
* @notice function returns element of on-chain history
* @param lenderVault address of vault
* @return element of on-chain quote history
*/
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 (uint256);
}
18 changes: 14 additions & 4 deletions 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,10 +3359,15 @@ describe('Peer-to-Peer: Local Tests', function () {
'OnChainQuoteAdded'
)

await expect(quoteHandler.connect(lender).deleteOnChainQuote(lenderVault.address, onChainQuote)).to.emit(
quoteHandler,
'OnChainQuoteDeleted'
)
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)
).to.emit(quoteHandler, 'OnChainQuoteDeleted')
})
})

Expand Down
Loading

0 comments on commit 5cd4ed6

Please sign in to comment.