forked from nimiq/safe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfaucet.js
More file actions
61 lines (55 loc) · 2.1 KB
/
Copy pathfaucet.js
File metadata and controls
61 lines (55 loc) · 2.1 KB
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
import XCaptcha from '../elements/x-captcha/x-captcha.js';
export default class Faucet {
static async tap(recipientAddress, captcha) {
const responseBody = {
address: recipientAddress,
};
if (captcha) {
if (captcha.provider) {
responseBody['captcha-provider'] = captcha.provider;
}
if (captcha.provider === XCaptcha.Providers.RECAPTCHA) {
responseBody['g-recaptcha-response'] = captcha.token;
} else {
responseBody['vaptcha-response'] = captcha.token;
}
}
const response = await fetch(`${Faucet.FAUCET_BACKEND}${Faucet.FAUCET_ENDPOINT_TAP}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(responseBody)
}).then(response => response.json());
if (!response.success) {
const error = new Error('Faucet error');
error.error = response.error;
error.msg = response.msg;
throw error;
}
const timestamp = Date.now();
localStorage[Faucet.KEY_LAST_TAPPED] = timestamp;
}
static async info() {
Faucet._infoPromise = Faucet._infoPromise
|| fetch(`${Faucet.FAUCET_BACKEND}${Faucet.FAUCET_ENDPOINT_INFO}`).then(response => response.json());
return Faucet._infoPromise;
}
static async getDispenseAmount() {
const info = await Faucet.info();
return info.dispenseAmount; // in NIM
}
static async getFaucetAddress() {
const info = await Faucet.info();
return info.address;
}
static getLastTapped() {
return localStorage[Faucet.KEY_LAST_TAPPED];
}
}
Faucet.FAUCET_BACKEND = window.location.origin.indexOf('nimiq.com') !== -1
? 'https://faucet.nimiq-network.com/' : 'https://faucet.nimiq-testnet.com/';
Faucet.CAPTCHA_REQUIRED = window.location.origin.indexOf('nimiq.com') !== -1;
Faucet.FAUCET_ENDPOINT_TAP = 'tapit';
Faucet.FAUCET_ENDPOINT_INFO = 'info';
Faucet.KEY_LAST_TAPPED = 'faucet-last-tapped';