Skip to content

Commit

Permalink
feat: adjust rBTC default amount, add xlarge url param, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlieC3 committed Dec 17, 2024
1 parent ced8242 commit b30f16a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/api/routes/faucets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,14 @@ export const FaucetRoutes: FastifyPluginAsync<
large: Type.Optional(
Type.Boolean({
description:
'Request a larger amount of regtest BTC than the default',
'Request a large amount of regtest BTC than the default',
default: false,
})
),
xlarge: Type.Optional(
Type.Boolean({
description:
'Request an extra large amount of regtest BTC than the default',
default: false,
})
),
Expand Down Expand Up @@ -132,7 +139,21 @@ export const FaucetRoutes: FastifyPluginAsync<
async (req, reply) => {
await btcFaucetRequestQueue.add(async () => {
const address = req.query.address || req.body?.address;
const btcAmount = req.query.large ? 0.5 : 0.01;
let btcAmount = 0.0001;

if (req.query.large && req.query.xlarge) {
return await reply.status(400).send({
error: 'cannot simultaneously request a large and xlarge amount',
success: false,
});
}

if (req.query.large) {
btcAmount = 0.01;
} else if (req.query.xlarge) {
btcAmount = 0.5;
}

if (!address) {
return await reply.status(400).send({
error: 'address required',
Expand Down
34 changes: 34 additions & 0 deletions tests/btc-faucet/faucet-btc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,40 @@ describe('btc faucet', () => {
`/extended/v1/faucets/btc/${addr}`
);
expect(balanceResponse.status).toBe(200);
expect(JSON.parse(balanceResponse.text)).toEqual({ balance: 0.0001 });
});

test('faucet http balance endpoint large', async () => {
const addr = getKeyAddress(ECPair.makeRandom({ network: regtest }));
const response = await supertest(apiServer.server).post(
`/extended/v1/faucets/btc?address=${addr}&large=true`
);
expect(response.status).toBe(200);
await getRpcClient().generatetoaddress({
address: getKeyAddress(ECPair.makeRandom({ network: regtest })),
nblocks: 1,
});
const balanceResponse = await supertest(apiServer.server).get(
`/extended/v1/faucets/btc/${addr}`
);
expect(balanceResponse.status).toBe(200);
expect(JSON.parse(balanceResponse.text)).toEqual({ balance: 0.01 });
});

test('faucet http balance endpoint xlarge', async () => {
const addr = getKeyAddress(ECPair.makeRandom({ network: regtest }));
const response = await supertest(apiServer.server).post(
`/extended/v1/faucets/btc?address=${addr}&xlarge=true`
);
expect(response.status).toBe(200);
await getRpcClient().generatetoaddress({
address: getKeyAddress(ECPair.makeRandom({ network: regtest })),
nblocks: 1,
});
const balanceResponse = await supertest(apiServer.server).get(
`/extended/v1/faucets/btc/${addr}`
);
expect(balanceResponse.status).toBe(200);
expect(JSON.parse(balanceResponse.text)).toEqual({ balance: 0.5 });
});

Expand Down

0 comments on commit b30f16a

Please sign in to comment.