From 1a02b40c8f504f1da9f7b27a1a12583bd613b4b7 Mon Sep 17 00:00:00 2001 From: Marcel Hernandez Date: Fri, 14 Jun 2024 18:23:52 +0200 Subject: [PATCH] new feature: custom fee rate --- .env.dist | 1 + README.md | 1 + src/Bitcoin/RPCClient.php | 8 +++++--- src/DI/Faucet.php | 1 + src/DI/Settings.php | 2 ++ tests/Integration/RPCClientTest.php | 2 +- 6 files changed, 11 insertions(+), 4 deletions(-) diff --git a/.env.dist b/.env.dist index 1228e4f..58f73f5 100644 --- a/.env.dist +++ b/.env.dist @@ -19,6 +19,7 @@ FAUCET_USE_BATCHING=0 # Optional value FAUCET_BITCOIN_RPC_COOKIE= FAUCET_BITCOIN_RPC_WALLET= +FAUCET_FEE_RATE=0 FAUCET_MEMPOOL_URL= FAUCET_USER_SESSION_MAX_BTC=20.0 FAUCET_GLOBAL_SESSION_MAX_BTC=150.0 diff --git a/README.md b/README.md index 315f53c..16b2971 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ The following environment variables can be used to configure your faucet instanc | `FAUCET_BITCOIN_RPC_USER` | RPC User. Prefer cookie auth | `user` | No | | `FAUCET_BITCOIN_RPC_PASS` | RPC Pass. Prefer cookie auth | `pass` | No | | `FAUCET_BITCOIN_RPC_WALLET` | Wallet name. Required when the node has more than one wallet | | No | +| `FAUCET_FEE_RATE` | Fee rate to use in the transactions. Default is 1 s/vB. | `1` | No | | `FAUCET_NAME` | Text displayed on the faucet | `Your Signet Faucet` | Yes | | `FAUCET_MIN_ONE_TIME_BTC` | Minimum payout users can claim | `0.001` | Yes | | `FAUCET_MAX_ONE_TIME_BTC` | Maximum payout users can claim | `5.0` | Yes | diff --git a/src/Bitcoin/RPCClient.php b/src/Bitcoin/RPCClient.php index fa34200..ef92f5c 100644 --- a/src/Bitcoin/RPCClient.php +++ b/src/Bitcoin/RPCClient.php @@ -16,8 +16,9 @@ private string $endpoint; private string $authString; + private float $feeRate; - public function __construct(string $endpoint, string $user, string $password, ?string $walletName) + public function __construct(string $endpoint, string $user, string $password, float $feeRate, ?string $walletName) { if (null !== $walletName) { $endpoint .= '/wallet/'.$walletName; @@ -25,6 +26,7 @@ public function __construct(string $endpoint, string $user, string $password, ?s $this->endpoint = $endpoint; $this->authString = base64_encode("$user:$password"); + $this->feeRate = $feeRate; } public function createWallet(string $name): void @@ -70,7 +72,7 @@ public function send(string $address, float $amount): string public function batchSend(array $payments): string { - return $this->doRequest('send', ['outputs' => $payments, 'fee_rate' => 0])->result->txid; + return $this->doRequest('send', ['outputs' => $payments, 'fee_rate' => $this->feeRate])->result->txid; } private function doRequest(string $method, array $params): \stdClass @@ -82,7 +84,7 @@ private function doRequest(string $method, array $params): \stdClass "Authorization: Basic {$this->authString}", 'Content-Type: application/json', ], - 'content' => json_encode(['jsonrpc' => '1.0', 'id' => 'faucet', 'method' => $method, 'params' => $params]), + 'content' => json_encode(['jsonrpc' => '1.0', 'id' => 'bbo-faucet', 'method' => $method, 'params' => $params]), 'ignore_errors' => true, ], ]); diff --git a/src/DI/Faucet.php b/src/DI/Faucet.php index 61bd2eb..dd7bfda 100644 --- a/src/DI/Faucet.php +++ b/src/DI/Faucet.php @@ -108,6 +108,7 @@ public function provide(Container $c): void $settings->bitcoinRpcEndpoint, $settings->bitcoinRpcUser, $settings->bitcoinRpcPass, + $settings->feeRate, $settings->bitcoinRpcWallet ); }); diff --git a/src/DI/Settings.php b/src/DI/Settings.php index 1c36e9c..74d6913 100644 --- a/src/DI/Settings.php +++ b/src/DI/Settings.php @@ -16,6 +16,7 @@ public string $bitcoinRpcUser; public string $bitcoinRpcPass; public ?string $bitcoinRpcWallet; + public float $feeRate; public string $faucetName; public ?string $mempoolUrl; @@ -57,6 +58,7 @@ public function __construct(array $values) $this->bitcoinRpcPass = $values['FAUCET_BITCOIN_RPC_PASS']; } + $this->feeRate = (float) $values['FAUCET_FEE_RATE'] ?: 1.0; $this->faucetName = $values['FAUCET_NAME']; $this->mempoolUrl = $values['FAUCET_MEMPOOL_URL'] ?: null; $this->minOneTimeBtc = (float) $values['FAUCET_MIN_ONE_TIME_BTC']; diff --git a/tests/Integration/RPCClientTest.php b/tests/Integration/RPCClientTest.php index 2f04508..80682be 100644 --- a/tests/Integration/RPCClientTest.php +++ b/tests/Integration/RPCClientTest.php @@ -13,7 +13,7 @@ final class RPCClientTest extends TestCase protected function setUp(): void { - $this->sut = new RPCClient($_ENV['RPC_URL'], $_ENV['RPC_USER'], $_ENV['RPC_PASS'], null); + $this->sut = new RPCClient($_ENV['RPC_URL'], $_ENV['RPC_USER'], $_ENV['RPC_PASS'], 0, null); } public function testValidateAddress(): void