-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheosio.faucet.hpp
203 lines (177 loc) · 6 KB
/
eosio.faucet.hpp
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
201
202
203
#pragma once
#include <eosio/crypto.hpp>
#include <eosio/eosio.hpp>
#include <eosio/system.hpp>
#include <eosio/asset.hpp>
#include <eosio/crypto.hpp>
#include <string>
using namespace eosio;
using namespace std;
class [[eosio::contract("eosio.faucet")]] faucet : public eosio::contract {
public:
using contract::contract;
// Token Transfer
const name TOKEN = "eosio.token"_n;
const symbol EOS = symbol{"EOS", 4};
const asset QUANTITY = asset{10000, EOS}; // (1.0000 EOS)
const asset QUANTITY_DECREMENT = asset{1000, EOS}; // (0.1 EOS) decrement amount per counter
const asset GAS_FEE = asset{100, EOS}; // (0.01 EOS)
const string MEMO = "received by https://faucet.testnet.evm.eosnetwork.com";
// Account creation
const asset NET = asset{1'0000, EOS};
const asset CPU = asset{1'0000, EOS};
const uint32_t RAM = 8000;
// Stats
const uint32_t STATS_INTERVAL = 3600; // 1 hour
// Data pruning
const uint32_t TTL_HISTORY = 86400 * 7; // 7 days
const uint32_t TTL_USER_RATE_LIMIT = 86400; // 24 hours
// Rate limits
const uint32_t USER_COOLDOWN = 60; // (1 minute) user cooldown timer per single faucet event
const uint32_t MAX_COUNTER_PER_USER = 10; // max rate limit per user counters (resests by TTL_USER_RATE_LIMIT)
const uint32_t MAX_COUNTER_PER_GLOBAL = 5000; // max rate limit per global counters (resets by STATS_INTERVAL)
/**
* ## TABLE `ratelimit`
*
* - `{uint64_t} id` - (primary key) incremental key
* - `{string} to` - receiver account (EOS or EVM)
* - `{uint64_t} counter` - counter used to rate limit total actions allowed per time
* - `{time_point_sec} next` - next available send
*
* ### example
*
* ```json
* {
* "id": 1,
* "address": "aa2F34E41B397aD905e2f48059338522D05CA534",
* "counter": 10,
* "last_send_time": "2022-07-24T00:00:00"
* }
* ```
*/
struct [[eosio::table("ratelimit")]] ratelimit_row {
uint64_t id;
string address;
uint64_t counter;
time_point_sec last_send_time;
checksum256 by_address() const { return to_checksum(address); }
uint64_t primary_key() const { return id; }
};
typedef eosio::multi_index< "ratelimit"_n, ratelimit_row,
indexed_by<"by.address"_n, const_mem_fun<ratelimit_row, checksum256, &ratelimit_row::by_address>>
> ratelimit_table;
static checksum256 to_checksum( string address )
{
if ( address.length() > 40 ) address = address.substr(2);
return sha256(address.c_str(), address.length());
}
/**
* ## TABLE `stats`
*
* - `{time_point_sec} timestamp` - timestamp for the stats
* - `{uint64_t} counter` - counter total send transactions
*
* ### example
*
* ```json
* {
* "timestamp": "2022-07-24T00:00:00",
* "counter": 10,
* }
* ```
*/
struct [[eosio::table("stats")]] stats_row {
time_point_sec timestamp;
uint64_t counter;
uint64_t primary_key() const { return timestamp.sec_since_epoch(); }
};
typedef eosio::multi_index< "stats"_n, stats_row> stats_table;
/**
* ## TABLE `history`
*
* - `{name} to` - receiver account
* - `{time_point_sec} next` - next available send
*
* ### example
*
* ```json
* {
* "id": "send",
* "total": 1000,
* "timestamp": "2022-07-24T00:00:00"
* }
* ```
*/
struct [[eosio::table("history")]] history_row {
uint64_t id;
string receiver;
time_point_sec timestamp;
uint64_t primary_key() const { return id; }
};
typedef eosio::multi_index< "history"_n, history_row > history_table;
/**
* ## ACTION `send`
*
* > Send tokens to {{to}} receiver account.
*
* - **authority**: `get_self()`
*
* ### params
*
* - `{string} to` - receiver account (EOS or EVM)
*
* ### Example
*
* ```bash
* $ cleos push action eosio.faucet send '["myaccount"]' -p anyaccount
* $ cleos push action eosio.faucet send '["0xaa2F34E41B397aD905e2f48059338522D05CA534"]' -p anyaccount
* ```
*/
[[eosio::action]]
void send( const string to );
[[eosio::action]]
void nonce( const uint64_t nonce );
/**
* ## ACTION `create`
*
* > Create account using {{key}} as active & owner permission.
*
* - **authority**: `get_self()`
*
* ### params
*
* - `{name} account` - account to be created
* - `{public_key} key` - EOSIO public key to be used as active/owner permission
*
* ### Example
*
* ```bash
* $ cleos push action eosio.faucet create '[myaccount, "EOS5uHeBsURAT6bBXNtvwKtWaiDSDJSdSmc96rHVws5M1qqVCkAm6"]' -p anyaccount
* ```
*/
[[eosio::action]]
void create( const name account, const public_key key );
// @debug
[[eosio::action]]
void test( const string address );
// @debug
[[eosio::action]]
void cleartable( const name table_name, const optional<name> scope, const optional<uint64_t> max_rows );
// action wrappers
using send_action = eosio::action_wrapper<"send"_n, &faucet::send>;
private :
// debug
template <typename T>
void clear_table( T& table, uint64_t rows_to_clear );
void create_account( const name account, const public_key key );
void transfer( const name from, const name to, const extended_asset value, const string& memo );
void send_eos( const string address );
void send_eos_evm( const string address );
void send_eos_native( const string address );
uint64_t add_ratelimit( const string address );
void add_history( const string address );
void prune_rate_limits();
void prune_rate_limit( const string address );
void prune_history();
void add_stats();
};