bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 28쪽 중 2쪽
lowing guidelines:
Have some familiarity with writing code in JavaScript.
Have installed Node.js
version 20
or later in your development environment.
If you want to build a web application, any modern web browser with JavaScript support should work fine.
Source Code
Click
Download
on the top right of the code preview panel to download the source code.
Steps
Follow the steps to create a simple application with
xrpl.js
.
1. Install Dependencies
Start a new project by creating an empty folder, then move into that folder and use
NPM
to install the latest version of xrpl.js:
npm
install
xrpl
This updates your
package.json
file, or creates a new one if it didn't already exist.
Your
package.json
file should look something like this:
{
"dependencies"
: {
"xrpl"
:
"^4.4.0"
},
"type"
:
"module"
}
2. Connect to the XRP Ledger
Connect to the XRP Ledger Testnet
To make queries and submit transactions, you need to connect to the XRP Ledger. To do this with
xrpl.js
, you create an instance of the
Client
class and use the
connect()
method.
Tip
Many network functions in
xrpl.js
use
Promises
to return values asynchronously. The code samples here use the
async/await
pattern
to wait for the actual result of the Promises.
The sample code shows you how to connect to the Testnet, which is one of the available
parallel networks
.
Connect to the XRP Ledger Mainnet
When you're ready to move to production, you'll need to connect to the XRP Ledger Mainnet. You can do that in two ways:
By
installing the core server
(
xrpld
) and running a node yourself. The core server connects to the Mainnet by default, but you can
change the configuration to use Testnet or Devnet
.
There are good reasons to run your own core server
. If you run your own server, you can connect to it like so:
const
MY_SERVER
=
"ws://localhost:6006/"
const
client
=
new
xrpl.
Client
(
MY_SERVER
)
await
client.
connect
()
See the example
core server config file
for more information about default values.
By using one of the available
public servers
:
const
PUBLIC_SERVER
=
"wss://xrplcluster.com/"
const
client
=
new
xrpl.
Client
(
PUBLIC_SERVER
)
await
client.
connect
()
3. Get Account
Create and Fund a Wallet
The
xrpl.js
library has a
Wallet
class for handling the keys and address of an XRP Ledger account. On Testnet, you can fund a new account as shown in the example.
(Optional) Generate a Wallet Only
If you want to generate a wallet without funding it, you can create a new
Wallet
instance. Keep in mind that you need to send XRP to the wallet for it to be a valid account on the ledger.
(Optional) Use Your Own Wallet Seed
To use an existing wallet seed encoded in
base58
, you can create a
Wallet
instance from it.
4. Query the XRP Ledger
Use the Client's
request()
method to access the XRP Ledger's
WebSocket API
.
5. Listen for Events
You can set up handlers for various types of events in
xrpl.js
, such as whenever the XRP Ledger's
consensus process
produces a new
ledger version
. To do that, first call the
subscribe method
to get the type of events you want, then attach an event handler using the
on(eventType, callback)
method of the client.
6. Disconnect
Call the
disconnect()
function so Node.js can end the process. The example code waits 10 seconds before disconnecting to allow time for the ledger event listener to receive and display events.
7. Run the Application
Finally, in your terminal, run the application like so:
node
get-acct-info.js
You should see output similar to the following:
Connected
to
Testnet
Creating
a
new
wallet
and
funding
it
with
Testnet
XRP...
Wallet:
rMnXR9p2sZT9iZ6ew3iEqvBMyPts1ADc4i
Balance:
10
Account
Testnet
Explorer
URL:
https://testnet.xrpl.org/accounts/rMnXR9p2sZT9iZ6ew3iEqvBMyPts1ADc4i
Getting
account
info...
{
"api_version"
:
2,
"id"
:
4,
"result"
:
{
"account_data"
:
{
"Account"
:
"rMnXR9p2sZT9iZ6ew3iEqvBMyPts1ADc4i",
"Balance"
:
"10000000",
"Flags"
:
0,
"LedgerEntryType"
:
"AccountRoot",
"OwnerCount"
:
0,
"PreviousTxnID"
:
"0FF9DB2FE141DD0DF82566A171B6AF70BB2C6EB6A53D496E65D42FC062C91A78",
"PreviousTxnLgrSeq"
:
9949268,
"Sequence"
:
9949268,
"index"
:
"4A9C9220AE778DC38C004B2B17A08E218416D90E01456AFCF844C18838B36D01"
},
"account_flags"
:
{
"allowTrustLineClawback"
:
false
,
"defaultRipple"
:
false
,
"depositAuth"
:
false
,
"disableMasterKey"
:
false
,
"disallowIncomingCheck"
:
false
,
"disallowIncomingNFTokenOffer"
:
false
,
"disallowIncomingPayChan"
:
false
,
"disallowIncomingTrustline"
:
false
,
"disallowIncomingXRP"
:
false
,
"globalFreeze"
:
false
,
"noFreeze"
:
false
,
"passwordSpent"
:
false
,
"requireAuthorization"
:
false
,
"requireDestinationTag"
:
false
},
"ledger_hash"
:
"304C7CC2A33B712BE43EB398B399E290C191A71FCB71784F584544DFB7C441B0",
"ledger_index"
:
9949268,
"validated"
:
true
},
"type"
:
"response"
}
Listening
for
ledger
close
events...
Ledger
#9949269 validated with 0 transactions!
Ledger
#9949270 validated with 0 transactions!
Ledger
#9949271 validated with 0 transactions!
Disconnected
See Also
Concepts:
XRP Ledger Overview
Client Libraries
Tutorials:
Send XRP
Issue a Fungible Token
Set up Secure Signing
References:
xrpl.js
Reference
Public API Methods
API Conventions
base58 Encodings
Transaction Formats
get-acct-info.js
Download
// Import the library
import
xrpl
from
"xrpl"
// Define the network client
const
SERVER_URL
=
"wss://s.altnet.rippletest.net:51233/"
const
client
=
new
xrpl.
Client
(
SERVER_URL
)
await
client.
connect
()
console.
log
(
"Connected to Testnet"
)
// Create a wallet and fund it with the Testnet faucet:
console.
log
(
"
\n
Creating a new wallet and funding it with Testnet XRP..."
)
const
fund_result
=
await
client.
fundWallet
()
const
test_wallet
=
fund_result.wallet
console.
log
(
`Wallet: ${
test_wallet
.
address
}`
)
console.
log
(
`Balance: ${
fund_result
.
balance
}`
)
console.
log
(
'Account Testnet Explorer URL:'
)
console.
log
(
` https://testnet.xrpl.org/accounts/${
test_wallet
.
address
}`
)
// To generate a wallet without funding it, uncomment the code below
// const test_wallet = xrpl.Wallet.generate()
// To provide your own seed, replace the test_wallet value with the below
// const test_wallet = xrpl.Wallet.fromSeed("your-seed-key")
// Get info from the ledger about the address we just funded
console.
log
(
"
\n
Getting account info..."
)
const
response
=
await
client.
request
({
"command"
:
"account_info"
,
"account"
: test_wallet.address,
"ledger_index"
:
"validated"
})
console.
log
(
JSON
.
stringify
(response,
null
,
2
))
// Listen to ledger close events
console.
log
(
"
\n
Listening for ledger close events..."
)
client.
request
({
"command"
:
"subscribe"
,
"streams"
: [
"ledger"
]
})
client.
on
(
"ledgerClosed"
,
async
(
ledger
)
=>
{
console.
log
(
`Ledger #${
ledger
.
ledger_index
} validated `
+
`with ${
ledger
.
txn_count
} transactions!`
)
})
// Disconnect when done so Node.js can end the process.
// Delay this by 10 seconds to give the ledger event listener time to receive
// and display some ledger events.
setTimeout
(
async
()
=>
{
await
client.
disconnect
();
console.
log
(
'
\n
Disconnected'
);
},
10000
);
Copy
## /docs/tutorials/get-started/get-started-python
Get Started Using Python Library
Copy
This tutorial walks you through the basics of building an XRP Ledger-connected application using the
xrpl-py
client library, a pure
Python
library built to interact with the XRP Ledger using native Python models and methods.
This tutorial is intended for beginners and should take no longer than 30 minutes to complete.
Goals
In this tutorial, you'll learn:
The basic building blocks of XRP Ledger-based applications.
How to connect to the XRP Ledger using
xrpl-py
.
How to get an account on the
Testnet
using
xrpl-py
.
How to use the
xrpl-py
library to look up information about an account on the XRP Ledger.
How to put these steps together to create a Python app.
Prerequisites
To complete this tutorial, you should meet the following guidelines:
Have a basic understanding of Python.
Have installed
Python 3.7
or later.
Source Code
Click
Download
on the top right of the code preview panel to download the source code.
Steps
Follow the steps to create a simple application with
xrpl-py
.
1. Install Dependencies
Start a new project by creating an empty folder, then move into that folder and set up a Python virtual environment with the necessary dependencies:
# Create and activate a virtual environment
python
-m
venv
.venv
source
.venv/bin/activate
# Install the xrpl-py library
pip
install
xrpl-py
Alternatively, if you're using the downloaded source code, you can install all dependencies from the
requirements.txt
file:
python
-m
venv
.venv
source
.venv/bin/activate
pip
install
-r
requirements.txt
2. Connect to the XRP Ledger
Connect to the XRP Ledger Testnet
To make queries and submit transactions, you need to connect to the XRP Ledger. To do this with
xrpl-py
, use the
xrp.clients
module
.
Note
The standard approach with
xrpl-py
is to use the JSON-RPC client. While a WebSocket client is available, it requires you to use
async
/
await
throughout your code. For most use cases, stick with JSON-RPC to avoid the complexity of asynchronous programming.
The sample code shows you how to connect to the Testnet, which is one of the available
parallel networks
.
Connect to the XRP Ledger Mainnet
The sample code in the previous section shows you how to connect to the Testnet, which is a
parallel network
for testing where the money has no real value. When you're ready to integrate with the production XRP Ledger, you'll need to connect to the Mainnet. You can do that in two ways:
By
installing the core server
(
xrpld
) and running a node yourself. The core server connects to the Mainnet by default, but you can
change the configuration to use Testnet or Devnet
.
There are good reasons to run your own core server
. If you run your own server, you can connect to it like so:
from
xrpl.clients
import
JsonRpcClient
JSON_RPC_URL
=
"http://localhost:5005/"
client
=
JsonRpcClient(
JSON_RPC_URL
)
See the example
core server config file
for more information about default values.
By using one of the available
public servers
:
from
xrpl.clients
import
JsonRpcClient
JSON_RPC_URL
=
"https://s2.ripple.com:51234/"
client
=
JsonRpcClient(
JSON_RPC_URL
)
3. Get account
To store value and execute transactions on the XRP Ledger, you need an account: a
set of keys
and an
address
that's been
funded with enough XRP
to meet the
account reserve
. The address is the identifier of your account and you use the
private key
to sign transactions that you submit to the XRP Ledger.
Tip
For testing and development purposes, you can use the
XRP Faucets
to generate keys and fund the account on the Testnet or Devnet. For production purposes, you should take care to store your keys and set up a
secure signing method
. Another difference in production is that XRP has real worth, so you can't get it for free from a faucet.
To create and fund an account on the Testnet,
xrpl-py
provides the
generate_faucet_wallet
method. This method returns a
Wallet
instance
.
4. Query the XRP Ledger
You can query the XRP Ledger to get information about
a specific account
,
a specific transaction
, the state of a
current or a historical ledger
, and
the XRP Ledger's decentralized exchange
. You need to make these queries, among other reasons, to look up account info to follow best practices for
reliable transaction submission
.
Use the
account_info method
to look up information about the account you got in the previous step. Use a request model like
AccountInfo
to validate the request format and catch errors sooner.
5. Run the Application
Finally, in your terminal, run the application like so:
python
get-acct-info.py
You should see output similar to this example:
Creating
a
new
wallet
and
funding
it
with
Testnet
XRP...
Attempting
to
fund
address
ravbHNootpSNQkxyEFCWevSkHsFGDHfyop
Faucet
fund
successful.
Wallet:
ravbHNootpSNQkxyEFCWevSkHsFGDHfyop
Account
Testnet
Explorer
URL:
https://testnet.xrpl.org/accounts/ravbHNootpSNQkxyEFCWevSkHsFGDHfyop
Getting
account
info...
Response
Status:
ResponseStatus.SUCCESS
{
"account_data"
:
{
"Account"
:
"ravbHNootpSNQkxyEFCWevSkHsFGDHfyop",
"Balance"
:
"100000000",
"Flags"
:
0,
"LedgerEntryType"
:
"AccountRoot",
"OwnerCount"
:
0,
"PreviousTxnID"
:
"3DACF2438AD39F294C4EFF6132D5D88BCB65D2F2261C7650F40AC1F6A54C83EA",
"PreviousTxnLgrSeq"
:
12039759,
"Sequence"
:
12039759,
"index"
:
"148E6F4B8E4C14018D679A2526200C292BDBC5AB77611BC3AE0CB97CD2FB84E5"
},
"account_flags"
:
{
"allowTrustLineClawback"
:
false
,
"defaultRipple"
:
false
,
"depositAuth"
:
false
,
"disableMasterKey"
:
false
,
"disallowIncomingCheck"
:
false
,
"disallowIncomingNFTokenOffer"
:
false
,
"disallowIncomingPayChan"
:
false
,
"disallowIncomingTrustline"
:
false
,
"disallowIncomingXRP"
:
false
,
"globalFreeze"
:
false
,
"noFreeze"
:
false
,
"passwordSpent"
:
false
,
"requireAuthorization"
:
false
,
"requireDestinationTag"
:
false
},
"ledger_hash"
:
"CA624D717C4FCDD03BAD8C193F374A77A14F7D2566354A4E9617A8DAD896DE71",
"ledger_index"
:
12039759,
"validated"
:
true
}
The response fields that you want to inspect in most cases are:
account_data.Balance
— This is the account's balance of
XRP, in drops
. You can use this to confirm that you have enough XRP to send (if you're making a payment) and to meet the
current transaction cost
for a given transaction.
validated
— Indicates whether the returned data is from a
validated ledger
. When inspecting transactions, it's important to confirm that
the results are final
before further processing the transaction. If
validated
is
true
then you know for sure the results won't change. For more information about best practices for transaction processing, see
Reliable Transaction Submission
.
For a detailed description of every response field, see
account_info
.
See Also
Concepts:
XRP Ledger Overview
Client Libraries
Tutorials:
Send XRP
Issue a Fungible Token
Set up Secure Signing
References:
xrpl-py
Reference
Public API Methods
API Conventions
base58 Encodings
Transaction Formats
get-acct-info.py
Download
# Define the network client
from
xrpl.clients
import
JsonRpcClient
from
xrpl.wallet
import
generate_faucet_wallet
from
xrpl.core
import
addresscodec
from
xrpl.models.requests.account_info
import
AccountInfo
import
json
JSON_RPC_URL
=
"https://s.altnet.rippletest.net:51234/"
client
=
JsonRpcClient(
JSON_RPC_URL
)
# Create a wallet using the Testnet faucet:
# https://xrpl.org/xrp-testnet-faucet.html
print
(
"
\n
Creating a new wallet and funding it with Testnet XRP..."
)
test_wallet
=
generate_faucet_wallet(client,
debug
=
True
)
test_account
=
test_wallet.classic_address
print
(
f
"Wallet:
{
test_account
}
"
)
print
(
f
"Account Testnet Explorer URL: "
)
print
(
f
" https://testnet.xrpl.org/accounts/
{
test_account
}
"
)
# Look up info about your account
print
(
"
\n
Getting account info..."
)
acct_info
=
AccountInfo(
account
=
test_account,
ledger_index
=
"validated"
,
strict
=
True
,
)
response
=
client.request(acct_info)
result
=
response.result
print
(
"Response Status: "
, response.status)
print
(json.dumps(response.result,
indent
=
4
,
sort_keys
=
True
))
Copy
## /docs/tutorials/get-started/get-started-php
Get Started Using PHP Library
Copy
This tutorial walks you through the basics of building an XRP Ledger-connected application using
XRPL_PHP
, a PHP library built to interact with the XRP Ledger.
This tutorial is intended for beginners and should take no longer than 30 minutes to complete.
Learning Goals
In this tutorial, you'll learn:
The basic building blocks of XRP Ledger-based applications.
How to connect to the XRP Ledger using
XRPL_PHP
.
How to get an account on the
Testnet
using
XRPL_PHP
.
How to use the
XRPL_PHP
library to look up information about an account on the XRP Ledger.
How to put these steps together to create a simple application.
Requirements
XRPL_PHP
requires PHP 8.1 and the PHP extension
GMP
.
The PHP extension
BCMATH
is recommended for increased performance.
Installation
XRPL_PHP
can be installed via
Composer
:
composer require hardcastle/xrpl_php
Start Building
When you're working with the XRP Ledger, there are a few things you'll need to manage, whether you're adding XRP to your
account
, integrating with the
decentralized exchange
, or
issuing tokens
. This tutorial walks you through basic patterns common to getting started with 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:
Connect to the XRP Ledger.
Get an account.
Query the XRP Ledger.
1. Connect to the XRP Ledger
To make queries and submit transactions, you need to connect to the XRP Ledger. To do this with
XRPL_PHP
, you can use the
JsonRpcClient
:
<?
php
// Use the Composer autoloader
require
__DIR__
.
'/vendor/autoload.php'
;
// Imports
use
XRPL_PHP\Client\JsonRpcClient
;
// Create a Client using the Testnet
$client
=
new
JsonRpcClient
(
"https://s.altnet.rippletest.net:51234"
);
Note that PHP has no native support for WebSockets, so the Client does not establish a permanent connection.
Connect to the production XRP Ledger
The sample code in the previous section shows you how to connect to the Testnet, which is one of the available
parallel networks
. When you're ready to integrate with the production XRP Ledger, you'll need to connect to the Mainnet. You can do that in two ways:
By
installing the core server
(
xrpld
) and running a node yourself. The core server connects to the Mainnet by default, but you can
change the configuration to use Testnet or Devnet
.
There are good reasons to run your own core server
. If you run your own server, you can connect to it like so:
use XRPL_PHP\Client\JsonRpcClient;
const LOCAL_JSON_RPC_URL = "http://localhost:5005/";
$client = new JsonRpcClient("LOCAL_JSON_RPC_URL");
See the example
core server config file
for more information about default values.
By using one of the available
public servers
:
use XRPL_PHP\Client\JsonRpcClient;
const MAINNET_JSON_RPC_URL = "https://s2.ripple.com:51234/";
$client = new JsonRpcClient("MAINNET_JSON_RPC_URL");
2. Get account
To store value and execute transactions on the XRP Ledger, you need to get an account: a
set of keys
and an
address
that's been
funded with enough XRP
to meet the
account reserve
. The address is the identifier of your account and you use the
private key
to sign transactions that you submit to the XRP Ledger. For production purposes, you should take care to store your keys and set up a
secure signing method
.
To generate a new account,
PHP_XRPL
provides the static
generate()
method in the
Wallet
class:
<?
php
// Use the Composer autoloader
require
__DIR__
.
'/vendor/autoload.php'
;
// Imports
use
XRPL_PHP\Wallet\Wallet
;
// Create a new wallet
$wallet
=
Wallet
::
generate
();
The result returns a
Wallet
instance:
// print wallet properties
print_r
([
'publicKey'
=>
$wallet
->
getPublicKey
(),
'privateKey'
=>
$wallet
->
getPrivateKey
(),
'classicAddress'
=>
$wallet
->
getAddress
(),
'seed'
=>
$wallet
->
getSeed
()
]);
// output
// Array
// (
// [publicKey] => ED2C4CE69F663254840905AEF5FB8596FC243EDEBE0295A6ECEE86CE8EB8F76210
// [privateKey] => -HIDDEN-
// [classicAddress] => rBi9u1P3ofRKTFPFTgrguANz2wRqsdKHvm
// [seed] => -HIDDEN-
//)
For testing and develop프로젝트로 돌아가기