COOPENOMICS  v1
Кооперативная Экономика
shared_loan.hpp
См. документацию.
1#pragma once
2
3#include <eosio/eosio.hpp>
4#include <eosio/asset.hpp>
5#include <optional>
6
7using namespace eosio;
8using std::string;
9
16// Сигнатуры как макросы
17#define CREATEDEBT_SIGNATURE name coopname, name username, checksum256 debt_hash, time_point_sec repaid_at, asset quantity
18#define SETTLEDEBT_SIGNATURE name coopname, name username, checksum256 debt_hash, asset quantity
19
20// Типы для compile-time проверки
23
24
25namespace Loan {
26 using namespace eosio;
27
36 struct [[eosio::table, eosio::contract(LOAN)]] debt {
37 uint64_t id;
38 name coopname;
39 name username;
40 checksum256 debt_hash;
41 asset amount;
42 time_point_sec created_at;
43 time_point_sec repaid_at;
44
45 uint64_t primary_key() const { return id; }
46 uint64_t by_username() const { return username.value; }
47 checksum256 by_debt_hash() const { return debt_hash; }
48 uint64_t by_created() const { return created_at.sec_since_epoch(); }
49 uint64_t by_repaid() const { return repaid_at.sec_since_epoch(); }
50 };
51
52 typedef multi_index<
53 "debts"_n,
54 debt,
55 indexed_by<"byusername"_n, const_mem_fun<debt, uint64_t, &debt::by_username>>,
56 indexed_by<"bydebthash"_n, const_mem_fun<debt, checksum256, &debt::by_debt_hash>>,
57 indexed_by<"bycreated"_n, const_mem_fun<debt, uint64_t, &debt::by_created>>,
58 indexed_by<"byrepaid"_n, const_mem_fun<debt, uint64_t, &debt::by_repaid>>
60
69 struct [[eosio::table, eosio::contract(LOAN)]] summary {
70 name username;
71 asset total;
72
73 uint64_t primary_key() const { return username.value; }
74 };
75
76 typedef multi_index<"summaries"_n, summary> summaries_index;
77
84 inline std::optional<debt> get_debt(name coopname, const checksum256& debt_hash) {
85 debts_index debts(_loan, coopname.value);
86 auto by_hash = debts.get_index<"bydebthash"_n>();
87 auto it = by_hash.find(debt_hash);
88 if (it == by_hash.end()) return std::nullopt;
89 return *it;
90 }
91
98 inline std::optional<summary> get_summary(name coopname, name username) {
99 summaries_index summaries(_loan, coopname.value);
100 auto it = summaries.find(username.value);
101 if (it == summaries.end()) return std::nullopt;
102 return *it;
103 }
104
111 inline void assert_no_expired_debts(name coopname, name username) {
112 debts_index debts(_loan, coopname.value);
113 auto by_repaid = debts.get_index<"byrepaid"_n>();
114
115 uint32_t now = time_point_sec(current_time_point()).sec_since_epoch();
116
117 for (auto itr = by_repaid.begin(); itr != by_repaid.end() && itr->repaid_at.sec_since_epoch() <= now; ++itr) {
118 if (itr->username == username) {
119 eosio::check(false, "У пользователя есть просроченные долги");
120 }
121 }
122 }
123
133 inline void create_debt(
134 name calling_contract,
136 ) {
137 //Создаём объект долга в контракте loan
138 Action::send<createdebt_interface>(
139 _loan,
141 calling_contract,
142 coopname,
143 username,
144 debt_hash,
145 repaid_at,
146 quantity
147 );
148 }
149
158 inline void settle_debt(
159 name calling_contract,
161 ) {
162 Action::send<settledebt_interface>(
163 _loan,
165 calling_contract,
166 coopname,
167 username,
168 debt_hash,
169 quantity
170 );
171 }
172
173}
static constexpr eosio::name _loan
Definition: consts.hpp:161
contract
Definition: eosio.msig_tests.cpp:977
#define CREATEDEBT_SIGNATURE
Сигнатуры действий контракта loan.
Definition: shared_loan.hpp:17
Definition: shared_loan.hpp:25
std::optional< debt > get_debt(name coopname, const checksum256 &debt_hash)
Получает долговое обязательство по хэшу.
Definition: shared_loan.hpp:84
multi_index<"summaries"_n, summary > summaries_index
Definition: shared_loan.hpp:76
void settle_debt(name calling_contract, SETTLEDEBT_SIGNATURE)
Погашает долговое обязательство пайщика.
Definition: shared_loan.hpp:158
multi_index< "debts"_n, debt, indexed_by<"byusername"_n, const_mem_fun< debt, uint64_t, &debt::by_username > >, indexed_by<"bydebthash"_n, const_mem_fun< debt, checksum256, &debt::by_debt_hash > >, indexed_by<"bycreated"_n, const_mem_fun< debt, uint64_t, &debt::by_created > >, indexed_by<"byrepaid"_n, const_mem_fun< debt, uint64_t, &debt::by_repaid > > > debts_index
Definition: shared_loan.hpp:59
void assert_no_expired_debts(name coopname, name username)
Проверяет отсутствие просроченных долгов у пользователя.
Definition: shared_loan.hpp:111
void create_debt(name calling_contract, CREATEDEBT_SIGNATURE)
Создает долговое обязательство пайщика.
Definition: shared_loan.hpp:133
std::optional< summary > get_summary(name coopname, name username)
Получает сводку по долгам пользователя.
Definition: shared_loan.hpp:98
constexpr eosio::name CREATE_DEBT
Definition: shared_names.hpp:73
constexpr eosio::name SETTLE_DEBT
Definition: shared_names.hpp:74
Definition: eosio.msig.hpp:34
#define SETTLEDEBT_SIGNATURE
Definition: shared_loan.hpp:18
void(SETTLEDEBT_SIGNATURE) settledebt_interface
Definition: shared_loan.hpp:22
void(CREATEDEBT_SIGNATURE) createdebt_interface
Definition: shared_loan.hpp:21
Структура долгового обязательства.
Definition: shared_loan.hpp:36
checksum256 debt_hash
Хэш долгового обязательства
Definition: shared_loan.hpp:40
asset amount
Сумма долга
Definition: shared_loan.hpp:41
name coopname
Имя кооператива
Definition: shared_loan.hpp:38
uint64_t by_username() const
Definition: shared_loan.hpp:46
uint64_t by_created() const
Definition: shared_loan.hpp:48
checksum256 by_debt_hash() const
Definition: shared_loan.hpp:47
time_point_sec repaid_at
Срок погашения долга
Definition: shared_loan.hpp:43
uint64_t primary_key() const
Definition: shared_loan.hpp:45
name username
Имя пользователя-должника
Definition: shared_loan.hpp:39
time_point_sec created_at
Время создания долга
Definition: shared_loan.hpp:42
uint64_t by_repaid() const
Definition: shared_loan.hpp:49
uint64_t id
Идентификатор долга
Definition: shared_loan.hpp:37
Структура сводки по долгам пользователя.
Definition: shared_loan.hpp:69
name username
Имя пользователя
Definition: shared_loan.hpp:70
asset total
Общая сумма долгов пользователя
Definition: shared_loan.hpp:71
uint64_t primary_key() const
Definition: shared_loan.hpp:73
Definition: shared_debts.hpp:1