Skip to content

Commit

Permalink
Completes implementation for funds return and double staking prevention.
Browse files Browse the repository at this point in the history
  • Loading branch information
ckartik committed Jan 31, 2024
1 parent b0041f2 commit e849274
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
21 changes: 20 additions & 1 deletion contracts/BidderRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ contract BidderRegistry is IBidderRegistry, Ownable, ReentrancyGuard {
if (bidState.state == State.UnPreConfirmed) {
BidPayment[commitmentDigest] = BidState({
bidAmt: bid,
state: State.PreConfirmed
state: State.PreConfirmed,
bidder: bidder
});
bidderPrepaidBalances[bidder] -= bid;
}
Expand Down Expand Up @@ -186,6 +187,24 @@ contract BidderRegistry is IBidderRegistry, Ownable, ReentrancyGuard {
emit FundsRetrieved(commitmentDigest, amt);
}

/**
* @dev Return funds to a bidder's prepay (only callable by the pre-confirmations contract).
* @dev reenterancy not necessary but still putting here for precaution
* @param bidID is the Bid ID that allows us to identify the bid, and prepayment
*/
function returnFunds(bytes32 bidID) external nonReentrant onlyPreConfirmationEngine() {
BidState memory bidState = BidPayment[bidID];
require(bidState.state == State.PreConfirmed, "The bid was not preconfirmed");
uint256 amt = bidState.bidAmt;
bidderPrepaidBalances[bidState.bidder] += amt;


BidPayment[bidID].state = State.Withdrawn;
BidPayment[bidID].bidAmt = 0;

emit FundsRetrieved(bidID, amt);
}

/**
* @notice Sets the new fee recipient
* @dev onlyOwner restriction
Expand Down
8 changes: 7 additions & 1 deletion contracts/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {PreConfCommitmentStore} from "./PreConfirmations.sol";
import {IProviderRegistry} from "./interfaces/IProviderRegistry.sol";
import {IPreConfCommitmentStore} from './interfaces/IPreConfirmations.sol';
import {IBidderRegistry} from './interfaces/IBidderRegistry.sol';


/// @title Oracle Contract
/// @author Kartik Chopra
Expand Down Expand Up @@ -44,6 +46,8 @@ contract Oracle is Ownable {
/// @dev Reference to the PreConfCommitmentStore contract interface.
IPreConfCommitmentStore private preConfContract;

IBidderRegistry private bidderRegistry;

/**
* @dev Constructor to initialize the contract with a PreConfirmations contract.
* @param _preConfContract The address of the pre-confirmations contract.
Expand All @@ -52,10 +56,12 @@ contract Oracle is Ownable {
*/
constructor(
address _preConfContract,
address _bidderRegistry,
uint256 _nextRequestedBlockNumber,
address _owner
) Ownable() {
preConfContract = IPreConfCommitmentStore(_preConfContract);
bidderRegistry = IBidderRegistry(_bidderRegistry);
nextRequestedBlockNumber = _nextRequestedBlockNumber;
_transferOwnership(_owner);
}
Expand Down Expand Up @@ -122,7 +128,7 @@ contract Oracle is Ownable {
/**
*/
function returnFunds(bytes32 bidID) external onlyOwner {
// Bidder registery.returnFunds(bidID);
bidderRegistry.returnFunds(bidID);
}

/**
Expand Down
6 changes: 1 addition & 5 deletions contracts/PreConfirmations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,7 @@ contract PreConfCommitmentStore is Ownable {
payable(commitment.bidder)
);

// Return funds to bidder
bidderRegistry.retrieveFunds(
commitment.commitmentHash,
payable(commitment.bidder)
);
bidderRegistry.returnFunds(commitment.commitmentHash);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions contracts/interfaces/IBidderRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface IBidderRegistry {


struct BidState {
address bidder;
uint64 bidAmt;
State state;
}
Expand All @@ -34,4 +35,6 @@ interface IBidderRegistry {
bytes32 commitmentDigest,
address payable provider
) external;

function returnFunds(bytes32 bidID) external;
}
2 changes: 1 addition & 1 deletion scripts/DeployScripts.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ contract DeployScript is Script, Create2Deployer {
bidderRegistry.setPreconfirmationsContract(address(preConfCommitmentStore));
console.log("BidderRegistry updated with PreConfCommitmentStore address:", address(preConfCommitmentStore));

Oracle oracle = new Oracle{salt: salt}(address(preConfCommitmentStore), nextRequestedBlockNumber, msg.sender);
Oracle oracle = new Oracle{salt: salt}(address(preConfCommitmentStore), address(bidderRegistry), nextRequestedBlockNumber, msg.sender);
console.log("Oracle deployed to:", address(oracle));

preConfCommitmentStore.updateOracle(address(oracle));
Expand Down
2 changes: 1 addition & 1 deletion test/OracleTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ contract OracleTest is Test {
vm.startPrank(ownerInstance);
bidderRegistry.prepay{value: 2 ether}();

oracle = new Oracle(address(preConfCommitmentStore), 2, ownerInstance);
oracle = new Oracle(address(preConfCommitmentStore), address(bidderRegistry), 2, ownerInstance);
oracle.addBuilderAddress("mev builder", ownerInstance);
vm.stopPrank();

Expand Down

0 comments on commit e849274

Please sign in to comment.