mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-21 12:15:50 +00:00
revise sample app and tutorial
This commit is contained in:
@@ -1,25 +1,12 @@
|
|||||||
import xrpl
|
import xrpl
|
||||||
|
|
||||||
# Core modules to
|
|
||||||
# To generate keypairs and encode addresses
|
|
||||||
from xrpl.core import keypairs
|
from xrpl.core import keypairs
|
||||||
from xrpl.core.addresscodec.main import classic_address_to_xaddress
|
from xrpl.core.addresscodec.main import classic_address_to_xaddress
|
||||||
|
|
||||||
# Network client
|
|
||||||
from xrpl.clients.json_rpc_client import JsonRpcClient
|
from xrpl.clients.json_rpc_client import JsonRpcClient
|
||||||
|
|
||||||
# To create test wallets
|
|
||||||
from xrpl.wallet import generate_faucet_wallet
|
from xrpl.wallet import generate_faucet_wallet
|
||||||
|
|
||||||
# Request models
|
|
||||||
from xrpl.models.requests.account_info import AccountInfo
|
from xrpl.models.requests.account_info import AccountInfo
|
||||||
|
|
||||||
# Wallet models
|
|
||||||
from xrpl.wallet import Wallet
|
from xrpl.wallet import Wallet
|
||||||
|
from xrpl.models.transactions.transaction import Transaction, TransactionType, Memo
|
||||||
# Transaction models
|
from xrpl.models.transactions import AccountSet, Payment
|
||||||
from xrpl.models.transactions.transaction import Transaction, TransactionType, AccountSet, Payment
|
|
||||||
from xrpl.models.transactions import AccountSet
|
|
||||||
from xrpl.transaction import (
|
from xrpl.transaction import (
|
||||||
LastLedgerSequenceExpiredException,
|
LastLedgerSequenceExpiredException,
|
||||||
)
|
)
|
||||||
@@ -30,14 +17,7 @@ from xrpl.transaction.main import (
|
|||||||
transaction_json_to_binary_codec_form,
|
transaction_json_to_binary_codec_form,
|
||||||
)
|
)
|
||||||
from xrpl.transaction.reliable_submission import send_reliable_submission
|
from xrpl.transaction.reliable_submission import send_reliable_submission
|
||||||
|
|
||||||
# Account models
|
|
||||||
from xrpl.account import get_next_valid_seq_number
|
from xrpl.account import get_next_valid_seq_number
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#JSON handling
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
# Define the network client
|
# Define the network client
|
||||||
@@ -63,30 +43,37 @@ TESTNET_DESTINATION_ACCOUNT = TESTNET_DESTINATION_WALLET.classic_address
|
|||||||
|
|
||||||
# Define variables to use for transactions
|
# Define variables to use for transactions
|
||||||
_ACCOUNT = "r9LqNeG6qHxjeUocjvVki2XR35weJ9mZgQ"
|
_ACCOUNT = "r9LqNeG6qHxjeUocjvVki2XR35weJ9mZgQ"
|
||||||
|
_AMOUNT = "20"
|
||||||
_FEE = "10"
|
_FEE = "10"
|
||||||
|
_MEMO = "I sent this with xrpl-py!"
|
||||||
_SEQUENCE = 19048
|
_SEQUENCE = 19048
|
||||||
SET_FLAG = 8
|
SET_FLAG = 8
|
||||||
MEMO = "I built this with xrpl-py!"
|
|
||||||
|
|
||||||
# Secrets to use for signing transactions
|
# Secrets to use for signing transactions
|
||||||
_SECRET = "randomsecretkey"
|
_SECRET = ""
|
||||||
_SEED = "randomsecretseedforakey"
|
_SEED = ""
|
||||||
_SEED_HEX = "EACEB081770D8ADE216C85445DD6FB002C6B5A2930F2DECE006DA18150CB18F6"
|
_SEED_HEX = "EACEB081770D8ADE216C85445DD6FB002C6B5A2930F2DECE006DA18150CB18F6"
|
||||||
_PASSPHRASE = "mytopsecretpassphrasethatwillneverbehacked"
|
_PASSPHRASE = "mytopsecretpassphrasethatwillneverbehacked"
|
||||||
|
|
||||||
|
|
||||||
# Create an XRP Ledger wallet
|
# Create an XRP Ledger wallet from the
|
||||||
# And create an x-address for the address
|
# testnet faucet and derive an
|
||||||
def create_wallet():
|
# x-address for the address
|
||||||
|
def create_wallet_from_faucet():
|
||||||
TESTNET_WALLET = generate_faucet_wallet(JSON_RPC_CLIENT_TESTNET)
|
TESTNET_WALLET = generate_faucet_wallet(JSON_RPC_CLIENT_TESTNET)
|
||||||
wallet2_address = wallet2.classic_address
|
wallet_classic_address = TESTNET_WALLET.classic_address
|
||||||
xaddress = addresscodec.classic_address_to_xaddress(
|
xaddress = addresscodec.classic_address_to_xaddress(
|
||||||
wallet2_address, tag, True
|
wallet2_address, tag, True
|
||||||
)
|
)
|
||||||
print("Classic address:", address)
|
print("\nClassic address:\n\n", wallet_classic_address)
|
||||||
print("X-address:", xaddress)
|
print("X-address:\n\n", xaddress)
|
||||||
|
|
||||||
|
|
||||||
|
# Create an XRP Ledger wallet from
|
||||||
|
# a generated seed
|
||||||
|
def create_wallet_from_seed():
|
||||||
|
seed = keypairs.generate_seed()
|
||||||
|
wallet_from_seed = xrpl.wallet.Wallet(seed)
|
||||||
|
print(wallet_from_seed)
|
||||||
|
|
||||||
# Optionally generate private and public keys
|
# Optionally generate private and public keys
|
||||||
# to manage your XRP Ledger account
|
# to manage your XRP Ledger account
|
||||||
@@ -94,12 +81,12 @@ def generate_keys():
|
|||||||
seed = keypairs.generate_seed()
|
seed = keypairs.generate_seed()
|
||||||
public, private = keypairs.derive_keypair(seed)
|
public, private = keypairs.derive_keypair(seed)
|
||||||
CLASSIC_ACCOUNT = keypairs.derive_classic_address(public)
|
CLASSIC_ACCOUNT = keypairs.derive_classic_address(public)
|
||||||
print("Here's the public key: ", public + "Store this in a secure place.")
|
print(f"Here's the public key:\n", public)
|
||||||
print("Here's the private key: ", private)
|
print(f"Here's the private key:\n", private + "\nStore this in a secure place.")
|
||||||
|
|
||||||
# Look up information about your account
|
# Look up information about your account
|
||||||
def get_acct_info():
|
def get_acct_info():
|
||||||
client = JsonRpcClient(JSON_RPC_URL)
|
client = JsonRpcClient(JSON_RPC_URL_TESTNET)
|
||||||
acct_info = AccountInfo(
|
acct_info = AccountInfo(
|
||||||
account=wallet,
|
account=wallet,
|
||||||
ledger_index="current",
|
ledger_index="current",
|
||||||
@@ -112,12 +99,30 @@ def get_acct_info():
|
|||||||
print("response.id: ", response.id)
|
print("response.id: ", response.id)
|
||||||
print("response.type: ", response.type)
|
print("response.type: ", response.type)
|
||||||
print("response.result.account_data.Balance", response.result.account_data.Balance)
|
print("response.result.account_data.Balance", response.result.account_data.Balance)
|
||||||
for key, value in response.result():
|
|
||||||
print(key, value)
|
|
||||||
for k, v in value.result():
|
|
||||||
print(k,v)
|
|
||||||
|
|
||||||
|
|
||||||
|
# FUTURE Prepare tx
|
||||||
|
def prepare_tx_future():
|
||||||
|
my_tx_future = Transaction.from_xrpl(
|
||||||
|
{
|
||||||
|
"TransactionType": "Payment",
|
||||||
|
"Amount": "50",
|
||||||
|
"Destination": "rNZz9HS8mmKqb3jGk28PjNMkkRXRzGdTda",
|
||||||
|
"Account": "rKQxjvLW67ZkMQdu9wmy73vhrbPcir21SV"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
TESTNET_WALLET.next_sequence_num = get_next_valid_seq_number(TESTNET_CLASSIC_ACCOUNT, JSON_RPC_CLIENT_TESTNET)
|
||||||
|
|
||||||
|
# CURRENT Prepare tx
|
||||||
|
def prepare_tx_current():
|
||||||
|
my_tx_current = Payment(
|
||||||
|
account=TESTNET_CLASSIC_ACCOUNT,
|
||||||
|
amount=_AMOUNT,
|
||||||
|
destination=TESTNET_DESTINATION_ACCOUNT,
|
||||||
|
memos=_MEMO,
|
||||||
|
transaction_type=TransactionType.PAYMENT,
|
||||||
|
)
|
||||||
|
TESTNET_WALLET.next_sequence_num = get_next_valid_seq_number(TESTNET_CLASSIC_ACCOUNT, JSON_RPC_CLIENT_TESTNET)
|
||||||
|
|
||||||
# Prepare the tx by formatting it into
|
# Prepare the tx by formatting it into
|
||||||
# the shape expected by the XRP Ledger
|
# the shape expected by the XRP Ledger
|
||||||
@@ -139,9 +144,6 @@ def prepare_sign_submit_tx():
|
|||||||
)
|
)
|
||||||
print("Signed transaction: ", signed_tx)
|
print("Signed transaction: ", signed_tx)
|
||||||
payment_transaction = Payment.from_dict(value)
|
payment_transaction = Payment.from_dict(value)
|
||||||
response = send_reliable_submission(
|
|
||||||
payment_transaction, CLASSIC_ACCOUNT, JSON_RPC_URL
|
|
||||||
)
|
|
||||||
print("response.result.validated: ", response["validated"])
|
print("response.result.validated: ", response["validated"])
|
||||||
print("response.result.meta: ", response.result["meta"]["TransactionResult"], "tesSUCCESS")
|
print("response.result.meta: ", response.result["meta"]["TransactionResult"], "tesSUCCESS")
|
||||||
if response.result["meta"]["TransactionResult"] != "tesSUCCESS":
|
if response.result["meta"]["TransactionResult"] != "tesSUCCESS":
|
||||||
|
|||||||
@@ -1,177 +0,0 @@
|
|||||||
---
|
|
||||||
html: build-apps.html
|
|
||||||
funnel: Build
|
|
||||||
doc_type: Tutorials
|
|
||||||
category: Get Started
|
|
||||||
blurb: Learn the common patterns for building apps on the XRP Ledger.
|
|
||||||
cta_text: Build Apps
|
|
||||||
---
|
|
||||||
|
|
||||||
# Build Apps on the XRP Ledger
|
|
||||||
|
|
||||||
|
|
||||||
When you're working with the XRP Ledger, there are a few things you'll need to manage with your app or integration, whether you're adding XRP into your [wallet](xref: wallets.md), integrating with the [decentralized exchange](xref: decentralized-exchange.md), or [issuing and managing tokens](xref:issued-currencies.md). This tutorial walks you through the patterns common to all of these use cases and provides sample code for implementing them.
|
|
||||||
|
|
||||||
Here are the basic steps you'll need to cover for almost any XRP Ledger project:
|
|
||||||
|
|
||||||
1. [Generate keys.](#generate-keys)
|
|
||||||
2. [Connect to the XRP Ledger.](#connect)
|
|
||||||
3. [Query the XRP Ledger.](#query)
|
|
||||||
4. [Submit a transaction.](#submit-transaction)
|
|
||||||
5. [Verify results.](#verify-results)
|
|
||||||
|
|
||||||
**Tip:** These steps roughly correspond to the best practices described in the [reliable transaction submission guide](xref:reliable-transaction-submission.md). Refer to that before you start building for production.
|
|
||||||
|
|
||||||
## Learning Goals
|
|
||||||
|
|
||||||
In this tutorial, you'll learn:
|
|
||||||
|
|
||||||
* The basic building blocks of XRP Ledger-based applications.
|
|
||||||
* How to generate keys.
|
|
||||||
* How to connect to the XRP Ledger.
|
|
||||||
* How to submit a transaction, including preparing and signing it.
|
|
||||||
* How to put these steps together to create a simple app that submits a transaction to the XRP Ledger Testnet.
|
|
||||||
|
|
||||||
## Set up your environment
|
|
||||||
|
|
||||||
Requirements for setting up your environment vary depending on the programming lanuage and tools you choose to use in your project; see the docs for the tools and libaries for details specific to the framework you want to work with.
|
|
||||||
|
|
||||||
Regardless of the tools you use, there are a few things you'll need to work with the XRP Ledger. Most available libraries provide built-in network clients and creating accounts.
|
|
||||||
|
|
||||||
|
|
||||||
* **Network client** - You need a network client to query the XRP Ledger. SDKs and other libraries typically use the framework-native network modules. If you're only testing or exploring, you could use the [Websocket API](https://xrpl.org/websocket-api-tool.html) or [JSON-RPC](https://xrpl.org/xrp-ledger-rpc-tool.html) tools.
|
|
||||||
* **[Account](xref: accounts.md)** - If you want to interact with the Ledger by sumbitting transactions, you'll need an address, an XRP balance, and secret keys to manage the account. To get started on the Testnet or Devnet, you can use the [XRP Faucets](https://xrpl.org/xrp-testnet-faucet.html) to get an address, XRP balances, and keys.
|
|
||||||
|
|
||||||
|
|
||||||
For library-specific requirements, see the README for each project:
|
|
||||||
|
|
||||||
* **JavaScript:** [ripple-lib](https://github.com/ripple/ripple-lib)
|
|
||||||
* **Python:** [xrpl-py](https://github.com/xpring-eng/xrpl-py)
|
|
||||||
* **Java:** [xrpl-4j](https://github.com/XRPLF/xrpl4j)
|
|
||||||
|
|
||||||
## Generate keys
|
|
||||||
|
|
||||||
You need [keys](https://xrpl.org/cryptographic-keys.html) to sign transactions that you submit to the XRP Ledger.
|
|
||||||
|
|
||||||
For testing and development purposes, you can get keys (and XRP balances) from [XRP Faucets](https://xrpl.org/xrp-testnet-faucet.html).
|
|
||||||
|
|
||||||
Otherwise, you should take care to store your keys and set up a [secure signing method](https://xrpl.org/set-up-secure-signing.html).
|
|
||||||
|
|
||||||
To generate keys, you can use any of the following methods:
|
|
||||||
|
|
||||||
<!-- MULTICODE_BLOCK_START -->
|
|
||||||
|
|
||||||
*Python (xrpl-py)*
|
|
||||||
|
|
||||||
```py
|
|
||||||
seed = keypairs.generate_seed()
|
|
||||||
public, private = keypairs.derive_keypair(seed)
|
|
||||||
```
|
|
||||||
|
|
||||||
*JavaScript (ripple-lib)*
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
return api.generateAddress();
|
|
||||||
```
|
|
||||||
|
|
||||||
*Java (xrpl4j)*
|
|
||||||
|
|
||||||
```java
|
|
||||||
WalletFactory walletFactory = DefaultWalletFactory.getInstance();
|
|
||||||
SeedWalletGenerationResult seedWalletGenerationResult = walletFactory.randomWallet(true);
|
|
||||||
```
|
|
||||||
|
|
||||||
<!-- MULTICODE_BLOCK_END -->
|
|
||||||
|
|
||||||
|
|
||||||
## Connect
|
|
||||||
|
|
||||||
To make queries that you can use in your app and submit transactions, you need to establish a connection to the XRP Ledger. There are a few ways to do this. The following sections describe each option in more detail.
|
|
||||||
|
|
||||||
**Warning:** Never use publicly available servers to sign transactions. For more information about security and signing, see [](xref: set-up-secure-signing.md).
|
|
||||||
|
|
||||||
If you only want to explore the XRP Ledger, you can use the [Ledger Explorer](https://livenet.xrpl.org/) to see the Ledger progress in real-time and dig into specific accounts or transactions.
|
|
||||||
|
|
||||||
|
|
||||||
<!-- MULTICODE_BLOCK_START -->
|
|
||||||
|
|
||||||
*Python (xrpl-py)*
|
|
||||||
|
|
||||||
```py
|
|
||||||
client = JsonRpcClient(http://s1.ripple.com:51234/)
|
|
||||||
```
|
|
||||||
|
|
||||||
*JavaScript (ripple-lib)*
|
|
||||||
|
|
||||||
```js
|
|
||||||
ripple = require('ripple-lib')
|
|
||||||
api = new ripple.RippleAPI({server: 'wss://s.altnet.rippletest.net:51233'})
|
|
||||||
api.connect()
|
|
||||||
```
|
|
||||||
|
|
||||||
*Java (xrpl4j)*
|
|
||||||
|
|
||||||
```java
|
|
||||||
private final XrplClient xrplClient = new XrplClient(HttpUrl.parse("https://s1.ripple.com:51234"));
|
|
||||||
```
|
|
||||||
|
|
||||||
<!-- MULTICODE_BLOCK_END -->
|
|
||||||
|
|
||||||
|
|
||||||
### Other connection options
|
|
||||||
|
|
||||||
|
|
||||||
#### Connect via Core Server
|
|
||||||
|
|
||||||
The XRP Ledger's core server software, [`rippled`](the-rippled-server.html), natively exposes several interfaces that you can access by connecting to [publicly available servers](xref: get-started-with-the-rippled-api.md#public-servers) or running your own server. These interfaces include:
|
|
||||||
|
|
||||||
* [JSON-RPC](xref: get-started-with-the-rippled-api.md#json-rpc) - If you're familiar with RESTful APIs, this interface will be the most familiar to you. You can use standard HTTP clients (like [Postman](https://www.postman.com/), [Insomnia](https://insomnia.rest/), or [RESTED for Firefox](https://addons.mozilla.org/en-US/firefox/addon/rested/)) to make calls via this interface.
|
|
||||||
* [WebSocket API](xref:get-started-with-the-rippled-api.md#websocket-api) -
|
|
||||||
|
|
||||||
For a complete list of these interfaces and their associated methods, see [](xref: get-started-with-the-rippled-api.md).
|
|
||||||
|
|
||||||
|
|
||||||
## Query
|
|
||||||
|
|
||||||
Before you submit a transaction to the XRP Ledger, you should query it to check your account status and balances to make sure that the transaction will succeed.
|
|
||||||
|
|
||||||
<!-- MULTICODE_BLOCK_START -->
|
|
||||||
|
|
||||||
*Python (xrpl-py)*
|
|
||||||
|
|
||||||
```py
|
|
||||||
account_info = RequestMethod.ACCOUNT_INFO(rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe)
|
|
||||||
```
|
|
||||||
|
|
||||||
*JavaScript (ripple-lib)*
|
|
||||||
|
|
||||||
```js
|
|
||||||
const myAddress = 'rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe';
|
|
||||||
|
|
||||||
console.log('getting account info for', myAddress);
|
|
||||||
return api.getAccountInfo(myAddress);
|
|
||||||
```
|
|
||||||
|
|
||||||
*Java (xrpl4j)*
|
|
||||||
|
|
||||||
```java
|
|
||||||
AccountInfoResult accountInfo = this.scanForResult(() -> this.getValidatedAccountInfo(wallet.classicAddress()));
|
|
||||||
assertThat(accountInfo.status()).isNotEmpty().get().isEqualTo("success");
|
|
||||||
```
|
|
||||||
|
|
||||||
<!-- MULTICODE_BLOCK_END -->
|
|
||||||
|
|
||||||
|
|
||||||
## Submit transaction
|
|
||||||
|
|
||||||
When you're ready
|
|
||||||
|
|
||||||
### Prepare transaction
|
|
||||||
|
|
||||||
### Sign transaction
|
|
||||||
|
|
||||||
## Verify results
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ blurb: Build a simple Python app that interacts with the XRP Ledger.
|
|||||||
cta_text: Build Apps
|
cta_text: Build Apps
|
||||||
---
|
---
|
||||||
|
|
||||||
# Get started using Python
|
# Get Started Using Python
|
||||||
|
|
||||||
This tutorial walks you through the basics of building an XRP Ledger-connected application using [xrpl-py](https://github.com/xpring-eng/xrpl-py), a [Python](https://www.python.org) library that makes it easy to integrate XRP Ledger functionality into your apps.
|
This tutorial walks you through the basics of building an XRP Ledger-connected application using [xrpl-py](https://github.com/xpring-eng/xrpl-py), a [Python](https://www.python.org) library that makes it easy to integrate XRP Ledger functionality into your apps.
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@ This tutorial walks you through the basics of building an XRP Ledger-connected a
|
|||||||
|
|
||||||
In this tutorial, you'll learn:
|
In this tutorial, you'll learn:
|
||||||
|
|
||||||
* How to set up your environment for Python development. See [](xref:python-env-setup.md).
|
* How to set up your environment for Python development. See [](python-env-setup.html).
|
||||||
* The basic building blocks of XRP Ledger-based applications.
|
* The basic building blocks of XRP Ledger-based applications.
|
||||||
* How to generate keys.
|
* How to generate keys.
|
||||||
* How to connect to the XRP Ledger.
|
* How to connect to the XRP Ledger.
|
||||||
|
|||||||
Reference in New Issue
Block a user