Skip to content

Commit

Permalink
Added payment for gas, query to result, saving result in secret
Browse files Browse the repository at this point in the history
  • Loading branch information
SecretSaturn committed Jan 19, 2024
1 parent ef72aa4 commit 8b713c0
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 58 deletions.
4 changes: 2 additions & 2 deletions TNLS-Gateways/public-gateway/script/UpgradeScript.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ contract UpgradeScript is Script {
// Deploy New Gateway Logic Contract
newGatewayLogic = new Gateway();

gatewayProxyAdmin = ProxyAdmin(0x7C0f8810f989cB92533b09c3535B1EfA80393EAD);
gatewayProxyAdmin = ProxyAdmin(0x9eA72D83533D8B753d000D9C233a80CC08FFb072);

bytes memory selector = abi.encodeWithSelector(Gateway.upgradeHandler.selector);
gatewayProxyAdmin.upgradeAndCall(ITransparentUpgradeableProxy(0xBFE44aF8e40B6468946e9AA88fe2c6c9D0352F62), address(newGatewayLogic),selector);
gatewayProxyAdmin.upgradeAndCall(ITransparentUpgradeableProxy(0x286B1e6B58a913E457509f0C30Ad4393C78f4F84), address(newGatewayLogic),selector);

vm.stopBroadcast();
}
Expand Down
61 changes: 43 additions & 18 deletions TNLS-Gateways/public-gateway/src/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,17 @@ contract GatewayProxy is TransparentUpgradeableProxy {
constructor(address _logic, address admin_, bytes memory _data) TransparentUpgradeableProxy(_logic, admin_, _data) {}
}

/*//////////////////////////////////////////////////////////////
Secret VRF Interface
//////////////////////////////////////////////////////////////*/
interface IRandomness {
function fulfillRandomWords(uint256 requestId, uint256[] calldata randomWords) external;
}

contract Gateway is Initializable {
/*//////////////////////////////////////////////////////////////
Constants
//////////////////////////////////////////////////////////////*/

//Use hard coded constant values instead of storage variables for Secret VRF, saves around 10,000+ in gas per TX.
//Since contract is upgradeable, we can update these values as well with it.
bytes constant routing_info = "secret1a9jvkwanwgs66222a74p607reh333nfsspgp93";
bytes constant routing_info = "secret14hlku6qen0tkhfq0cklx02hcdu9jph8un4lsga";
bytes constant routing_code_hash = "ba0006753cb18a8b12fe266707289098bfb8a3ae83de54ecece591231ada2abf";
string constant task_destination_network = "secret-4";
address constant secret_gateway_signer_address = 0x9199301b86c1eC0C180B51B1CFb6B4966A0D6bFf;
address constant secret_gateway_signer_address = 0x1b153e8fc101c2c6C9e0a9250aca99e957354a8E;


/*//////////////////////////////////////////////////////////////
Expand All @@ -49,6 +42,7 @@ contract Gateway is Initializable {
string task_destination_network;
string handle;
bytes12 nonce;
uint32 callback_gas_limit;
bytes payload;
bytes payload_signature;
}
Expand Down Expand Up @@ -101,6 +95,9 @@ contract Gateway is Initializable {
/// @notice thrown when the user requests more Random Words than allowed
error TooManyVRFRandomWordsRequested();

/// @notice thrown when the paid fee was lower than expected:
error PaidRequestFeeTooLow();

/*//////////////////////////////////////////////////////////////
Helpers
//////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -268,14 +265,32 @@ contract Gateway is Initializable {
Maintainance Functions
//////////////////////////////////////////////////////////////*/

/// @notice Increase the task_id to check for problems
/// @notice Increase the task_id if needed
/// @param _newTaskId the new task_id

function increaseTaskId(uint256 _newTaskId) external onlyOwner {
require (_newTaskId > taskId, "New task id must be higher than the old task_id");
taskId = _newTaskId;
}

/// @notice Payout the paid balance to the owner

function payoutBalance() external onlyOwner {
payable(owner).transfer(address(this).balance);
}

/*//////////////////////////////////////////////////////////////
Gas Price Payment Functions
//////////////////////////////////////////////////////////////*/

/// @notice Increase the task_id to check for problems
/// @param _callbackGasLimit the Callback Gas Limit

function estimateRequestPrice(uint32 _callbackGasLimit) private view returns (uint256) {
uint256 baseFee = _callbackGasLimit*tx.gasprice;
return baseFee;
}

/*//////////////////////////////////////////////////////////////
Pre Execution
//////////////////////////////////////////////////////////////*/
Expand All @@ -293,6 +308,10 @@ contract Gateway is Initializable {
ExecutionInfo calldata _info)
external payable {

if (estimateRequestPrice(_info.callback_gas_limit) > msg.value) {
revert PaidRequestFeeTooLow();
}

// Payload hash verification
if (keccak256(bytes.concat("\x19Ethereum Signed Message:\n32", keccak256(_info.payload))) != _payloadHash) {
revert InvalidPayloadHash();
Expand Down Expand Up @@ -335,6 +354,10 @@ contract Gateway is Initializable {
revert TooManyVRFRandomWordsRequested();
}

if (estimateRequestPrice(_callbackGasLimit) > msg.value) {
revert PaidRequestFeeTooLow();
}

//Encode the callback_address as Base64
string memory callback_address = encodeBase64(bytes.concat(bytes20(msg.sender)));

Expand All @@ -361,6 +384,7 @@ contract Gateway is Initializable {
task_destination_network: task_destination_network,
handle: "request_random",
nonce: bytes12(0),
callback_gas_limit:_callbackGasLimit,
payload: payload,
payload_signature: new bytes(64) // empty signature, fill with 0 bytes
});
Expand Down Expand Up @@ -432,19 +456,20 @@ contract Gateway is Initializable {
// Continue with the function execution

// Additional conversion for Secret VRF into uint256[] if callback_selector matches the fullfillRandomWords selector.
if (_info.callback_selector == bytes4(0x38ba4614)) {
bool callbackSuccessful;
if (_info.callback_selector == 0x38ba4614) {
uint256[] memory randomWords = bytesToUint256Array(_info.result);
IRandomness randomness = IRandomness(address(_info.callback_address));
randomness.fulfillRandomWords(_taskId, randomWords);
(callbackSuccessful, ) = address(_info.callback_address).call(
abi.encodeWithSelector(0x38ba4614, _taskId, randomWords)
);
}
else {
bool val;
(val, ) = address(_info.callback_address).call{gas: uint32(_info.callback_gas_limit)}(
(callbackSuccessful, ) = address(_info.callback_address).call(
abi.encodeWithSelector(_info.callback_selector, _taskId, _info.result)
);
if (!val) {
revert CallbackError();
}
}
if (!callbackSuccessful) {
revert CallbackError();
}
}

Expand Down
5 changes: 4 additions & 1 deletion TNLS-Gateways/public-gateway/test/Contract.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ contract ContractTest is Test {
task_destination_network: "pulsar-3",
handle: "some kinda handle",
nonce: "ssssssssssss",
callback_gas_limit: 300000,
payload: payload,
payload_signature: getPayloadSignature(payload, 5)
});
Expand Down Expand Up @@ -220,6 +221,7 @@ contract ContractTest is Test {
task_destination_network: "pulsar-3",
handle: "some kinda handle",
nonce: "ssssssssssss",
callback_gas_limit: 300000,
payload: payload,
payload_signature: getPayloadSignature(payload, 7)
});
Expand Down Expand Up @@ -319,11 +321,12 @@ contract ContractTest is Test {
task_destination_network: "pulsar-3",
handle: "some kinda handle",
nonce: "ssssssssssss",
callback_gas_limit: 300000,
payload: payload,
payload_signature: payloadSignature
});

gateway.send(payloadHash, userAddress,routingInfo, assembledInfo );
gateway.send(payloadHash, userAddress, routingInfo, assembledInfo );

(bytes31 tempPayloadHash,) = gateway.tasks(1);
assertEq(tempPayloadHash, sliceLastByte(payloadHash));
Expand Down
Binary file modified TNLS-Gateways/secret/contract.wasm.gz
Binary file not shown.
Loading

0 comments on commit 8b713c0

Please sign in to comment.