mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-20 19:55:54 +00:00
migrate from js and py
This commit is contained in:
49
content/_code-samples/paths/js/paths.ts
Normal file
49
content/_code-samples/paths/js/paths.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { Client, Payment, RipplePathFindResponse } from 'xrpl'
|
||||
|
||||
const client = new Client('wss://s.altnet.rippletest.net:51233')
|
||||
|
||||
async function createTxWithPaths(): Promise<void> {
|
||||
await client.connect()
|
||||
|
||||
const { wallet } = await client.fundWallet()
|
||||
const destination_account = 'rKT4JX4cCof6LcDYRz8o3rGRu7qxzZ2Zwj'
|
||||
const destination_amount = {
|
||||
value: '0.001',
|
||||
currency: 'USD',
|
||||
issuer: 'rVnYNK9yuxBz4uP8zC8LEFokM2nqH3poc',
|
||||
}
|
||||
|
||||
const request = {
|
||||
command: 'ripple_path_find',
|
||||
source_account: wallet.classicAddress,
|
||||
source_currencies: [
|
||||
{
|
||||
currency: 'XRP',
|
||||
},
|
||||
],
|
||||
destination_account,
|
||||
destination_amount,
|
||||
}
|
||||
|
||||
const resp: RipplePathFindResponse = await client.request(request)
|
||||
console.log(resp)
|
||||
|
||||
const paths = resp.result.alternatives[0].paths_computed
|
||||
console.log(paths)
|
||||
|
||||
const tx: Payment = {
|
||||
TransactionType: 'Payment',
|
||||
Account: wallet.classicAddress,
|
||||
Amount: destination_amount,
|
||||
Destination: destination_account,
|
||||
Paths: paths,
|
||||
}
|
||||
|
||||
await client.autofill(tx)
|
||||
const signed = wallet.sign(tx)
|
||||
console.log('signed:', signed)
|
||||
|
||||
await client.disconnect()
|
||||
}
|
||||
|
||||
void createTxWithPaths()
|
||||
50
content/_code-samples/paths/py/paths.py
Normal file
50
content/_code-samples/paths/py/paths.py
Normal file
@@ -0,0 +1,50 @@
|
||||
"""Example of how to find the best path to trade with"""
|
||||
from xrpl.clients import JsonRpcClient
|
||||
from xrpl.models.amounts import IssuedCurrencyAmount
|
||||
from xrpl.models.currencies.xrp import XRP
|
||||
from xrpl.models.requests import RipplePathFind
|
||||
from xrpl.models.transactions import Payment
|
||||
from xrpl.transaction import autofill_and_sign
|
||||
from xrpl.wallet import generate_faucet_wallet
|
||||
|
||||
# References
|
||||
# - https://xrpl.org/paths.html#paths
|
||||
# - https://xrpl.org/ripple_path_find.html#ripple_path_find
|
||||
|
||||
# Create a client to connect to the test network
|
||||
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
|
||||
|
||||
# Creating wallet to send money from
|
||||
wallet = generate_faucet_wallet(client, debug=True)
|
||||
|
||||
# Create account and amount variables for later transaction
|
||||
destination_account = "rKT4JX4cCof6LcDYRz8o3rGRu7qxzZ2Zwj"
|
||||
destination_amount = IssuedCurrencyAmount(
|
||||
value="0.001",
|
||||
currency="USD",
|
||||
issuer="rVnYNK9yuxBz4uP8zC8LEFokM2nqH3poc",
|
||||
)
|
||||
|
||||
# Create a RipplePathFind request and have the client call it
|
||||
path_request = RipplePathFind(
|
||||
source_account=wallet.classic_address,
|
||||
source_currencies=[XRP()],
|
||||
destination_account=destination_account,
|
||||
destination_amount=destination_amount,
|
||||
)
|
||||
path_response = client.request(path_request)
|
||||
print(path_response)
|
||||
|
||||
# Extract out paths from the RipplePathFind response
|
||||
paths = path_response.result["alternatives"][0]["paths_computed"]
|
||||
print(paths)
|
||||
|
||||
# # Create a Payment to send money from wallet to destination_account using path
|
||||
payment_tx = Payment(
|
||||
account=wallet.classic_address,
|
||||
amount=destination_amount,
|
||||
destination=destination_account,
|
||||
paths=paths,
|
||||
)
|
||||
|
||||
print("signed: ", autofill_and_sign(payment_tx, wallet, client))
|
||||
Reference in New Issue
Block a user