-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.sol
More file actions
84 lines (72 loc) · 2.67 KB
/
main.sol
File metadata and controls
84 lines (72 loc) · 2.67 KB
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.6.0;
import "interface.sol";
contract main {
struct MetaData { // Struct with necessary data to do callback
uint payload;
uint64 timeInt;
address addr;
}
address errorAddress; // Address there is contract will be send message and raise error. It can be 0 or can be another contract
bool isRunning = false;
uint lastBlockTimestamp = 0;
MetaData[] binders; // Main array how contain data to callback
function delMeta(uint index) internal {
for (uint i = index; i + 1 < binders.length; ++i){
binders[i] = binders[i + 1]; // Method from ArrayHelper example. Delete element by number
}
binders.pop();
}
modifier onlyOwnerAndAccept {
require(msg.pubkey() == tvm.pubkey()); // Check owner
tvm.accept();
_;
}
function setCode(TvmCell newcode) public view onlyOwnerAndAccept {
tvm.setcode(newcode);
tvm.setCurrentCode(newcode);
}
function createHandler(uint _payload,uint64 _time) public {
require(msg.sender != address(0),100);
tvm.accept();
MetaData obj;
obj.addr = msg.sender;
obj.payload = _payload;
obj.timeInt = _time;
binders.push(obj);
if (isRunning == false) {
raiseError(errorAddress).error{value: 1 ton}();
}
}
function createTimer(uint _payload,uint64 _time) public {
require(msg.sender != address(0),100);
tvm.accept();
MetaData obj;
obj.addr = msg.sender;
obj.payload = _payload;
obj.timeInt = _time + now;
binders.push(obj);
if (isRunning == false) {
raiseError(errorAddress).error{value: 1 ton}();
}
}
function handler() public {
// protection of gas leaking
require(now > lastBlockTimestamp,101,"Double calling");
lastBlockTimestamp = now;
// For loop to check all handlers
for (uint i = 0; i < binders.length; i++) {
if (binders[i].timeInt <= lastBlockTimestamp) {
MetaData obj = binders[i]; // Save obj
delMeta(i); // Delete element from handlers
client(obj.addr)._timer_handler{value: 0}(obj.payload);
}
}
isRunning = binders.length > 0; // Check needing to work
if (isRunning) {
raiseError(errorAddress).error{value: 1 ton}();// Run another contract to run OnBounce throug one block (about 5 seconds in 0 workchain and about 0.2 in -1)
}
}
onBounce(TvmSlice slice) external {
handler(); // Handler to chceck timer
}
}