-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBeQuestERC20-V1.sol
150 lines (113 loc) · 4.29 KB
/
BeQuestERC20-V1.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol";
import "@chainlink/contracts/src/v0.8/AutomationCompatible.sol";
contract BequestERC20 is AutomationCompatibleInterface{
enum Status{
active,
inactive,
executed,
failed
}
struct will{
Status status;
uint id;
uint currentTime;
uint dedline;
uint amt;
string message;
string video;
address from;
address to;
address contractAddress;
}
uint public willCount;
mapping(address=>uint[]) public beneficiary;
mapping(address=>uint[]) public testator;
mapping(uint=>will) public idToWill;
will[] public willList;
function signWill(uint _dedline, uint _amt, address _to, address _contractAddress, string memory _message, string memory _video) public{
will memory temp;
temp.status = Status.active;
temp.id = willCount;
temp.currentTime = block.timestamp;
temp.dedline = block.timestamp + _dedline;
temp.amt = _amt;
temp.from = msg.sender;
temp.to = _to;
temp.contractAddress = _contractAddress;
temp.message = _message;
temp.video = _video;
willList.push(temp);
idToWill[willCount] = temp;
testator[msg.sender].push(willCount);
beneficiary[_to].push(willCount);
willCount++;
}
function editWill(uint _id, uint _dedline, uint _amt) public{
require(willList[_id].from == msg.sender, "You Can't Edit others will");
require(willList[_id].status != Status.inactive , "Your Will is inactive");
require(willList[_id].status != Status.executed , "Your Will is Already Executed");
require(willList[_id].status != Status.failed , "Your Will is Already failed");
will storage temp = willList[_id];
temp.dedline = _dedline;
temp.amt = _amt;
}
function stopWill(uint _id) public {
require(willList[_id].status != Status.active , "will is already inactive");
willList[_id].status = Status.inactive;
}
function resumeWill(uint _id) public {
require(willList[_id].status == Status.inactive , "will is already active");
willList[_id].status = Status.active;
}
function executeWill(uint _id) public{
require(willList[_id].status == Status.active , "will should be active");
will storage temp = willList[_id];
require(temp.dedline < block.timestamp , "time is pending");
uint examt = temp.amt;
bytes4 BLNC_OF = bytes4(keccak256("balanceOf(address)"));
bytes4 TRF_FROM = bytes4(keccak256("transferFrom(address,address,uint256)"));
(bool success1, bytes memory data1) = address(temp.contractAddress).call(abi.encodeWithSelector(BLNC_OF,temp.from));
if(data1.length == 32){
uint balance = abi.decode(data1, (uint256));
if(balance<temp.amt){
examt = balance;
}
(bool success2, bytes memory data2) = address(temp.contractAddress).call(abi.encodeWithSelector(TRF_FROM,temp.from,temp.to,examt));
if(success2){
temp.status = Status.executed;
}else{
temp.status = Status.failed;
}
}else{
temp.status = Status.failed;
}
}
function execution() public{
require(willList.length != 0, "No Will yet come");
for(uint i = 0 ; i < willList.length; i++){
if(willList[i].dedline < block.timestamp){
if(willList[i].status == Status.active){
executeWill(i);
}
}
}
}
function checkUpkeep(bytes calldata /* checkData */) external view override returns (bool upkeepNeeded, bytes memory /* performData */) {
require(willList.length != 0, "No Will yet come");
for(uint i = 0 ; i < willList.length; i++){
if(willList[i].dedline < block.timestamp){
if(willList[i].status == Status.active){
upkeepNeeded = true;
}
}
}
// We don't use the checkData in this example. The checkData is defined when the Upkeep was registered.
}
function performUpkeep(bytes calldata /* performData */) external override {
//We highly recommend revalidating the upkeep in the performUpkeep function
execution();
// We don't use the performData in this example. The performData is generated by the Automation Node's call to your checkUpkeep function
}
}