Skip to content

Commit

Permalink
Reimbursing for a callback in the Service Contract
Browse files Browse the repository at this point in the history
  • Loading branch information
dimpar committed Jan 27, 2020
1 parent 7790049 commit 6c4848c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
30 changes: 30 additions & 0 deletions contracts/solidity/contracts/KeepRandomBeaconOperator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,36 @@ contract KeepRandomBeaconOperator is ReentrancyGuard {
currentEntryStartBlock = 0;
}

/**
* @dev Reimbursment for calling customer specified callback for the relay entry request.
* @param callbackFunds Available funds for callback reimbursment.
* @param submitter Relay entry submitter address.
* @param surplusRecipient Surplus recipient address.
*/
function reimburseCallback(uint256 callbackFunds, address payable submitter, address payable surplusRecipient)
public payable onlyServiceContract {

bool success; // Store status of external contract call.

// If we spent less on the callback than the customer transferred for the
// callback execution, we need to reimburse the difference.
if (msg.value < callbackFunds) {
uint256 callbackSurplus = callbackFunds.sub(msg.value);
// Reimburse submitter with his actual callback cost.
(success, ) = submitter.call.value(msg.value)("");
require(success, "Failed reimburse actual callback cost");

// Return callback surplus to the requestor.
(success, ) = surplusRecipient.call.value(callbackSurplus)("");
require(success, "Failed send callback surplus");

} else {
// Reimburse submitter with the callback payment sent by the requestor.
(success, ) = submitter.call.value(callbackFunds)("");
require(success, "Failed reimburse callback payment");
}
}

/**
* @dev Get rewards breakdown in wei for successful entry for the current signing request.
*/
Expand Down
31 changes: 11 additions & 20 deletions contracts/solidity/contracts/KeepRandomBeaconServiceImplV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ interface OperatorContract {
) external payable;
function numberOfGroups() external view returns(uint256);
function createGroup(uint256 newEntry, address payable submitter) external payable;
function reimburseCallback(
uint256 callbackFunds,
address payable submitter,
address payable surplusRecipient
) external payable;
function isGroupSelectionPossible() external view returns (bool);
}

Expand Down Expand Up @@ -337,26 +342,12 @@ contract KeepRandomBeaconServiceImplV1 is DelayedWithdrawal, ReentrancyGuard {
}

// Obtain the actual callback gas expenditure and refund the surplus.
uint256 callbackSurplus = 0;
uint256 callbackFee = gasSpent.mul(gasPrice);

// If we spent less on the callback than the customer transferred for the
// callback execution, we need to reimburse the difference.
if (callbackFee < _callbacks[requestId].callbackFee) {
callbackSurplus = _callbacks[requestId].callbackFee.sub(callbackFee);
// Reimburse submitter with his actual callback cost.
(success, ) = submitter.call.value(callbackFee)("");
require(success, "Failed reimburse actual callback cost");

// Return callback surplus to the requestor.
(success, ) = _callbacks[requestId].surplusRecipient.call.value(callbackSurplus)("");
require(success, "Failed send callback surplus");

} else {
// Reimburse submitter with the callback payment sent by the requestor.
(success, ) = submitter.call.value(_callbacks[requestId].callbackFee)("");
require(success, "Failed reimburse callback payment");
}
// uint256 callbackFee = gasSpent.mul(gasPrice);

// revert("bbbb");
address latestOperatorContract = _operatorContracts[_operatorContracts.length.sub(1)];
OperatorContract(latestOperatorContract).reimburseCallback.value(gasSpent.mul(gasPrice))(
_callbacks[requestId].callbackFee, submitter, _callbacks[requestId].surplusRecipient);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions contracts/solidity/test/TestKeepRandomBeaconServicePricing.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,14 @@ contract('TestKeepRandomBeaconServicePricing', function(accounts) {
});

it("should send group reward to each operator.", async function() {
let entryFeeEstimate = await serviceContract.entryFeeEstimate(0)
let callbackGas = await callbackContract.callback.estimateGas(bls.groupSignature);
let excessCallbackGas = web3.utils.toBN(callbackGas).mul(web3.utils.toBN(2));
let entryFeeEstimate = await serviceContract.entryFeeEstimate(excessCallbackGas)

let tx = await serviceContract.methods['requestRelayEntry(address,string,uint256)'](
callbackContract.address,
"callback(uint256)",
0,
excessCallbackGas,
{value: entryFeeEstimate, from: requestor}
);

Expand Down Expand Up @@ -161,10 +164,7 @@ contract('TestKeepRandomBeaconServicePricing', function(accounts) {
// subsidy = 5250000000000000 - 207407407407407 * 5 - 210648148148148 = 4002314814814817 wei

let entryFeeEstimate = await serviceContract.entryFeeEstimate(0)
let tx = await serviceContract.methods['requestRelayEntry(address,string,uint256)'](
callbackContract.address,
"callback(uint256)",
0,
let tx = await serviceContract.methods['requestRelayEntry()'](
{value: entryFeeEstimate, from: requestor}
);

Expand Down

0 comments on commit 6c4848c

Please sign in to comment.