@0x:contracts-staking Add a batchExecute function to the staking contracts

This commit is contained in:
Alex Towle
2019-09-12 00:01:23 -07:00
parent 7ba6c601e5
commit 2ed63970d4
3 changed files with 72 additions and 0 deletions

View File

@@ -22,6 +22,7 @@ import "@0x/contracts-utils/contracts/src/Ownable.sol";
import "./libs/LibProxy.sol";
import "./immutable/MixinStorage.sol";
import "./interfaces/IStorageInit.sol";
import "./interfaces/IStaking.sol";
import "./interfaces/IStakingEvents.sol";
import "./interfaces/IStakingProxy.sol";
@@ -71,6 +72,36 @@ contract StakingProxy is
_attachStakingContract(_stakingContract);
}
/// @dev Batch executes a series of calls to the exchange contract.
/// @param data An array of data that encodes a sequence of functions to
/// call in the staking contracts.
function batchExecute(bytes[] memory data)
public
returns (bytes[] memory batchReturnData)
{
bool success;
bytes memory returnData;
batchReturnData = new bytes[](data.length);
// Execute all of the calls encoded in the provided calldata.
for (uint256 i = 0; i < data.length; i++) {
// Call the staking contract with the provided calldata.
(success, returnData) = stakingContract.simpleProxyCallWithData(data[i]);
// Revert on failure.
if (!success) {
assembly {
revert(add(0x20, returnData), mload(returnData))
}
}
// Add the returndata to the batch returndata.
batchReturnData[i] = returnData;
}
return batchReturnData
}
/// @dev Detach the current staking contract.
/// Note that this is callable only by this contract's owner.
function detachStakingContract()

View File

@@ -17,10 +17,25 @@
*/
pragma solidity ^0.5.9;
pragma experimental ABIEncoderV2;
import "./IStructs.sol";
interface IStaking {
/// @dev Moves stake between statuses: 'active', 'inactive' or 'delegated'.
/// This change comes into effect next epoch.
/// @param from status to move stake out of.
/// @param to status to move stake into.
/// @param amount of stake to move.
function moveStake(
IStructs.StakeInfo calldata from,
IStructs.StakeInfo calldata to,
uint256 amount
)
external;
/// @dev Pays a protocol fee in ETH.
/// @param makerAddress The address of the order's maker.
/// @param payerAddress The address that is responsible for paying the protocol fee.
@@ -32,4 +47,10 @@ interface IStaking {
)
external
payable;
/// @dev Stake ZRX tokens. Tokens are deposited into the ZRX Vault. Unstake to retrieve the ZRX.
/// Stake is in the 'Active' status.
/// @param amount of ZRX to stake.
function stake(uint256 amount)
external;
}

View File

@@ -109,4 +109,24 @@ library LibProxy {
return(0, returndatasize())
}
}
/// @dev Proxies incoming call to destination contract with specified calldata.
/// @param destination Address to call.
/// @param data The calldata that will be sent to the destination.
function simpleProxyCallWithData(
address destination,
bytes memory data
)
internal
returns (bool, bytes memory)
{
if (destination == address(0)) {
LibRichErrors.rrevert(
LibStakingRichErrors.ProxyDestinationCannotBeNil()
);
}
// delegatecall into the destination address, and return the result.
return destination.delegatecall(data);
}
}