Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed per address limit and available balance #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
node_modules
package-lock.json
*.local.conf
bin/*
.seeds
Expand Down
2 changes: 1 addition & 1 deletion http/faucet-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class KaspaFaucetApp extends FlowApp {
<div col class='form-wrapper'>
<faucet-info limit="${limit}" available="${available}" address="${address}"></faucet-info>
<faucet-form network="${network}" .networks="${this.networks}" address="${address}" @network-change="${this.onNetworkChange}">
<div slot="captcha" class="${this.captchaKey? 'g-recaptcha': ''}"
<div slot="captcha" class="${this.captchaKey? 'h-captcha': ''}"
data-sitekey="${this.captchaKey}"></div>
</faucet-form>
</div>
Expand Down
4 changes: 2 additions & 2 deletions http/faucet-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class FaucetForm extends BaseElement {
let address = qS(".address").value;
let network = qS(".network").value;
let amount = qS(".amount").value;
let captcha = this.querySelector('.g-recaptcha .g-recaptcha-response')?.value;
let captcha = this.querySelector('[name=h-captcha-response]')?.value;
//let network = flow.app.network;
//kaspatest:qq0nvlmn07f6edcdfynt4nu4l4r58rkquuvgt635ac
console.log({ address, network, amount, captcha });
Expand Down Expand Up @@ -173,5 +173,5 @@ export class FaucetForm extends BaseElement {
}

FaucetForm.define("faucet-form",{
"window.grecaptcha":"https://www.google.com/recaptcha/api.js?onload=OnReCaptchaLoad&render=explicit"
"window.grecaptcha":"https://js.hcaptcha.com/1/api.js?onload=OnReCaptchaLoad&render=explicit"
});
30 changes: 22 additions & 8 deletions kaspa-faucet.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,26 @@ class KaspaFaucet extends EventEmitter{
if(this.limits[network] == Number.MAX_SAFE_INTEGER)
return Number.MAX_SAFE_INTEGER;

let user_by_addr = this.address_limit_map.get(address);
let user_by_ip = this.ip_limit_map.get(ip);

let user = user_by_addr || user_by_ip;
if(!user) {
user = { };
this.ip_limit_map.set(ip,user);
let user_by_addr = address && address !== 'default' ? this.address_limit_map.get(address) : undefined;
let user_by_ip = ip ? this.ip_limit_map.get(ip) : undefined;

let user;
if (!user_by_addr && user_by_ip) {
log.info(`Using limit for ip ${ip} for unknown address ${address}`);
this.address_limit_map.set(address,user_by_ip);
user = user_by_ip;
} else if (!user_by_ip && user_by_addr) {
log.info(`Using limit for address ${address} for unknown ip ${ip}`);
this.ip_limit_map.set(ip, user_by_addr);
user = user_by_addr;
} else if (user_by_addr && user_by_ip) {
log.info(`Using limit for address ${address} for known ip ${ip}`);
this.ip_limit_map.set(ip, user_by_addr);
user = user_by_addr;
} else {
user = {};
this.address_limit_map.set(address,user);
this.ip_limit_map.set(ip,user);
}

if(!user[network])
Expand All @@ -198,6 +210,7 @@ class KaspaFaucet extends EventEmitter{
const spent = transactions.reduce((v, tx) => tx.amount+v, 0);
const available = this.limits[network] - spent;
const period = transactions.length ? transactions[0].ts - period_start : null;
log.info(`Address ${address} on ip ${ip} has ${available / 100000000} KAS available`);
return { available, period };
}

Expand All @@ -219,7 +232,8 @@ class KaspaFaucet extends EventEmitter{
(async()=>{
for await(const event of socketConnections) {
const { networks, addresses, limits } = this;
const { socket, ip } = event;
const { socket } = event;
const ip = socket.ip;
socket.publish('networks', { networks });
socket.publish('addresses', { addresses });
networks.forEach(network=>{
Expand Down
Loading