mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-12-01 00:55:50 +00:00
Re-level non-docs content to top of repo and rename content→docs
This commit is contained in:
34
_code-samples/freeze/js/check-global-freeze.js
Normal file
34
_code-samples/freeze/js/check-global-freeze.js
Normal file
@@ -0,0 +1,34 @@
|
||||
// Dependencies for Node.js.
|
||||
// In browsers, use <script> tags as in the example demo.html.
|
||||
if (typeof module !== "undefined") {
|
||||
// Use var here because const/let are block-scoped to the if statement.
|
||||
var xrpl = require('xrpl')
|
||||
var { AccountRootFlags } = require('xrpl/dist/npm/models/ledger')
|
||||
}
|
||||
|
||||
// Connect -------------------------------------------------------------------
|
||||
async function main() {
|
||||
console.log("Connecting to Mainnet...")
|
||||
const client = new xrpl.Client('wss://s1.ripple.com')
|
||||
await client.connect()
|
||||
|
||||
const my_address = 'rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn';
|
||||
|
||||
// Request account info for my_address to check account settings ------------
|
||||
const response = await client.request(
|
||||
{command: 'account_info', account: my_address })
|
||||
const settings = response.result
|
||||
const lsfGlobalFreeze = xrpl.LedgerEntry.AccountRootFlags.lsfGlobalFreeze
|
||||
|
||||
console.log('Got settings for address', my_address);
|
||||
console.log('Global Freeze enabled?',
|
||||
((settings.account_data.Flags & lsfGlobalFreeze)
|
||||
=== lsfGlobalFreeze))
|
||||
|
||||
await client.disconnect()
|
||||
|
||||
// End main()
|
||||
}
|
||||
|
||||
main().catch(console.error)
|
||||
|
||||
54
_code-samples/freeze/js/check-individual-freeze.js
Normal file
54
_code-samples/freeze/js/check-individual-freeze.js
Normal file
@@ -0,0 +1,54 @@
|
||||
// Dependencies for Node.js.
|
||||
// In browsers, use <script> tags as in the example demo.html.
|
||||
if (typeof module !== "undefined") {
|
||||
// Use var here because const/let are block-scoped to the if statement.
|
||||
var xrpl = require('xrpl')
|
||||
}
|
||||
|
||||
// Connect -------------------------------------------------------------------
|
||||
async function main() {
|
||||
console.log("Connecting to Mainnet...")
|
||||
const client = new xrpl.Client('wss://s1.ripple.com')
|
||||
await client.connect()
|
||||
|
||||
const my_address = 'rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn'
|
||||
const counterparty_address = 'rUpy3eEg8rqjqfUoLeBnZkscbKbFsKXC3v'
|
||||
const frozen_currency = 'USD'
|
||||
|
||||
// Look up current state of the trust line ----------------------------------
|
||||
const account_lines = {
|
||||
command: 'account_lines',
|
||||
account: my_address,
|
||||
peer: counterparty_address,
|
||||
}
|
||||
|
||||
console.log(`Looking up all trust lines from
|
||||
${counterparty_address} to ${my_address}`)
|
||||
|
||||
const data = await client.request(account_lines)
|
||||
|
||||
// Find the trust line for our frozen_currency ------------------------------
|
||||
let trustline = null
|
||||
for (let i = 0; i < data.result.lines.length; i++) {
|
||||
if(data.result.lines[i].currency === frozen_currency) {
|
||||
trustline = data.result.lines[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if(trustline === null) {
|
||||
throw `There was no ${frozen_currency} trust line`
|
||||
}
|
||||
|
||||
// Check if the trust line is frozen -----------------------------------------
|
||||
console.log('Trust line frozen from our side?',
|
||||
trustline.freeze === true)
|
||||
console.log('Trust line frozen from counterparty\'s side?',
|
||||
trustline.freeze_peer === true)
|
||||
|
||||
await client.disconnect()
|
||||
|
||||
// End main()
|
||||
}
|
||||
|
||||
main().catch(console.error)
|
||||
33
_code-samples/freeze/js/check-no-freeze.js
Normal file
33
_code-samples/freeze/js/check-no-freeze.js
Normal file
@@ -0,0 +1,33 @@
|
||||
// Dependencies for Node.js.
|
||||
// In browsers, use <script> tags as in the example demo.html.
|
||||
if (typeof module !== "undefined") {
|
||||
// Use var here because const/let are block-scoped to the if statement.
|
||||
var xrpl = require('xrpl')
|
||||
}
|
||||
|
||||
|
||||
// Connect -------------------------------------------------------------------
|
||||
async function main() {
|
||||
console.log("Connecting to Mainnet...")
|
||||
const client = new xrpl.Client('wss://s1.ripple.com')
|
||||
await client.connect()
|
||||
|
||||
const my_address = 'rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn';
|
||||
|
||||
// Request account info for my_address to check account settings ------------
|
||||
const response = await client.request(
|
||||
{command: 'account_info', account: my_address })
|
||||
const settings = response.result
|
||||
const lsfNoFreeze = xrpl.LedgerEntry.AccountRootFlags.lsfNoFreeze
|
||||
|
||||
console.log('Got settings for address', my_address);
|
||||
console.log('No Freeze enabled?',
|
||||
(settings.account_data.Flags & lsfNoFreeze)
|
||||
=== lsfNoFreeze)
|
||||
|
||||
await client.disconnect()
|
||||
|
||||
// End main()
|
||||
}
|
||||
|
||||
main().catch(console.error)
|
||||
8
_code-samples/freeze/js/package.json
Normal file
8
_code-samples/freeze/js/package.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"name": "freeze-examples",
|
||||
"version": "0.1.0",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"xrpl": "^2.11.0"
|
||||
}
|
||||
}
|
||||
81
_code-samples/freeze/js/set-global-freeze-with-checks.js
Normal file
81
_code-samples/freeze/js/set-global-freeze-with-checks.js
Normal file
@@ -0,0 +1,81 @@
|
||||
// Dependencies for Node.js.
|
||||
// In browsers, use <script> tags as in the example demo.html.
|
||||
if (typeof module !== "undefined") {
|
||||
// Use var here because const/let are block-scoped to the if statement.
|
||||
var xrpl = require('xrpl')
|
||||
}
|
||||
|
||||
async function main() {
|
||||
// Connect -------------------------------------------------------------------
|
||||
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233')
|
||||
await client.connect()
|
||||
|
||||
// Get credentials from the Testnet Faucet ------------------------------------
|
||||
console.log("Requesting an address from the Testnet faucet...")
|
||||
const { wallet, balance } = await client.fundWallet()
|
||||
|
||||
// Prepare an AccountSet transaction to enable global freeze ------------------
|
||||
const accountSetTx = {
|
||||
TransactionType: "AccountSet",
|
||||
Account: wallet.address,
|
||||
// Set a flag to turn on a global freeze on this account
|
||||
SetFlag: xrpl.AccountSetAsfFlags.asfGlobalFreeze
|
||||
}
|
||||
|
||||
// Best practice for JS users - validate checks if a transaction is well-formed
|
||||
xrpl.validate(accountSetTx)
|
||||
|
||||
// Sign and submit the AccountSet transaction to enable a global freeze -------
|
||||
console.log('Signing and submitting the transaction:', accountSetTx)
|
||||
await client.submitAndWait(accountSetTx, { wallet: wallet })
|
||||
console.log("Finished submitting!")
|
||||
|
||||
// Checking the status of the global freeze -----------------------------------
|
||||
const response = await client.request(
|
||||
{command: 'account_info', account: wallet.address})
|
||||
const settings = response.result
|
||||
const lsfGlobalFreeze = 0x00400000
|
||||
|
||||
console.log(settings)
|
||||
console.log('Got settings for address', wallet.address);
|
||||
console.log('Global Freeze enabled?',
|
||||
((settings.account_data.Flags & lsfGlobalFreeze) === lsfGlobalFreeze))
|
||||
|
||||
// Investigate ----------------------------------------------------------------
|
||||
console.log(
|
||||
`You would investigate whatever prompted you to freeze the account now...`)
|
||||
await new Promise(resolve => setTimeout(resolve, 3000))
|
||||
|
||||
// Now we disable the global freeze -------------------------------------------
|
||||
const accountSetTx2 = {
|
||||
TransactionType: "AccountSet",
|
||||
Account: wallet.address,
|
||||
// ClearFlag let's us turn off a global freeze on this account
|
||||
ClearFlag: xrpl.AccountSetAsfFlags.asfGlobalFreeze
|
||||
}
|
||||
|
||||
// Best practice for JS users - validate checks if a transaction is well-formed
|
||||
xrpl.validate(accountSetTx2)
|
||||
|
||||
// Sign and submit the AccountSet transaction to enable a global freeze -------
|
||||
console.log('Signing and submitting the transaction:', accountSetTx2)
|
||||
const result = await client.submitAndWait(accountSetTx2, { wallet: wallet })
|
||||
console.log("Finished submitting!")
|
||||
|
||||
// Checking the status of the global freeze -----------------------------------
|
||||
const response2 = await client.request(
|
||||
{command: 'account_info', account: wallet.address})
|
||||
const settings2 = response2.result
|
||||
|
||||
console.log(settings2)
|
||||
console.log('Got settings for address', wallet.address);
|
||||
console.log('Global Freeze enabled?',
|
||||
((settings2.account_data.Flags & lsfGlobalFreeze) === lsfGlobalFreeze))
|
||||
|
||||
console.log("Disconnecting")
|
||||
await client.disconnect()
|
||||
|
||||
// End main()
|
||||
}
|
||||
|
||||
main().catch(console.error)
|
||||
61
_code-samples/freeze/js/set-global-freeze.js
Normal file
61
_code-samples/freeze/js/set-global-freeze.js
Normal file
@@ -0,0 +1,61 @@
|
||||
// Dependencies for Node.js.
|
||||
// In browsers, use <script> tags as in the example demo.html.
|
||||
if (typeof module !== "undefined") {
|
||||
// Use var here because const/let are block-scoped to the if statement.
|
||||
var xrpl = require('xrpl')
|
||||
}
|
||||
|
||||
async function main() {
|
||||
// Connect -------------------------------------------------------------------
|
||||
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233')
|
||||
await client.connect()
|
||||
|
||||
// Get credentials from the Testnet Faucet -----------------------------------
|
||||
console.log("Requesting an address from the Testnet faucet...")
|
||||
const { wallet, balance } = await client.fundWallet()
|
||||
|
||||
// Prepare an AccountSet transaction to enable global freeze -----------------
|
||||
const accountSetTx = {
|
||||
TransactionType: "AccountSet",
|
||||
Account: wallet.address,
|
||||
// Set a flag to turn on a global freeze on this account
|
||||
SetFlag: xrpl.AccountSetAsfFlags.asfGlobalFreeze
|
||||
}
|
||||
|
||||
// Best practice for JS users - validate checks if a transaction is well-formed
|
||||
xrpl.validate(accountSetTx)
|
||||
|
||||
// Sign and submit the AccountSet transaction to enable a global freeze ------
|
||||
console.log('Signing and submitting the transaction:', accountSetTx)
|
||||
await client.submitAndWait(accountSetTx, { wallet })
|
||||
console.log(`Finished submitting! ${wallet.address} should be frozen now.`)
|
||||
|
||||
// Investigate ---------------------------------------------------------------
|
||||
console.log(
|
||||
`You would investigate whatever prompted you to freeze the account now...`)
|
||||
await new Promise(resolve => setTimeout(resolve, 5000))
|
||||
|
||||
// Now we disable the global freeze ------------------------------------------
|
||||
const accountSetTx2 = {
|
||||
TransactionType: "AccountSet",
|
||||
Account: wallet.address,
|
||||
// ClearFlag let's us turn off a global freeze on this account
|
||||
ClearFlag: xrpl.AccountSetAsfFlags.asfGlobalFreeze
|
||||
}
|
||||
|
||||
// Best practice for JS users - validate checks if a transaction is well-formed
|
||||
xrpl.validate(accountSetTx2)
|
||||
|
||||
// Sign and submit the AccountSet transaction to end a global freeze ---------
|
||||
console.log('Signing and submitting the transaction:', accountSetTx2)
|
||||
const result = await client.submitAndWait(accountSetTx2, { wallet: wallet })
|
||||
console.log("Finished submitting!")
|
||||
|
||||
// Global freeze disabled
|
||||
console.log("Disconnecting")
|
||||
await client.disconnect()
|
||||
|
||||
// End main()
|
||||
}
|
||||
|
||||
main().catch(console.error)
|
||||
133
_code-samples/freeze/js/set-individual-freeze.js
Normal file
133
_code-samples/freeze/js/set-individual-freeze.js
Normal file
@@ -0,0 +1,133 @@
|
||||
// Dependencies for Node.js.
|
||||
// In browsers, use <script> tags as in the example demo.html.
|
||||
if (typeof module !== "undefined") {
|
||||
// Use var here because const/let are block-scoped to the if statement.
|
||||
var xrpl = require('xrpl')
|
||||
}
|
||||
|
||||
async function main() {
|
||||
// Connect -------------------------------------------------------------------
|
||||
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233')
|
||||
await client.connect()
|
||||
|
||||
// Get credentials from the Testnet Faucet -----------------------------------
|
||||
console.log("Requesting an address from the Testnet faucet...")
|
||||
const { wallet, balance } = await client.fundWallet()
|
||||
|
||||
// Set up an incoming trust line so we have something to freeze --------------
|
||||
// We use a hard-coded example wallet here so the sample code can have a hard-
|
||||
// coded example address below. Don't use this seed for anything real.
|
||||
const peer = xrpl.Wallet.fromSeed("sho56WRm8fKLrfixaWa9cxhYJg8hc")
|
||||
await client.fundWallet(peer)
|
||||
const submitted = await client.submitAndWait({
|
||||
"TransactionType": "TrustSet",
|
||||
"Account": peer.address,
|
||||
"LimitAmount": {
|
||||
"currency": 'FOO',
|
||||
"issuer": wallet.address,
|
||||
"value": "123456.789" // arbitrary limit
|
||||
}
|
||||
}, {wallet: peer})
|
||||
console.log("Set up incoming trust line result:", submitted)
|
||||
|
||||
// Look up current trust lines -----------------------------------------------
|
||||
const issuing_address = wallet.address
|
||||
const address_to_freeze = 'rhPuJPcd9kcSRCGHWudV3tjUuTvvysi6sv'
|
||||
const currency_to_freeze = 'FOO'
|
||||
|
||||
console.log('Looking up', currency_to_freeze, 'trust line from',
|
||||
issuing_address, 'to', address_to_freeze)
|
||||
const account_lines = await client.request({
|
||||
"command": "account_lines",
|
||||
"account": issuing_address,
|
||||
"peer": address_to_freeze,
|
||||
"ledger_index": "validated"
|
||||
})
|
||||
const trustlines = account_lines.result.lines
|
||||
console.log("Found lines:", trustlines)
|
||||
|
||||
// Find the trust line for our currency_to_freeze ----------------------------
|
||||
let trustline = null
|
||||
for (let i = 0; i < trustlines.length; i++) {
|
||||
if(trustlines[i].currency === currency_to_freeze) {
|
||||
trustline = trustlines[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (trustline === null) {
|
||||
console.error(`Couldn't find a ${currency_to_freeze} trustline between
|
||||
${issuing_address} and ${address_to_freeze}`)
|
||||
return
|
||||
}
|
||||
|
||||
// Send a TrustSet transaction to set an individual freeze -------------------
|
||||
// Construct a TrustSet, preserving our existing limit value
|
||||
const trust_set = {
|
||||
"TransactionType": 'TrustSet',
|
||||
"Account": issuing_address,
|
||||
"LimitAmount": {
|
||||
"value": trustline.limit,
|
||||
"currency": trustline.currency,
|
||||
"issuer": trustline.account
|
||||
},
|
||||
"Flags": xrpl.TrustSetFlags.tfSetFreeze
|
||||
}
|
||||
|
||||
// Best practice for JavaScript users: use validate(tx_json) to confirm
|
||||
// that a transaction is well-formed or throw ValidationError if it isn't.
|
||||
xrpl.validate(trust_set)
|
||||
|
||||
console.log('Submitting TrustSet tx:', trust_set)
|
||||
const result = await client.submitAndWait(trust_set, { wallet: wallet })
|
||||
console.log("Transaction result:", result)
|
||||
|
||||
// Confirm trust line status -------------------------------------------------
|
||||
const account_lines_2 = await client.request({
|
||||
"command": "account_lines",
|
||||
"account": issuing_address,
|
||||
"peer": address_to_freeze,
|
||||
"ledger_index": "validated"
|
||||
})
|
||||
const trustlines_2 = account_lines_2.result.lines
|
||||
|
||||
let line = null
|
||||
for (let i = 0; i < trustlines_2.length; i++) {
|
||||
if(trustlines_2[i].currency === currency_to_freeze) {
|
||||
line = trustlines_2[i]
|
||||
console.log(`Status of ${currency_to_freeze} line between
|
||||
${issuing_address} and ${address_to_freeze}:
|
||||
${JSON.stringify(line, null, 2)}`)
|
||||
if (line.freeze === true) {
|
||||
console.log(`✅ Line is frozen.`)
|
||||
} else {
|
||||
console.error(`❌ Line is NOT FROZEN.`)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (line === null) {
|
||||
console.error(`Couldn't find a ${CURRENCY_TO_FREEZE} line between
|
||||
${issuing_address} and ${address_to_freeze}.`)
|
||||
}
|
||||
|
||||
// Investigate ---------------------------------------------------------------
|
||||
console.log(`You would investigate whatever prompted you to freeze the
|
||||
trust line now... (This script waits 5000ms to end the freeze.)`)
|
||||
await new Promise(resolve => setTimeout(resolve, 5000))
|
||||
|
||||
// Clear the individual freeze -----------------------------------------------
|
||||
// We're reusing our TrustSet transaction from earlier with a different flag.
|
||||
trust_set.Flags = xrpl.TrustSetFlags.tfClearFreeze
|
||||
|
||||
// Submit a TrustSet transaction to clear an individual freeze ---------------
|
||||
console.log('Submitting TrustSet tx:', trust_set)
|
||||
const result2 = await client.submitAndWait(trust_set, { wallet: wallet })
|
||||
console.log("Transaction result:", result2)
|
||||
|
||||
console.log("Finished submitting. Now disconnecting.")
|
||||
await client.disconnect()
|
||||
|
||||
// End main()
|
||||
}
|
||||
|
||||
main().catch(console.error)
|
||||
39
_code-samples/freeze/js/set-no-freeze.js
Normal file
39
_code-samples/freeze/js/set-no-freeze.js
Normal file
@@ -0,0 +1,39 @@
|
||||
// Dependencies for Node.js.
|
||||
// In browsers, use <script> tags as in the example demo.html.
|
||||
if (typeof module !== "undefined") {
|
||||
// Use var here because const/let are block-scoped to the if statement.
|
||||
var xrpl = require('xrpl')
|
||||
}
|
||||
|
||||
async function main() {
|
||||
// Connect -------------------------------------------------------------------
|
||||
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233')
|
||||
await client.connect()
|
||||
console.log("Connected to Testnet")
|
||||
|
||||
// Get credentials from the Testnet Faucet ------------------------------------
|
||||
console.log("Requesting an address from the Testnet faucet...")
|
||||
const { wallet, balance } = await client.fundWallet()
|
||||
|
||||
// Submit an AccountSet transaction to enable No Freeze ----------------------
|
||||
const accountSetTx = {
|
||||
TransactionType: "AccountSet",
|
||||
Account: wallet.address,
|
||||
// Set the NoFreeze flag for this account
|
||||
SetFlag: xrpl.AccountSetAsfFlags.asfNoFreeze
|
||||
}
|
||||
|
||||
// Best practice for JS users - validate checks if a transaction is well-formed
|
||||
xrpl.validate(accountSetTx)
|
||||
|
||||
console.log('Sign and submit the transaction:', accountSetTx)
|
||||
await client.submitAndWait(accountSetTx, { wallet: wallet })
|
||||
|
||||
// Done submitting
|
||||
console.log("Finished submitting. Now disconnecting.")
|
||||
await client.disconnect()
|
||||
|
||||
// End main()
|
||||
}
|
||||
|
||||
main().catch(console.error)
|
||||
Reference in New Issue
Block a user