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

Removes use of User Registry in Preconfirmations flow #64

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 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
28 changes: 15 additions & 13 deletions contracts/PreConfirmations.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: BSL 1.1
pragma solidity ^0.8.15;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

Expand Down Expand Up @@ -46,6 +48,8 @@ contract PreConfCommitmentStore is Ownable {
/// @dev Address of userRegistry
IUserRegistry public userRegistry;

IERC20 public nativeToken;

/// @dev Mapping from provider to commitments count
mapping(address => uint256) public commitmentsCount;

Expand Down Expand Up @@ -114,11 +118,13 @@ contract PreConfCommitmentStore is Ownable {
address _providerRegistry,
address _userRegistry,
address _oracle,
address _owner
address _owner,
address _nativeToken
) {
oracle = _oracle;
providerRegistry = IProviderRegistry(_providerRegistry);
userRegistry = IUserRegistry(_userRegistry);
nativeToken = IERC20(_nativeToken);
_transferOwnership(_owner);

// EIP-712 domain separator
Expand Down Expand Up @@ -165,6 +171,7 @@ contract PreConfCommitmentStore is Ownable {
);
}


/**
* @dev Gives digest to be signed for pre confirmation
* @param _txnHash transaction Hash.
Expand Down Expand Up @@ -207,7 +214,7 @@ contract PreConfCommitmentStore is Ownable {
* @param bidSignature bid signature.
* @return messageDigest returns the bid hash for given bid id.
* @return recoveredAddress the address from the bid hash.
* @return stake the stake amount of the address for bid id user.
* @return allowance the amount extractable from the address of bid id user.
*/
function verifyBid(
uint64 bid,
Expand All @@ -217,12 +224,12 @@ contract PreConfCommitmentStore is Ownable {
)
public
view
returns (bytes32 messageDigest, address recoveredAddress, uint256 stake)
returns (bytes32 messageDigest, address recoveredAddress, uint256 allowance)
{
messageDigest = getBidHash(txnHash, bid, blockNumber);
recoveredAddress = messageDigest.recover(bidSignature);
stake = userRegistry.checkStake(recoveredAddress);
require(stake > (10 * bid), "Invalid bid");
allowance = nativeToken.allowance(recoveredAddress, address(this));
require(allowance >= bid, "Insufficient allowance");
}

/**
Expand Down Expand Up @@ -286,7 +293,7 @@ contract PreConfCommitmentStore is Ownable {
bytes calldata bidSignature,
bytes memory commitmentSignature
) public returns (bytes32 commitmentIndex) {
(bytes32 bHash, address bidderAddress, uint256 stake) = verifyBid(
(bytes32 bHash, address bidderAddress, uint256 allowance) = verifyBid(
bid,
blockNumber,
txnHash,
Expand All @@ -304,7 +311,7 @@ contract PreConfCommitmentStore is Ownable {

address commiterAddress = preConfHash.recover(commitmentSignature);

require(stake > (10 * bid), "Stake too low");
require(allowance > bid, "Stake too low");
ckartik marked this conversation as resolved.
Show resolved Hide resolved

PreConfCommitment memory newCommitment = PreConfCommitment(
false,
Expand All @@ -321,7 +328,6 @@ contract PreConfCommitmentStore is Ownable {

commitmentIndex = getCommitmentIndex(newCommitment);


// Store commitment
commitments[commitmentIndex] = newCommitment;

Expand Down Expand Up @@ -421,11 +427,7 @@ contract PreConfCommitmentStore is Ownable {
commitments[commitmentIndex].commitmentUsed = true;
commitmentsCount[commitment.commiter] -= 1;

userRegistry.retrieveFunds(
commitment.bidder,
commitment.bid,
payable(commitment.commiter)
);
nativeToken.transferFrom(commitment.bidder, commitment.commiter, commitment.bid);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions test/DummyERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract DummyERC20 is ERC20 {
constructor(string memory name, string memory symbol) ERC20(name, symbol) {}

function mint(address to, uint256 amount) public {
_mint(to, amount);
}
}
32 changes: 22 additions & 10 deletions test/OracleTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import "../contracts/PreConfirmations.sol";
import "../contracts/interfaces/IPreConfirmations.sol";
import "../contracts/ProviderRegistry.sol";
import "../contracts/UserRegistry.sol";
import "./DummyERC20.sol";


contract OracleTest is Test {
address internal owner;
Expand All @@ -20,6 +22,7 @@ contract OracleTest is Test {
uint256 testNumber;
uint64 testNumber2;
UserRegistry internal userRegistry;
DummyERC20 internal dummyToken;


// Events to match against
Expand All @@ -35,6 +38,11 @@ contract OracleTest is Test {
minStake = 1e18 wei;
feeRecipient = vm.addr(9);

// Deploy the dummy ERC20 token
dummyToken = new DummyERC20("DummyToken", "DTK");
// Optionally mint some tokens for testing
dummyToken.mint(address(this), 10000 ether);

providerRegistry = new ProviderRegistry(
minStake,
feeRecipient,
Expand All @@ -47,13 +55,15 @@ contract OracleTest is Test {
address(providerRegistry), // Provider Registry
address(userRegistry), // User Registry
feeRecipient, // Oracle
address(this) // Owner
address(this), // Owner
address(dummyToken)
);

address ownerInstance = 0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3;
dummyToken.transfer(ownerInstance, 5 ether);
vm.deal(ownerInstance, 5 ether);
vm.startPrank(ownerInstance);
userRegistry.registerAndStake{value: 2 ether}();
dummyToken.approve(address(preConfCommitmentStore), 2 ether);

oracle = new Oracle(address(preConfCommitmentStore), 2, ownerInstance);
oracle.addBuilderAddress("mev builder", ownerInstance);
Expand Down Expand Up @@ -98,11 +108,11 @@ contract OracleTest is Test {
assertEq(oracle.blockBuilderNameToAddress("zk builder"), builder4);
vm.stopPrank();

vm.deal(user, 1000 ether);

vm.deal(provider, 1000 ether);

vm.startPrank(user);
userRegistry.registerAndStake{value: 250 ether }();
dummyToken.approve(address(preConfCommitmentStore), 2 ether);
vm.stopPrank();

vm.startPrank(provider);
Expand Down Expand Up @@ -190,11 +200,13 @@ contract OracleTest is Test {
(address user, uint256 userPk) = makeAddrAndKey("alice");
(address provider, uint256 providerPk) = makeAddrAndKey("kartik");

vm.deal(user, 200000 ether);

dummyToken.mint(user, 200000 ether);
vm.startPrank(user);
userRegistry.registerAndStake{value: 250 ether }();
dummyToken.approve(address(preConfCommitmentStore), 2 ether);
vm.stopPrank();


vm.deal(provider, 200000 ether);
vm.startPrank(provider);
providerRegistry.registerAndStake{value: 250 ether}();
Expand All @@ -209,7 +221,7 @@ contract OracleTest is Test {

bytes32[] memory commitmentHashes = preConfCommitmentStore.getCommitmentsByBlockNumber(blockNumber);
assertEq(commitmentHashes.length, 1);
assertEq(userRegistry.getProviderAmount(provider), bid);
assertEq(200000 ether - dummyToken.balanceOf(user), bid);

}

Expand All @@ -223,11 +235,11 @@ contract OracleTest is Test {
(address user, uint256 userPk) = makeAddrAndKey("alice");
(address provider, uint256 providerPk) = makeAddrAndKey("bob");

vm.deal(user, 200000 ether);
dummyToken.mint(user, 200000 ether);
vm.deal(provider, 200000 ether);

vm.startPrank(user);
userRegistry.registerAndStake{value: 250 ether }();
dummyToken.approve(address(preConfCommitmentStore), 250 ether);
vm.stopPrank();

vm.startPrank(provider);
Expand All @@ -253,7 +265,7 @@ contract OracleTest is Test {
// Detect slashing
uint256 postSlashStake = providerRegistry.checkStake(provider);
assertEq(postSlashStake + bid, ogStake);
assertEq(userRegistry.checkStake(user), 250 ether);
assertEq(dummyToken.allowance(user, address(preConfCommitmentStore)), 250 ether);

}

Expand Down
34 changes: 26 additions & 8 deletions test/PreConfirmationConfTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import "forge-std/Test.sol";
import {PreConfCommitmentStore} from "../contracts/PreConfirmations.sol";
import "../contracts/ProviderRegistry.sol";
import "../contracts/UserRegistry.sol";
import "./DummyERC20.sol";


contract TestPreConfCommitmentStore is Test {
PreConfCommitmentStore internal preConfCommitmentStore;
Expand All @@ -17,13 +19,21 @@ contract TestPreConfCommitmentStore is Test {
ProviderRegistry internal providerRegistry;
uint256 testNumber;
uint64 testNumber2;
DummyERC20 internal dummyToken;

UserRegistry internal userRegistry;

function setUp() public {
testNumber = 2;
testNumber2 = 2;


// Deploy the dummy ERC20 token
dummyToken = new DummyERC20("DummyToken", "DTK");
// Optionally mint some tokens for testing
dummyToken.mint(address(this), 1000 ether);


feePercent = 10;
minStake = 1e18 wei;
feeRecipient = vm.addr(9);
Expand All @@ -39,7 +49,8 @@ contract TestPreConfCommitmentStore is Test {
address(providerRegistry), // Provider Registry
address(userRegistry), // User Registry
feeRecipient, // Oracle
address(this) // Owner
address(this), // Owner
address(dummyToken)
);
}

Expand Down Expand Up @@ -68,10 +79,11 @@ contract TestPreConfCommitmentStore is Test {

vm.deal(user, 200000 ether);
vm.prank(user);
userRegistry.registerAndStake{value: 1e18 wei}();
(bytes32 digest, address recoveredAddress, uint256 stake) = preConfCommitmentStore.verifyBid(200 wei, 3000, "0xkartik", signature);
dummyToken.approve(address(preConfCommitmentStore), 1e18 wei);

(bytes32 digest, address recoveredAddress, uint256 allowance) = preConfCommitmentStore.verifyBid(200 wei, 3000, "0xkartik", signature);

assertEq(stake, 1e18 wei);
assertEq(allowance, 1e18 wei);
assertEq(user, recoveredAddress);
assertEq(digest, bidHash);

Expand Down Expand Up @@ -144,7 +156,8 @@ contract TestPreConfCommitmentStore is Test {
address signer = 0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3;
vm.deal(signer, 5 ether);
vm.prank(signer);
userRegistry.registerAndStake{value: 2 ether}();
dummyToken.approve(address(preConfCommitmentStore), 2 ether);

string memory txnHash = "0xkartik";
bytes
memory signature = "0xb170d082db1bf77fa0b589b9438444010dcb1e6dd326b661b02eb92abe4c066e243bb0d214b01667750ba2c53ff1ab445fd784b441dbc1f30280c379f002cc571c";
Expand Down Expand Up @@ -285,7 +298,8 @@ contract TestPreConfCommitmentStore is Test {
address signer = 0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3;
vm.deal(signer, 5 ether);
vm.prank(signer);
userRegistry.registerAndStake{value: 2 ether}();
dummyToken.approve(address(preConfCommitmentStore), 2 ether);

string memory txnHash = "0xkartik";
bytes
memory signature = "0xb170d082db1bf77fa0b589b9438444010dcb1e6dd326b661b02eb92abe4c066e243bb0d214b01667750ba2c53ff1ab445fd784b441dbc1f30280c379f002cc571c";
Expand Down Expand Up @@ -325,7 +339,8 @@ contract TestPreConfCommitmentStore is Test {
address signer = 0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3;
vm.deal(signer, 5 ether);
vm.prank(signer);
userRegistry.registerAndStake{value: 2 ether}();
dummyToken.approve(address(preConfCommitmentStore), 2 ether);

string memory txnHash = "0xkartik";
bytes
memory signature = "0xb170d082db1bf77fa0b589b9438444010dcb1e6dd326b661b02eb92abe4c066e243bb0d214b01667750ba2c53ff1ab445fd784b441dbc1f30280c379f002cc571c";
Expand Down Expand Up @@ -386,10 +401,13 @@ contract TestPreConfCommitmentStore is Test {
function test_InitiateReward() public {
// Assuming you have a stored commitment
{
dummyToken.transfer(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3, 5 ether);
address signer = 0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3;
vm.deal(signer, 5 ether);
vm.prank(signer);
userRegistry.registerAndStake{value: 2 ether}();

dummyToken.approve(address(preConfCommitmentStore), 2 ether);

string memory txnHash = "0xkartik";
bytes
memory signature = "0xb170d082db1bf77fa0b589b9438444010dcb1e6dd326b661b02eb92abe4c066e243bb0d214b01667750ba2c53ff1ab445fd784b441dbc1f30280c379f002cc571c";
Expand Down
11 changes: 10 additions & 1 deletion test/ProviderRegistryTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
pragma solidity ^0.8.20;

import "forge-std/Test.sol";
import "./DummyERC20.sol";

import {ProviderRegistry} from "../contracts/ProviderRegistry.sol";
import {UserRegistry} from "../contracts/UserRegistry.sol";
import {PreConfCommitmentStore} from "../contracts/PreConfirmations.sol";
Expand All @@ -24,6 +26,12 @@ contract ProviderRegistryTest is Test {
minStake = 1e18 wei;
feeRecipient = vm.addr(9);


// Deploy the dummy ERC20 token
DummyERC20 dummyToken = new DummyERC20("DummyToken", "DTK");
// Optionally mint some tokens for testing
dummyToken.mint(address(this), 1000 ether);

providerRegistry = new ProviderRegistry(
minStake,
feeRecipient,
Expand All @@ -36,7 +44,8 @@ contract ProviderRegistryTest is Test {
address(providerRegistry), // Provider Registry
address(userRegistry), // User Registry
feeRecipient, // Oracle
address(this) // Owner
address(this), // Owner
address(dummyToken)
);

provider = vm.addr(1);
Expand Down