Compare commits
No commits in common. "shopify" and "main" have entirely different histories.
@ -8,7 +8,6 @@
|
|||||||
|
|
||||||
[server]
|
[server]
|
||||||
port = 3000
|
port = 3000
|
||||||
enableRateLimit=false
|
|
||||||
transferAmount = 10000000000 # 1 * 10^10 alnt
|
transferAmount = 10000000000 # 1 * 10^10 alnt
|
||||||
periodTransferLimit = 30000000000 # 3 * 10^10 alnt
|
periodTransferLimit = 30000000000 # 3 * 10^10 alnt
|
||||||
dbDir = "db"
|
dbDir = "db"
|
||||||
|
15
src/index.ts
15
src/index.ts
@ -25,7 +25,6 @@ interface Config {
|
|||||||
},
|
},
|
||||||
server: {
|
server: {
|
||||||
port: number
|
port: number
|
||||||
enableRateLimit: boolean
|
|
||||||
transferAmount: string
|
transferAmount: string
|
||||||
periodTransferLimit: string
|
periodTransferLimit: string
|
||||||
dbDir: string
|
dbDir: string
|
||||||
@ -45,7 +44,7 @@ async function main (): Promise<void> {
|
|||||||
app.use(cors());
|
app.use(cors());
|
||||||
|
|
||||||
app.post('/faucet', async (req, res) => {
|
app.post('/faucet', async (req, res) => {
|
||||||
const { address: accountAddress, amount } = req.body;
|
const { address: accountAddress } = req.body;
|
||||||
|
|
||||||
if (!accountAddress) {
|
if (!accountAddress) {
|
||||||
return res.status(400).json({ error: 'address is required' });
|
return res.status(400).json({ error: 'address is required' });
|
||||||
@ -55,24 +54,22 @@ async function main (): Promise<void> {
|
|||||||
return res.status(400).json({ error: 'invalid address' });
|
return res.status(400).json({ error: 'invalid address' });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check rate limit
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const today = new Date(now).toISOString().split('T')[0];
|
const today = new Date(now).toISOString().split('T')[0];
|
||||||
const faucetStoreKey = `${accountAddress}:${today}`;
|
const faucetStoreKey = `${accountAddress}:${today}`;
|
||||||
const amountSentToAddress = await faucetDataStore.get(faucetStoreKey) || '0';
|
const amountSentToAddress = await faucetDataStore.get(faucetStoreKey) || '0';
|
||||||
|
|
||||||
if (config.server.enableRateLimit) {
|
if (BigInt(amountSentToAddress) + BigInt(config.server.transferAmount) > BigInt(config.server.periodTransferLimit)) {
|
||||||
// Check rate limit
|
return res.status(429).json({ error: 'Limit exceeded' });
|
||||||
if (BigInt(amountSentToAddress) + BigInt(amount) > BigInt(config.server.periodTransferLimit)) {
|
|
||||||
return res.status(429).json({ error: 'Limit exceeded' });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const txHash = await sendTokens(config, accountAddress, String(amount));
|
const txHash = await sendTokens(config, accountAddress, String(config.server.transferAmount));
|
||||||
console.log(`Sent tokens to address: ${accountAddress}, txHash: ${txHash}`);
|
console.log(`Sent tokens to address: ${accountAddress}, txHash: ${txHash}`);
|
||||||
|
|
||||||
// Update rate limit
|
// Update rate limit
|
||||||
config.server.enableRateLimit && await faucetDataStore.set(faucetStoreKey, (BigInt(amountSentToAddress) + BigInt(amount)).toString(), FAUCET_DATA_TTL);
|
await faucetDataStore.set(faucetStoreKey, (BigInt(amountSentToAddress) + BigInt(config.server.transferAmount)).toString(), FAUCET_DATA_TTL);
|
||||||
|
|
||||||
res.json({ success: true, txHash });
|
res.json({ success: true, txHash });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user