-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathProxy.sol
54 lines (45 loc) · 1.39 KB
/
Proxy.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
contract Proxy {
bool _traceFunctionCalls = false;
event Trace(address caller, bytes data, uint value);
function Proxy(bool traceFunctionCalls) {
_traceFunctionCalls = traceFunctionCalls;
}
function setMock(bytes4 method, uint8 operationType, address target, bytes data) {
operations[method] = Operation(operationType, target, data);
}
function setMockWithArgs(bytes methodWithData, uint8 operationType, address target, bytes data) {
operationsWithArgs[methodWithData] = Operation(operationType, target, data);
}
function() {
if (_traceFunctionCalls) {
Trace(msg.sender, msg.data, msg.value);
}
Operation memory operation = operationsWithArgs[msg.data];
uint8 operationType = operation.operationType;
if (operationType == 0) {
operation = operations[msg.sig];
operationType = operation.operationType;
if (operationType == 0) {
throw;
}
}
if (operationType == 1) {
address target = operation.target;
var retVal = target.call(operation.data);
} else if (operationType == 2) {
bytes memory data = operation.data;
assembly {
return(add(data, 32), mload(data))
}
} else {
throw;
}
}
struct Operation {
uint8 operationType;
address target;
bytes data;
}
mapping (bytes4 => Operation) operations;
mapping (bytes => Operation) operationsWithArgs;
}