-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheosdacvote.cpp
More file actions
85 lines (71 loc) · 2.95 KB
/
eosdacvote.cpp
File metadata and controls
85 lines (71 loc) · 2.95 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
85
#include "eosdacvote.hpp"
#include <eosiolib/system.h>
using namespace eosio;
eosdacvote::eosdacvote(account_name s)
: contract(s),
_weights(_self, s),
_contract_vote_weight(1),
_user_vote_weight(1),
_contract_time_weight(1),
_user_time_weight(1),
_max_time_limit(60 * 24 * 30 * 3) // 90 days
{
}
eosdacvote::~eosdacvote()
{
}
void eosdacvote::vote(name from, name to, asset quantity, bool yeas)
{
eosio_assert(from != to, "cannot vote to self"); // can I ?
eosio_assert(is_account(from), "Invalid account");
eosio_assert(is_account(to), "Invalid account");
//eosio_assert(!(!is_contract_account(from) && !is_contract_account(to)), "Can't be two normal accounts");
require_auth(from);
require_recipient(from);
require_recipient(to);
eosio_assert(quantity.amount > 0, "must vote positive stake");
//accounts from_acnts(N("eosio.token"), from);
//const auto& f = from_acnts.get(quantity.symbol.name(), "no balance object found");
//eosio_assert(f.balance.amount >= quantity.amount, "overddrawn balance");
//asset f_balance = eosdactoken(tokenContract).get_balance(from, quantity.symbol.name());
//eosio_assert(f_balance.amount >= quantity.amount, "overddrawn balance");
int64_t vote_weight = 1, time_weight = 1;
if (is_contract_account(from)) {
vote_weight = _contract_vote_weight;
time_weight = _contract_time_weight;
} else {
vote_weight = _user_vote_weight;
time_weight = _user_time_weight;
}
uint32_t minutes = now() / 60;
auto to_weight = _weights.find(to);
if (to_weight == _weights.end()) {
_weights.emplace(_self, [&](auto& a){
a.owner = to;
a.weight = quantity.amount * (yeas ? vote_weight : (-1 * vote_weight))
+ ((minutes - a.last_vote_time) > _max_time_limit ? _max_time_limit : (minutes - a.last_vote_time)) * (yeas ? time_weight : (-1 * time_weight));
a.last_vote_time = minutes;
});
} else {
_weights.modify(to_weight, _self, [&](auto& a){
a.weight += (quantity.amount * (yeas ? vote_weight : (-1 * vote_weight))
+ ((minutes - a.last_vote_time) > _max_time_limit ? _max_time_limit : (minutes - a.last_vote_time)) * (yeas ? time_weight : (-1 * time_weight)));
a.last_vote_time = minutes;
});
}
}
void eosdacvote::configvote(int64_t contract_vote_weight, int64_t user_vote_weight, int64_t contract_time_weight, int64_t user_time_weight, int64_t max_time_limit)
{
require_auth(_self);
_contract_time_weight = contract_vote_weight;
_user_vote_weight = user_vote_weight;
_contract_time_weight = contract_time_weight;
_user_time_weight = user_time_weight;
_max_time_limit = max_time_limit;
}
bool eosdacvote::is_contract_account(name name)
{
// if cleos get code {name} returns no zero, means contract account
return false;
}
EOSIO_ABI( eosdacvote, (configvote) (vote) )