COOPENOMICS  v1
Кооперативная Экономика
invests.hpp
См. документацию.
1#pragma once
2
3using namespace eosio;
4using std::string;
5
6namespace Capital::Invests {
13 namespace Status {
14 const eosio::name CREATED = "created"_n;
15 }
16}
17
18namespace Capital {
27 struct [[eosio::table, eosio::contract(CAPITAL)]] invest {
28 uint64_t id;
29 name coopname;
30 name username;
31 checksum256 invest_hash;
32 checksum256 project_hash;
33 name status;
34 eosio::asset amount = asset(0, _root_govern_symbol);
35 time_point_sec invested_at;
37
38 // Координаторская информация
39 eosio::name coordinator;
40 eosio::asset coordinator_amount = asset(0, _root_govern_symbol);
41
42 uint64_t primary_key() const { return id; }
43 uint64_t by_username() const { return username.value; }
44 checksum256 by_project() const { return project_hash; }
45 checksum256 by_hash() const { return invest_hash; }
46 uint128_t by_project_user() const { return combine_checksum_ids(project_hash, username); }
47 };
48
49typedef eosio::multi_index<
50 "invests"_n, invest,
51 indexed_by<"byhash"_n, const_mem_fun<invest, checksum256, &invest::by_hash>>,
52 indexed_by<"byusername"_n, const_mem_fun<invest, uint64_t, &invest::by_username>>,
53 indexed_by<"byproject"_n, const_mem_fun<invest, checksum256, &invest::by_project>>,
54 indexed_by<"byprojuser"_n, const_mem_fun<invest, uint128_t, &invest::by_project_user>>
56
57} // namespace Capital
58
59namespace Capital::Invests {
60
61inline std::optional<invest> get_invest(eosio::name coopname, const checksum256 &invest_hash) {
62 invest_index invests(_capital, coopname.value);
63 auto invest_hash_index = invests.get_index<"byhash"_n>();
64
65 auto invest_itr = invest_hash_index.find(invest_hash);
66 if (invest_itr == invest_hash_index.end()) {
67 return std::nullopt;
68 }
69
70 return *invest_itr;
71}
72
79inline invest get_invest_or_fail(eosio::name coopname, const checksum256 &invest_hash) {
80 auto investment = get_invest(coopname, invest_hash);
81 eosio::check(investment.has_value(), "Инвестиция не найдена");
82 return *investment;
83}
84
85
92inline std::optional<std::pair<eosio::name, eosio::asset>> get_coordinator_amount(
93 eosio::name coopname,
94 eosio::name investor_username,
95 const eosio::asset &investment_amount
96) {
97 auto investor_account = get_account_or_fail(investor_username);
98
99 // Проверяем, есть ли координатор (реферер)
100 if (investor_account.referer == eosio::name{}) {
101 return std::nullopt;
102 }
103
104 // Проверяем, зарегистрирован ли инвестор менее 30 дней назад
105 auto current_time = eosio::current_time_point();
106 auto registration_time = investor_account.registered_at;
107
108 auto time_since_registration = current_time.sec_since_epoch() - registration_time.sec_since_epoch();
109
110 auto coop_config_seconds = Capital::get_global_state(coopname).config.coordinator_invite_validity_days * 24 * 60 * 60;
111 if (time_since_registration >= coop_config_seconds) {
112 return std::nullopt;
113 }
114
115 // Возвращаем координатора и сумму взноса с координатором (равную сумме инвестиции)
116 return std::make_pair(investor_account.referer, investment_amount);
117}
118
129 eosio::name coopname,
130 eosio::name username,
131 checksum256 project_hash,
132 checksum256 invest_hash,
133 eosio::asset amount,
134 document2 statement
135) {
136 // Создаем инвестицию
137 invest_index invests(_capital, coopname.value);
138 uint64_t invest_id = get_global_id_in_scope(_capital, coopname, "invests"_n);
139
140 invests.emplace(coopname, [&](auto &i){
141 i.id = invest_id;
142 i.coopname = coopname;
143 i.username = username;
144 i.project_hash = project_hash;
145 i.invest_hash = invest_hash;
147 i.invested_at = current_time_point();
148 i.statement = statement;
149 i.amount = amount;
150 });
151
152 // Отправляем на approve председателю
154 _capital,
155 coopname,
156 username,
157 statement,
159 invest_hash,
160 _capital,
163 std::string("")
164 );
165}
166
175 eosio::name coopname,
176 checksum256 invest_hash,
177 eosio::name coordinator_username,
178 eosio::asset coordinator_amount
179) {
180 invest_index invests(_capital, coopname.value);
181 auto invest_hash_index = invests.get_index<"byhash"_n>();
182 auto invest_iterator = invest_hash_index.find(invest_hash);
183
184 eosio::check(invest_iterator != invest_hash_index.end(), "Инвестиция не найдена");
185
186 // Обновляем запись с информацией о координаторе
187 invest_hash_index.modify(invest_iterator, coopname, [&](auto &i){
188 i.coordinator = coordinator_username;
189 i.coordinator_amount = coordinator_amount;
190 });
191}
192
198inline void delete_invest(eosio::name coopname, const checksum256 &invest_hash) {
199 invest_index invests(_capital, coopname.value);
200 auto invest_hash_index = invests.get_index<"byhash"_n>();
201
202 auto itr = invest_hash_index.find(invest_hash);
203 eosio::check(itr != invest_hash_index.end(), "Инвестиция не найдена");
204
205 invests.erase(*itr);
206}
207
208
209} // namespace Capital::Invests
account get_account_or_fail(eosio::name username)
Definition: accounts.hpp:405
static constexpr eosio::name _capital
Definition: consts.hpp:150
static constexpr eosio::symbol _root_govern_symbol
Definition: consts.hpp:210
contract
Definition: eosio.msig_tests.cpp:977
share_type amount
Definition: eosio.token_tests.cpp:174
uint64_t get_global_id_in_scope(eosio::name _me, eosio::name scope, eosio::name key)
Definition: counts.hpp:61
const eosio::name CREATED
Инвестиция создана
Definition: invests.hpp:14
Definition: invests.hpp:6
invest get_invest_or_fail(eosio::name coopname, const checksum256 &invest_hash)
Получает инвестицию по хэшу или прерывает выполнение с ошибкой.
Definition: invests.hpp:79
std::optional< invest > get_invest(eosio::name coopname, const checksum256 &invest_hash)
Definition: invests.hpp:61
void set_coordinator_info(eosio::name coopname, checksum256 invest_hash, eosio::name coordinator_username, eosio::asset coordinator_amount)
Устанавливает информацию о координаторе в инвестиции.
Definition: invests.hpp:174
void delete_invest(eosio::name coopname, const checksum256 &invest_hash)
Удаляет инвестицию по хэшу.
Definition: invests.hpp:198
void create_invest_with_approve(eosio::name coopname, eosio::name username, checksum256 project_hash, checksum256 invest_hash, eosio::asset amount, document2 statement)
Создает инвестицию и отправляет её на утверждение.
Definition: invests.hpp:128
std::optional< std::pair< eosio::name, eosio::asset > > get_coordinator_amount(eosio::name coopname, eosio::name investor_username, const eosio::asset &investment_amount)
Вычисляет сумму координаторского взноса, если инвестор зарегистрирован менее 30 дней назад.
Definition: invests.hpp:92
Definition: balances.cpp:6
eosio::multi_index< "invests"_n, invest, indexed_by<"byhash"_n, const_mem_fun< invest, checksum256, &invest::by_hash > >, indexed_by<"byusername"_n, const_mem_fun< invest, uint64_t, &invest::by_username > >, indexed_by<"byproject"_n, const_mem_fun< invest, checksum256, &invest::by_project > >, indexed_by<"byprojuser"_n, const_mem_fun< invest, uint128_t, &invest::by_project_user > > > invest_index
Таблица для хранения инвестиций.
Definition: invests.hpp:55
global_state get_global_state(name coopname)
Получает текущее глобальное состояние.
Definition: global_state.hpp:66
constexpr eosio::name DECLINE_INVESTMENT
Definition: shared_names.hpp:36
constexpr eosio::name CREATE_INVESTMENT
Definition: shared_names.hpp:108
constexpr eosio::name APPROVE_INVESTMENT
Definition: shared_names.hpp:35
void create_approval(name calling_contract, CREATEAPPRV_SIGNATURE)
Создаёт аппрув в совете
Definition: shared_soviet.hpp:73
Definition: eosio.msig.hpp:34
uint32_t coordinator_invite_validity_days
Срок действия приглашения координатора (по умолчанию 30 дней)
Definition: global_state.hpp:14
config config
Управляемая конфигурация контракта
Definition: global_state.hpp:36
Таблица инвестиций хранит данные о вложениях в проекты.
Definition: invests.hpp:27
checksum256 by_project() const
Индекс по проекту (3)
Definition: invests.hpp:44
uint64_t id
ID инвестиции (внутренний ключ)
Definition: invests.hpp:28
time_point_sec invested_at
Дата приёма инвестиции
Definition: invests.hpp:35
name username
Имя инвестора
Definition: invests.hpp:30
checksum256 by_hash() const
Индекс по хэшу инвестиции (4)
Definition: invests.hpp:45
document2 statement
Заявление на зачёт из кошелька
Definition: invests.hpp:36
checksum256 invest_hash
Хэш инвестиции
Definition: invests.hpp:31
eosio::name coordinator
Имя координатора (реферера), если есть
Definition: invests.hpp:39
uint64_t primary_key() const
Первичный ключ (1)
Definition: invests.hpp:42
name coopname
Имя кооператива
Definition: invests.hpp:29
checksum256 project_hash
Хэш проекта
Definition: invests.hpp:32
uint64_t by_username() const
Индекс по имени пользователя (2)
Definition: invests.hpp:43
name status
Статус инвестиции (created)
Definition: invests.hpp:33
uint128_t by_project_user() const
Индекс по проекту и пользователю (5)
Definition: invests.hpp:46
Definition: drafts.hpp:28
static uint128_t combine_checksum_ids(const checksum256 &hash, eosio::name username)
Definition: utils.hpp:9