Skip to content

Commit

Permalink
Automatic reimbursing of ritual costs via ReimbursementPool
Browse files Browse the repository at this point in the history
  • Loading branch information
cygnusv committed Jun 14, 2023
1 parent 22a8774 commit f31cef5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
30 changes: 30 additions & 0 deletions contracts/contracts/coordination/Coordinator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pragma solidity ^0.8.0;
import "@openzeppelin/contracts/access/AccessControlDefaultAdminRules.sol";
import "@openzeppelin/contracts/token/IERC20.sol";
import "./IFeeModel.sol";
import "./IReimbursementPool.sol";
import "../lib/BLS12381.sol";
import "../../threshold/IAccessControlApplication.sol";

Expand Down Expand Up @@ -68,6 +69,7 @@ contract Coordinator is AccessControlDefaultAdminRules {
uint16 public maxDkgSize;
bool public isInitiationPublic;
IFeeModel feeModel; // TODO: Consider making feeModel specific to each ritual
IReimbursementPool reimbursementPool;

constructor(
IAccessControlApplication app,
Expand Down Expand Up @@ -131,6 +133,16 @@ contract Coordinator is AccessControlDefaultAdminRules {
maxDkgSize = newSize;
}

function setReimbursementPool(IReimbursementPool pool) external onlyRole(DEFAULT_ADMIN_ROLE) {
if (address(pool) == address(0)){
delete reimbursementPool;
} else {
require(pool.isAuthorized(address(this)), "Coordinator not authorized");
reimbursementPool = pool;
// TODO: Events
}
}

function numberOfRituals() external view returns(uint256) {
return rituals.length;
}
Expand Down Expand Up @@ -191,6 +203,8 @@ contract Coordinator is AccessControlDefaultAdminRules {
}

function postTranscript(uint32 ritualId, bytes calldata transcript) external {
uint256 initialGasLeft = gasleft();

Ritual storage ritual = rituals[ritualId];
require(
getRitualState(ritual) == RitualState.AWAITING_TRANSCRIPTS,
Expand Down Expand Up @@ -221,6 +235,7 @@ contract Coordinator is AccessControlDefaultAdminRules {
if (ritual.totalTranscripts == ritual.dkgSize){
emit StartAggregationRound(ritualId);
}
processReimbursement(initialGasLeft);
}

function postAggregation(
Expand All @@ -229,6 +244,8 @@ contract Coordinator is AccessControlDefaultAdminRules {
BLS12381.G1Point calldata publicKey,
bytes calldata decryptionRequestStaticKey
) external {
uint256 initialGasLeft = gasleft();

Ritual storage ritual = rituals[ritualId];
require(
getRitualState(ritual) == RitualState.AWAITING_AGGREGATIONS,
Expand Down Expand Up @@ -271,6 +288,7 @@ contract Coordinator is AccessControlDefaultAdminRules {
successful: false
});
// TODO: Consider freeing ritual storage
processReimbursement(initialGasLeft);
return;
}

Expand All @@ -282,6 +300,7 @@ contract Coordinator is AccessControlDefaultAdminRules {
});
// TODO: Consider including public key in event
}
processReimbursement(initialGasLeft);
}

function getParticipantFromProvider(
Expand All @@ -305,4 +324,15 @@ contract Coordinator is AccessControlDefaultAdminRules {
) external view returns (Participant memory) {
return getParticipantFromProvider(rituals[ritualID], provider);
}

function processReimbursement(uint256 initialGasLeft) internal {
if(reimbursementPool != address(0)){ // TODO: Consider defining a method
uint256 gasUsed = initialGasLeft - gasleft();
try reimbursementPool.refund(gasUsed, msg.sender) {
return;
} catch {
return;
}
}
}
}
12 changes: 12 additions & 0 deletions contracts/contracts/coordination/IReimbursementPool.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

pragma solidity ^0.8.0;

/**
* @title IReimbursementPool
* @notice IReimbursementPool
*/
interface IReimbursementPool {
function isAuthorized(address caller) external view returns(bool);
function refund(uint256 gasSpent, address receiver) external;
}

0 comments on commit f31cef5

Please sign in to comment.