-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersonalVaultFactory.sol
104 lines (85 loc) · 2.87 KB
/
personalVaultFactory.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
pragma solidity ^0.4.24;
interface TokenERC20 {
function balanceOf(address owner) view external returns(uint256);
function allowance(address owner, address spender) view external returns(uint256);
function transfer(address to, uint256 value) external returns(bool);
function transferFrom(address from, address to, uint256 value) external returns(bool);
function approve(address spender, uint256 value) external returns(bool);
}
contract walletOwnership {
address public owner;
address public recovery;
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
modifier onlyRecovery(){
require(msg.sender == recovery);
_;
}
modifier onlyOwnerOrRecovery(){
require(msg.sender == owner || msg.sender == recovery);
_;
}
function setOwner(address _owner) onlyRecovery external{
owner = _owner;
}
function setRecovery(address _recovery) onlyRecovery external{
recovery = _recovery;
}
}
contract personalVault is walletOwnership {
mapping(address => uint256) public dailyLimit;
mapping(address => uint256) public lastDay;
mapping(address => uint256) public spentToday;
event WithdrawEther(uint256 amount);
event WithdrawToken(address token, uint256 amount);
constructor(address _owner, address _recovery) public {
owner = _owner;
recovery = _recovery;
}
function ()public payable{
}
function withdraw(address token, uint256 amount) onlyOwnerOrRecovery external{
require(isUnderLimit(token, amount) || msg.sender == recovery);
if (token == address(0)){
if(owner.send(amount)){
spentToday[address(0)] += amount;
emit WithdrawEther(amount);
}
}
else{
if(TokenERC20(token).transfer(owner,amount)){
spentToday[token] += amount;
emit WithdrawToken(token, amount);
}
}
}
function setLimit(address token, uint256 limit) onlyOwnerOrRecovery external{
if(msg.sender == recovery
|| dailyLimit[token] == 0
|| dailyLimit[token] > limit)
{
dailyLimit[token] = limit;
}
}
function isUnderLimit(address token, uint256 amount)
internal
returns (bool)
{
if (now > lastDay[token] + 24 hours) {
lastDay[token] = now;
spentToday[token] = 0;
}
if (spentToday[token] + amount > dailyLimit[token] || spentToday[token] + amount < spentToday[token])
return false;
return true;
}
}
contract personalVaultFactory {
mapping(address => bool) public isVault;
function create(address _owner, address _recovery) public returns (address vault) {
vault = new personalVault(_owner, _recovery);
isVault[vault] = true;
}
}