Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: optimize ContractCallRequestModule #9

Merged
merged 10 commits into from
Nov 12, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ The `ContractCallRequestModule` is a module for requesting on-chain information.

### Key Methods

- `decodeRequestData(bytes32 _requestId)`: This method decodes the request data for a given request ID. It returns the target contract address, the function selector, the encoded arguments of the function to call, the accounting extension to bond and release funds, the payment token, and the payment amount.
- `finalizeRequest(bytes32 _requestId, address)`: This method finalizes a request by paying the response proposer. It is only callable by the oracle.
- `decodeRequestData`: This method decodes the request data for a given request ID. It returns the target contract address, the function selector, the encoded arguments of the function to call, the accounting extension to bond and release funds, the payment token, and the payment amount.
- `finalizeRequest`: This method finalizes a request by paying the response proposer. It is only callable by the oracle.

### Request Parameters

Expand Down
112 changes: 55 additions & 57 deletions solidity/contracts/modules/request/ContractCallRequestModule.sol
Original file line number Diff line number Diff line change
@@ -1,64 +1,62 @@
// // SPDX-License-Identifier: MIT
// pragma solidity ^0.8.19;
// 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';
// 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 {IContractCallRequestModule} from '../../../interfaces/modules/request/IContractCallRequestModule.sol';
import {IContractCallRequestModule} from '../../../interfaces/modules/request/IContractCallRequestModule.sol';

// contract ContractCallRequestModule is Module, IContractCallRequestModule {
// constructor(IOracle _oracle) Module(_oracle) {}
contract ContractCallRequestModule is Module, IContractCallRequestModule {
constructor(IOracle _oracle) Module(_oracle) {}

// /// @inheritdoc IModule
// function moduleName() public pure returns (string memory _moduleName) {
// _moduleName = 'ContractCallRequestModule';
// }
/// @inheritdoc IModule
function moduleName() public pure returns (string memory _moduleName) {
_moduleName = 'ContractCallRequestModule';
}

// /// @inheritdoc IContractCallRequestModule
// function decodeRequestData(bytes32 _requestId) public view returns (RequestParameters memory _params) {
// _params = abi.decode(requestData[_requestId], (RequestParameters));
// }
/// @inheritdoc IContractCallRequestModule
function decodeRequestData(bytes calldata _data) public pure returns (RequestParameters memory _params) {
_params = abi.decode(_data, (RequestParameters));
}

// /**
// * @notice Bonds the requester's funds through the accounting extension
// * @param _requestId The id of the request being set up
// */
// 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 IContractCallRequestModule
function createRequest(bytes32 _requestId, bytes calldata _data, address _requester) external onlyOracle {
RequestParameters memory _params = decodeRequestData(_data);

// /// @inheritdoc IContractCallRequestModule
// function finalizeRequest(
// bytes32 _requestId,
// address _finalizer
// ) external override(IContractCallRequestModule, 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);
// }
// }
_params.accountingExtension.bond({
_bonder: _requester,
_requestId: _requestId,
_token: _params.paymentToken,
_amount: _params.paymentAmount
});
}

/// @inheritdoc IContractCallRequestModule
function finalizeRequest(
IOracle.Request calldata _request,
IOracle.Response calldata _response,
address _finalizer
) external override(IContractCallRequestModule, 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);
}
}
98 changes: 58 additions & 40 deletions solidity/interfaces/modules/request/IContractCallRequestModule.sol
Original file line number Diff line number Diff line change
@@ -1,45 +1,63 @@
// // 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 {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';
import {IAccountingExtension} from '../../../interfaces/extensions/IAccountingExtension.sol';

// /**
// * @title ContractCallRequestModule
// * @notice Request module for making contract calls
// */
// interface IContractCallRequestModule is IRequestModule {
// /**
// * @notice Parameters of the request as stored in the module
// * @param target The address of the contract to do the call on
// * @param functionSelector The selector of the function to call
// * @param data The encoded arguments of the function to call (optional)
// * @param accountingExtension The accounting extension to bond and release funds
// * @param paymentToken The token in which the response proposer will be paid
// * @param paymentAmount The amount of `paymentToken` to pay to the response proposer
// */
// struct RequestParameters {
// address target;
// bytes4 functionSelector;
// bytes data;
// IAccountingExtension accountingExtension;
// IERC20 paymentToken;
// uint256 paymentAmount;
// }
/**
* @title ContractCallRequestModule
* @notice Request module for making contract calls
*/
interface IContractCallRequestModule is IRequestModule {
/**
* @notice Parameters of the request as stored in the module
* @param target The address of the contract to do the call on
* @param functionSelector The selector of the function to call
* @param data The encoded arguments of the function to call (optional)
* @param accountingExtension The accounting extension to bond and release funds
* @param paymentToken The token in which the response proposer will be paid
* @param paymentAmount The amount of `paymentToken` to pay to the response proposer
*/
struct RequestParameters {
address target;
bytes4 functionSelector;
bytes data;
IAccountingExtension accountingExtension;
IERC20 paymentToken;
uint256 paymentAmount;
}

// /**
// * @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 response proposer
// * @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
Loading