-
Notifications
You must be signed in to change notification settings - Fork 545
refactor: use NetworkID
in faucet calculations
#2950
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
Changes from 7 commits
2debdf7
38d335e
3f0e6a1
dcf566b
204cb26
fa0d8ee
194ff00
6fc5b04
6c575b5
9b749bd
2fe57e5
134f320
6c4a315
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -16,11 +16,16 @@ export enum FaucetNetwork { | |||||
Devnet = 'faucet.devnet.rippletest.net', | ||||||
} | ||||||
|
||||||
export const FaucetNetworkPaths: Record<string, string> = { | ||||||
export const faucetNetworkPaths: Record<string, string> = { | ||||||
[FaucetNetwork.Testnet]: '/accounts', | ||||||
[FaucetNetwork.Devnet]: '/accounts', | ||||||
} | ||||||
|
||||||
export const faucetNetworkIDs: Map<number, FaucetNetwork> = new Map([ | ||||||
[1, FaucetNetwork.Testnet], | ||||||
[2, FaucetNetwork.Devnet], | ||||||
]) | ||||||
|
||||||
/** | ||||||
* Get the faucet host based on the Client connection. | ||||||
* | ||||||
|
@@ -29,21 +34,19 @@ export const FaucetNetworkPaths: Record<string, string> = { | |||||
* @throws When the client url is not on altnet or devnet. | ||||||
mvadari marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
*/ | ||||||
export function getFaucetHost(client: Client): FaucetNetwork | undefined { | ||||||
const connectionUrl = client.url | ||||||
|
||||||
// 'altnet' for Ripple Testnet server and 'testnet' for XRPL Labs Testnet server | ||||||
if (connectionUrl.includes('altnet') || connectionUrl.includes('testnet')) { | ||||||
return FaucetNetwork.Testnet | ||||||
} | ||||||
|
||||||
if (connectionUrl.includes('sidechain-net2')) { | ||||||
ckeshava marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
if (client.networkID == null) { | ||||||
throw new XRPLFaucetError( | ||||||
'Cannot fund an account on an issuing chain. Accounts must be created via the bridge.', | ||||||
'Cannot create faucet URL without networkID or the faucet_host information', | ||||||
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.
Suggested change
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. Why do you want to remove the extra clause? If someone provides the 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. How will 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. True, but the error is more reasonable from a user perspective, who is never directly calling 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. I don't understand the relevance of They will need to go through XRPL documentation about I don't feel too strongly about it. However, the error message is not intuitive. 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.
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. Sure, but 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. Have you read through all the options on 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.
I had not. |
||||||
) | ||||||
} | ||||||
|
||||||
if (connectionUrl.includes('devnet')) { | ||||||
return FaucetNetwork.Devnet | ||||||
if (faucetNetworkIDs.has(client.networkID)) { | ||||||
return faucetNetworkIDs.get(client.networkID) | ||||||
} | ||||||
|
||||||
if (client.networkID === 0) { | ||||||
// mainnet does not have a faucet | ||||||
throw new XRPLFaucetError('Faucet is not available for mainnet.') | ||||||
} | ||||||
|
||||||
throw new XRPLFaucetError('Faucet URL is not defined or inferrable.') | ||||||
|
@@ -54,11 +57,11 @@ export function getFaucetHost(client: Client): FaucetNetwork | undefined { | |||||
* | ||||||
* @param hostname - hostname. | ||||||
* @returns A String with the correct path for the input hostname. | ||||||
* If hostname undefined or cannot find (key, value) pair in {@link FaucetNetworkPaths}, defaults to '/accounts' | ||||||
* If hostname undefined or cannot find (key, value) pair in {@link faucetNetworkPaths}, defaults to '/accounts' | ||||||
*/ | ||||||
export function getDefaultFaucetPath(hostname: string | undefined): string { | ||||||
export function getFaucetPath(hostname: string | undefined): string { | ||||||
if (hostname === undefined) { | ||||||
return '/accounts' | ||||||
} | ||||||
return FaucetNetworkPaths[hostname] || '/accounts' | ||||||
return faucetNetworkPaths[hostname] || '/accounts' | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,7 @@ import { isValidClassicAddress } from 'ripple-address-codec' | |
import type { Client } from '../client' | ||
import { XRPLFaucetError } from '../errors' | ||
|
||
import { | ||
FaucetWallet, | ||
getFaucetHost, | ||
getDefaultFaucetPath, | ||
} from './defaultFaucets' | ||
import { FaucetWallet, getFaucetHost, getFaucetPath } from './defaultFaucets' | ||
|
||
import { Wallet } from '.' | ||
|
||
|
@@ -148,7 +144,7 @@ export async function requestFunding( | |
if (!hostname) { | ||
throw new XRPLFaucetError('No faucet hostname could be derived') | ||
} | ||
const pathname = options.faucetPath ?? getDefaultFaucetPath(hostname) | ||
const pathname = options.faucetPath ?? getFaucetPath(hostname) | ||
const response = await fetch(`https://${hostname}${pathname}`, { | ||
method: 'POST', | ||
headers: { | ||
|
@@ -190,31 +186,24 @@ async function processSuccessfulResponse( | |
new XRPLFaucetError(`The faucet account is undefined`), | ||
) | ||
} | ||
try { | ||
// Check at regular interval if the address is enabled on the XRPL and funded | ||
const updatedBalance = await getUpdatedBalance( | ||
client, | ||
classicAddress, | ||
startingBalance, | ||
) | ||
// Check at regular interval if the address is enabled on the XRPL and funded | ||
const updatedBalance = await getUpdatedBalance( | ||
client, | ||
classicAddress, | ||
startingBalance, | ||
) | ||
|
||
if (updatedBalance > startingBalance) { | ||
return { | ||
wallet: walletToFund, | ||
balance: updatedBalance, | ||
} | ||
if (updatedBalance > startingBalance) { | ||
return { | ||
wallet: walletToFund, | ||
balance: updatedBalance, | ||
} | ||
throw new XRPLFaucetError( | ||
`Unable to fund address with faucet after waiting ${ | ||
INTERVAL_SECONDS * MAX_ATTEMPTS | ||
} seconds`, | ||
) | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
throw new XRPLFaucetError(err.message) | ||
} | ||
throw err | ||
} | ||
throw new XRPLFaucetError( | ||
`Unable to fund address with faucet after waiting ${ | ||
INTERVAL_SECONDS * MAX_ATTEMPTS | ||
} seconds`, | ||
) | ||
Comment on lines
+203
to
+206
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. This error message is not relevant for the However, it is relevant for 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. No, the errors are correct. The faucet can also be used to add funds to an existing wallet, in which case the error should fall through to here, not throw in 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. Can you point me to the code where we do the waiting (or) retry mechanism? Most of the usages of the faucet have been to generate a new wallet. Even while funding an existing wallet, the operation is tried exactly once. I don't see the retry-operation. 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.
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. Yes, that's what I mentioned in my first comment. The 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.
|
||
} | ||
|
||
async function processError(response: Response, body): Promise<never> { | ||
|
Uh oh!
There was an error while loading. Please reload this page.