COOPENOMICS  v1
Кооперативная Экономика
wallets.hpp
См. документацию.
1namespace Capital {
2
11 struct [[eosio::table, eosio::contract(CAPITAL)]] capital_wallet {
12 uint64_t id;
13 eosio::name coopname;
14 eosio::name username;
15 double last_program_crps = 0.0;
16 eosio::asset capital_available = asset(0, _root_govern_symbol);
17
18 uint64_t primary_key() const { return id; }
19 uint64_t by_username() const { return username.value; }
20 };
21
22 typedef eosio::multi_index<
23 "capwallets"_n, capital_wallet,
24 indexed_by<"byusername"_n, const_mem_fun<capital_wallet, uint64_t, &capital_wallet::by_username>>
26
35 struct [[eosio::table, eosio::contract(CAPITAL)]] project_wallet {
36 uint64_t id;
37 eosio::name coopname;
38 checksum256 project_hash;
39 eosio::name username;
40 eosio::asset shares = asset(0, _root_govern_symbol);
41 double last_membership_reward_per_share = 0.0;
42 eosio::asset membership_available = asset(0, _root_govern_symbol);
43
44 uint64_t primary_key() const { return id; }
45 checksum256 by_project_hash() const { return project_hash; }
46 uint64_t by_username() const { return username.value; }
47
48 uint128_t by_project_user() const {
49 return combine_checksum_ids(project_hash, username);
50 }
51 };
52
53 typedef eosio::multi_index<
54 "projwallets"_n, project_wallet,
55 indexed_by<"byproject"_n, const_mem_fun<project_wallet, checksum256, &project_wallet::by_project_hash>>,
56 indexed_by<"byusername"_n, const_mem_fun<project_wallet, uint64_t, &project_wallet::by_username>>,
57 indexed_by<"byprojuser"_n, const_mem_fun<project_wallet, uint128_t, &project_wallet::by_project_user>>
59
60namespace Wallets {
61
62 inline std::optional<progwallet> get_program_capital_wallet(eosio::name coopname, eosio::name username) {
63
64 auto program_id = get_program_id(_capital_program);
65
66 auto program = get_program_or_fail(coopname, program_id);
67
68 auto capital_wallet = get_program_wallet(coopname, username, _capital_program);
69
70 if (!capital_wallet.has_value()) {
71 return std::nullopt;
72 }
73
74 return *capital_wallet;
75
76 }
77
81 inline std::optional<capital_wallet> get_capital_wallet_by_username(eosio::name coopname, eosio::name username) {
82 capital_wallets_index capital_wallets(_capital, coopname.value);
83 auto idx = capital_wallets.get_index<"byusername"_n>();
84
85 auto itr = idx.find(username.value);
86 if (itr == idx.end()) {
87 return std::nullopt;
88 }
89
90 return *itr;
91 }
92
96 inline capital_wallet get_capital_wallet_or_fail(eosio::name coopname, eosio::name username, const char* msg = "Кошелек капитализации не найден") {
97 auto wallet_opt = get_capital_wallet_by_username(coopname, username);
98 eosio::check(wallet_opt.has_value(), msg);
99 return *wallet_opt;
100 }
101
105 inline void upsert_capital_wallet(eosio::name coopname,
106 eosio::name username,
107 int64_t last_program_crps,
108 eosio::asset capital_available) {
109 capital_wallets_index capital_wallets(_capital, coopname.value);
110 auto idx = capital_wallets.get_index<"byusername"_n>();
111
112 auto itr = idx.find(username.value);
113 if (itr == idx.end()) {
114 // Создаем новый кошелек
115 capital_wallets.emplace(coopname, [&](auto &w) {
116 w.id = get_global_id_in_scope(_capital, coopname, "capwallets"_n);
117 w.coopname = coopname;
118 w.username = username;
119 w.last_program_crps = last_program_crps;
120 w.capital_available = capital_available;
121 });
122 } else {
123 // Обновляем существующий
124 idx.modify(itr, coopname, [&](auto &w) {
125 if (last_program_crps != 0) w.last_program_crps = last_program_crps;
126 if (capital_available.amount != 0) w.capital_available += capital_available;
127 });
128 }
129 }
130
134 inline std::optional<project_wallet> get_project_wallet(eosio::name coopname, const checksum256 &project_hash, eosio::name username) {
135 project_wallets_index project_wallets(_capital, coopname.value);
136 auto idx = project_wallets.get_index<"byprojuser"_n>();
137 auto key = combine_checksum_ids(project_hash, username);
138
139 auto itr = idx.find(key);
140 if (itr == idx.end()) {
141 return std::nullopt;
142 }
143
144 return *itr;
145 }
146
150 inline project_wallet get_project_wallet_or_fail(eosio::name coopname, const checksum256 &project_hash, eosio::name username, const char* msg = "Кошелек проекта не найден") {
151 auto wallet_opt = get_project_wallet(coopname, project_hash, username);
152 eosio::check(wallet_opt.has_value(), msg);
153 return *wallet_opt;
154 }
155
159 inline void upsert_project_wallet(eosio::name coopname, const checksum256 &project_hash, eosio::name username,
160 const eosio::asset &shares, eosio::name payer = _capital) {
161 project_wallets_index project_wallets(_capital, coopname.value);
162 auto idx = project_wallets.get_index<"byprojuser"_n>();
163 auto key = combine_checksum_ids(project_hash, username);
164
165 auto itr = idx.find(key);
166 if (itr == idx.end()) {
167 // Создаем новый кошелек
168 project_wallets.emplace(payer, [&](auto &w) {
169 w.id = get_global_id_in_scope(_capital, coopname, "projwallets"_n);
170 w.coopname = coopname;
171 w.project_hash = project_hash;
172 w.username = username;
173 w.shares = shares;
174 });
175 } else {
176 // Добавляем доли к существующим
177 idx.modify(itr, payer, [&](auto &w) {
178 w.shares += shares;
179 });
180 }
181 }
182
183} //namespace Capital::Wallet
184} // namespace Capital
185
static constexpr eosio::name _capital
Definition: consts.hpp:150
static constexpr eosio::symbol _root_govern_symbol
Definition: consts.hpp:210
static constexpr eosio::name _capital_program
Кошелёк программы "Капитализация".
Definition: consts.hpp:83
contract
Definition: eosio.msig_tests.cpp:977
uint64_t get_global_id_in_scope(eosio::name _me, eosio::name scope, eosio::name key)
Definition: counts.hpp:61
void upsert_capital_wallet(eosio::name coopname, eosio::name username, int64_t last_program_crps, eosio::asset capital_available)
Создает или обновляет кошелек капитализации
Definition: wallets.hpp:105
std::optional< progwallet > get_program_capital_wallet(eosio::name coopname, eosio::name username)
Definition: wallets.hpp:62
project_wallet get_project_wallet_or_fail(eosio::name coopname, const checksum256 &project_hash, eosio::name username, const char *msg="Кошелек проекта не найден")
Получает кошелек проекта или падает с ошибкой
Definition: wallets.hpp:150
std::optional< project_wallet > get_project_wallet(eosio::name coopname, const checksum256 &project_hash, eosio::name username)
Получает кошелек проекта по хэшу проекта и имени пользователя
Definition: wallets.hpp:134
capital_wallet get_capital_wallet_or_fail(eosio::name coopname, eosio::name username, const char *msg="Кошелек капитализации не найден")
Получает кошелек капитализации или падает с ошибкой
Definition: wallets.hpp:96
std::optional< capital_wallet > get_capital_wallet_by_username(eosio::name coopname, eosio::name username)
Получает кошелек капитализации по имени пользователя
Definition: wallets.hpp:81
void upsert_project_wallet(eosio::name coopname, const checksum256 &project_hash, eosio::name username, const eosio::asset &shares, eosio::name payer=_capital)
Создает или обновляет кошелек проекта, добавляя доли к существующим
Definition: wallets.hpp:159
Definition: balances.cpp:6
eosio::multi_index< "capwallets"_n, capital_wallet, indexed_by<"byusername"_n, const_mem_fun< capital_wallet, uint64_t, &capital_wallet::by_username > > > capital_wallets_index
Definition: wallets.hpp:25
eosio::multi_index< "projwallets"_n, project_wallet, indexed_by<"byproject"_n, const_mem_fun< project_wallet, checksum256, &project_wallet::by_project_hash > >, indexed_by<"byusername"_n, const_mem_fun< project_wallet, uint64_t, &project_wallet::by_username > >, indexed_by<"byprojuser"_n, const_mem_fun< project_wallet, uint128_t, &project_wallet::by_project_user > > > project_wallets_index
Definition: wallets.hpp:58
Definition: eosio.msig.hpp:34
program get_program_or_fail(eosio::name coopname, uint64_t program_id)
Definition: programs.hpp:57
std::optional< progwallet > get_program_wallet(eosio::name coopname, eosio::name username, name type)
Definition: programs.hpp:156
uint64_t get_program_id(const eosio::name &type)
Definition: programs.hpp:142
Кошелек программы капитализации для учёта CRPS и доступных средств от членских взносов
Definition: wallets.hpp:11
uint64_t primary_key() const
Первичный ключ (1)
Definition: wallets.hpp:18
uint64_t id
ID кошелька (внутренний ключ)
Definition: wallets.hpp:12
eosio::name username
Имя пользователя
Definition: wallets.hpp:14
eosio::name coopname
Имя кооператива
Definition: wallets.hpp:13
uint64_t by_username() const
Индекс по имени пользователя (2)
Definition: wallets.hpp:19
Таблица кошельков проектов хранит данные о долях участников в проектах для получения членских взносов...
Definition: wallets.hpp:35
uint128_t by_project_user() const
Definition: wallets.hpp:48
uint64_t by_username() const
Индекс по имени пользователя (3)
Definition: wallets.hpp:46
uint64_t primary_key() const
Первичный ключ (1)
Definition: wallets.hpp:44
eosio::name coopname
Имя кооператива
Definition: wallets.hpp:37
checksum256 project_hash
Хэш проекта
Definition: wallets.hpp:38
checksum256 by_project_hash() const
Индекс по хэшу проекта (2)
Definition: wallets.hpp:45
eosio::name username
Имя пользователя
Definition: wallets.hpp:39
uint64_t id
ID кошелька проекта (внутренний ключ)
Definition: wallets.hpp:36
Таблица программ кооператива
Definition: programs.hpp:14
static uint128_t combine_checksum_ids(const checksum256 &hash, eosio::name username)
Definition: utils.hpp:9