-
Notifications
You must be signed in to change notification settings - Fork 0
/
autodelegate.cpp
69 lines (53 loc) · 1.92 KB
/
autodelegate.cpp
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
#include <eosio/asset.hpp>
#include <eosio/eosio.hpp>
CONTRACT autodelegate : public eosio::contract {
public:
using eosio::contract::contract;
[[eosio::on_notify("eosio.token::transfer")]] void receive_token_transfer(
eosio::name & from, eosio::name & to, eosio::asset & quantity,
std::string & memo) {
if (from == get_self() || to != get_self()) {
return;
}
if (memo.rfind("stake:", 0) != 0) {
return;
}
auto adu = autodelusers(get_self(), get_self().value);
adu.require_find(from.value, "User is not on the allow list.");
eosio::check(get_first_receiver() == eosio::name("eosio.token"),
"Invalid token contract");
eosio::name target = eosio::name(memo.erase(0, 6));
eosio::asset net_bw;
net_bw.amount = 0;
net_bw.symbol = quantity.symbol;
eosio::asset cpu_bw;
cpu_bw.amount = quantity.amount;
cpu_bw.symbol = quantity.symbol;
eosio::action(eosio::permission_level{get_self(), eosio::name("del")},
eosio::name("eosio"), eosio::name("delegatebw"),
std::make_tuple(get_self(), target, net_bw, cpu_bw, false))
.send();
}
[[eosio::action]] void adduser(eosio::name user) {
require_auth(get_self());
auto adu = autodelusers(get_self(), get_self().value);
auto it = adu.find(user.value);
if (it != adu.end()) {
return;
}
adu.emplace(get_self(), [&](auto &row) { row.user = user; });
}
[[eosio::action]] void rmuser(eosio::name user) {
require_auth(get_self());
auto adu = autodelusers(get_self(), get_self().value);
auto e = adu.require_find(user.value, "User is not on the allow list.");
adu.erase(e);
}
private:
struct [[eosio::table("autodelusers")]] autodeluser {
eosio::name user;
auto primary_key() const { return user.value; };
};
typedef eosio::multi_index<eosio::name("autodelusers"), autodeluser>
autodelusers;
};