Skip to content

Commit

Permalink
new feature: custom fee rate
Browse files Browse the repository at this point in the history
  • Loading branch information
1ma committed Jun 14, 2024
1 parent f36f776 commit 1a02b40
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
8 changes: 5 additions & 3 deletions src/Bitcoin/RPCClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@

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;
}

$this->endpoint = $endpoint;
$this->authString = base64_encode("$user:$password");
$this->feeRate = $feeRate;
}

public function createWallet(string $name): void
Expand Down Expand Up @@ -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
Expand All @@ -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,
],
]);
Expand Down
1 change: 1 addition & 0 deletions src/DI/Faucet.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public function provide(Container $c): void
$settings->bitcoinRpcEndpoint,
$settings->bitcoinRpcUser,
$settings->bitcoinRpcPass,
$settings->feeRate,
$settings->bitcoinRpcWallet
);
});
Expand Down
2 changes: 2 additions & 0 deletions src/DI/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
public string $bitcoinRpcUser;
public string $bitcoinRpcPass;
public ?string $bitcoinRpcWallet;
public float $feeRate;

public string $faucetName;
public ?string $mempoolUrl;
Expand Down Expand Up @@ -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'];
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/RPCClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1a02b40

Please sign in to comment.