-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfinal contract v2
165 lines (131 loc) · 4.95 KB
/
final contract v2
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// 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
}
address gnosisSafeAddress;
struct will{
Status status;
uint id;
uint dedline;
uint amt;
uint decimal;
string message;
string video;
string info;
string tokenName;
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;
constructor(address _x){
gnosisSafeAddress = _x;
}
function signWill(string memory _tokenName,uint _dedline, uint _amt, address _to, address _contractAddress, string memory _message, string memory _video, uint _decimal) public payable{
will memory temp;
temp.status = Status.active;
temp.tokenName = _tokenName;
temp.id = willCount;
temp.dedline = block.timestamp + _dedline;
temp.decimal = _decimal;
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++;
gnosisSafeAddress.call{value : msg.value}("");
}
function extendtWill(uint _id, uint _dedline) public{
require(willList[_id].from == msg.sender, "You Can't Edit others will");
require(willList[_id].status == Status.active, "Your Will is inactive");
will storage temp = willList[_id];
temp.dedline = block.timestamp + _dedline;
}
function stopWill(uint _id) public {
require(willList[_id].from == msg.sender, "You Can't Edit others will");
require(willList[_id].status == Status.active, "will is not active");
willList[_id].status = Status.inactive;
}
function resumeWill(uint _id) public {
require(willList[_id].from == msg.sender, "You Can't Edit others will");
require(willList[_id].status == Status.inactive , "will is already active");
willList[_id].status = Status.active;
}
function executeWill(uint _id) public{
uint aaa = 0;
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;
aaa = 1;
}
(bool success2, bytes memory data2) = address(temp.contractAddress).call(abi.encodeWithSelector(TRF_FROM,temp.from,temp.to,examt));
if(success2){
temp.status = Status.executed;
if(aaa==1) temp.info = "Success but Testator had low balance than Reqest amount and you got all of it.";
}else{
temp.status = Status.failed;
//allownace was not enough for amt
}
}else{
temp.status = Status.failed;
//contract was not erc
}
}
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
}
function getAllUsersId(address _add) view public returns(uint[] memory){
return testator[_add];
}
function getAllBeneficiaryId(address _add) view public returns(uint[] memory){
return beneficiary[_add];
}
}