-
Notifications
You must be signed in to change notification settings - Fork 2
/
SnowflakeResolver.sol
84 lines (68 loc) · 3.41 KB
/
SnowflakeResolver.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
pragma solidity ^0.5.0;
import "./zeppelin/ownership/Ownable.sol";
import "./interfaces/HydroInterface.sol";
import "./interfaces/SnowflakeInterface.sol";
import "./interfaces/SnowflakeResolverInterface.sol";
contract SnowflakeResolver is Ownable {
string public snowflakeName;
string public snowflakeDescription;
address public snowflakeAddress;
bool public callOnAddition;
bool public callOnRemoval;
constructor(
string memory _snowflakeName, string memory _snowflakeDescription,
address _snowflakeAddress,
bool _callOnAddition, bool _callOnRemoval
)
public
{
snowflakeName = _snowflakeName;
snowflakeDescription = _snowflakeDescription;
setSnowflakeAddress(_snowflakeAddress);
callOnAddition = _callOnAddition;
callOnRemoval = _callOnRemoval;
}
modifier senderIsSnowflake() {
require(msg.sender == snowflakeAddress, "Did not originate from Snowflake.");
_;
}
// this can be overriden to initialize other variables, such as e.g. an ERC20 object to wrap the HYDRO token
function setSnowflakeAddress(address _snowflakeAddress) public onlyOwner {
snowflakeAddress = _snowflakeAddress;
}
// if callOnAddition is true, onAddition is called every time a user adds the contract as a resolver
// this implementation **must** use the senderIsSnowflake modifier
// returning false will disallow users from adding the contract as a resolver
function onAddition(uint ein, uint allowance, bytes memory extraData) public returns (bool);
// if callOnRemoval is true, onRemoval is called every time a user removes the contract as a resolver
// this function **must** use the senderIsSnowflake modifier
// returning false soft prevents users from removing the contract as a resolver
// however, note that they can force remove the resolver, bypassing onRemoval
function onRemoval(uint ein, bytes memory extraData) public returns (bool);
function transferHydroBalanceTo(uint einTo, uint amount) internal {
HydroInterface hydro = HydroInterface(SnowflakeInterface(snowflakeAddress).hydroTokenAddress());
require(hydro.approveAndCall(snowflakeAddress, amount, abi.encode(einTo)), "Unsuccessful approveAndCall.");
}
function withdrawHydroBalanceTo(address to, uint amount) internal {
HydroInterface hydro = HydroInterface(SnowflakeInterface(snowflakeAddress).hydroTokenAddress());
require(hydro.transfer(to, amount), "Unsuccessful transfer.");
}
function transferHydroBalanceToVia(address via, uint einTo, uint amount, bytes memory snowflakeCallBytes) internal {
HydroInterface hydro = HydroInterface(SnowflakeInterface(snowflakeAddress).hydroTokenAddress());
require(
hydro.approveAndCall(
snowflakeAddress, amount, abi.encode(true, address(this), via, einTo, snowflakeCallBytes)
),
"Unsuccessful approveAndCall."
);
}
function withdrawHydroBalanceToVia(address via, address to, uint amount, bytes memory snowflakeCallBytes) internal {
HydroInterface hydro = HydroInterface(SnowflakeInterface(snowflakeAddress).hydroTokenAddress());
require(
hydro.approveAndCall(
snowflakeAddress, amount, abi.encode(false, address(this), via, to, snowflakeCallBytes)
),
"Unsuccessful approveAndCall."
);
}
}