Skip to content

Commit

Permalink
Merge branch 'develop' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlieC3 authored Dec 17, 2024
2 parents 64ad5bc + 1632698 commit 5e450da
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 36 deletions.
48 changes: 22 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
"dotenv-flow": "3.2.0",
"duckdb": "0.9.2",
"ecpair": "2.1.0",
"elliptic": "6.5.7",
"elliptic": "6.6.0",
"escape-goat": "3.0.0",
"evt": "1.10.1",
"express": "4.21.2",
Expand Down
45 changes: 36 additions & 9 deletions src/api/routes/faucets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,39 @@ export const FaucetRoutes: FastifyPluginAsync<
preHandler: missingBtcConfigMiddleware,
schema: {
operationId: 'run_faucet_btc',
summary: 'Add testnet BTC tokens to address',
description: `Add 1 BTC token to the specified testnet BTC address.
summary: 'Add regtest BTC tokens to address',
description: `Add 0.01 BTC token to the specified regtest BTC address.
The endpoint returns the transaction ID, which you can use to view the transaction in a testnet Bitcoin block
The endpoint returns the transaction ID, which you can use to view the transaction in a regtest Bitcoin block
explorer. The tokens are delivered once the transaction has been included in a block.
**Note:** This is a testnet only endpoint. This endpoint will not work on the mainnet.`,
**Note:** This is a Bitcoin regtest-only endpoint. This endpoint will not work on the Bitcoin mainnet.`,
tags: ['Faucets'],
querystring: Type.Object({
address: Type.Optional(
Type.String({
description: 'A valid testnet BTC address',
description: 'A valid regtest BTC address',
examples: ['2N4M94S1ZPt8HfxydXzL2P7qyzgVq7MHWts'],
})
),
large: Type.Optional(
Type.Boolean({
description: '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,
})
),
}),
body: OptionalNullable(
Type.Object({
address: Type.Optional(
Type.String({
description: 'A valid testnet BTC address',
description: 'A valid regtest BTC address',
examples: ['2N4M94S1ZPt8HfxydXzL2P7qyzgVq7MHWts'],
})
),
Expand All @@ -112,7 +124,7 @@ export const FaucetRoutes: FastifyPluginAsync<
{
title: 'RunFaucetResponse',
description:
'POST request that initiates a transfer of tokens to a specified testnet address',
'POST request that initiates a transfer of tokens to a specified Bitcoin regtest address',
}
),
'4xx': Type.Object({
Expand All @@ -125,6 +137,21 @@ export const FaucetRoutes: FastifyPluginAsync<
async (req, reply) => {
await btcFaucetRequestQueue.add(async () => {
const address = req.query.address || req.body?.address;
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 Expand Up @@ -156,7 +183,7 @@ export const FaucetRoutes: FastifyPluginAsync<
});
}

const tx = await makeBtcFaucetPayment(btc.networks.regtest, address, 0.5);
const tx = await makeBtcFaucetPayment(btc.networks.regtest, address, btcAmount);
await fastify.writeDb?.insertFaucetRequest({
ip: `${ip}`,
address: address,
Expand All @@ -183,7 +210,7 @@ export const FaucetRoutes: FastifyPluginAsync<
tags: ['Faucets'],
params: Type.Object({
address: Type.String({
description: 'A valid testnet BTC address',
description: 'A valid regtest BTC address',
examples: ['2N4M94S1ZPt8HfxydXzL2P7qyzgVq7MHWts'],
}),
}),
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 5e450da

Please sign in to comment.