Skip to content
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: make docs not output confusing information in xrpl client #2337

Merged
merged 59 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
f1aa271
fix: make docs not output confusing information in xrpl client
justinr1234 Jun 9, 2023
c55c594
additional fixes
justinr1234 Jun 9, 2023
1e67153
merge main
justinr1234 Jun 13, 2023
5bf3bfe
merge main
justinr1234 Jun 14, 2023
4d01a24
fix merge conflict with main
justinr1234 Jun 16, 2023
29a017a
Merge branch 'main' into docs-fixes
justinr1234 Jun 22, 2023
829c8fd
Merge branch 'main' into docs-fixes
justinr1234 Jun 22, 2023
6997e24
request refactor done
justinr1234 Jul 12, 2023
f192334
requestAll refactor
justinr1234 Jul 13, 2023
d07d3be
on refactor
justinr1234 Jul 13, 2023
7e186aa
merge main and fix conflicts
justinr1234 Jul 13, 2023
52f8d3a
fix lock file
justinr1234 Jul 13, 2023
571f752
fix package lock
justinr1234 Jul 13, 2023
231e068
fix typescript
justinr1234 Jul 13, 2023
ad1c307
fixes
justinr1234 Jul 13, 2023
1e57fa2
add various combo of flags for ledger request
justinr1234 Jul 14, 2023
2d24eab
fix export
justinr1234 Jul 14, 2023
a39b496
Merge branch 'main' into docs-fixes
justinr1234 Jul 17, 2023
8cc5b6d
fix submit and submitAndWait
justinr1234 Jul 17, 2023
91cc6b6
add examples and todo comment
justinr1234 Jul 17, 2023
4e50507
Update packages/xrpl/src/sugar/submit.ts
justinr1234 Jul 17, 2023
975e88f
add todo for path_find
justinr1234 Jul 17, 2023
bb5a658
fix docs for getSignedTx
justinr1234 Jul 17, 2023
0873e25
undo commented out error handling
justinr1234 Jul 17, 2023
a33a9d0
Update packages/xrpl/src/client/index.ts
justinr1234 Jul 17, 2023
82c4f54
Update packages/xrpl/src/sugar/autofill.ts
justinr1234 Jul 17, 2023
d9f846f
Update packages/xrpl/src/sugar/autofill.ts
justinr1234 Jul 17, 2023
e1f414a
Update packages/xrpl/src/sugar/autofill.ts
justinr1234 Jul 17, 2023
47e573e
remove dev comments
justinr1234 Jul 17, 2023
da9f234
Update getOrderbookOptions docs
justinr1234 Jul 17, 2023
5f74e47
Expain typescript type matching
justinr1234 Jul 17, 2023
a809f0f
Make run-s dev dependency
justinr1234 Jul 17, 2023
612257c
remove docs
justinr1234 Jul 17, 2023
64eaff5
remove more docs
justinr1234 Jul 17, 2023
68195c3
Merge branch 'main' into docs-fixes
justinr1234 Jul 17, 2023
6191952
use Balance interface in balances sugar
justinr1234 Jul 17, 2023
7440aad
refactor doFundWalletRequest
justinr1234 Jul 18, 2023
f071615
fix type error
justinr1234 Jul 18, 2023
cf8b48b
feat: bump typescript to 5.x (#2387)
ckniffen Jul 18, 2023
ba9d0a6
feat: remove node 14 support (#2386)
ckniffen Jul 20, 2023
ca14f68
merge 3.0 upstream
justinr1234 Jul 25, 2023
452485b
fix typescript
justinr1234 Jul 25, 2023
151b66f
feat: bump typescript to 5.x (#2387)
ckniffen Jul 18, 2023
5b336d8
feat: remove node 14 support (#2386)
ckniffen Jul 20, 2023
ab3adbe
feat: remove `lodash` as a dependency (#2378)
ckniffen Jul 25, 2023
d0c8d09
feat: remove `decimal.js` and `big-integer` (#2379)
ckniffen Jul 26, 2023
3df5004
feat(deps): make `https-proxy-agent` optional (#2388)
ckniffen Jul 26, 2023
e41967f
feat: remove 3 http related polyfills (#2375)
ckniffen Jul 26, 2023
12512d8
feat(deps): remove `assert-browserify` (#2389)
ckniffen Jul 26, 2023
6bdcf62
feat: remove `BroadcastClient` (#2408)
ckniffen Jul 27, 2023
958c97b
BREAKING CHANGE(fix): `deriveKeypair` ignoring a manual `algorithm` b…
JST5000 Aug 8, 2023
40b276a
Merge branch '3.0' into docs-fixes
JST5000 Aug 9, 2023
c9e21d7
Lint fundWallet
JST5000 Aug 9, 2023
ba8bb52
Merge branch '3.0' into docs-fixes
JST5000 Aug 9, 2023
bcc9057
fix: Fix invariant assertion in binary-codec (#2429)
JST5000 Aug 9, 2023
bb5b848
Update package
JST5000 Aug 9, 2023
fba9e96
Re-add run-s
JST5000 Aug 11, 2023
33a967c
Merge branch '3.0' into docs-fixes
JST5000 Aug 11, 2023
66b1f69
Merge branch '3.0' into docs-fixes
JST5000 Aug 15, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21,626 changes: 4,168 additions & 17,458 deletions package-lock.json
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What caused this to remove many entries? The only dependency change was "run-s": "^0.0.0", which I would think might add entries and not remove.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I may have chosen the wrong version to merge then - there was a package-lock conflict and I assumed the 3.0 version would be shorter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why it still changed - but I used the 3.0 version and ran npm i - does that new result look sensible, or is there something I accidentally changed?

Copy link
Collaborator

@ckniffen ckniffen Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try copying the 3.0 version of the file and run npm i again and see what happens

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ckniffen Just did that again, and this was still the result. I think it's the correct updates now as it's mostly updating things to 'dev' dependencies or updating the 'integrity' line, which seems minor. (Along with adding run-s)

Large diffs are not rendered by default.

2,735 changes: 0 additions & 2,735 deletions packages/xrpl/package-lock.json

This file was deleted.

1 change: 1 addition & 0 deletions packages/xrpl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"ripple-address-codec": "^4.3.0",
"ripple-binary-codec": "^1.7.0",
"ripple-keypairs": "^1.3.0",
"run-s": "0.0.0",
justinr1234 marked this conversation as resolved.
Show resolved Hide resolved
"ws": "^8.2.2"
},
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions packages/xrpl/snippets/src/getTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Client, LedgerResponse, TxResponse } from '../../src'
import { Client } from '../../src'

const client = new Client('wss://s2.ripple.com:51233')

async function getTransaction(): Promise<void> {
await client.connect()
const ledger: LedgerResponse = await client.request({
const ledger = await client.request({
command: 'ledger',
transactions: true,
ledger_index: 'validated',
})
console.log(ledger)

const transactions = ledger.result.ledger.transactions
if (transactions) {
const tx: TxResponse = await client.request({
if (transactions && transactions.length > 0) {
const tx = await client.request({
command: 'tx',
transaction: transactions[0],
})
Expand Down
8 changes: 3 additions & 5 deletions packages/xrpl/snippets/src/paths.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client, Payment, RipplePathFindResponse } from '../../src'
import { Client, Payment } from '../../src'

const client = new Client('wss://s.altnet.rippletest.net:51233')

Expand All @@ -15,7 +15,7 @@ async function createTxWithPaths(): Promise<void> {
issuer: 'rVnYNK9yuxBz4uP8zC8LEFokM2nqH3poc',
}

const request = {
const resp = await client.request({
command: 'ripple_path_find',
justinr1234 marked this conversation as resolved.
Show resolved Hide resolved
source_account: wallet.classicAddress,
source_currencies: [
Expand All @@ -25,9 +25,7 @@ async function createTxWithPaths(): Promise<void> {
],
destination_account,
destination_amount,
}

const resp: RipplePathFindResponse = await client.request(request)
})
console.log(resp)

const paths = resp.result.alternatives[0].paths_computed
Expand Down
198 changes: 79 additions & 119 deletions packages/xrpl/src/Wallet/fundWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { request as httpsRequest, RequestOptions } from 'https'
import { isValidClassicAddress } from 'ripple-address-codec'

import type { Client } from '../client'
import { RippledError, XRPLFaucetError } from '../errors'
import { XRPLFaucetError } from '../errors'

import {
FaucetWallet,
Expand All @@ -20,133 +20,58 @@ const INTERVAL_SECONDS = 1
const MAX_ATTEMPTS = 20

/**
* The fundWallet() method is used to send an amount of XRP (usually 1000) to a new (randomly generated)
* or existing XRP Ledger wallet.
* Generate a new wallet to fund if no existing wallet is provided or its address is invalid.
*
* @example
*
* Example 1: Fund a randomly generated wallet
* const { Client, Wallet } = require('xrpl')
*
* const client = new Client('wss://s.altnet.rippletest.net:51233')
justinr1234 marked this conversation as resolved.
Show resolved Hide resolved
* await client.connect()
* const { balance, wallet } = await client.fundWallet()
*
* Under the hood, this will use `Wallet.generate()` to create a new random wallet, then ask a testnet faucet
* To send it XRP on ledger to make it a real account. If successful, this will return the new account balance in XRP
* Along with the Wallet object to track the keys for that account. If you'd like, you can also re-fill an existing
* Account by passing in a Wallet you already have.
* ```ts
* const api = new xrpl.Client("wss://s.altnet.rippletest.net:51233")
* await api.connect()
* const { wallet, balance } = await api.fundWallet()
* ```
*
* Example 2: Fund wallet using a custom faucet host and known wallet address
*
* `fundWallet` will try to infer the url of a faucet API from the network your client is connected to.
* There are hardcoded default faucets for popular test networks like testnet and devnet.
* However, if you're working with a newer or more obscure network, you may have to specify the faucetHost
* And faucetPath so `fundWallet` can ask that faucet to fund your wallet.
*
* ```ts
* const newWallet = Wallet.generate()
* const { balance, wallet } = await client.fundWallet(newWallet, {
* amount: '10',
* faucetHost: 'https://custom-faucet.example.com',
* faucetPath: '/accounts'
* })
* console.log(`Sent 10 XRP to wallet: ${address} from the given faucet. Resulting balance: ${balance} XRP`)
* } catch (error) {
* console.error(`Failed to fund wallet: ${error}`)
* }
* }
* ```
*
* @param this - Client.
* @param wallet - An existing XRPL Wallet to fund. If undefined or null, a new Wallet will be created.
* @param options - See below.
* @param options.faucetHost - A custom host for a faucet server. On devnet,
* testnet, AMM devnet, and HooksV3 testnet, `fundWallet` will
* attempt to determine the correct server automatically. In other environments,
* or if you would like to customize the faucet host in devnet or testnet,
* you should provide the host using this option.
* @param options.faucetPath - A custom path for a faucet server. On devnet,
* testnet, AMM devnet, and HooksV3 testnet, `fundWallet` will
* attempt to determine the correct path automatically. In other environments,
* or if you would like to customize the faucet path in devnet or testnet,
* you should provide the path using this option.
* Ex: client.fundWallet(null,{'faucet.altnet.rippletest.net', '/accounts'})
* specifies a request to 'faucet.altnet.rippletest.net/accounts' to fund a new wallet.
* @param options.amount - A custom amount to fund, if undefined or null, the default amount will be 1000.
* @param options.usageContext - An optional field to indicate the use case context of the faucet transaction
* Ex: integration test, code snippets.
* @returns A Wallet on the Testnet or Devnet that contains some amount of XRP,
* and that wallet's balance in XRP.
* @throws When either Client isn't connected or unable to fund wallet address.
* @param wallet - Optional existing wallet.
* @returns The wallet to fund.
*/
// eslint-disable-next-line max-lines-per-function -- All lines necessary
async function fundWallet(
JST5000 marked this conversation as resolved.
Show resolved Hide resolved
this: Client,
wallet?: Wallet | null,
options?: {
faucetHost?: string
faucetPath?: string
amount?: string
usageContext?: string
},
): Promise<{
wallet: Wallet
balance: number
}> {
if (!this.isConnected()) {
throw new RippledError('Client not connected, cannot call faucet')
export function generateWalletToFund(wallet?: Wallet | null): Wallet {
if (wallet && isValidClassicAddress(wallet.classicAddress)) {
return wallet
}
return Wallet.generate()
}

// Generate a new Wallet if no existing Wallet is provided or its address is invalid to fund
const walletToFund =
wallet && isValidClassicAddress(wallet.classicAddress)
? wallet
: Wallet.generate()

// Create the POST request body
const postBody = Buffer.from(
new TextEncoder().encode(
JSON.stringify({
destination: walletToFund.classicAddress,
xrpAmount: options?.amount,
userAgent: 'xrpl.js',
usageContext: options?.usageContext,
}),
),
)

/**
* Get the starting balance of the wallet.
*
* @param client - The client object.
* @param classicAddress - The classic address of the wallet.
* @returns The starting balance.
*/
export async function getStartingBalance(
client: Client,
classicAddress: string,
): Promise<number> {
let startingBalance = 0
try {
startingBalance = Number(
await this.getXrpBalance(walletToFund.classicAddress),
)
startingBalance = Number(await client.getXrpBalance(classicAddress))
} catch {
/* startingBalance remains '0' */
// startingBalance remains '0'
}
// Options to pass to https.request
const httpOptions = getHTTPOptions(this, postBody, {
hostname: options?.faucetHost,
pathname: options?.faucetPath,
})
return startingBalance
}

return returnPromise(
httpOptions,
this,
startingBalance,
walletToFund,
postBody,
)
export interface FundWalletOptions {
faucetHost?: string
faucetPath?: string
amount?: string
usageContext?: string
}

/**
* Perform the fund wallet request.
*
* @param options - Optional additional options.
* @param client - The client object.
* @param startingBalance - The starting balance of the wallet.
* @param walletToFund - The wallet to fund.
* @param postBody - The body of the POST request.
* @returns A promise that resolves to the funded wallet and balance.
*/
// eslint-disable-next-line max-params -- Helper function created for organizational purposes
async function returnPromise(
options: RequestOptions,
export async function doFundWalletRequest(
options: FundWalletOptions | undefined,
JST5000 marked this conversation as resolved.
Show resolved Hide resolved
client: Client,
startingBalance: number,
walletToFund: Wallet,
Expand All @@ -155,8 +80,12 @@ async function returnPromise(
wallet: Wallet
balance: number
}> {
const httpOptions = getHTTPOptions(client, postBody, {
hostname: options?.faucetHost,
pathname: options?.faucetPath,
})
return new Promise((resolve, reject) => {
const request = httpsRequest(options, (response) => {
const request = httpsRequest(httpOptions, (response) => {
const chunks: Uint8Array[] = []
response.on('data', (data) => chunks.push(data))
// eslint-disable-next-line @typescript-eslint/no-misused-promises -- not actually misused, different resolve/reject
Expand All @@ -183,6 +112,16 @@ async function returnPromise(
})
}

/**
* Get the HTTP options for the request.
*
* @param client - The client object.
* @param postBody - The body of the request.
* @param options - Optional additional options.
* @param options.hostname - Optional hostname of the faucet server.
* @param options.pathname - Optional path of the faucet server. Such as /accounts
* @returns The HTTP options for the request.
*/
function getHTTPOptions(
client: Client,
postBody: Uint8Array,
Expand All @@ -205,6 +144,18 @@ function getHTTPOptions(
}
}

/**
* Handle the 'end' event of the response.
*
* @param response - The incoming message response.
* @param chunks - The array of data chunks received in the response.
* @param client - The client object.
* @param startingBalance - The starting balance of the wallet.
* @param walletToFund - The wallet to fund.
* @param resolve - The function to resolve the promise.
* @param reject - The function to reject the promise.
* @returns A promise that resolves when the processing is complete.
*/
// eslint-disable-next-line max-params -- Helper function created for organizational purposes
async function onEnd(
response: IncomingMessage,
Expand Down Expand Up @@ -243,6 +194,17 @@ async function onEnd(
}
}

/**
* Process a successful response from the faucet.
*
* @param client - The client object.
* @param classicAddress - The classic address of the faucet account.
* @param walletToFund - The wallet to fund.
* @param startingBalance - The starting balance of the wallet.
* @param resolve - The function to resolve the promise.
* @param reject - The function to reject the promise.
* @returns A promise that resolves when the processing is complete.
*/
// eslint-disable-next-line max-params, max-lines-per-function -- Only used as a helper function, lines inc due to added balance.
async function processSuccessfulResponse(
client: Client,
Expand Down Expand Up @@ -340,5 +302,3 @@ async function getUpdatedBalance(
}, INTERVAL_SECONDS * 1000)
})
}

export default fundWallet
Loading