|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.12; |
| 3 | + |
| 4 | +interface NonceMgr { |
| 5 | + function getNonce(address sender, uint192 key) external view returns (uint256 nonce); |
| 6 | +} |
| 7 | + |
| 8 | +import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; |
| 9 | + |
| 10 | +contract HCHelper { |
| 11 | + using SafeERC20 for IERC20; |
| 12 | + |
| 13 | + // Owner (creator) of this contract. |
| 14 | + address public owner; |
| 15 | + |
| 16 | + // Response data is stored here by PutResponse() and then consumed by TryCallOffchain(). |
| 17 | + // The storage slot must not be changed unless the corresponding code is updated in the Bundler. |
| 18 | + mapping(bytes32=>bytes) ResponseCache; |
| 19 | + |
| 20 | + // BOBA token address |
| 21 | + address public tokenAddr; |
| 22 | + |
| 23 | + // Token amount required to purchase each prepaid credit (may be 0 for testing) |
| 24 | + uint256 public pricePerCall; |
| 25 | + |
| 26 | + // Account which is used to insert system error responses. Currently a single |
| 27 | + // address but could be extended to a list of authorized accounts if needed. |
| 28 | + address public systemAccount; |
| 29 | + |
| 30 | + |
| 31 | + struct callerInfo { |
| 32 | + address owner; |
| 33 | + string url; |
| 34 | + uint256 credits; |
| 35 | + } |
| 36 | + |
| 37 | + // Contracts which are allowed to use Hybrid Compute. |
| 38 | + mapping(address=>callerInfo) public RegisteredCallers; |
| 39 | + |
| 40 | + address immutable entryPoint; |
| 41 | + |
| 42 | + constructor(address _entryPoint, address _tokenAddr, uint256 _pricePerCall) { |
| 43 | + owner = msg.sender; |
| 44 | + entryPoint = _entryPoint; |
| 45 | + tokenAddr = _tokenAddr; |
| 46 | + pricePerCall = _pricePerCall; |
| 47 | + } |
| 48 | + |
| 49 | + function RegisterUrl(address contract_addr, string calldata url) public { |
| 50 | + // Temporary method, until an auto-registration protocol is developed |
| 51 | + RegisteredCallers[contract_addr].owner = msg.sender; |
| 52 | + RegisteredCallers[contract_addr].url = url; |
| 53 | + } |
| 54 | + |
| 55 | + function SetSystemAccount(address _systemAccount) public { |
| 56 | + require(msg.sender == owner, "Only owner"); |
| 57 | + systemAccount = _systemAccount; |
| 58 | + } |
| 59 | + |
| 60 | + // Placeholder - this will transfer Boba tokens to this contract (or to a beneficiary) |
| 61 | + // and add a corresponding number of credits for the specified contract address. |
| 62 | + function AddCredit(address contract_addr, uint256 numCredits) public { |
| 63 | + if (pricePerCall > 0) { |
| 64 | + uint256 tokenPrice = numCredits * pricePerCall; |
| 65 | + IERC20(tokenAddr).safeTransferFrom(msg.sender, address(this), tokenPrice); |
| 66 | + } |
| 67 | + RegisteredCallers[contract_addr].credits += numCredits; |
| 68 | + } |
| 69 | + |
| 70 | + function WithdrawTokens(uint256 amount, address withdrawTo) public { |
| 71 | + require(msg.sender == owner, "Only owner"); |
| 72 | + IERC20(tokenAddr).safeTransferFrom(address(this), withdrawTo, amount); |
| 73 | + } |
| 74 | + |
| 75 | + // Called from a HybridAccount contract, to populate the response which it will |
| 76 | + // subsequently request in TryCallOffchain() |
| 77 | + function PutResponse(bytes32 subKey, bytes calldata response) public { |
| 78 | + require(RegisteredCallers[msg.sender].owner != address(0), "Unregistered caller"); |
| 79 | + require(response.length >= 32*4, "Response too short"); |
| 80 | + |
| 81 | + (,, uint32 errCode,) = abi.decode(response,(address, uint256, uint32, bytes)); |
| 82 | + require(errCode < 2, "invalid errCode for PutResponse()"); |
| 83 | + |
| 84 | + bytes32 mapKey = keccak256(abi.encodePacked(msg.sender, subKey)); |
| 85 | + ResponseCache[mapKey] = response; |
| 86 | + } |
| 87 | + |
| 88 | + // Allow the system to supply an error response for unsuccessful requests. |
| 89 | + // Any such response will only be retrieved if there was nothing supplied |
| 90 | + // by PutResponse() |
| 91 | + function PutSysResponse(bytes32 subKey, bytes calldata response) public { |
| 92 | + require(msg.sender == systemAccount, "Only systemAccount may call PutSysResponse"); |
| 93 | + require(response.length >= 32*4, "Response too short"); |
| 94 | + |
| 95 | + (,, uint32 errCode,) = abi.decode(response,(address, uint256, uint32, bytes)); |
| 96 | + require(errCode >= 2, "PutSysResponse() may only be used for error responses"); |
| 97 | + |
| 98 | + bytes32 mapKey = keccak256(abi.encodePacked(address(this), subKey)); |
| 99 | + ResponseCache[mapKey] = response; |
| 100 | + } |
| 101 | + |
| 102 | + // Remove one or more map entries (only needed if response was not retrieved normally). |
| 103 | + function RemoveResponse(bytes32[] calldata mapKeys) public { |
| 104 | + require(msg.sender == owner, "Only owner"); |
| 105 | + for (uint32 i = 0; i < mapKeys.length; i++) { |
| 106 | + delete(ResponseCache[mapKeys[i]]); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Try to retrieve an entry, also removing it from the mapping. This |
| 111 | + // function will check for stale entries by checking the nonce of the srcAccount. |
| 112 | + // Stale entries will return a "not found" condition. |
| 113 | + function getEntry(bytes32 mapKey) internal returns (bool, uint32, bytes memory) { |
| 114 | + bytes memory entry; |
| 115 | + bool found; |
| 116 | + uint32 errCode; |
| 117 | + bytes memory response; |
| 118 | + address srcAddr; |
| 119 | + uint256 srcNonce; |
| 120 | + |
| 121 | + entry = ResponseCache[mapKey]; |
| 122 | + if (entry.length == 1) { |
| 123 | + // Used during state simulation to verify that a trigger request actually came from this helper contract |
| 124 | + revert("_HC_VRFY"); |
| 125 | + } else if (entry.length != 0) { |
| 126 | + found = true; |
| 127 | + (srcAddr, srcNonce, errCode, response) = abi.decode(entry,(address, uint256, uint32, bytes)); |
| 128 | + uint192 nonceKey = uint192(srcNonce >> 64); |
| 129 | + |
| 130 | + NonceMgr NM = NonceMgr(entryPoint); |
| 131 | + uint256 actualNonce = NM.getNonce(srcAddr, nonceKey); |
| 132 | + |
| 133 | + if (srcNonce + 1 != actualNonce) { |
| 134 | + // stale entry |
| 135 | + found = false; |
| 136 | + errCode = 0; |
| 137 | + response = "0x"; |
| 138 | + } |
| 139 | + |
| 140 | + delete(ResponseCache[mapKey]); |
| 141 | + } |
| 142 | + return (found, errCode, response); |
| 143 | + } |
| 144 | + |
| 145 | + // Make an offchain call to a pre-registered endpoint. |
| 146 | + function TryCallOffchain(bytes32 userKey, bytes memory req) public returns (uint32, bytes memory) { |
| 147 | + bool found; |
| 148 | + uint32 errCode; |
| 149 | + bytes memory ret; |
| 150 | + |
| 151 | + require(RegisteredCallers[msg.sender].owner != address(0), "Calling contract not registered"); |
| 152 | + |
| 153 | + if (RegisteredCallers[msg.sender].credits == 0) { |
| 154 | + return (5, "Insufficient credit"); |
| 155 | + } |
| 156 | + RegisteredCallers[msg.sender].credits -= 1; |
| 157 | + |
| 158 | + bytes32 subKey = keccak256(abi.encodePacked(userKey, req)); |
| 159 | + bytes32 mapKey = keccak256(abi.encodePacked(msg.sender, subKey)); |
| 160 | + |
| 161 | + (found, errCode, ret) = getEntry(mapKey); |
| 162 | + |
| 163 | + if (found) { |
| 164 | + return (errCode, ret); |
| 165 | + } else { |
| 166 | + // If no off-chain response, check for a system error response. |
| 167 | + bytes32 errKey = keccak256(abi.encodePacked(address(this), subKey)); |
| 168 | + |
| 169 | + (found, errCode, ret) = getEntry(errKey); |
| 170 | + if (found) { |
| 171 | + require(errCode >= 2, "invalid errCode"); |
| 172 | + return (errCode, ret); |
| 173 | + } else { |
| 174 | + // Nothing found, so trigger a new request. |
| 175 | + bytes memory prefix = "_HC_TRIG"; |
| 176 | + bytes memory r2 = bytes.concat(prefix, abi.encodePacked(msg.sender, userKey, req)); |
| 177 | + assembly { |
| 178 | + revert(add(r2, 32), mload(r2)) |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments