Skip to content

Commit

Permalink
add conf request helper for forge (#98)
Browse files Browse the repository at this point in the history
* add conf request helper for forge

* don't use *

* loosen solidity version restriction on ConfRequest lib
  • Loading branch information
zeroXbrock authored Aug 5, 2024
1 parent 2704332 commit 5199304
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/forge/ConfidentialRequest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// Send confidential requests in Forge.
library ConfRequest {
/// Sends a confidential request; calls offchain function and onchain callback.
function sendConfRequest(address to, bytes memory data) internal returns (Status, bytes memory callbackResult) {
// offchain execution
(bool success, bytes memory suaveCalldata) = to.call(data);
if (!success) {
return (Status.FAILURE_OFFCHAIN, suaveCalldata);
}
suaveCalldata = abi.decode(suaveCalldata, (bytes));
// onchain callback
(success, callbackResult) = to.call(suaveCalldata);
if (!success) {
return (Status.FAILURE_ONCHAIN, callbackResult);
}
return (Status.SUCCESS, callbackResult);
}
}

enum Status {
SUCCESS,
FAILURE_OFFCHAIN,
FAILURE_ONCHAIN
}
20 changes: 20 additions & 0 deletions test/forge/ConfidentialRequest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import "../../src/Test.sol";
import {ConfRequest, Status} from "src/forge/ConfidentialRequest.sol";
import {NumberSuapp} from "../Forge.t.sol";

contract ConfRequestTest is Test, SuaveEnabled {
using ConfRequest for address;

NumberSuapp numberSuapp = new NumberSuapp();

function testConfRequest() public {
ctx.setConfidentialInputs(abi.encode(0x42));
(Status s,) = address(numberSuapp).sendConfRequest(abi.encodeWithSelector(NumberSuapp.setNumber.selector));
assertEq(uint256(s), uint256(Status.SUCCESS));
assertEq(numberSuapp.number(), 0x42);
}
}

0 comments on commit 5199304

Please sign in to comment.