Skip to content

Commit

Permalink
refactor: remove internal decodeRequestData
Browse files Browse the repository at this point in the history
  • Loading branch information
0xShaito committed Jul 26, 2023
1 parent 88adebf commit 4a8f8e9
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 117 deletions.
2 changes: 1 addition & 1 deletion solidity/contracts/modules/ArbitratorModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract ArbitratorModule is Module, IArbitratorModule {
}

// get the arbitrator address for a dispute (the same arbitrator is fixed for a given request)
function decodeRequestData(bytes32 _requestId) external view returns (address _arbitrator) {
function decodeRequestData(bytes32 _requestId) public view returns (address _arbitrator) {
// Get the arbitrator address associated with the request id
_arbitrator = abi.decode(requestData[_requestId], (address));
}
Expand Down
36 changes: 8 additions & 28 deletions solidity/contracts/modules/BondEscalationModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ contract BondEscalationModule is Module, IBondEscalationModule {

if (_dispute.requestId == bytes32(0)) revert BondEscalationModule_DisputeDoesNotExist();

(,,,, uint256 _bondEscalationDeadline, uint256 _tyingBuffer) = _decodeRequestData(requestData[_dispute.requestId]);
(,,,, uint256 _bondEscalationDeadline, uint256 _tyingBuffer) = decodeRequestData(_dispute.requestId);

// If the bond escalation deadline is not over, no dispute can be escalated
if (block.timestamp <= _bondEscalationDeadline) revert BondEscalationModule_BondEscalationNotOver();
Expand Down Expand Up @@ -93,7 +93,7 @@ contract BondEscalationModule is Module, IBondEscalationModule {
uint256 _bondSize,
,
uint256 _bondEscalationDeadline,
) = _decodeRequestData(requestData[_requestId]);
) = decodeRequestData(_requestId);

// if the bond escalation is not over and there's an active dispute going through it, revert
if (block.timestamp <= _bondEscalationDeadline && bondEscalationStatus[_requestId] == BondEscalationStatus.Active) {
Expand Down Expand Up @@ -123,7 +123,7 @@ contract BondEscalationModule is Module, IBondEscalationModule {

function updateDisputeStatus(bytes32 _disputeId, IOracle.Dispute memory _dispute) external onlyOracle {
(IBondEscalationAccounting _accountingExtension, IERC20 _bondToken, uint256 _bondSize,,,) =
_decodeRequestData(requestData[_dispute.requestId]);
decodeRequestData(_dispute.requestId);

bool _won = _dispute.status == IOracle.DisputeStatus.Won;

Expand Down Expand Up @@ -198,7 +198,7 @@ contract BondEscalationModule is Module, IBondEscalationModule {
uint256 _maxNumberOfEscalations,
uint256 _bondEscalationDeadline,
uint256 _tyingBuffer
) = _decodeRequestData(requestData[_dispute.requestId]);
) = decodeRequestData(_dispute.requestId);

if (_maxNumberOfEscalations == 0 || _bondSize == 0) revert BondEscalationModule_ZeroValue();

Expand Down Expand Up @@ -262,7 +262,7 @@ contract BondEscalationModule is Module, IBondEscalationModule {
uint256 _maxNumberOfEscalations,
uint256 _bondEscalationDeadline,
uint256 _tyingBuffer
) = _decodeRequestData(requestData[_dispute.requestId]);
) = decodeRequestData(_dispute.requestId);

if (_maxNumberOfEscalations == 0 || _bondSize == 0) revert BondEscalationModule_ZeroValue();

Expand Down Expand Up @@ -313,7 +313,7 @@ contract BondEscalationModule is Module, IBondEscalationModule {
,
uint256 _bondEscalationDeadline,
uint256 _tyingBuffer
) = _decodeRequestData(requestData[_requestId]);
) = decodeRequestData(_requestId);

if (block.timestamp <= _bondEscalationDeadline + _tyingBuffer) {
revert BondEscalationModule_BondEscalationNotOver();
Expand Down Expand Up @@ -370,7 +370,7 @@ contract BondEscalationModule is Module, IBondEscalationModule {
* party to tie if at the end of the initial deadline the pledgess weren't tied.
*/
function decodeRequestData(bytes32 _requestId)
external
public
view
returns (
IBondEscalationAccounting _accountingExtension,
Expand All @@ -382,7 +382,7 @@ contract BondEscalationModule is Module, IBondEscalationModule {
)
{
(_accountingExtension, _bondToken, _bondSize, _maxNumberOfEscalations, _bondEscalationDeadline, _tyingBuffer) =
_decodeRequestData(requestData[_requestId]);
abi.decode(requestData[_requestId], (IBondEscalationAccounting, IERC20, uint256, uint256, uint256, uint256));
}

/**
Expand Down Expand Up @@ -430,24 +430,4 @@ contract BondEscalationModule is Module, IBondEscalationModule {
}
}
}

////////////////////////////////////////////////////////////////////
// Pure Functions
////////////////////////////////////////////////////////////////////

function _decodeRequestData(bytes memory _data)
internal
pure
returns (
IBondEscalationAccounting _accountingExtension,
IERC20 _bondToken,
uint256 _bondSize,
uint256 _maxNumberOfEscalations,
uint256 _bondEscalationDeadline,
uint256 _tyingBuffer
)
{
(_accountingExtension, _bondToken, _bondSize, _maxNumberOfEscalations, _bondEscalationDeadline, _tyingBuffer) =
abi.decode(_data, (IBondEscalationAccounting, IERC20, uint256, uint256, uint256, uint256));
}
}
18 changes: 1 addition & 17 deletions solidity/contracts/modules/BondEscalationResolutionModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,7 @@ contract BondEscalationResolutionModule is Module, IBondEscalationResolutionModu
)
{
(_accountingExtension, _token, _percentageDiff, _pledgeThreshold, _timeUntilDeadline, _timeToBreakInequality) =
_decodeRequestData(requestData[_requestId]);
}

function _decodeRequestData(bytes memory _data)
internal
pure
returns (
IBondEscalationAccounting _accountingExtension,
IERC20 _token,
uint256 _percentageDiff,
uint256 _pledgeThreshold,
uint256 _timeUntilDeadline,
uint256 _timeToBreakInequality
)
{
(_accountingExtension, _token, _percentageDiff, _pledgeThreshold, _timeUntilDeadline, _timeToBreakInequality) =
abi.decode(_data, (IBondEscalationAccounting, IERC20, uint256, uint256, uint256, uint256));
abi.decode(requestData[_requestId], (IBondEscalationAccounting, IERC20, uint256, uint256, uint256, uint256));
}

function startResolution(bytes32 _disputeId) external onlyOracle {
Expand Down
18 changes: 5 additions & 13 deletions solidity/contracts/modules/BondedDisputeModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ contract BondedDisputeModule is Module, IBondedDisputeModule {
}

function decodeRequestData(bytes32 _requestId)
external
public
view
returns (IAccountingExtension _accountingExtension, IERC20 _bondToken, uint256 _bondSize)
{
(_accountingExtension, _bondToken, _bondSize) = _decodeRequestData(requestData[_requestId]);
(_accountingExtension, _bondToken, _bondSize) =
abi.decode(requestData[_requestId], (IAccountingExtension, IERC20, uint256));
}

function disputeEscalated(bytes32 _disputeId) external onlyOracle {}
Expand All @@ -41,15 +42,14 @@ contract BondedDisputeModule is Module, IBondedDisputeModule {
createdAt: block.timestamp
});

(IAccountingExtension _accountingExtension, IERC20 _bondToken, uint256 _bondSize) =
_decodeRequestData(requestData[_requestId]);
(IAccountingExtension _accountingExtension, IERC20 _bondToken, uint256 _bondSize) = decodeRequestData(_requestId);
_accountingExtension.bond(_disputer, _requestId, _bondToken, _bondSize);
}

// TODO: This doesn't handle the cases of unconclusive statuses
function updateDisputeStatus(bytes32, /* _disputeId */ IOracle.Dispute memory _dispute) external onlyOracle {
(IAccountingExtension _accountingExtension, IERC20 _bondToken, uint256 _bondSize) =
_decodeRequestData(requestData[_dispute.requestId]);
decodeRequestData(_dispute.requestId);
bool _won = _dispute.status == IOracle.DisputeStatus.Won;

_accountingExtension.pay(
Expand All @@ -64,12 +64,4 @@ contract BondedDisputeModule is Module, IBondedDisputeModule {
_won ? _dispute.disputer : _dispute.proposer, _dispute.requestId, _bondToken, _bondSize
);
}

function _decodeRequestData(bytes memory _data)
internal
pure
returns (IAccountingExtension _accountingExtension, IERC20 _bondToken, uint256 _bondSize)
{
(_accountingExtension, _bondToken, _bondSize) = abi.decode(_data, (IAccountingExtension, IERC20, uint256));
}
}
16 changes: 4 additions & 12 deletions solidity/contracts/modules/BondedResponseModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,12 @@ contract BondedResponseModule is Module, IBondedResponseModule {
constructor(IOracle _oracle) Module(_oracle) {}

function decodeRequestData(bytes32 _requestId)
external
public
view
returns (IAccountingExtension _accountingExtension, IERC20 _bondToken, uint256 _bondSize, uint256 _deadline)
{
(_accountingExtension, _bondToken, _bondSize, _deadline) = _decodeRequestData(requestData[_requestId]);
}

function _decodeRequestData(bytes memory _data)
internal
pure
returns (IAccountingExtension _accountingExtension, IERC20 _bondToken, uint256 _bondSize, uint256 _deadline)
{
(_accountingExtension, _bondToken, _bondSize, _deadline) =
abi.decode(_data, (IAccountingExtension, IERC20, uint256, uint256));
abi.decode(requestData[_requestId], (IAccountingExtension, IERC20, uint256, uint256));
}

function propose(
Expand All @@ -35,7 +27,7 @@ contract BondedResponseModule is Module, IBondedResponseModule {
bytes calldata _responseData
) external onlyOracle returns (IOracle.Response memory _response) {
(IAccountingExtension _accountingExtension, IERC20 _bondToken, uint256 _bondSize, uint256 _deadline) =
_decodeRequestData(requestData[_requestId]);
decodeRequestData(_requestId);

// Cannot propose after the deadline
if (block.timestamp >= _deadline) revert BondedResponseModule_TooLateToPropose();
Expand Down Expand Up @@ -66,7 +58,7 @@ contract BondedResponseModule is Module, IBondedResponseModule {

function finalizeRequest(bytes32 _requestId) external override(IModule, Module) onlyOracle {
(IAccountingExtension _accountingExtension, IERC20 _bondToken, uint256 _bondSize, uint256 _deadline) =
_decodeRequestData(requestData[_requestId]);
decodeRequestData(_requestId);

if (block.timestamp < _deadline) revert BondedResponseModule_TooEarlyToFinalize();

Expand Down
2 changes: 1 addition & 1 deletion solidity/contracts/modules/CallbackModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {IModule, Module} from '../Module.sol';
contract CallbackModule is Module, ICallbackModule {
constructor(IOracle _oracle) Module(_oracle) {}

function decodeRequestData(bytes32 _requestId) external view returns (address _target, bytes memory _data) {
function decodeRequestData(bytes32 _requestId) public view returns (address _target, bytes memory _data) {
(_target, _data) = abi.decode(requestData[_requestId], (address, bytes));
}

Expand Down
24 changes: 4 additions & 20 deletions solidity/contracts/modules/ContractCallRequestModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ contract ContractCallRequestModule is Module, IContractCallRequestModule {
constructor(IOracle _oracle) Module(_oracle) {}

function decodeRequestData(bytes32 _requestId)
external
public
view
returns (
address _target,
Expand All @@ -24,28 +24,12 @@ contract ContractCallRequestModule is Module, IContractCallRequestModule {
)
{
(_target, _functionSelector, _data, _accountingExtension, _paymentToken, _paymentAmount) =
_decodeRequestData(requestData[_requestId]);
}

function _decodeRequestData(bytes memory _encodedData)
internal
pure
returns (
address _target,
bytes4 _functionSelector,
bytes memory _data,
IAccountingExtension _accountingExtension,
IERC20 _paymentToken,
uint256 _paymentAmount
)
{
(_target, _functionSelector, _data, _accountingExtension, _paymentToken, _paymentAmount) =
abi.decode(_encodedData, (address, bytes4, bytes, IAccountingExtension, IERC20, uint256));
abi.decode(requestData[_requestId], (address, bytes4, bytes, IAccountingExtension, IERC20, uint256));
}

function _afterSetupRequest(bytes32 _requestId, bytes calldata _data) internal override {
(,,, IAccountingExtension _accountingExtension, IERC20 _paymentToken, uint256 _paymentAmount) =
_decodeRequestData(_data);
decodeRequestData(_requestId);
IOracle.Request memory _request = ORACLE.getRequest(_requestId);
_accountingExtension.bond(_request.requester, _requestId, _paymentToken, _paymentAmount);
}
Expand All @@ -54,7 +38,7 @@ contract ContractCallRequestModule is Module, IContractCallRequestModule {
IOracle.Request memory _request = ORACLE.getRequest(_requestId);
IOracle.Response memory _response = ORACLE.getFinalizedResponse(_requestId);
(,,, IAccountingExtension _accountingExtension, IERC20 _paymentToken, uint256 _paymentAmount) =
_decodeRequestData(requestData[_requestId]);
decodeRequestData(_requestId);
if (_response.createdAt != 0) {
_accountingExtension.pay(_requestId, _request.requester, _response.proposer, _paymentToken, _paymentAmount);
} else {
Expand Down
24 changes: 4 additions & 20 deletions solidity/contracts/modules/HttpRequestModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ contract HttpRequestModule is Module, IHttpRequestModule {
constructor(IOracle _oracle) Module(_oracle) {}

function decodeRequestData(bytes32 _requestId)
external
public
view
returns (
string memory _url,
Expand All @@ -24,37 +24,21 @@ contract HttpRequestModule is Module, IHttpRequestModule {
)
{
(_url, _method, _body, _accountingExtension, _paymentToken, _paymentAmount) =
_decodeRequestData(requestData[_requestId]);
abi.decode(requestData[_requestId], (string, HttpMethod, string, IAccountingExtension, IERC20, uint256));
}

function _afterSetupRequest(bytes32 _requestId, bytes calldata _data) internal override {
(,,, IAccountingExtension _accountingExtension, IERC20 _paymentToken, uint256 _paymentAmount) =
_decodeRequestData(_data);
decodeRequestData(_requestId);
IOracle.Request memory _request = ORACLE.getRequest(_requestId);
_accountingExtension.bond(_request.requester, _requestId, _paymentToken, _paymentAmount);
}

function _decodeRequestData(bytes memory _data)
internal
pure
returns (
string memory _url,
HttpMethod _method,
string memory _body,
IAccountingExtension _accountingExtension,
IERC20 _paymentToken,
uint256 _paymentAmount
)
{
(_url, _method, _body, _accountingExtension, _paymentToken, _paymentAmount) =
abi.decode(_data, (string, HttpMethod, string, IAccountingExtension, IERC20, uint256));
}

function finalizeRequest(bytes32 _requestId) external override(IModule, Module) onlyOracle {
IOracle.Request memory _request = ORACLE.getRequest(_requestId);
IOracle.Response memory _response = ORACLE.getFinalizedResponse(_requestId);
(,,, IAccountingExtension _accountingExtension, IERC20 _paymentToken, uint256 _paymentAmount) =
_decodeRequestData(requestData[_requestId]);
decodeRequestData(_requestId);
if (_response.createdAt != 0) {
_accountingExtension.pay(_requestId, _request.requester, _response.proposer, _paymentToken, _paymentAmount);
} else {
Expand Down
6 changes: 1 addition & 5 deletions solidity/contracts/modules/MultipleCallbacksModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import {IModule, Module} from '../Module.sol';
contract MultipleCallbacksModule is Module, ICallbackModule {
constructor(IOracle _oracle) Module(_oracle) {}

function decodeRequestData(bytes32 _requestId)
external
view
returns (address[] memory _targets, bytes[] memory _datas)
{
function decodeRequestData(bytes32 _requestId) public view returns (address[] memory _targets, bytes[] memory _datas) {
(_targets, _datas) = abi.decode(requestData[_requestId], (address[], bytes[]));
}

Expand Down

0 comments on commit 4a8f8e9

Please sign in to comment.