|
| 1 | +pragma solidity 0.4.24; |
| 2 | + |
| 3 | +import "openzeppelin-eth/contracts/ownership/Ownable.sol"; |
| 4 | + |
| 5 | +import "./UFragmentsPolicy.sol"; |
| 6 | + |
| 7 | + |
| 8 | +/** |
| 9 | + * @title Orchestrator |
| 10 | + * @notice The orchestrator is the main entry point for rebase operations. It coordinates the policy |
| 11 | + * actions with external consumers. |
| 12 | + */ |
| 13 | +contract Orchestrator is Ownable { |
| 14 | + |
| 15 | + struct Transaction { |
| 16 | + address destination; |
| 17 | + bytes data; |
| 18 | + bool enabled; |
| 19 | + } |
| 20 | + |
| 21 | + event TransactionFailed(address indexed destination, uint index, bytes data); |
| 22 | + |
| 23 | + // Stable ordering is not guaranteed. |
| 24 | + Transaction[] public transactions; |
| 25 | + |
| 26 | + UFragmentsPolicy public policy; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param policy_ Address of the UFragments policy. |
| 30 | + */ |
| 31 | + constructor(address policy_) public { |
| 32 | + Ownable.initialize(msg.sender); |
| 33 | + policy = UFragmentsPolicy(policy_); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @notice Main entry point to initiate a rebase operation. |
| 38 | + * The Orchestrator calls rebase on the policy and notifies downstream applications. |
| 39 | + * Contracts are guarded from calling, to avoid flash loan attacks on liquidity |
| 40 | + * providers. |
| 41 | + * If a transaction in the transaction list reverts, it is swallowed and the remaining |
| 42 | + * transactions are executed. |
| 43 | + */ |
| 44 | + function rebase() |
| 45 | + external |
| 46 | + { |
| 47 | + require(msg.sender == tx.origin); // solhint-disable-line avoid-tx-origin |
| 48 | + |
| 49 | + policy.rebase(); |
| 50 | + |
| 51 | + for (uint i = 0; i < transactions.length; i++) { |
| 52 | + Transaction storage t = transactions[i]; |
| 53 | + if (t.enabled) { |
| 54 | + bool result = |
| 55 | + externalCall(t.destination, 0, t.data.length, t.data); |
| 56 | + if (!result) { |
| 57 | + emit TransactionFailed(t.destination, i, t.data); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @notice Adds a transaction that gets called for a downstream receiver of rebases |
| 65 | + * @param destination Address of contract destination |
| 66 | + * @param data Transaction data payload |
| 67 | + */ |
| 68 | + function addTransaction(address destination, bytes data) |
| 69 | + external |
| 70 | + onlyOwner |
| 71 | + { |
| 72 | + transactions.push(Transaction({ |
| 73 | + destination: destination, |
| 74 | + data: data, |
| 75 | + enabled: true |
| 76 | + })); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param index Index of transaction to remove. |
| 81 | + * Transaction ordering may have changed since adding. |
| 82 | + */ |
| 83 | + function removeTransaction(uint index) |
| 84 | + external |
| 85 | + onlyOwner |
| 86 | + { |
| 87 | + require(index < transactions.length, "index out of bounds"); |
| 88 | + |
| 89 | + if (index < transactions.length - 1) { |
| 90 | + transactions[index] = transactions[transactions.length - 1]; |
| 91 | + } |
| 92 | + |
| 93 | + delete transactions[transactions.length - 1]; |
| 94 | + transactions.length--; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @param index Index of transaction. Transaction ordering may have changed since adding. |
| 99 | + * @param enabled True for enabled, false for disabled. |
| 100 | + */ |
| 101 | + function setTransactionEnabled(uint index, bool enabled) |
| 102 | + external |
| 103 | + onlyOwner |
| 104 | + { |
| 105 | + require(index < transactions.length, "index must be in range of stored tx list"); |
| 106 | + transactions[index].enabled = enabled; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @return Number of transactions, both enabled and disabled, in transactions list. |
| 111 | + */ |
| 112 | + function transactionsLength() |
| 113 | + external |
| 114 | + view |
| 115 | + returns (uint256) |
| 116 | + { |
| 117 | + return transactions.length; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @dev wrapper to call the encoded transactions on downstream consumers. |
| 122 | + * @param destination Address of destination contract. |
| 123 | + * @param transferValueWei ETH value to send, in wei. |
| 124 | + * @param dataLength Size of data param. |
| 125 | + * @param data The encoded data payload. |
| 126 | + * @return True on success |
| 127 | + */ |
| 128 | + function externalCall(address destination, uint transferValueWei, uint dataLength, bytes data) |
| 129 | + internal |
| 130 | + returns (bool) |
| 131 | + { |
| 132 | + bool result; |
| 133 | + assembly { // solhint-disable-line no-inline-assembly |
| 134 | + // "Allocate" memory for output |
| 135 | + // (0x40 is where "free memory" pointer is stored by convention) |
| 136 | + let outputAddress := mload(0x40) |
| 137 | + |
| 138 | + // First 32 bytes are the padded length of data, so exclude that |
| 139 | + let dataAddress := add(data, 32) |
| 140 | + |
| 141 | + result := call( |
| 142 | + // 34710 is the value that solidity is currently emitting |
| 143 | + // It includes callGas (700) + callVeryLow (3, to pay for SUB) |
| 144 | + // + callValueTransferGas (9000) + callNewAccountGas |
| 145 | + // (25000, in case the destination address does not exist and needs creating) |
| 146 | + sub(gas, 34710), |
| 147 | + |
| 148 | + |
| 149 | + destination, |
| 150 | + transferValueWei, |
| 151 | + dataAddress, |
| 152 | + dataLength, // Size of the input (in bytes). This is what fixes the padding problem |
| 153 | + outputAddress, |
| 154 | + 0 // Output is ignored, therefore the output size is zero |
| 155 | + ) |
| 156 | + } |
| 157 | + return result; |
| 158 | + } |
| 159 | +} |
0 commit comments