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:
40
41 static eosio::name get_valid_wallet_action(const eosio::name &action) {
42 eosio::check(wallet_callback_actions.contains(action), "Недопустимое имя действия wallet");
43 return action;
44 }
45
46 static void validate_asset(const eosio::asset &amount) {
47 check(amount.symbol == _root_govern_symbol, "Неверный символ токена");
48 check(amount.is_valid(), "Неверный актив");
49 check(amount.amount >= 0, "Сумма должна быть неотрицательной");
50 }
51
52 static void add_available_funds(eosio::name contract, eosio::name coopname, eosio::name username,
53 eosio::asset amount, eosio::name program_type, std::string memo) {
54 auto program_row = get_program_or_fail(coopname, get_program_id(program_type));
55
56 action(permission_level{contract, "active"_n}, _soviet, "addbal"_n,
57 std::make_tuple(coopname, username, program_row.id, amount, memo))
58 .send();
59 }
60
61 static void sub_available_funds(eosio::name contract, eosio::name coopname, eosio::name username,
62 eosio::asset amount, eosio::name program_type, std::string memo) {
63 auto program_row = get_program_or_fail(coopname, get_program_id(program_type));
64
65 action(permission_level{contract, "active"_n}, _soviet, "subbal"_n,
66 std::make_tuple(coopname, username, program_row.id, amount, false, memo))
67 .send();
68 }
69
70 static void add_blocked_funds(eosio::name contract, eosio::name coopname, eosio::name username,
71 eosio::asset amount, eosio::name program_type, std::string memo) {
72 auto program_row = get_program_or_fail(coopname, get_program_id(program_type));
73
74 action(permission_level{contract, "active"_n}, _soviet, "addbal"_n,
75 std::make_tuple(coopname, username, program_row.id, amount, memo))
76 .send();
77
78 action(permission_level{contract, "active"_n}, _soviet, "blockbal"_n,
79 std::make_tuple(coopname, username, program_row.id, amount, memo))
80 .send();
81 }
82
83 static void sub_blocked_funds(eosio::name contract, eosio::name coopname, eosio::name username,
84 eosio::asset amount, eosio::name program_type, std::string memo) {
85 auto program_row = get_program_or_fail(coopname, get_program_id(program_type));
86 print("▶ Уменьшаем заблокированные средства кошелька пользователя: ", amount, " для пользователя: ",
87 username);
88 print("▶ Программа: ", program_row.id);
89 print("▶ Доступно: ", program_row.available->amount);
90 print("▶ Заблокированные средства: ", program_row.blocked->amount);
91 print("▶ Сумма для уменьшения: ", amount.amount);
92
93 action(permission_level{contract, "active"_n}, _soviet, "unblockbal"_n,
94 std::make_tuple(coopname, username, program_row.id, amount, memo))
95 .send();
96
97 action(permission_level{contract, "active"_n}, _soviet, "subbal"_n,
98 std::make_tuple(coopname, username, program_row.id, amount, false, memo))
99 .send();
100 }
101
102 static void block_funds(eosio::name contract, eosio::name coopname, eosio::name username,
103 eosio::asset amount, eosio::name program_type, std::string memo) {
104 auto program_row = get_program_or_fail(coopname, get_program_id(program_type));
105
106 action(permission_level{contract, "active"_n}, _soviet, "blockbal"_n,
107 std::make_tuple(coopname, username, program_row.id, amount, memo))
108 .send();
109 }
110
111 static void unblock_funds(eosio::name contract, eosio::name coopname, eosio::name username,
112 eosio::asset amount, eosio::name program_type, std::string memo) {
113 auto program_row = get_program_or_fail(coopname, get_program_id(program_type));
114
115 action(permission_level{contract, "active"_n}, _soviet, "unblockbal"_n,
116 std::make_tuple(coopname, username, program_row.id, amount, memo))
117 .send();
118 }
119
120 static void pay_membership_fee(name contract, name coopname, name username, eosio::asset amount,
121 uint64_t program_id, std::string memo) {
122 auto program_row = get_program_or_fail(coopname, program_id);
123
124 action(permission_level{contract, "active"_n}, _soviet, "addmemberfee"_n,
125 std::make_tuple(coopname, username, program_row.id, amount, memo))
126 .send();
127 }
128
129 static void unpay_membership_fee(name contract, name coopname, name username, eosio::asset amount,
130 uint64_t program_id, std::string memo) {
131 auto program_row = get_program_or_fail(coopname, program_id);
132
133 action(permission_level{contract, "active"_n}, _soviet, "submemberfee"_n,
134 std::make_tuple(coopname, username, program_row.id, amount, memo))
135 .send();
136 }
137
138 static std::optional<deposit> get_deposit(eosio::name coopname, const checksum256 &hash) {
139 deposits_index primary_index(_wallet, coopname.value);
140 auto secondary_index = primary_index.get_index<"byhash"_n>();
141
142 auto itr = secondary_index.find(hash);
143 if (itr == secondary_index.end()) {
144 return std::nullopt;
145 }
146
147 return *itr;
148 }
149
150 static std::optional<withdraw> get_withdraw(eosio::name coopname, const checksum256 &hash) {
151 withdraws_index primary_index(_wallet, coopname.value);
152 auto secondary_index = primary_index.get_index<"byhash"_n>();
153
154 auto itr = secondary_index.find(hash);
155 if (itr == secondary_index.end()) {
156 return std::nullopt;
157 }
158
159 return *itr;
160 }
161};
Definition: wallet.hpp:31
static void validate_asset(const eosio::asset &amount)
Definition: wallet.hpp:46
static eosio::name get_valid_wallet_action(const eosio::name &action)
Definition: wallet.hpp:41
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:70
static void unpay_membership_fee(name contract, name coopname, name username, eosio::asset amount, uint64_t program_id, std::string memo)
Definition: wallet.hpp:129
static std::optional< withdraw > get_withdraw(eosio::name coopname, const checksum256 &hash)
Definition: wallet.hpp:150
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:111
WalletTables::users_index users_index
Definition: wallet.hpp:39
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:61
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:52
static std::optional< deposit > get_deposit(eosio::name coopname, const checksum256 &hash)
Definition: wallet.hpp:138
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:102
static void pay_membership_fee(name contract, name coopname, name username, eosio::asset amount, uint64_t program_id, std::string memo)
Definition: wallet.hpp:120
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:83
static constexpr eosio::symbol _root_govern_symbol
Definition: consts.hpp:236
static constexpr eosio::name _wallet
Definition: consts.hpp:174
static constexpr eosio::name _soviet
Definition: consts.hpp:177
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:47
multi_index<"users"_n, user > users_index
Definition: table_wallet_users.hpp:46
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:43
Definition: eosio.msig.hpp:34
program get_program_or_fail(eosio::name coopname, uint64_t program_id)
Definition: programs.hpp:24
uint64_t get_program_id(const eosio::name &type)
Definition: programs.hpp:99
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:19
Definition: table_wallet_users.hpp:23
Definition: table_wallet_users.hpp:39
Definition: table_wallet_withdraws.hpp:20