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

Procedural CRA #435

Draft
wants to merge 9 commits into
base: feature/zk-cra
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 0 additions & 5 deletions contracts/market/CreateLoanFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ import {
} from "../storage/market.sol";
import { AppStorageLib } from "../storage/app.sol";

// hardhat helpers
import "hardhat/console.sol";

contract CreateLoanFacet is RolesMods, ReentryMods, PausableMods {
/**
* @notice This event is emitted when a loan has been successfully taken out
Expand Down Expand Up @@ -237,8 +234,6 @@ library CreateLoanLib {
request.request.duration,
"Teller: max loan duration exceeded"
);

// address of processRequestLib: 0xe7168c514A022345ed07E4Fad73eC3921C2b7bDb
// Get consensus values from request
(uint16 interestRate, uint16 collateralRatio, uint256 maxLoanAmount) =
ProcessRequestLib.processMarketRequest(request);
Expand Down
14 changes: 9 additions & 5 deletions contracts/market/ProviderFactoryFacet.sol
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;

import { Provider } from "./cra/Provider.sol";
import { DataProvider } from "./cra/DataProvider.sol";

contract ProviderFactoryFacet {
address private admin;
Provider[] private providers;
address public admin;
DataProvider[] public providers;

modifier onlyAdmin() {
require(admin == msg.sender, "Only the admin can call this!");
_;
}

// whichever address that deployst the provider factory facet is the admin of the factory
// whichever address that deploys the provider factory facet is the admin of the factory
constructor() {
admin = msg.sender;
}
Expand All @@ -22,7 +22,11 @@ contract ProviderFactoryFacet {
* admin of the said provider.
*/
function createProvider() public {
Provider provider = new Provider();
DataProvider provider = new DataProvider(msg.sender);
providers.push(provider);
}

function getProviders() public view returns (DataProvider[] memory) {
return providers;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;

contract Provider {
contract DataProvider {
mapping(address => bool) public admins;
mapping(address => bool) public signers;

Expand All @@ -10,8 +10,8 @@ contract Provider {
_;
}

constructor() {
admins[msg.sender] = true;
constructor(address initAdmin) {
admins[initAdmin] = true;
}

/**
Expand Down
38 changes: 22 additions & 16 deletions contracts/market/cra/ProcessRequestLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { MarketHandler } from "../cra/market-handler/MarketHandler.sol";
import { LibLoans } from "../libraries/LibLoans.sol";
import { Verifier } from "../cra/verifier.sol";
import { ECDSA } from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import { DataProvider } from "./DataProvider.sol";

library ProcessRequestLib {
/**
Expand Down Expand Up @@ -43,8 +44,12 @@ library ProcessRequestLib {
Verifier.verifyTx(request.snarkProof, request.snarkWitnesses),
"Proof not verified"
);

// signatures length
uint8 signaturesLength = marketHandler.numberOfSignaturesRequired();

// get variable amount of commitments from market handler
bytes32[] memory commitments = new bytes32[](0);
bytes32[] memory commitments = new bytes32[](signaturesLength);

// constructing our commitments to verify with our signature data
for (uint8 i = 0; i < commitments.length; i++) {
Expand All @@ -69,12 +74,14 @@ library ProcessRequestLib {
_verifySignatures(
commitments,
request.dataProviderSignatures,
request.marketHandlerAddress
request.marketHandlerAddress,
request.providers
);

// The second witness item (after identifier) is the market
// score
uint256 marketScore = uint256(request.snarkWitnesses[1]);
require(marketScore > 5, "Teller: market score not high enough");

// Let the market handle the loan request and disperse the loan.

Expand All @@ -86,20 +93,18 @@ library ProcessRequestLib {
marketScore,
request
);
interestRate = 1000;
collateralRatio = 15000;
maxLoanAmount = 25000;
return (interestRate, collateralRatio, maxLoanAmount);
}

function _verifySignatures(
bytes32[] memory commitments,
DataProviderSignature[] memory signatureData,
address marketHandlerAddress
address marketHandlerAddress,
address[] memory providers
) private {
MarketHandler marketHandler = MarketHandler(marketHandlerAddress);
for (uint256 i = 0; i < signatureData.length; i++) {
bytes32 providerId = bytes32(i);
for (uint256 i = 0; i < commitments.length; i++) {
address providerAddress = providers[i];
require(
signatureData[i].signedAt > block.timestamp - 5 days,
"Signed at less than max age"
Expand All @@ -113,7 +118,7 @@ library ProcessRequestLib {
_validateSignature(
signatureData[i].signature,
commitments[i],
providerId
providerAddress
);
}
}
Expand All @@ -122,13 +127,13 @@ library ProcessRequestLib {
* @notice It validates whether a signature is valid or not.
* @param signature signature to validate.
* @param commitment used to recover the signer.
* @param providerId the expected signer address.
* @param providerAddress the provider address to check for the recovered signer.
*/
function _validateSignature(
Signature memory signature,
bytes32 commitment,
bytes32 providerId
) private pure {
address providerAddress
) private view {
address recoveredSigner =
ECDSA.recover(
keccak256(
Expand All @@ -141,9 +146,10 @@ library ProcessRequestLib {
signature.r,
signature.s
);
// require(
// MarketLib.p(providerId).signer[recoveredSigner],
// "Teller: not valid signature"
// );
DataProvider provider = DataProvider(providerAddress);
require(
provider.signers(recoveredSigner),
"Teller: not valid signature"
);
}
}
9 changes: 4 additions & 5 deletions contracts/market/cra/market-handler/MarketHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity ^0.8.3;

import { LoanRequest } from "../../../storage/market.sol";
import { Provider } from "../Provider.sol";
import { DataProvider } from "../DataProvider.sol";
import {
EnumerableSet
} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
Expand All @@ -21,6 +21,7 @@ abstract contract MarketHandler {

// signature related variables
mapping(bytes32 => bool) public usedCommitments;
mapping(bytes32 => uint16) public stateCappedInterestRate;
EnumerableSet.AddressSet internal providers;

modifier onlyAdmin() {
Expand All @@ -32,13 +33,11 @@ abstract contract MarketHandler {
uint16 maxInterestRate_,
uint16 maxCollateralRatio_,
uint256 maxLoanAmount_
) // provider addresses
{
) {
admins[msg.sender] = true;
maxInterestRate = maxInterestRate_;
maxCollateralRatio = maxCollateralRatio_;
maxLoanAmount = maxLoanAmount_;
// addProviders(addresses)
}

/**
Expand All @@ -60,7 +59,7 @@ abstract contract MarketHandler {
uint256 userLoanAmount
);

function addCommitment(bytes32 commitment) public onlyAdmin {
function addCommitment(bytes32 commitment) public {
usedCommitments[commitment] = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pragma solidity ^0.8.3;

import { LoanRequest } from "../../../storage/market.sol";
import { Provider } from "../Provider.sol";
import {
EnumerableSet
} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/market/libraries/MarketLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import {
} from "../../storage/market.sol";
import { Verifier } from "../cra/verifier.sol";
import { LibLoans } from "../libraries/LibLoans.sol";

// import reentry guard
import "hardhat/console.sol";

library MarketLib {
// function s() internal pure returns (MarketStorage storage s_) {
Expand Down
1 change: 1 addition & 0 deletions contracts/storage/market.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ struct LoanRequest {
Verifier.Proof snarkProof;
uint256[26] snarkWitnesses;
DataProviderSignature[] dataProviderSignatures;
address[] providers;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,7 @@ export default <HardhatUserConfig>{
timeout: 10000000,
}),
},
mocha: {},
mocha: {
timeout: 1000000000,
},
}
Loading