Skip to content

Commit

Permalink
Merge pull request #10 from defi-wonderland/perf/opo-600-http-request…
Browse files Browse the repository at this point in the history
…-module

perf: optimize `HttpRequestModule`
  • Loading branch information
0xmoebius authored Nov 12, 2023
2 parents 23c9a60 + efcd4da commit 432c850
Show file tree
Hide file tree
Showing 4 changed files with 351 additions and 421 deletions.
4 changes: 2 additions & 2 deletions docs/src/content/modules/request/http_request_module.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ The `HttpRequestModule` is a contract that allows users to request HTTP calls.

### Key Methods

- `decodeRequestData(bytes32 _requestId)`: This method decodes the data for a request given its ID. It returns the URL, HTTP method, body, accounting extension, payment token, and payment amount associated with the request.
- `finalizeRequest(bytes32 _requestId, address)`: This method finalizes a request by paying the proposer if there is a valid response, or releases the requester bond if no valid response was provided.
- `decodeRequestData`: This method decodes request parameters. It returns the URL, HTTP method, body, accounting extension, payment token, and payment amount from the given data.
- `finalizeRequest`: This method finalizes a request by paying the proposer if there is a valid response, or releases the requester bond if no valid response was provided.

### Request Parameters

Expand Down
125 changes: 62 additions & 63 deletions solidity/contracts/modules/request/HttpRequestModule.sol
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 solidity/interfaces/modules/request/IHttpRequestModule.sol
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;
}
Loading

0 comments on commit 432c850

Please sign in to comment.