COOPENOMICS  v1
Кооперативная Экономика
table_branch_weights.hpp
См. документацию.
1#pragma once
2
3
4#include "../core/ram_payer.hpp"
5#include <eosio/asset.hpp>
6#include <eosio/eosio.hpp>
7
8#include "../consts.hpp"
9#include "../core/utils.hpp"
10
29struct [[eosio::table, eosio::contract(BRANCH)]] branch_weight {
30 uint64_t id;
31 eosio::name braname;
32 eosio::name contract;
33 eosio::name username;
34 uint64_t weight;
35
36 uint64_t primary_key() const { return id; }
37 uint128_t by_contract_branch() const { return combine_ids(contract.value, braname.value); }
38 uint64_t by_branch() const { return braname.value; }
39};
40
41typedef eosio::multi_index<
42 "weights"_n, branch_weight,
43 eosio::indexed_by<"bycontrbra"_n, eosio::const_mem_fun<branch_weight, uint128_t, &branch_weight::by_contract_branch>>,
44 eosio::indexed_by<"bybranch"_n, eosio::const_mem_fun<branch_weight, uint64_t, &branch_weight::by_branch>>>
46
57struct [[eosio::table, eosio::contract(BRANCH)]] branch_weight_total {
58 uint64_t id;
59 eosio::name braname;
60 eosio::name contract;
61 uint64_t total_weight;
62
63 uint64_t primary_key() const { return id; }
64 uint128_t by_contract_branch() const { return combine_ids(contract.value, braname.value); }
65 uint64_t by_branch() const { return braname.value; }
66};
67
68typedef eosio::multi_index<
69 "weighttotals"_n, branch_weight_total,
70 eosio::indexed_by<"bycontrbra"_n, eosio::const_mem_fun<branch_weight_total, uint128_t, &branch_weight_total::by_contract_branch>>,
71 eosio::indexed_by<"bybranch"_n, eosio::const_mem_fun<branch_weight_total, uint64_t, &branch_weight_total::by_branch>>>
73
79void apply_weight_total_delta(eosio::name coopname, eosio::name braname,
80 eosio::name contract, int64_t delta) {
81 if (delta == 0) return;
82
83 branch_weight_totals_index totals(_branch, coopname.value);
84 auto idx = totals.get_index<"bycontrbra"_n>();
85 auto it = idx.find(combine_ids(contract.value, braname.value));
86
87 if (it == idx.end()) {
88 eosio::check(delta > 0, "Системная ошибка: агрегат весов распределения отсутствует");
89 totals.emplace(RamPayer::of(totals, coopname), [&](auto& t) {
90 t.id = totals.available_primary_key();
91 t.braname = braname;
92 t.contract = contract;
93 t.total_weight = static_cast<uint64_t>(delta);
94 });
95 return;
96 }
97
98 eosio::check(delta > 0 || it->total_weight >= static_cast<uint64_t>(-delta),
99 "Системная ошибка: агрегат весов распределения ушёл бы в минус");
100 const uint64_t new_total = (delta > 0)
101 ? it->total_weight + static_cast<uint64_t>(delta)
102 : it->total_weight - static_cast<uint64_t>(-delta);
103
104 if (new_total == 0) {
105 idx.erase(it);
106 } else {
107 idx.modify(it, RamPayer::of(idx, coopname), [&](auto& t) { t.total_weight = new_total; });
108 }
109}
110
114uint64_t get_weight_total(eosio::name coopname, eosio::name braname, eosio::name contract) {
115 branch_weight_totals_index totals(_branch, coopname.value);
116 auto idx = totals.get_index<"bycontrbra"_n>();
117 auto it = idx.find(combine_ids(contract.value, braname.value));
118 return it == idx.end() ? 0 : it->total_weight;
119}
120
121// Плательщик за оперативную память строк таблицы — правило в lib/core/ram_payer.hpp.
static constexpr eosio::name _branch
Definition: consts.hpp:181
contract
Definition: eosio.msig_tests.cpp:977
eosio::name of(const Table &table, eosio::name coopname)
Definition: ram_payer.hpp:78
Definition: eosio.msig.hpp:34
Агрегат весов по (КУ, контракт-источник): сумма весов всех участников распределения....
Definition: table_branch_weights.hpp:57
uint64_t by_branch() const
Definition: table_branch_weights.hpp:65
uint128_t by_contract_branch() const
Definition: table_branch_weights.hpp:64
uint64_t total_weight
Σ весов участников
Definition: table_branch_weights.hpp:61
eosio::name braname
кооперативный участок
Definition: table_branch_weights.hpp:59
uint64_t id
суррогатный ключ (scope coopname)
Definition: table_branch_weights.hpp:58
uint64_t primary_key() const
Definition: table_branch_weights.hpp:63
eosio::name contract
контракт-источник распределения
Definition: table_branch_weights.hpp:60
Реестр распределения членских взносов кооперативного участка (requirement b6 «Экономика КУ»).
Definition: table_branch_weights.hpp:29
uint64_t primary_key() const
Definition: table_branch_weights.hpp:36
uint128_t by_contract_branch() const
Definition: table_branch_weights.hpp:37
uint64_t id
суррогатный ключ (scope coopname)
Definition: table_branch_weights.hpp:30
eosio::name username
председатель или доверенный КУ
Definition: table_branch_weights.hpp:33
uint64_t weight
вес; доля = weight / Σ weights(braname, contract)
Definition: table_branch_weights.hpp:34
eosio::name braname
кооперативный участок
Definition: table_branch_weights.hpp:31
uint64_t by_branch() const
Definition: table_branch_weights.hpp:38
eosio::name contract
контракт-источник распределения (например, marketplace)
Definition: table_branch_weights.hpp:32
eosio::multi_index< "weights"_n, branch_weight, eosio::indexed_by<"bycontrbra"_n, eosio::const_mem_fun< branch_weight, uint128_t, &branch_weight::by_contract_branch > >, eosio::indexed_by<"bybranch"_n, eosio::const_mem_fun< branch_weight, uint64_t, &branch_weight::by_branch > > > branch_weights_index
Definition: table_branch_weights.hpp:45
RAM_PAYER_CLASS(branch_weight, contract)
uint64_t get_weight_total(eosio::name coopname, eosio::name braname, eosio::name contract)
Σ весов по (braname, contract); 0 — распределение не настроено.
Definition: table_branch_weights.hpp:114
void apply_weight_total_delta(eosio::name coopname, eosio::name braname, eosio::name contract, int64_t delta)
Применить дельту к агрегату весов (braname, contract). Создаёт запись при первой положительной дельте...
Definition: table_branch_weights.hpp:79
eosio::multi_index< "weighttotals"_n, branch_weight_total, eosio::indexed_by<"bycontrbra"_n, eosio::const_mem_fun< branch_weight_total, uint128_t, &branch_weight_total::by_contract_branch > >, eosio::indexed_by<"bybranch"_n, eosio::const_mem_fun< branch_weight_total, uint64_t, &branch_weight_total::by_branch > > > branch_weight_totals_index
Definition: table_branch_weights.hpp:72
static uint128_t combine_ids(const uint64_t &x, const uint64_t &y)
Definition: utils.hpp:7