COOPENOMICS  v1
Кооперативная Экономика
wallet.hpp
См. документацию.
1#pragma once
2
3#include <optional>
4#include <set>
5#include <string>
6
7#include <eosio/eosio.hpp>
8
9#include "../../consts.hpp"
10#include "../actions.hpp"
11#include "../programs.hpp"
12#include "../soviet/soviet.hpp"
13
14using namespace eosio;
15
16#define COMPLETEWTHD_SIGNATURE name coopname, checksum256 withdraw_hash
17#define DECLINEWTHD_SIGNATURE name coopname, checksum256 withdraw_hash, std::string reason
18
21
22#define AUTHWTHD_SIGNATURE AUTHORIZE_CALLBACK_SIGNATURE
24
25static const std::set<eosio::name> wallet_callback_actions = {
26 "authwthd"_n,
27 "declinewthd"_n,
28 "completewthd"_n,
29};
30
31class Wallet {
32public:
37
38 static eosio::name get_valid_wallet_action(const eosio::name &action) {
39 eosio::check(wallet_callback_actions.contains(action), "Недопустимое имя действия wallet");
40 return action;
41 }
42
43 static void validate_asset(const eosio::asset &amount) {
44 check(amount.symbol == _root_govern_symbol, "Неверный символ токена");
45 check(amount.is_valid(), "Неверный актив");
46 check(amount.amount >= 0, "Сумма должна быть неотрицательной");
47 }
48
49 static void add_available_funds(eosio::name contract, eosio::name coopname, eosio::name username,
50 eosio::asset amount, eosio::name program_type, std::string memo) {
51 auto program_row = get_program_or_fail(coopname, get_program_id(program_type));
52
53 action(permission_level{contract, "active"_n}, _soviet, "addbal"_n,
54 std::make_tuple(coopname, username, program_row.id, amount, memo))
55 .send();
56 }
57
58 static void sub_available_funds(eosio::name contract, eosio::name coopname, eosio::name username,
59 eosio::asset amount, eosio::name program_type, std::string memo) {
60 auto program_row = get_program_or_fail(coopname, get_program_id(program_type));
61
62 action(permission_level{contract, "active"_n}, _soviet, "subbal"_n,
63 std::make_tuple(coopname, username, program_row.id, amount, false, memo))
64 .send();
65 }
66
67 static void add_blocked_funds(eosio::name contract, eosio::name coopname, eosio::name username,
68 eosio::asset amount, eosio::name program_type, std::string memo) {
69 auto program_row = get_program_or_fail(coopname, get_program_id(program_type));
70
71 action(permission_level{contract, "active"_n}, _soviet, "addbal"_n,
72 std::make_tuple(coopname, username, program_row.id, amount, memo))
73 .send();
74
75 action(permission_level{contract, "active"_n}, _soviet, "blockbal"_n,
76 std::make_tuple(coopname, username, program_row.id, amount, memo))
77 .send();
78 }
79
80 static void sub_blocked_funds(eosio::name contract, eosio::name coopname, eosio::name username,
81 eosio::asset amount, eosio::name program_type, std::string memo) {
82 auto program_row = get_program_or_fail(coopname, get_program_id(program_type));
83 print("▶ Уменьшаем заблокированные средства кошелька пользователя: ", amount, " для пользователя: ",
84 username);
85 print("▶ Программа: ", program_row.id);
86 print("▶ Доступно: ", program_row.available->amount);
87 print("▶ Заблокированные средства: ", program_row.blocked->amount);
88 print("▶ Сумма для уменьшения: ", amount.amount);
89
90 action(permission_level{contract, "active"_n}, _soviet, "unblockbal"_n,
91 std::make_tuple(coopname, username, program_row.id, amount, memo))
92 .send();
93
94 action(permission_level{contract, "active"_n}, _soviet, "subbal"_n,
95 std::make_tuple(coopname, username, program_row.id, amount, false, memo))
96 .send();
97 }
98
99 static void block_funds(eosio::name contract, eosio::name coopname, eosio::name username,
100 eosio::asset amount, eosio::name program_type, std::string memo) {
101 auto program_row = get_program_or_fail(coopname, get_program_id(program_type));
102
103 action(permission_level{contract, "active"_n}, _soviet, "blockbal"_n,
104 std::make_tuple(coopname, username, program_row.id, amount, memo))
105 .send();
106 }
107
108 static void unblock_funds(eosio::name contract, eosio::name coopname, eosio::name username,
109 eosio::asset amount, eosio::name program_type, std::string memo) {
110 auto program_row = get_program_or_fail(coopname, get_program_id(program_type));
111
112 action(permission_level{contract, "active"_n}, _soviet, "unblockbal"_n,
113 std::make_tuple(coopname, username, program_row.id, amount, memo))
114 .send();
115 }
116
117 static void pay_membership_fee(name contract, name coopname, name username, eosio::asset amount,
118 uint64_t program_id, std::string memo) {
119 auto program_row = get_program_or_fail(coopname, program_id);
120
121 action(permission_level{contract, "active"_n}, _soviet, "addmemberfee"_n,
122 std::make_tuple(coopname, username, program_row.id, amount, memo))
123 .send();
124 }
125
126 static void unpay_membership_fee(name contract, name coopname, name username, eosio::asset amount,
127 uint64_t program_id, std::string memo) {
128 auto program_row = get_program_or_fail(coopname, program_id);
129
130 action(permission_level{contract, "active"_n}, _soviet, "submemberfee"_n,
131 std::make_tuple(coopname, username, program_row.id, amount, memo))
132 .send();
133 }
134
135 static std::optional<deposit> get_deposit(eosio::name coopname, const checksum256 &hash) {
136 deposits_index primary_index(_wallet, coopname.value);
137 auto secondary_index = primary_index.get_index<"byhash"_n>();
138
139 auto itr = secondary_index.find(hash);
140 if (itr == secondary_index.end()) {
141 return std::nullopt;
142 }
143
144 return *itr;
145 }
146
147 static std::optional<withdraw> get_withdraw(eosio::name coopname, const checksum256 &hash) {
148 withdraws_index primary_index(_wallet, coopname.value);
149 auto secondary_index = primary_index.get_index<"byhash"_n>();
150
151 auto itr = secondary_index.find(hash);
152 if (itr == secondary_index.end()) {
153 return std::nullopt;
154 }
155
156 return *itr;
157 }
158};
Definition: wallet.hpp:31
static void validate_asset(const eosio::asset &amount)
Definition: wallet.hpp:43
static eosio::name get_valid_wallet_action(const eosio::name &action)
Definition: wallet.hpp:38
WalletTables::deposits_index deposits_index
Definition: wallet.hpp:35
static void add_blocked_funds(eosio::name contract, eosio::name coopname, eosio::name username, eosio::asset amount, eosio::name program_type, std::string memo)
Definition: wallet.hpp:67
static void unpay_membership_fee(name contract, name coopname, name username, eosio::asset amount, uint64_t program_id, std::string memo)
Definition: wallet.hpp:126
static std::optional< withdraw > get_withdraw(eosio::name coopname, const checksum256 &hash)
Definition: wallet.hpp:147
static void unblock_funds(eosio::name contract, eosio::name coopname, eosio::name username, eosio::asset amount, eosio::name program_type, std::string memo)
Definition: wallet.hpp:108
static void sub_available_funds(eosio::name contract, eosio::name coopname, eosio::name username, eosio::asset amount, eosio::name program_type, std::string memo)
Definition: wallet.hpp:58
static void add_available_funds(eosio::name contract, eosio::name coopname, eosio::name username, eosio::asset amount, eosio::name program_type, std::string memo)
Definition: wallet.hpp:49
static std::optional< deposit > get_deposit(eosio::name coopname, const checksum256 &hash)
Definition: wallet.hpp:135
WalletTables::withdraws_index withdraws_index
Definition: wallet.hpp:36
static void block_funds(eosio::name contract, eosio::name coopname, eosio::name username, eosio::asset amount, eosio::name program_type, std::string memo)
Definition: wallet.hpp:99
static void pay_membership_fee(name contract, name coopname, name username, eosio::asset amount, uint64_t program_id, std::string memo)
Definition: wallet.hpp:117
static void sub_blocked_funds(eosio::name contract, eosio::name coopname, eosio::name username, eosio::asset amount, eosio::name program_type, std::string memo)
Definition: wallet.hpp:80
static constexpr eosio::symbol _root_govern_symbol
Definition: consts.hpp:209
static constexpr eosio::name _wallet
Definition: consts.hpp:153
static constexpr eosio::name _soviet
Definition: consts.hpp:156
contract
Definition: eosio.msig_tests.cpp:977
permission_level
Definition: eosio.msig_tests.cpp:896
share_type amount
Definition: eosio.token_tests.cpp:174
#define AUTHWTHD_SIGNATURE
Definition: wallet.hpp:22
#define COMPLETEWTHD_SIGNATURE
Definition: wallet.hpp:16
void(COMPLETEWTHD_SIGNATURE) completewthd_interface
Definition: wallet.hpp:19
static const std::set< eosio::name > wallet_callback_actions
Definition: wallet.hpp:25
#define DECLINEWTHD_SIGNATURE
Definition: wallet.hpp:17
void(AUTHWTHD_SIGNATURE) authwthd_interface
Definition: wallet.hpp:23
void(DECLINEWTHD_SIGNATURE) declinewthd_interface
Definition: wallet.hpp:20
void send(name contract, name action_name, name actor, Args &&... args)
Definition: actions.hpp:10
multi_index< "withdraws"_n, withdraw, indexed_by<"byusername"_n, const_mem_fun< withdraw, uint64_t, &withdraw::by_username > >, indexed_by<"byhash"_n, const_mem_fun< withdraw, checksum256, &withdraw::by_hash > >, indexed_by<"bystatus"_n, const_mem_fun< withdraw, uint64_t, &withdraw::by_status > >, indexed_by<"bycreated"_n, const_mem_fun< withdraw, uint64_t, &withdraw::by_created > > > withdraws_index
Definition: table_wallet_withdraws.hpp:45
multi_index< "deposits"_n, deposit, indexed_by<"byhash"_n, const_mem_fun< deposit, checksum256, &deposit::by_hash > >, indexed_by<"byusername"_n, const_mem_fun< deposit, uint64_t, &deposit::by_username > >, indexed_by<"bystatus"_n, const_mem_fun< deposit, uint64_t, &deposit::by_status > >, indexed_by<"bycreated"_n, const_mem_fun< deposit, uint64_t, &deposit::by_created > > > deposits_index
Definition: table_wallet_deposits.hpp:41
Definition: eosio.msig.hpp:34
program get_program_or_fail(eosio::name coopname, uint64_t program_id)
Definition: programs.hpp:16
uint64_t get_program_id(const eosio::name &type)
Definition: programs.hpp:93
action(permission_level{ _gateway, "active"_n}, _gateway, "adduser"_n, std::make_tuple(coopname, deposit->username, to_spread, to_circulation, eosio::current_time_point(), true)).send()
Definition: table_wallet_deposits.hpp:17
Definition: table_wallet_withdraws.hpp:18