This repository has been archived by the owner on Nov 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
claim.pomelo.cpp
200 lines (166 loc) · 8.02 KB
/
claim.pomelo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <eosio.token/eosio.token.hpp>
#include <eosio.system/eosio.system.hpp>
#include <sx.utils/utils.hpp>
#include <pomelo.app/app.pomelo.hpp>
#include <eosn.login/login.eosn.hpp>
#include "claim.pomelo.hpp"
// error messages to user
static const string CONTACT_ADMIN = ", please contact Pomelo admin";
static const string ERROR_MAINTENANCE = "claim.pomelo: contract is under maintenance" + CONTACT_ADMIN;
static const string ERROR_REQUIRE_APPROVE = "claim.pomelo::claim: grant requires admin approval" + CONTACT_ADMIN;
static const string ERROR_NO_BALANCE = "claim.pomelo::transfer: not enough balance to transfer" + CONTACT_ADMIN;
static const string ERROR_AUTHORITY_BY = "claim.pomelo::claim: invalid claiming account (claim must be authorized by [funding_account] or [author_user_id] )";
static const string ERROR_ALREADY_CLAIMED = "claim.pomelo::claim: grant has already claimed";
static const char* ERROR_GRANT_NOT_EXISTS = "claim.pomelo::claim: [grant_id] does not exists";
static const char* ERROR_KYC_NOT_FOUND = "claim.pomelo::check_kyc: [author_user_id] not found";
static const string ERROR_KYC_REQUIRED = "claim.pomelo::check_kyc: grant [author_user_id] needs to first pass KYC";
// variables
static const name KYC_PROVIDER = "shufti"_n;
static const uint32_t CLAIM_PERIOD_DAYS = 90;
[[eosio::action]]
void claimpomelo::setconfig( const optional<config_row> config )
{
require_auth( get_self() );
config_table _config( get_self(), get_self().value );
if ( !config ) {
_config.remove();
return;
}
check( is_account(config->pomelo_app), "claim.pomelo: invalid pomelo app account");
check( is_account(config->login_contract), "claim.pomelo: invalid login contract");
_config.set(*config, get_self());
}
[[eosio::action]]
void claimpomelo::setclaim( const uint16_t round_id, const name grant_id, const extended_asset claim )
{
require_auth( get_self() );
check_status();
config_table _config( get_self(), get_self().value );
claims_table _claims( get_self(), round_id );
const name POMELO_APP = _config.get().pomelo_app;
// get author & funding directly from pomelo app
pomelo::grants_table _grants( POMELO_APP, POMELO_APP.value );
auto grant = _grants.get( grant_id.value, "claim.pomelo::setclaim: [grant_id] does not exists in pomelo.app::grants");
check( grant.status == "published"_n, "claim.pomelo::setclaim: [grant.status] must be published");
// validate grant has received matching amounts
pomelo::match_table _match( POMELO_APP, round_id );
auto match = _match.get( grant_id.value, "claim.pomelo::setclaim: [grant_id] does not exists in pomelo.app::match");
check( match.total_users > 0, "claim.pomelo::setclaim: [match.total_users] has no contributing users");
check( match.sum_value > 0, "claim.pomelo::setclaim: [match.sum_value] has no contributions");
// no duplicates grant claims
check( _claims.find( grant_id.value ) == _claims.end(), "claim.pomelo::setclaim: [grant_id] already exists");
// check if token is valid
check( is_account( claim.contract ), "claim.pomelo::setclaim: [claim] token contract does not exists");
const symbol sym = claim.quantity.symbol;
const asset balance = token::get_balance(claim.contract, get_self(), sym.code() );
check( balance.symbol == sym, "claim.pomelo::setclaim: [claim] has invalid balance symbol");
_claims.emplace( get_self(), [&]( auto& row ) {
row.funding_account = grant.funding_account;
row.author_user_id = grant.author_user_id;
row.grant_id = grant_id;
row.created_at = current_time_point();
row.expires_at = time_point_sec(current_time_point().sec_since_epoch() + CLAIM_PERIOD_DAYS * 24 * 60 * 60);
row.claim = claim;
row.claimed = { 0, claim.get_extended_symbol() };
});
}
[[eosio::action]]
void claimpomelo::claim( const uint16_t round_id, const name grant_id, const bool staked )
{
// ** authority check is later
check_status();
claims_table _claims( get_self(), round_id );
const auto& claim = _claims.get( grant_id.value, ERROR_GRANT_NOT_EXISTS );
// validate claim
// ==============
// authority by [author or funding or self]
if ( !has_auth(claim.funding_account) && !has_auth(claim.author_user_id) && !has_auth(get_self()) ) {
check( false, ERROR_AUTHORITY_BY );
}
check( claim.claimed_at.sec_since_epoch() == 0, ERROR_ALREADY_CLAIMED );
check( claim.claim.quantity.amount != 0, ERROR_ALREADY_CLAIMED );
check( claim.approved, ERROR_REQUIRE_APPROVE );
check_kyc( claim.author_user_id );
// transfer matching prize to funding account
// 1. receive matching prize as staked
const symbol EOS = symbol{"EOS", 4};
if ( staked && claim.claim.quantity.symbol == EOS ) {
eosiosystem::system_contract::delegatebw_action delegatebw( "eosio"_n, { get_self(), "active"_n });
delegatebw.send( get_self(), claim.funding_account, asset{0, EOS}, claim.claim.quantity, true );
// 2. receive matching as liquid
} else {
transfer( claim.funding_account, claim.claim, "🍈 " + grant_id.to_string() + " matching prize received via Pomelo.io" );
}
// update claim status
_claims.modify( claim, get_self(), [&]( auto& row ) {
row.claimed_at = current_time_point();
row.claimed = claim.claim;
row.claim.quantity.amount = 0;
row.trx_id = get_trx_id();
});
// logging
claimlog_action claim_log( get_self(), { get_self(), "active"_n });
claim_log.send( round_id, grant_id, claim.funding_account, claim.author_user_id, claim.claimed );
}
[[eosio::action]]
void claimpomelo::cancel( const uint16_t round_id, const name grant_id )
{
require_auth( get_self() );
claims_table _claims( get_self(), round_id );
const auto& claim = _claims.get( grant_id.value, ERROR_GRANT_NOT_EXISTS );
_claims.erase( claim );
}
[[eosio::action]]
void claimpomelo::approve( const uint16_t round_id, const name grant_id, const bool approved )
{
require_auth( get_self() );
claims_table _claims( get_self(), round_id );
const auto& claim = _claims.get( grant_id.value, "claim.pomelo::approve: [grant_id] does not exists");
check( claim.approved != approved, "claim.pomelo::approve: [approved] was not modified");
_claims.modify( claim, get_self(), [&]( auto& row ) {
row.approved = approved;
});
}
void claimpomelo::check_status()
{
config_table _config(get_self(), get_self().value);
check( _config.exists() && _config.get().status == "ok"_n, ERROR_MAINTENANCE);
}
[[eosio::action]]
void claimpomelo::claimlog( const uint16_t round_id, const name grant_id, const name funding_account, const name author_user_id, const extended_asset claimed )
{
require_auth( get_self() );
require_recipient( funding_account );
require_recipient( author_user_id );
}
void claimpomelo::transfer( const name to, const extended_asset value, const string memo )
{
const auto balance = sx::utils::get_balance(value.get_extended_symbol(), get_self()).quantity;
check( balance >= value.quantity, ERROR_NO_BALANCE );
eosio::token::transfer_action transfer( value.contract, { get_self(), "active"_n });
transfer.send( get_self(), to, value.quantity, memo );
}
void claimpomelo::check_kyc( const name author_user_id )
{
config_table _config(get_self(), get_self().value);
const auto& config = _config.get();
eosn::login::users_table _users( config.login_contract, config.login_contract.value );
const auto& user = _users.get( author_user_id.value, ERROR_KYC_NOT_FOUND);
check( user.socials.count(KYC_PROVIDER), ERROR_KYC_REQUIRED );
}
template <typename T>
void claimpomelo::clear_table( T& table, uint64_t rows_to_clear )
{
auto itr = table.begin();
while ( itr != table.end() && rows_to_clear-- ) {
itr = table.erase( itr );
}
}
checksum256 claimpomelo::get_trx_id()
{
size_t size = transaction_size();
char buf[size];
size_t read = read_transaction( buf, size );
check( size == read, "claimpomelo::get_trx_id: read_transaction failed");
return sha256( buf, read );
}