-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from defi-wonderland/perf/opo-600-http-request…
…-module perf: optimize `HttpRequestModule`
- Loading branch information
Showing
4 changed files
with
351 additions
and
421 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 62 additions & 63 deletions
125
solidity/contracts/modules/request/HttpRequestModule.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,62 @@ | ||
// // SPDX-License-Identifier: MIT | ||
// pragma solidity ^0.8.19; | ||
|
||
// // solhint-disable-next-line no-unused-import | ||
// import {Module, IModule} from '@defi-wonderland/prophet-core-contracts/solidity/contracts/Module.sol'; | ||
// import {IOracle} from '@defi-wonderland/prophet-core-contracts/solidity/interfaces/IOracle.sol'; | ||
|
||
// import {IHttpRequestModule} from '../../../interfaces/modules/request/IHttpRequestModule.sol'; | ||
|
||
// contract HttpRequestModule is Module, IHttpRequestModule { | ||
// constructor(IOracle _oracle) Module(_oracle) {} | ||
|
||
// /// @inheritdoc IModule | ||
// function moduleName() public pure returns (string memory _moduleName) { | ||
// _moduleName = 'HttpRequestModule'; | ||
// } | ||
|
||
// /// @inheritdoc IHttpRequestModule | ||
// function decodeRequestData(bytes32 _requestId) public view returns (RequestParameters memory _params) { | ||
// _params = abi.decode(requestData[_requestId], (RequestParameters)); | ||
// } | ||
|
||
// /** | ||
// * @notice Bonds the requester tokens to use as payment for the response proposer. | ||
// */ | ||
// function _afterSetupRequest(bytes32 _requestId, bytes calldata) internal override { | ||
// RequestParameters memory _params = decodeRequestData(_requestId); | ||
// IOracle.Request memory _request = ORACLE.getRequest(_requestId); | ||
// _params.accountingExtension.bond({ | ||
// _bonder: _request.requester, | ||
// _requestId: _requestId, | ||
// _token: _params.paymentToken, | ||
// _amount: _params.paymentAmount | ||
// }); | ||
// } | ||
|
||
// /// @inheritdoc IHttpRequestModule | ||
// function finalizeRequest( | ||
// bytes32 _requestId, | ||
// address _finalizer | ||
// ) external override(IHttpRequestModule, Module) onlyOracle { | ||
// IOracle.Request memory _request = ORACLE.getRequest(_requestId); | ||
// IOracle.Response memory _response = ORACLE.getFinalizedResponse(_requestId); | ||
// RequestParameters memory _params = decodeRequestData(_requestId); | ||
// if (_response.createdAt != 0) { | ||
// _params.accountingExtension.pay({ | ||
// _requestId: _requestId, | ||
// _payer: _request.requester, | ||
// _receiver: _response.proposer, | ||
// _token: _params.paymentToken, | ||
// _amount: _params.paymentAmount | ||
// }); | ||
// } else { | ||
// _params.accountingExtension.release({ | ||
// _bonder: _request.requester, | ||
// _requestId: _requestId, | ||
// _token: _params.paymentToken, | ||
// _amount: _params.paymentAmount | ||
// }); | ||
// } | ||
// emit RequestFinalized(_requestId, _finalizer); | ||
// } | ||
// } | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.19; | ||
|
||
// solhint-disable-next-line no-unused-import | ||
import {Module, IModule} from '@defi-wonderland/prophet-core-contracts/solidity/contracts/Module.sol'; | ||
import {IOracle} from '@defi-wonderland/prophet-core-contracts/solidity/interfaces/IOracle.sol'; | ||
|
||
import {IHttpRequestModule} from '../../../interfaces/modules/request/IHttpRequestModule.sol'; | ||
|
||
contract HttpRequestModule is Module, IHttpRequestModule { | ||
constructor(IOracle _oracle) Module(_oracle) {} | ||
|
||
/// @inheritdoc IModule | ||
function moduleName() public pure returns (string memory _moduleName) { | ||
_moduleName = 'HttpRequestModule'; | ||
} | ||
|
||
/// @inheritdoc IHttpRequestModule | ||
function decodeRequestData(bytes calldata _data) public pure returns (RequestParameters memory _params) { | ||
_params = abi.decode(_data, (RequestParameters)); | ||
} | ||
|
||
/// @inheritdoc IHttpRequestModule | ||
function createRequest(bytes32 _requestId, bytes calldata _data, address _requester) external onlyOracle { | ||
RequestParameters memory _params = decodeRequestData(_data); | ||
|
||
_params.accountingExtension.bond({ | ||
_bonder: _requester, | ||
_requestId: _requestId, | ||
_token: _params.paymentToken, | ||
_amount: _params.paymentAmount | ||
}); | ||
} | ||
|
||
/// @inheritdoc IHttpRequestModule | ||
function finalizeRequest( | ||
IOracle.Request calldata _request, | ||
IOracle.Response calldata _response, | ||
address _finalizer | ||
) external override(IHttpRequestModule, Module) onlyOracle { | ||
RequestParameters memory _params = decodeRequestData(_request.requestModuleData); | ||
|
||
if (ORACLE.createdAt(_getId(_response)) != 0) { | ||
_params.accountingExtension.pay({ | ||
_requestId: _response.requestId, | ||
_payer: _request.requester, | ||
_receiver: _response.proposer, | ||
_token: _params.paymentToken, | ||
_amount: _params.paymentAmount | ||
}); | ||
} else { | ||
_params.accountingExtension.release({ | ||
_bonder: _request.requester, | ||
_requestId: _response.requestId, | ||
_token: _params.paymentToken, | ||
_amount: _params.paymentAmount | ||
}); | ||
} | ||
|
||
emit RequestFinalized(_response.requestId, _response, _finalizer); | ||
} | ||
} |
131 changes: 74 additions & 57 deletions
131
solidity/interfaces/modules/request/IHttpRequestModule.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,82 @@ | ||
// // SPDX-License-Identifier: MIT | ||
// pragma solidity ^0.8.19; | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.19; | ||
|
||
// import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; | ||
// import {IRequestModule} from | ||
// '@defi-wonderland/prophet-core-contracts/solidity/interfaces/modules/request/IRequestModule.sol'; | ||
// import {IAccountingExtension} from '../../../interfaces/extensions/IAccountingExtension.sol'; | ||
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; | ||
import {IOracle} from '@defi-wonderland/prophet-core-contracts/solidity/interfaces/IOracle.sol'; | ||
import {IRequestModule} from | ||
'@defi-wonderland/prophet-core-contracts/solidity/interfaces/modules/request/IRequestModule.sol'; | ||
import {IAccountingExtension} from '../../../interfaces/extensions/IAccountingExtension.sol'; | ||
|
||
// /* | ||
// * @title HttpRequestModule | ||
// * @notice Module allowing users to request HTTP calls | ||
// */ | ||
// interface IHttpRequestModule is IRequestModule { | ||
// /*/////////////////////////////////////////////////////////////// | ||
// STRUCTS | ||
// //////////////////////////////////////////////////////////////*/ | ||
/* | ||
* @title HttpRequestModule | ||
* @notice Module allowing users to request HTTP calls | ||
*/ | ||
interface IHttpRequestModule is IRequestModule { | ||
/*/////////////////////////////////////////////////////////////// | ||
STRUCTS | ||
//////////////////////////////////////////////////////////////*/ | ||
|
||
// /** | ||
// * @notice Parameters of the request as stored in the module | ||
// * @param url The url to make the request to | ||
// * @param method The HTTP method to use for the request | ||
// * @param body The HTTP body to use for the request | ||
// * @param accountingExtension The accounting extension used to bond and release tokens | ||
// * @param paymentToken The token used to pay for the request | ||
// * @param paymentAmount The amount of tokens to pay for the request | ||
// */ | ||
// struct RequestParameters { | ||
// string url; | ||
// string body; | ||
// HttpMethod method; | ||
// IAccountingExtension accountingExtension; | ||
// IERC20 paymentToken; | ||
// uint256 paymentAmount; | ||
// } | ||
/** | ||
* @notice Parameters of the request as stored in the module | ||
* @param url The url to make the request to | ||
* @param method The HTTP method to use for the request | ||
* @param body The HTTP body to use for the request | ||
* @param accountingExtension The accounting extension used to bond and release tokens | ||
* @param paymentToken The token used to pay for the request | ||
* @param paymentAmount The amount of tokens to pay for the request | ||
*/ | ||
struct RequestParameters { | ||
string url; | ||
string body; | ||
HttpMethod method; | ||
IAccountingExtension accountingExtension; | ||
IERC20 paymentToken; | ||
uint256 paymentAmount; | ||
} | ||
|
||
// /*/////////////////////////////////////////////////////////////// | ||
// ENUMS | ||
// //////////////////////////////////////////////////////////////*/ | ||
/*/////////////////////////////////////////////////////////////// | ||
ENUMS | ||
//////////////////////////////////////////////////////////////*/ | ||
|
||
// /** | ||
// * @notice Available HTTP methods | ||
// */ | ||
// enum HttpMethod { | ||
// GET, | ||
// POST | ||
// } | ||
/** | ||
* @notice Available HTTP methods | ||
*/ | ||
enum HttpMethod { | ||
GET, | ||
POST | ||
} | ||
|
||
// /*/////////////////////////////////////////////////////////////// | ||
// LOGIC | ||
// //////////////////////////////////////////////////////////////*/ | ||
/*/////////////////////////////////////////////////////////////// | ||
LOGIC | ||
//////////////////////////////////////////////////////////////*/ | ||
|
||
// /** | ||
// * @notice Returns the decoded data for a request | ||
// * @param _requestId The ID of the request | ||
// * @return _params The struct containing the parameters for the request | ||
// */ | ||
// function decodeRequestData(bytes32 _requestId) external view returns (RequestParameters memory _params); | ||
/** | ||
* @notice Returns the decoded data for a request | ||
* | ||
* @param _data The encoded request parameters | ||
* @return _params The struct containing the parameters for the request | ||
*/ | ||
function decodeRequestData(bytes calldata _data) external view returns (RequestParameters memory _params); | ||
|
||
// /** | ||
// * @notice Finalizes a request by paying the proposer if there is a valid response | ||
// * or releases the requester bond if no valid response was provided | ||
// * @param _requestId The ID of the request | ||
// */ | ||
// function finalizeRequest(bytes32 _requestId, address) external; | ||
// } | ||
/** | ||
* @notice Executes pre-request logic, bonding the requester's funds | ||
* | ||
* @param _requestId The id of the request | ||
* @param _data The encoded request parameters | ||
* @param _requester The user who triggered the request | ||
*/ | ||
function createRequest(bytes32 _requestId, bytes calldata _data, address _requester) external; | ||
|
||
/** | ||
* @notice Finalizes the request by paying the proposer for the response or releasing the requester's bond if no response was submitted | ||
* | ||
* @param _request The request that is being finalized | ||
* @param _response The final response | ||
* @param _finalizer The user who triggered the finalization | ||
*/ | ||
function finalizeRequest( | ||
IOracle.Request calldata _request, | ||
IOracle.Response calldata _response, | ||
address _finalizer | ||
) external; | ||
} |
Oops, something went wrong.