Class: XRPL::Faucet

Inherits:
Object
  • Object
show all
Defined in:
lib/xrpl/faucet.rb

Overview

Funds (and thereby activates) a wallet on an XRP Ledger test network via the public faucet, then waits until the account shows up on the ledger.

This is the Ruby equivalent of the fundWallet() helper used in the other XRPL SDKs. It is intended for Testnet/Devnet only.

Examples:

Fund a fresh wallet on the Testnet

client = XRPL::Client.new(:testnet)
client.connect!
result = XRPL.fund_wallet(client)
result[:wallet].classic_address # => "r..."
result[:balance]                # => 100000000000 (drops)

Constant Summary collapse

TESTNET_FAUCET =
'https://faucet.altnet.rippletest.net/accounts'
DEVNET_FAUCET =
'https://faucet.devnet.rippletest.net/accounts'
DEFAULT_TIMEOUT =
40
DEFAULT_POLL_INTERVAL =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(faucet_url: TESTNET_FAUCET) ⇒ Faucet

Returns a new instance of Faucet.

Parameters:

  • faucet_url (String) (defaults to: TESTNET_FAUCET)

    the faucet endpoint to POST to.



35
36
37
# File 'lib/xrpl/faucet.rb', line 35

def initialize(faucet_url: TESTNET_FAUCET)
  @faucet_url = faucet_url
end

Instance Attribute Details

#faucet_urlObject (readonly)

Returns the value of attribute faucet_url.



32
33
34
# File 'lib/xrpl/faucet.rb', line 32

def faucet_url
  @faucet_url
end

Class Method Details

.fund_wallet(client, wallet = nil, faucet_url: TESTNET_FAUCET, **opts) ⇒ Hash

Convenience wrapper matching the tutorial call style.

Parameters:

  • client (XRPL::Client)

    a connected client used to poll for funding.

  • wallet (Wallet::Wallet, nil) (defaults to: nil)

    wallet to fund; a new one is generated when nil.

Returns:

  • (Hash)

    { wallet:, balance: } (balance in drops).



44
45
46
# File 'lib/xrpl/faucet.rb', line 44

def self.fund_wallet(client, wallet = nil, faucet_url: TESTNET_FAUCET, **opts)
  new(faucet_url: faucet_url).fund_wallet(client, wallet, **opts)
end

Instance Method Details

#fund_wallet(client, wallet = nil, amount: nil, timeout: DEFAULT_TIMEOUT, poll_interval: DEFAULT_POLL_INTERVAL) ⇒ Hash

Funds (and activates) a wallet on a test network.

Parameters:

  • client (XRPL::Client)

    a connected client used to poll for funding.

  • wallet (Wallet::Wallet, nil) (defaults to: nil)

    wallet to fund; a new one is generated when nil.

  • amount (Numeric, String, nil) (defaults to: nil)

    optional XRP amount to request.

  • timeout (Numeric) (defaults to: DEFAULT_TIMEOUT)

    seconds to wait for the account to be funded.

  • poll_interval (Numeric) (defaults to: DEFAULT_POLL_INTERVAL)

    seconds between ledger polls.

Returns:

  • (Hash)

    { wallet:, balance: } (balance in drops).

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/xrpl/faucet.rb', line 57

def fund_wallet(client, wallet = nil, amount: nil, timeout: DEFAULT_TIMEOUT,
                poll_interval: DEFAULT_POLL_INTERVAL)
  wallet ||= Wallet::Wallet.generate

  request_funds(wallet.classic_address, amount)
  balance = await_funding(
    client, wallet.classic_address,
    timeout: timeout, poll_interval: poll_interval
  )

  { wallet: wallet, balance: balance }
end