-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
161 lines (136 loc) · 4.12 KB
/
app.js
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
import fs from 'fs';
import axios from 'axios';
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
import swaggerJsdoc from "swagger-jsdoc";
import { serve, setup } from "swagger-ui-express";
import { DirectSecp256k1HdWallet, coin, coins } from "@cosmjs/proto-signing";
import bech32 from "bech32";
import pkg from '@cosmjs/stargate';
const { assertIsDeliverTxSuccess, SigningStargateClient, defaultRegistryTypes, GasPrice } = pkg;
const app = express();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
let rawdata = fs.readFileSync('config.json');
let config = JSON.parse(rawdata);
function checkBech32Address(address) {
try {
bech32.decode(address);
return true;
} catch (error) {
return false;
}
}
function checkBech32Prefix(address) {
try {
const { prefix } = bech32.decode(address);
if (prefix === config.prefix) {
return true;
}
} catch (error) {
return false;
}
}
app.get('/faucet/ui', async function(req, res) {
if (config.enableUi) {
res.sendFile(path.join(__dirname, '/claim.html'));
} else {
res.status(403).send('Forbidden');
}
})
app.get('/faucet/available', async function(req, res) {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(config.mnemonic, { prefix: config.prefix });
const [firstAccount] = await wallet.getAccounts();
const account = await axios.get(config.lcdUrl + '/cosmos/bank/v1beta1/spendable_balances/' + firstAccount.address)
let available = 0;
account.data.balances.forEach(function (balance) {
if (balance.denom == config.denom) {
available = balance.amount;
}
})
res.json({
available: available
})
})
app.get('/faucet/last-claim', async function(req, res) {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(config.mnemonic, { prefix: config.prefix });
const [firstAccount] = await wallet.getAccounts();
const resultSender = await axios(
config.lcdUrl +
"/cosmos/tx/v1beta1/txs?events=message.sender=%27" +
firstAccount.address +
"%27&limit=10&order_by=2"
);
res.json({
lastclaim: resultSender.data
})
})
app.get('/faucet/claim/:address', async function(req, res) {
let addressTo = req.params.address;
if (!checkBech32Address(addressTo)) {
res.status(403).json({
result: "Invalid address"
})
return;
}
if (!checkBech32Prefix(addressTo)) {
res.status(403).json({
result: "Invalid address prefix"
})
return;
}
const account = await axios.get(config.lcdUrl + '/cosmos/bank/v1beta1/spendable_balances/' + addressTo)
const found = account.data.balances.find((element) => element.denom === config.denom)
if (found.amount > 0) {
res.status(403).json({
result: "You already have funds"
})
return;
}
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(config.mnemonic, { prefix: config.prefix });
const [firstAccount] = await wallet.getAccounts();
const client = await SigningStargateClient.connectWithSigner(config.rpcUrl, wallet, {
gasPrice: GasPrice.fromString(
config.gasPrice + config.denom
)
});
const foundMsgType = defaultRegistryTypes.find(
(element) => element[0] === "/cosmos.bank.v1beta1.MsgSend"
)
const finalMsg = {
typeUrl: foundMsgType[0],
value: foundMsgType[1].fromPartial({
"fromAddress": firstAccount.address,
"toAddress": addressTo,
"amount": coins(config.faucetAmount, config.denom)
}),
}
const result = await client.signAndBroadcast(firstAccount.address, [finalMsg], "auto", "")
assertIsDeliverTxSuccess(result);
res.json({
result: result
})
})
if (config.enableSwagger) {
// Swagger
const options = {
definition: {
openapi: "3.1.0",
info: {
title: config.name + " faucet",
version: "0.1.0",
},
},
apis: ["./routes/*.js"],
};
const specs = swaggerJsdoc(options);
app.use(
"/",
serve,
setup(specs, { explorer: false })
);
}
app.listen(config.dappPort, () => {
console.log(config.name + ` faucet app listening on port ${config.dappPort}`);
})