-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUsersStorage.sol
130 lines (108 loc) · 3.28 KB
/
UsersStorage.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
pragma ton-solidity ^0.42.0;
pragma AbiHeader expire;
pragma AbiHeader time;
pragma AbiHeader pubkey;
import 'interfaces/LotteryInterface.sol';
import 'utils/ArrayUtil.sol';
/**
* Error codes
* • 100 — Method only for owner
* • 101 — Method only for root
* • 200 — Invalid referral id
*/
contract UsersStorage is ArrayUtil {
/*************
* CONSTANTS *
*************/
/**************
* STRUCTURES *
**************/
struct UserInfo {
uint64 userId;
uint64 referralId;
}
/*************
* VARIABLES *
*************/
address private _rootAddress;
mapping(address => UserInfo) _userDB;
address[] private _userKeys;
uint64 static _seed;
/*************
* MODIFIERS *
*************/
modifier accept {
tvm.accept();
_;
}
modifier onlyOwner {
require(msg.pubkey() == tvm.pubkey(), 100, "Method only for owner");
_;
}
modifier onlyRoot {
require(msg.sender == _rootAddress, 100, "Method only for root");
_;
}
modifier validReferralId(uint64 referrerId) {
require(referrerId == 0 || (referrerId >= 0 && referrerId < _userKeys.length), 200, "Invalid referral id");
_;
}
/***************
* CONSTRUCTOR *
***************/
constructor(
address rootAddress
) public accept {
_rootAddress = rootAddress;
}
/***********
* GETTERS *
***********/
function getRootAddress() public view returns (address rootAddress) { return _rootAddress; }
function getUsersCount() public view returns (uint64 usersCount) { return uint64(_userKeys.length); }
function getUsers(uint64 offset, uint64 limit) public view returns (
address[] users,
uint64[] userIds,
uint64[] referals,
uint64 totalLength
) {
uint64 endIndex = _getEndIndex(offset, limit, _userKeys.length);
for (uint64 i = offset; i < endIndex; i++) {
UserInfo info = _userDB[_userKeys[i]];
users.push(_userKeys[i]);
userIds.push(info.userId);
referals.push(info.referralId);
}
return (users, userIds, referals, uint64(users.length));
}
/***********************
* PUBLIC * ONLY OWNER *
***********************/
function setRoot(address rootAddress) public onlyOwner accept {
_rootAddress = rootAddress;
}
/************
* EXTERNAL *
************/
function dispatchWithReferral(address sender, uint128 value, uint64 referralId) external onlyRoot validReferralId(referralId) {
optional(UserInfo) info = _userDB.fetch(sender);
uint64 userId = 0;
UserInfo i;
if (info.hasValue()) {
i = info.get();
} else {
i = UserInfo(uint64(_userKeys.length), referralId);
_userDB[sender] = i;
_userKeys.push(sender);
userId = i.userId;
}
address referral = sender;
if (i.referralId > 0) {
referral = _userKeys[i.referralId];
}
LotteryInterface(_rootAddress).onReceiveReferralAddress{value: 0, flag: 128}(sender, value, referral, userId);
}
/***********
* PRIVATE *
***********/
}