-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransactionProxy.sol
173 lines (143 loc) · 4.95 KB
/
TransactionProxy.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
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity >= 0.6.0 < 0.7.0;
pragma experimental ABIEncoderV2;
import "./lib/dapphub/auth.sol";
import "./lib/dapphub/note.sol";
import "./lib/DSProxyCache.sol";
import "./lib/ChiGasSaver.sol";
import "./lib/TokenInterface.sol";
contract TransactionProxy is ChiGasSaver, DSAuth, DSNote {
DSProxyCache public cache;
address gasTank;
struct Call {
address _target;
uint256 _value;
bytes _callData;
}
fallback() external payable {
}
constructor (address _cacheAddr, address _gasTank)
public {
setCache(_cacheAddr);
setGasTank(_gasTank);
}
// proxy actions
function deploy(bytes memory _code)
public
payable
auth
returns (address target) {
target = cache.read(_code);
if (target == address(0)) {
// deploy contract & store its address in cache
target = cache.write(_code);
}
}
function deployAndBurn(bytes memory _code)
public
payable
auth
saveGas(payable(gasTank))
returns (address target) {
target = cache.read(_code);
if (target == address(0)) {
// deploy contract & store its address in cache
target = cache.write(_code);
}
}
function execute(bytes memory _code, bytes memory _data)
public
payable
auth
returns (address target, bytes memory response) {
target = cache.read(_code);
if (target == address(0)) {
// deploy contract & store its address in cache
target = cache.write(_code);
}
response = _execute(target, _data);
}
function executeAndBurn(bytes memory _code, bytes memory _data)
public
payable
auth
saveGas(payable(gasTank))
returns (address target, bytes memory response) {
target = cache.read(_code);
if (target == address(0)) {
// deploy contract & store its address in cache
target = cache.write(_code);
}
response = _execute(target, _data);
}
function runScript(Call[] memory calls)
public
payable
returns (bytes[] memory returnData) {
returnData = new bytes[](calls.length);
for(uint8 i = 0; i < calls.length; i++) {
(bool success, bytes memory data) = calls[i]._target.call.value(calls[i]._value)(calls[i]._callData);
require(success, string(data));
returnData[i] = data;
}
}
function runScriptAndBurn(Call[] memory calls)
public
payable
saveGas(payable(gasTank))
returns (bytes[] memory returnData) {
returnData = new bytes[](calls.length);
for(uint8 i = 0; i < calls.length; i++) {
(bool success, bytes memory data) = calls[i]._target.call.value(calls[i]._value)(calls[i]._callData);
require(success, string(data));
returnData[i] = data;
}
}
function _execute(address _target, bytes memory _data)
public
auth
note
payable
returns (bytes memory response) {
require(_target != address(0), "ds-proxy-target-address-required");
// call contract in current context
assembly {
let succeeded := delegatecall(sub(gas(), 5000), _target, add(_data, 0x20), mload(_data), 0, 0)
// let succeeded := delegatecall(gas(), _target, add(_data, 0x20), mload(_data), 0, 0)
let size := returndatasize()
response := mload(0x40)
mstore(0x40, add(response, and(add(add(size, 0x20), 0x1f), not(0x1f))))
mstore(response, size)
returndatacopy(add(response, 0x20), 0, size)
switch iszero(succeeded)
case 1 {
// throw if delegatecall failed
revert(add(response, 0x20), size)
}
}
}
// address updates
function setCache (address _cacheAddr) public auth note returns (bool) {
require (_cacheAddr != address(0), "ds-proxy-cache-address-required");
cache = DSProxyCache(_cacheAddr); // overwrite cache
return true;
}
function setGasTank (address _gasTank) public auth note returns (bool) {
require (_gasTank != address(0), "gasTank address required");
gasTank = _gasTank;
return true;
}
// token functions
function transfer(address token, address guy, uint wad) public auth {
require(TokenInterface(token).transfer(guy, wad), "Token transfer failed");
}
function approve(address token, address guy, uint wad) public auth {
TokenInterface(token).approve(guy, wad);
}
function deposit(address token) public payable auth {
TokenInterface(token).deposit{value: msg.value}();
}
function withdraw(address token, uint wad) public auth {
TokenInterface(token).withdraw(wad);
}
}