mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-20 11:45:50 +00:00
Issue a token: working code
- Fix bugs in submit and verify helper function - Tweak tutorial and placement of helper function - Finish drafting sample code - Put sample code into tutorial
This commit is contained in:
12
content/_code-samples/issue-a-token/demo.html
Normal file
12
content/_code-samples/issue-a-token/demo.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Code Sample - Issue a Token</title>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous"></script>
|
||||
<script src="https://unpkg.com/ripple-lib@1.9.8/build/ripple-latest-min.js"></script>
|
||||
<script type="application/javascript" src="../submit-and-verify/submit-and-verify.js"></script>
|
||||
<script type="application/javascript" src="issue-a-token.js"></script>
|
||||
</head>
|
||||
<body>Open your browser's console (F12) to see the logs.</body>
|
||||
</html>
|
||||
@@ -1,12 +1,7 @@
|
||||
// Example credentials
|
||||
let hot_address = "rMCcNuTcajgw7YTgBy1sys3b89QqjUrMpH"
|
||||
let hot_secret = "sn3nxiW7v8KXzPzAqzyHXbSSKNuN9"
|
||||
let cold_address = ""
|
||||
let cold_secret = ""
|
||||
|
||||
// Connect ---------------------------------------------------------------------
|
||||
// ripple = require('ripple-lib') // For Node.js. In browsers, use <script>.
|
||||
api = new ripple.RippleAPI({server: 'wss://s.altnet.rippletest.net:51233'})
|
||||
console.log("Connecting to Testnet...")
|
||||
api.connect()
|
||||
api.on('connected', async () => {
|
||||
|
||||
@@ -30,13 +25,14 @@ api.on('connected', async () => {
|
||||
return data
|
||||
}
|
||||
|
||||
console.log("Requesting addresses from the Testnet faucet...")
|
||||
const hot_data = await get_faucet_address()
|
||||
hot_address = hot_data.account.address
|
||||
hot_secret = hot_data.account.secret
|
||||
const hot_address = hot_data.account.address
|
||||
const hot_secret = hot_data.account.secret
|
||||
|
||||
const cold_data = await get_faucet_address()
|
||||
cold_address = cold_data.account.address
|
||||
cold_secret = cold_data.account.secret
|
||||
const cold_address = cold_data.account.address
|
||||
const cold_secret = cold_data.account.secret
|
||||
|
||||
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
|
||||
@@ -45,31 +41,120 @@ api.on('connected', async () => {
|
||||
// the faucet.
|
||||
while (true) {
|
||||
try {
|
||||
await api.request("account_info", {account: address, ledger_index: "validated"})
|
||||
await api.request("account_info", {account: cold_address, ledger_index: "validated"})
|
||||
await api.request("account_info", {account: hot_address, 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_address} and cold address ${cold_address}.`)
|
||||
|
||||
// Prepare AccountSet transaction for the issuer (cold address)
|
||||
// Configure issuer (cold address) settings ----------------------------------
|
||||
const cold_settings_tx = {
|
||||
"TransactionType": "AccountSet",
|
||||
"Account": cold_address,
|
||||
"TransferFee": 0,
|
||||
"TransferRate": 0,
|
||||
"TickSize": 5,
|
||||
"SetFlag": 8 // enable Default Ripple
|
||||
//"Flags": (api.txFlags.AccountSet.DisallowXRP |
|
||||
// api.txFlags.AccountSet.RequireDestTag)
|
||||
}
|
||||
|
||||
const prepared_cst = await api.prepareTransaction(cold_settings_tx, {maxLedgerVersionOffset: 10})
|
||||
|
||||
|
||||
const cst_prepared = await api.prepareTransaction(cold_settings_tx, {maxLedgerVersionOffset: 10})
|
||||
const cst_signed = api.sign(cst_prepared.txJSON, cold_secret)
|
||||
// submit_and_verify helper function from:
|
||||
// https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/submit-and-verify/
|
||||
console.log("Sending cold address AccountSet transaction...")
|
||||
const cst_result = await submit_and_verify(api, cst_signed.signedTransaction)
|
||||
if (cst_result == "tesSUCCESS") {
|
||||
console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${cst_signed.id}`)
|
||||
} else {
|
||||
throw `Error sending transaction: ${cst_result}`
|
||||
}
|
||||
|
||||
|
||||
// Configure hot address settings --------------------------------------------
|
||||
|
||||
const hot_settings_tx = {
|
||||
"TransactionType": "AccountSet",
|
||||
"Account": hot_address,
|
||||
"SetFlag": 2 // enable Require Auth so we can't use trust lines that users
|
||||
// make to the hot address, even by accident.
|
||||
//"Flags": (api.txFlags.AccountSet.DisallowXRP |
|
||||
// api.txFlags.AccountSet.RequireDestTag)
|
||||
}
|
||||
|
||||
const hst_prepared = await api.prepareTransaction(hot_settings_tx, {maxLedgerVersionOffset: 10})
|
||||
const hst_signed = api.sign(hst_prepared.txJSON, hot_secret)
|
||||
console.log("Sending hot address AccountSet transaction...")
|
||||
const hst_result = await submit_and_verify(api, hst_signed.signedTransaction)
|
||||
if (hst_result == "tesSUCCESS") {
|
||||
console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${hst_signed.id}`)
|
||||
} else {
|
||||
throw `Error sending transaction: ${hst_result}`
|
||||
}
|
||||
|
||||
|
||||
// Create trust line from hot to cold address --------------------------------
|
||||
const currency_code = "FOO"
|
||||
const trust_set_tx = {
|
||||
"TransactionType": "TrustSet",
|
||||
"Account": hot_address,
|
||||
"LimitAmount": {
|
||||
"currency": currency_code,
|
||||
"issuer": cold_address,
|
||||
"value": "10000000000" // Large limit, arbitrarily chosen
|
||||
}
|
||||
}
|
||||
|
||||
const ts_prepared = await api.prepareTransaction(trust_set_tx, {maxLedgerVersionOffset: 10})
|
||||
const ts_signed = api.sign(ts_prepared.txJSON, hot_secret)
|
||||
console.log("Creating trust line from hot address to issuer...")
|
||||
const ts_result = await submit_and_verify(api, ts_signed.signedTransaction)
|
||||
if (ts_result == "tesSUCCESS") {
|
||||
console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${ts_signed.id}`)
|
||||
} else {
|
||||
throw `Error sending transaction: ${ts_result}`
|
||||
}
|
||||
|
||||
|
||||
// Send token ----------------------------------------------------------------
|
||||
const issue_quantity = "3840"
|
||||
const send_token_tx = {
|
||||
"TransactionType": "Payment",
|
||||
"Account": cold_address,
|
||||
"Amount": {
|
||||
"currency": currency_code,
|
||||
"value": issue_quantity,
|
||||
"issuer": cold_address
|
||||
},
|
||||
"Destination": hot_address
|
||||
}
|
||||
|
||||
const payment_prepared = await api.prepareTransaction(send_token_tx, {maxLedgerVersionOffset: 10})
|
||||
const payment_signed = api.sign(payment_prepared.txJSON, cold_secret)
|
||||
// submit_and_verify helper from _code-samples/submit-and-verify
|
||||
console.log(`Sending ${issue_quantity} ${currency_code} to ${hot_address}...`)
|
||||
const payment_result = await submit_and_verify(api, payment_signed.signedTransaction)
|
||||
if (payment_result == "tesSUCCESS") {
|
||||
console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${payment_signed.id}`)
|
||||
} else {
|
||||
throw `Error sending transaction: ${payment_result}`
|
||||
}
|
||||
|
||||
// Check balances ------------------------------------------------------------
|
||||
console.log("Getting hot address balances...")
|
||||
const hot_balances = await api.request("account_lines", {account: hot_address, ledger_index: "validated"})
|
||||
console.log(hot_balances)
|
||||
|
||||
console.log("Getting cold address balances...")
|
||||
const cold_balances = await api.request("gateway_balances", {
|
||||
account: cold_address,
|
||||
ledger_index: "validated",
|
||||
hotwallet: [hot_address]
|
||||
})
|
||||
console.log(cold_balances)
|
||||
|
||||
}) // End of api.on.('connected')
|
||||
|
||||
Reference in New Issue
Block a user