-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathProtocol.sol
87 lines (78 loc) · 2.66 KB
/
Protocol.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
/**
* Copyright 2017-2021, bZeroX, LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0.
*/
pragma solidity 0.5.17;
pragma experimental ABIEncoderV2;
import "./State.sol";
/**
* @title Sovryn Protocol contract.
* @notice This contract code comes from bZx. bZx is a protocol for tokenized
* margin trading and lending https://bzx.network similar to the dYdX protocol.
*
* This contract contains the proxy functionality to deploy Protocol anchor
* and logic apart, turning it upgradable.
*
* @dev TODO: can I change this proxy to EIP-1822 proxy standard, please.
* https://eips.ethereum.org/EIPS/eip-1822
* */
contract sovrynProtocol is State {
/**
* @notice Fallback function performs a delegate call
* to the actual implementation address is pointing this proxy.
* Returns whatever the implementation call returns.
* */
function() external payable {
if (gasleft() <= 2300) {
return;
}
address target = logicTargets[msg.sig];
require(target != address(0), "target not active");
bytes memory data = msg.data;
assembly {
let result := delegatecall(gas, target, add(data, 0x20), mload(data), 0, 0)
let size := returndatasize
let ptr := mload(0x40)
returndatacopy(ptr, 0, size)
switch result
case 0 {
revert(ptr, size)
}
default {
return(ptr, size)
}
}
}
/**
* @notice External owner target initializer.
* @param target The target addresses.
* */
function replaceContract(address target) external onlyOwner {
(bool success, ) = target.delegatecall(
abi.encodeWithSignature("initialize(address)", target)
);
require(success, "setup failed");
}
/**
* @notice External owner setter for target addresses.
* @param sigsArr The array of signatures.
* @param targetsArr The array of addresses.
* */
function setTargets(
string[] calldata sigsArr,
address[] calldata targetsArr
) external onlyOwner {
require(sigsArr.length == targetsArr.length, "count mismatch");
for (uint256 i = 0; i < sigsArr.length; i++) {
_setTarget(bytes4(keccak256(abi.encodePacked(sigsArr[i]))), targetsArr[i]);
}
}
/**
* @notice External getter for target addresses.
* @param sig The signature.
* @return The address for a given signature.
* */
function getTarget(string calldata sig) external view returns (address) {
return logicTargets[bytes4(keccak256(abi.encodePacked(sig)))];
}
}