-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathColonyArbitraryTransaction.sol
188 lines (162 loc) · 6.57 KB
/
ColonyArbitraryTransaction.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// SPDX-License-Identifier: GPL-3.0-or-later
/*
This file is part of The Colony Network.
The Colony Network is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The Colony Network is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with The Colony Network. If not, see <http://www.gnu.org/licenses/>.
*/
pragma solidity 0.8.27;
pragma experimental ABIEncoderV2;
import { ERC20Extended } from "./../common/ERC20Extended.sol";
import { IEtherRouter } from "./../common/IEtherRouter.sol";
import { MultiChain } from "./../common/MultiChain.sol";
import { ITokenLocking } from "./../tokenLocking/ITokenLocking.sol";
import { ColonyStorage } from "./ColonyStorage.sol";
import { IColonyNetwork } from "./../colonyNetwork/IColonyNetwork.sol";
import { ColonyExtension } from "./../extensions/ColonyExtension.sol";
contract ColonyArbitraryTransaction is ColonyStorage {
bytes4 constant APPROVE_SIG = bytes4(keccak256("approve(address,uint256)"));
bytes4 constant TRANSFER_SIG = bytes4(keccak256("transfer(address,uint256)"));
bytes4 constant TRANSFER_FROM_SIG = bytes4(keccak256("transferFrom(address,address,uint256)"));
bytes4 constant BURN_SIG = bytes4(keccak256("burn(uint256)"));
bytes4 constant BURN_GUY_SIG = bytes4(keccak256("burn(address,uint256)"));
function makeArbitraryTransaction(
address _to,
bytes memory _action
) public stoppable auth returns (bool) {
return this.makeSingleArbitraryTransaction(_to, _action);
}
function makeArbitraryTransactions(
address[] memory _targets,
bytes[] memory _actions,
bool _strict
) public stoppable auth returns (bool) {
require(_targets.length == _actions.length, "colony-targets-and-actions-length-mismatch");
for (uint256 i; i < _targets.length; i += 1) {
bool success = true;
// slither-disable-next-line unused-return
try this.makeSingleArbitraryTransaction(_targets[i], _actions[i]) returns (bool ret) {
if (_strict) {
success = ret;
}
} catch {
// We failed in a require, which is only okay if we're not in strict mode
if (_strict) {
success = false;
}
}
require(success, "colony-arbitrary-transaction-failed");
}
return true;
}
function makeSingleArbitraryTransaction(
address _to,
bytes memory _action
) external stoppable self returns (bool) {
// Prevent transactions to network contracts
require(_to != address(this), "colony-cannot-target-self");
require(_to != colonyNetworkAddress, "colony-cannot-target-network");
require(_to != tokenLockingAddress, "colony-cannot-target-token-locking");
// Prevent transactions to transfer held tokens
bytes4 sig;
assembly {
sig := mload(add(_action, 0x20))
}
if (sig == APPROVE_SIG) {
approveTransactionPreparation(_to, _action);
} else if (sig == BURN_SIG) {
burnTransactionPreparation(_to, _action);
} else if (sig == TRANSFER_SIG) {
transferTransactionPreparation(_to, _action);
} else if (sig == BURN_GUY_SIG || sig == TRANSFER_FROM_SIG) {
burnGuyOrTransferFromTransactionPreparation(_action);
}
// Prevent transactions to network-managed extensions installed in this colony
require(isContract(_to), "colony-to-must-be-contract");
// slither-disable-next-line unused-return
try ColonyExtension(_to).identifier() returns (bytes32 extensionId) {
require(
IColonyNetwork(colonyNetworkAddress).getExtensionInstallation(extensionId, address(this)) !=
_to,
"colony-cannot-target-extensions"
);
} catch {}
bool res = executeCall(_to, 0, _action);
if (sig == APPROVE_SIG) {
approveTransactionCleanup(_to, _action);
}
emit ArbitraryTransaction(_to, _action, res);
return res;
}
function approveTransactionPreparation(address _to, bytes memory _action) internal {
address spender;
assembly {
spender := mload(add(_action, 0x24))
}
updateApprovalAmountInternal(_to, spender, false);
}
function approveTransactionCleanup(address _to, bytes memory _action) internal {
address spender;
assembly {
spender := mload(add(_action, 0x24))
}
updateApprovalAmountInternal(_to, spender, true);
}
function burnTransactionPreparation(address _to, bytes memory _action) internal {
uint256 amount;
assembly {
amount := mload(add(_action, 0x24))
}
fundingPots[1].balance[_to] -= amount;
require(fundingPots[1].balance[_to] >= tokenApprovalTotals[_to], "colony-not-enough-tokens");
}
function transferTransactionPreparation(address _to, bytes memory _action) internal {
uint256 amount;
assembly {
amount := mload(add(_action, 0x44))
}
fundingPots[1].balance[_to] -= amount;
require(fundingPots[1].balance[_to] >= tokenApprovalTotals[_to], "colony-not-enough-tokens");
}
function burnGuyOrTransferFromTransactionPreparation(bytes memory _action) internal view {
address spender;
assembly {
spender := mload(add(_action, 0x24))
}
require(spender != address(this), "colony-cannot-spend-own-allowance");
}
function updateApprovalAmount(address _token, address _spender) public stoppable {
updateApprovalAmountInternal(_token, _spender, false);
}
function updateApprovalAmountInternal(
address _token,
address _spender,
bool _postApproval
) internal {
uint256 recordedApproval = tokenApprovals[_token][_spender];
uint256 actualApproval = ERC20Extended(_token).allowance(address(this), _spender);
if (recordedApproval == actualApproval) {
return;
}
if (recordedApproval > actualApproval && !_postApproval) {
// They've spend some tokens out of root. Adjust balances accordingly
// If we are post approval, then they have not spent tokens
fundingPots[1].balance[_token] =
(fundingPots[1].balance[_token] - recordedApproval) +
actualApproval;
}
tokenApprovalTotals[_token] = (tokenApprovalTotals[_token] - recordedApproval) + actualApproval;
require(
fundingPots[1].balance[_token] >= tokenApprovalTotals[_token],
"colony-approval-exceeds-balance"
);
tokenApprovals[_token][_spender] = actualApproval;
}
}