mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-20 03:35:51 +00:00
code updates for xrpl.js 2.0 beta 5
This commit is contained in:
@@ -6,7 +6,6 @@
|
||||
if (typeof module !== "undefined") {
|
||||
// gotta use var here because const/let are block-scoped to the if statement.
|
||||
var xrpl = require('xrpl')
|
||||
var submit_and_verify = require('../../submit-and-verify/submit-and-verify2.js').submit_and_verify
|
||||
} else {
|
||||
// TODO: remove when webpack is fixed
|
||||
var xrpl = ripple;
|
||||
@@ -20,55 +19,28 @@ async function main() {
|
||||
|
||||
// Get credentials from the Testnet Faucet -----------------------------------
|
||||
console.log("Requesting addresses from the Testnet faucet...")
|
||||
const hot_wallet = await api.generateFaucetWallet()
|
||||
const cold_wallet = await api.generateFaucetWallet()
|
||||
|
||||
console.log("Waiting until we have a validated starting sequence number...")
|
||||
// If you go too soon, the funding transaction might slip back a ledger and
|
||||
// then your starting Sequence number will be off. This is mostly relevant
|
||||
// when you want to use a Testnet account right after getting a reply from
|
||||
// the faucet.
|
||||
while (true) {
|
||||
try {
|
||||
await api.request({
|
||||
command: "account_info",
|
||||
account: cold_wallet.classicAddress,
|
||||
ledger_index: "validated"
|
||||
})
|
||||
await api.request({
|
||||
command: "account_info",
|
||||
account: hot_wallet.classicAddress,
|
||||
ledger_index: "validated"
|
||||
})
|
||||
break
|
||||
} catch(e) {
|
||||
if (e.data.error != 'actNotFound') throw e
|
||||
await new Promise(resolve => setTimeout(resolve, 1000))
|
||||
}
|
||||
}
|
||||
console.log(`Got hot address ${hot_wallet.classicAddress} and cold address ${cold_wallet.classicAddress}.`)
|
||||
const hot_wallet = (await api.fundWallet()).wallet
|
||||
const cold_wallet = (await api.fundWallet()).wallet
|
||||
console.log(`Got hot address ${hot_wallet.address} and cold address ${cold_wallet.address}.`)
|
||||
|
||||
// Configure issuer (cold address) settings ----------------------------------
|
||||
const cold_settings_tx = {
|
||||
"TransactionType": "AccountSet",
|
||||
"Account": cold_wallet.classicAddress,
|
||||
"Account": cold_wallet.address,
|
||||
"TransferRate": 0,
|
||||
"TickSize": 5,
|
||||
"Domain": "6578616D706C652E636F6D", // "example.com"
|
||||
"SetFlag": 8 // enable Default Ripple
|
||||
//"Flags": (api.txFlags.AccountSet.DisallowXRP |
|
||||
// api.txFlags.AccountSet.RequireDestTag)
|
||||
// TODO: update to txFlags' new location?
|
||||
"SetFlag": xrpl.AccountSetAsfFlags.asfDefaultRipple
|
||||
//"Flags": (xrpl.AccountSetTfFlags.tfDisallowXRP |
|
||||
// xrpl.AccountSetTfFlags.tfRequireDestTag)
|
||||
}
|
||||
|
||||
const cst_prepared = await api.autofill(cold_settings_tx)
|
||||
const cst_signed = cold_wallet.signTransaction(cst_prepared)
|
||||
// submit_and_verify helper function from:
|
||||
// https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/submit-and-verify/
|
||||
const cst_signed = cold_wallet.sign(cst_prepared)
|
||||
console.log("Sending cold address AccountSet transaction...")
|
||||
const cst_result = await submit_and_verify(api, cst_signed)
|
||||
if (cst_result == "tesSUCCESS") {
|
||||
console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${xrpl.computeSignedTransactionHash(cst_signed)}`)
|
||||
const cst_result = await api.submitSignedReliable(cst_signed.tx_blob)
|
||||
if (cst_result.result.meta.TransactionResult == "tesSUCCESS") {
|
||||
console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${cst_signed.hash}`)
|
||||
} else {
|
||||
throw `Error sending transaction: ${cst_result}`
|
||||
}
|
||||
@@ -78,7 +50,7 @@ async function main() {
|
||||
|
||||
const hot_settings_tx = {
|
||||
"TransactionType": "AccountSet",
|
||||
"Account": hot_wallet.classicAddress,
|
||||
"Account": hot_wallet.address,
|
||||
"Domain": "6578616D706C652E636F6D", // "example.com"
|
||||
"SetFlag": 2 // enable Require Auth so we can't use trust lines that users
|
||||
// make to the hot address, even by accident.
|
||||
@@ -87,13 +59,13 @@ async function main() {
|
||||
}
|
||||
|
||||
const hst_prepared = await api.autofill(hot_settings_tx)
|
||||
const hst_signed = hot_wallet.signTransaction(hst_prepared)
|
||||
const hst_signed = hot_wallet.sign(hst_prepared)
|
||||
console.log("Sending hot address AccountSet transaction...")
|
||||
const hst_result = await submit_and_verify(api, hst_signed)
|
||||
if (hst_result == "tesSUCCESS") {
|
||||
console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${xrpl.computeSignedTransactionHash(hst_signed)}`)
|
||||
const hst_result = await api.submitSignedReliable(hst_signed.tx_blob)
|
||||
if (hst_result.result.meta.TransactionResult == "tesSUCCESS") {
|
||||
console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${hst_signed.hash}`)
|
||||
} else {
|
||||
throw `Error sending transaction: ${hst_result}`
|
||||
throw `Error sending transaction: ${hst_result.result.meta.TransactionResult}`
|
||||
}
|
||||
|
||||
|
||||
@@ -101,22 +73,22 @@ async function main() {
|
||||
const currency_code = "FOO"
|
||||
const trust_set_tx = {
|
||||
"TransactionType": "TrustSet",
|
||||
"Account": hot_wallet.classicAddress,
|
||||
"Account": hot_wallet.address,
|
||||
"LimitAmount": {
|
||||
"currency": currency_code,
|
||||
"issuer": cold_wallet.classicAddress,
|
||||
"issuer": cold_wallet.address,
|
||||
"value": "10000000000" // Large limit, arbitrarily chosen
|
||||
}
|
||||
}
|
||||
|
||||
const ts_prepared = await api.autofill(trust_set_tx)
|
||||
const ts_signed = hot_wallet.signTransaction(ts_prepared)
|
||||
const ts_signed = hot_wallet.sign(ts_prepared)
|
||||
console.log("Creating trust line from hot address to issuer...")
|
||||
const ts_result = await submit_and_verify(api, ts_signed)
|
||||
if (ts_result == "tesSUCCESS") {
|
||||
console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${xrpl.computeSignedTransactionHash(ts_signed)}`)
|
||||
const ts_result = await api.submitSignedReliable(ts_signed.tx_blob)
|
||||
if (ts_result.result.meta.TransactionResult == "tesSUCCESS") {
|
||||
console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${ts_signed.hash}`)
|
||||
} else {
|
||||
throw `Error sending transaction: ${ts_result}`
|
||||
throw `Error sending transaction: ${ts_result.result.meta.TransactionResult}`
|
||||
}
|
||||
|
||||
|
||||
@@ -124,31 +96,30 @@ async function main() {
|
||||
const issue_quantity = "3840"
|
||||
const send_token_tx = {
|
||||
"TransactionType": "Payment",
|
||||
"Account": cold_wallet.classicAddress,
|
||||
"Account": cold_wallet.address,
|
||||
"Amount": {
|
||||
"currency": currency_code,
|
||||
"value": issue_quantity,
|
||||
"issuer": cold_wallet.classicAddress
|
||||
"issuer": cold_wallet.address
|
||||
},
|
||||
"Destination": hot_wallet.classicAddress
|
||||
"Destination": hot_wallet.address
|
||||
}
|
||||
|
||||
const pay_prepared = await api.autofill(send_token_tx)
|
||||
const pay_signed = cold_wallet.signTransaction(pay_prepared)
|
||||
// submit_and_verify helper from _code-samples/submit-and-verify
|
||||
console.log(`Sending ${issue_quantity} ${currency_code} to ${hot_wallet.classicAddress}...`)
|
||||
const pay_result = await submit_and_verify(api, pay_signed)
|
||||
if (pay_result == "tesSUCCESS") {
|
||||
console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${xrpl.computeSignedTransactionHash(pay_signed)}`)
|
||||
const pay_signed = cold_wallet.sign(pay_prepared)
|
||||
console.log(`Sending ${issue_quantity} ${currency_code} to ${hot_wallet.address}...`)
|
||||
const pay_result = await api.submitSignedReliable(pay_signed.tx_blob)
|
||||
if (pay_result.result.meta.TransactionResult == "tesSUCCESS") {
|
||||
console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${pay_signed.hash}`)
|
||||
} else {
|
||||
throw `Error sending transaction: ${pay_result}`
|
||||
throw `Error sending transaction: ${pay_result.result.meta.TransactionResult}`
|
||||
}
|
||||
|
||||
// Check balances ------------------------------------------------------------
|
||||
console.log("Getting hot address balances...")
|
||||
const hot_balances = await api.request({
|
||||
command: "account_lines",
|
||||
account: hot_wallet.classicAddress,
|
||||
account: hot_wallet.address,
|
||||
ledger_index: "validated"
|
||||
})
|
||||
console.log(hot_balances.result)
|
||||
@@ -156,9 +127,9 @@ async function main() {
|
||||
console.log("Getting cold address balances...")
|
||||
const cold_balances = await api.request({
|
||||
command: "gateway_balances",
|
||||
account: cold_wallet.classicAddress,
|
||||
account: cold_wallet.address,
|
||||
ledger_index: "validated",
|
||||
hotwallet: [hot_wallet.classicAddress]
|
||||
hotwallet: [hot_wallet.address]
|
||||
})
|
||||
console.log(JSON.stringify(cold_balances.result, null, 2))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user