-
Notifications
You must be signed in to change notification settings - Fork 8
/
counter-with-scope
52 lines (41 loc) · 1.24 KB
/
counter-with-scope
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
#include <eosiolib/eosio.hpp>
//@abi table
struct counter{
uint64_t actor;
uint64_t value;
uint64_t primary_key() const { return actor; }
};
typedef eosio::multi_index<N(counter),counter> counter_table;
class counter_contract:eosio::contract{
public:
counter_contract(account_name self):eosio::contract(self){}
void increase(account_name actor){
eosio::require_auth(actor);
value = get_value(actor);
eosio::print(eosio::name{actor} , " INCREASE => ",value++);
update_value(actor,value);
}
void decrease(account_name actor){
eosio::print(eosio::name{actor} , " DECREASE => ",value--);
}
private:
uint64_t value;
uint64_t get_value(account_name actor){
counter_table c(_self,actor);
auto iter = c.find(actor);
if(iter != c.end()) return iter->value;
c.emplace(actor,[&](auto& record){
record.actor = actor;
record.value = 0;
});
return 0;
}
void update_value(account_name actor,uint64_t value){
counter_table c(_self,actor);
auto iter = c.find(actor);
c.modify(iter,actor,[&](auto& record){
record.value = value;
});
}
};
EOSIO_ABI(counter_contract,(increase)(decrease))