COOPENOMICS  v1
Кооперативная Экономика
invests.hpp
См. документацию.
1#pragma once
2
3
4#include "../../../lib/core/ram_payer.hpp"
5using namespace eosio;
6using std::string;
7
8namespace Capital::Invests {
15 namespace Status {
16 const eosio::name CREATED = "created"_n;
17 }
18}
19
20namespace Capital {
29 struct [[eosio::table, eosio::contract(CAPITAL)]] invest {
30 uint64_t id;
31 name coopname;
32 name username;
33 checksum256 invest_hash;
34 checksum256 project_hash;
35 name status;
36 eosio::asset amount = asset(0, _root_govern_symbol);
37 time_point_sec invested_at;
39
40 // Координаторская информация
41 eosio::name coordinator;
42 eosio::asset coordinator_amount = asset(0, _root_govern_symbol);
43
44 uint64_t primary_key() const { return id; }
45 uint64_t by_username() const { return username.value; }
46 checksum256 by_project() const { return project_hash; }
47 checksum256 by_hash() const { return invest_hash; }
48 uint128_t by_project_user() const { return combine_checksum_ids(project_hash, username); }
49 };
50
51typedef eosio::multi_index<
52 "invests"_n, invest,
53 indexed_by<"byhash"_n, const_mem_fun<invest, checksum256, &invest::by_hash>>,
54 indexed_by<"byusername"_n, const_mem_fun<invest, uint64_t, &invest::by_username>>,
55 indexed_by<"byproject"_n, const_mem_fun<invest, checksum256, &invest::by_project>>,
56 indexed_by<"byprojuser"_n, const_mem_fun<invest, uint128_t, &invest::by_project_user>>
58
59} // namespace Capital
60
61namespace Capital::Invests {
62
63inline std::optional<invest> get_invest(eosio::name coopname, const checksum256 &invest_hash) {
64 invest_index invests(_capital, coopname.value);
65 auto invest_hash_index = invests.get_index<"byhash"_n>();
66
67 auto invest_itr = invest_hash_index.find(invest_hash);
68 if (invest_itr == invest_hash_index.end()) {
69 return std::nullopt;
70 }
71
72 return invest(*invest_itr);
73}
74
81inline invest get_invest_or_fail(eosio::name coopname, const checksum256 &invest_hash) {
82 auto investment = get_invest(coopname, invest_hash);
83 eosio::check(investment.has_value(), "Инвестиция не найдена");
84 return investment.value();
85}
86
87
94inline std::optional<std::pair<eosio::name, eosio::asset>> get_coordinator_amount(
95 eosio::name coopname,
96 eosio::name investor_username,
97 const eosio::asset &investment_amount
98) {
99 auto investor_account = get_account_or_fail(investor_username);
100
101 // Проверяем, есть ли координатор (реферер)
102 if (investor_account.referer == eosio::name{}) {
103 return std::nullopt;
104 }
105
106 // Проверяем, зарегистрирован ли инвестор менее 30 дней назад
107 auto current_time = eosio::current_time_point();
108 auto registration_time = investor_account.registered_at;
109
110 auto time_since_registration = current_time.sec_since_epoch() - registration_time.sec_since_epoch();
111
112 auto coop_config_seconds = Capital::State::get_global_state(coopname).config.coordinator_invite_validity_days * 24 * 60 * 60;
113 if (time_since_registration >= coop_config_seconds) {
114 return std::nullopt;
115 }
116
117 // Возвращаем координатора и сумму взноса с координатором (равную сумме инвестиции)
118 return std::make_pair(investor_account.referer, investment_amount);
119}
120
132 eosio::name coopname,
133 eosio::name username,
134 checksum256 project_hash,
135 checksum256 invest_hash,
136 eosio::asset amount,
137 document2 statement
138) {
139 // Создаем инвестицию
140 invest_index invests(_capital, coopname.value);
141 uint64_t invest_id = get_global_id_in_scope(_capital, coopname, "invests"_n);
142
143 invests.emplace(RamPayer::of(invests, coopname), [&](auto &i){
144 i.id = invest_id;
145 i.coopname = coopname;
146 i.username = username;
147 i.project_hash = project_hash;
148 i.invest_hash = invest_hash;
150 i.invested_at = current_time_point();
151 i.statement = statement;
152 i.amount = amount;
153 });
154
155 // Отправляем на approve председателю
157 _capital,
158 coopname,
159 username,
160 statement,
162 invest_hash,
163 _capital,
166 std::string("")
167 );
168}
169
179 eosio::name coopname,
180 checksum256 invest_hash,
181 eosio::name coordinator_username,
182 eosio::asset coordinator_amount
183) {
184 invest_index invests(_capital, coopname.value);
185 auto invest_hash_index = invests.get_index<"byhash"_n>();
186 auto invest_iterator = invest_hash_index.find(invest_hash);
187
188 eosio::check(invest_iterator != invest_hash_index.end(), "Инвестиция не найдена");
189
190 // Обновляем запись с информацией о координаторе
191 invest_hash_index.modify(invest_iterator, RamPayer::of(invest_hash_index, coopname), [&](auto &i){
192 i.coordinator = coordinator_username;
193 i.coordinator_amount = coordinator_amount;
194 });
195}
196
197
198} // namespace Capital::Invests
199
200// Плательщик за оперативную память строк таблицы — правило в lib/core/ram_payer.hpp.
static constexpr eosio::name _capital
Definition: consts.hpp:171
static constexpr eosio::symbol _root_govern_symbol
Definition: consts.hpp:236
contract
Definition: eosio.msig_tests.cpp:977
share_type amount
Definition: eosio.token_tests.cpp:174
RAM_PAYER_CLASS(Capital::invest, cooperative)
const eosio::name CREATED
Инвестиция создана
Definition: invests.hpp:16
Definition: invests.hpp:8
invest get_invest_or_fail(eosio::name coopname, const checksum256 &invest_hash)
Получает инвестицию по хэшу или прерывает выполнение с ошибкой.
Definition: invests.hpp:81
std::optional< invest > get_invest(eosio::name coopname, const checksum256 &invest_hash)
Definition: invests.hpp:63
void set_coordinator_info(eosio::name coopname, checksum256 invest_hash, eosio::name coordinator_username, eosio::asset coordinator_amount)
Устанавливает информацию о координаторе в инвестиции.
Definition: invests.hpp:178
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:131
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:94
global_state get_global_state(name coopname)
Получает текущее глобальное состояние.
Definition: global_state.hpp:120
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:57
constexpr eosio::name DECLINE_INVESTMENT
Definition: names.hpp:36
constexpr eosio::name CREATE_INVESTMENT
Definition: names.hpp:135
constexpr eosio::name APPROVE_INVESTMENT
Definition: names.hpp:35
eosio::name of(const Table &table, eosio::name coopname)
Definition: ram_payer.hpp:78
void create_approval(name calling_contract, CREATEAPPRV_SIGNATURE)
Definition: soviet.hpp:66
Definition: eosio.msig.hpp:34
account get_account_or_fail(eosio::name username)
Definition: registrator_account_access.hpp:8
uint32_t coordinator_invite_validity_days
Срок действия приглашения координатора (дней)
Definition: global_state.hpp:23
config config
Управляемая конфигурация контракта
Definition: global_state.hpp:51
Таблица инвестиций хранит данные о вложениях в проекты.
Definition: invests.hpp:29
checksum256 by_project() const
Индекс по проекту (3)
Definition: invests.hpp:46
uint64_t id
ID инвестиции (внутренний ключ)
Definition: invests.hpp:30
time_point_sec invested_at
Дата приёма инвестиции
Definition: invests.hpp:37
name username
Имя инвестора
Definition: invests.hpp:32
checksum256 by_hash() const
Индекс по хэшу инвестиции (4)
Definition: invests.hpp:47
document2 statement
Заявление на зачёт из кошелька
Definition: invests.hpp:38
checksum256 invest_hash
Хэш инвестиции
Definition: invests.hpp:33
eosio::name coordinator
Имя координатора (реферера), если есть
Definition: invests.hpp:41
uint64_t primary_key() const
Первичный ключ (1)
Definition: invests.hpp:44
name coopname
Имя кооператива
Definition: invests.hpp:31
checksum256 project_hash
Хэш проекта
Definition: invests.hpp:34
uint64_t by_username() const
Индекс по имени пользователя (2)
Definition: invests.hpp:45
name status
Статус инвестиции (created)
Definition: invests.hpp:35
uint128_t by_project_user() const
Индекс по проекту и пользователю (5)
Definition: invests.hpp:48
Definition: table_registrator_orgs.hpp:17
Definition: document_core.hpp:27
uint64_t get_global_id_in_scope(eosio::name _me, eosio::name scope, eosio::name key)
Definition: table_counts.hpp:62
static uint128_t combine_checksum_ids(const checksum256 &hash, eosio::name username)
Definition: utils.hpp:11