-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Fix #710] Use network_id to identify faucet_url #715
Merged
Merged
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ea6365a
[Fix #710] Use network_id to identify faucet_url
ckeshava 23d3eb3
additional check to prevent wasteful ServerInfo() requests
ckeshava 56430f3
Update xrpl/asyncio/wallet/wallet_generation.py
ckeshava ab918dc
address PR comments
ckeshava afcb4d3
refactor the generate faucet wallet logic
ckeshava 7f93914
remove extra checks on the existence of client.network_id
ckeshava cf3d8e5
Merge branch 'main' into faucet_url_logic
ckeshava f06e5c1
address PR comments
ckeshava bb62029
Update CHANGELOG.md
ckeshava 3881c7b
Update xrpl/asyncio/wallet/wallet_generation.py
ckeshava 0839082
Use a global dict map between network_id and URL
ckeshava fe616ec
address PR comments
ckeshava e346219
Merge branch 'main' into faucet_url_logic
ckeshava c30f772
Merge branch 'main' into faucet_url_logic
ckeshava 2c91e53
Merge branch 'main' into faucet_url_logic
ckeshava File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,20 +1,23 @@ | ||||||
"""Handles wallet generation from a faucet.""" | ||||||
|
||||||
import asyncio | ||||||
from typing import Optional | ||||||
from typing import Dict, Optional | ||||||
from urllib.parse import urlparse, urlunparse | ||||||
|
||||||
import httpx | ||||||
from typing_extensions import Final | ||||||
|
||||||
from xrpl.asyncio.account import get_balance, get_next_valid_seq_number | ||||||
from xrpl.asyncio.clients import Client, XRPLRequestFailureException | ||||||
from xrpl.asyncio.clients.client import get_network_id_and_build_version | ||||||
from xrpl.constants import XRPLException | ||||||
from xrpl.wallet.main import Wallet | ||||||
|
||||||
_TEST_FAUCET_URL: Final[str] = "https://faucet.altnet.rippletest.net/accounts" | ||||||
_DEV_FAUCET_URL: Final[str] = "https://faucet.devnet.rippletest.net/accounts" | ||||||
|
||||||
_TIMEOUT_SECONDS: Final[int] = 40 | ||||||
_MAP_NETWORK_ID_TO_URL: Dict[int, str] = {1: _TEST_FAUCET_URL, 2: _DEV_FAUCET_URL} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
|
||||||
|
||||||
|
||||||
class XRPLFaucetException(XRPLException): | ||||||
|
@@ -57,7 +60,17 @@ async def generate_faucet_wallet( | |||||
|
||||||
.. # noqa: DAR402 exception raised in private method | ||||||
""" | ||||||
faucet_url = get_faucet_url(client.url, faucet_host) | ||||||
await get_network_id_and_build_version(client) | ||||||
|
||||||
if faucet_host is not None: | ||||||
faucet_url = process_faucet_host_url(faucet_host) | ||||||
else: | ||||||
if client.network_id is None: | ||||||
raise XRPLFaucetException( | ||||||
"Cannot create faucet URL without network_id or the faucet_host " | ||||||
+ "information" | ||||||
) | ||||||
faucet_url = get_faucet_url(client.network_id) | ||||||
|
||||||
if wallet is None: | ||||||
wallet = Wallet.create() | ||||||
|
@@ -150,34 +163,40 @@ def process_faucet_host_url(input_url: str) -> str: | |||||
return final_url | ||||||
|
||||||
|
||||||
def get_faucet_url(url: str, faucet_host: Optional[str] = None) -> str: | ||||||
def get_faucet_url(network_id: int) -> str: | ||||||
""" | ||||||
Returns the URL of the faucet that should be used, based on whether the URL is from | ||||||
a testnet or devnet client. | ||||||
Returns the URL of the faucet that should be used, based on network_id | ||||||
|
||||||
Args: | ||||||
url: The URL that the client is using to access the ledger. | ||||||
faucet_host: A custom host to use for funding a wallet. | ||||||
network_id: The network_id corresponding to the XRPL network. This is parsed | ||||||
from a ServerInfo() rippled response | ||||||
|
||||||
Returns: | ||||||
The URL of the matching faucet. | ||||||
|
||||||
Raises: | ||||||
XRPLFaucetException: if the provided URL is not for the testnet or devnet. | ||||||
XRPLFaucetException: if the provided network_id does not correspond to testnet | ||||||
or devnet. | ||||||
""" | ||||||
if faucet_host is not None: | ||||||
return process_faucet_host_url(faucet_host) | ||||||
if "altnet" in url or "testnet" in url: # testnet | ||||||
return _TEST_FAUCET_URL | ||||||
if "sidechain-net2" in url: # sidechain issuing chain devnet | ||||||
if network_id in _MAP_NETWORK_ID_TO_URL: | ||||||
return _MAP_NETWORK_ID_TO_URL[network_id] | ||||||
|
||||||
# corresponds to sidechain-net2 network | ||||||
if network_id == 262: | ||||||
raise XRPLFaucetException( | ||||||
"Cannot fund an account on an issuing chain. Accounts must be created via " | ||||||
"the bridge." | ||||||
) | ||||||
if "devnet" in url: # devnet | ||||||
return _DEV_FAUCET_URL | ||||||
elif network_id == 0: | ||||||
raise XRPLFaucetException("Cannot create faucet with a client on mainnet.") | ||||||
elif network_id < 0: | ||||||
raise XRPLFaucetException("Unable to parse the specified network_id.") | ||||||
|
||||||
# this line is unreachable. Custom devnets must specify a faucet_host input | ||||||
raise XRPLFaucetException( | ||||||
"Cannot fund an account with a client that is not on the testnet or devnet." | ||||||
"The NetworkID of the provided network ( " | ||||||
+ str(network_id) | ||||||
+ ") does not have a known faucet." | ||||||
) | ||||||
|
||||||
|
||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these test cases are already covered in the below class
TestProcessFaucetHostURL