Incorporate MPT Use Case 2 with Use Case 1

This commit is contained in:
Dennis Dawson
2025-03-20 14:00:46 -07:00
parent a1d8cb1063
commit 09826b9035
18 changed files with 1103 additions and 5 deletions

View File

@@ -0,0 +1,103 @@
// ******************************************************
// ************* Get the Preferred Network **************
// ******************************************************
function getNet() {
let net
if (document.getElementById("tn").checked) net = "wss://s.altnet.rippletest.net:51233"
if (document.getElementById("dn").checked) net = "wss://s.devnet.rippletest.net:51233"
return net
} // End of getNet()
// *******************************************************
// ************* Get Account *****************************
// *******************************************************
async function getAccount(type) {
let net = getNet()
const client = new xrpl.Client(net)
results = 'Connecting to ' + net + '....'
// This uses the default faucet for Testnet/Devnet
let faucetHost = null
if (type == 'left') {
leftResultField.value = results
} else {
rightResultField.value = results
}
await client.connect()
results += '\nConnected, funding wallet.'
if (type == 'left') {
leftResultField.value = results
} else {
rightResultField.value = results
}
// -----------------------------------Create and fund a test account wallet
const my_wallet = (await client.fundWallet(null, { faucetHost })).wallet
results += '\nGot a wallet.'
if (type == 'left') {
leftResultField.value = results
} else {
rightResultField.value = results
}
// ------------------------------------------------------Get the current balance.
const my_balance = (await client.getXrpBalance(my_wallet.address))
if (type == 'left') {
leftAccountField.value = my_wallet.address
leftBalanceField.value = (await client.getXrpBalance(my_wallet.address))
leftSeedField.value = my_wallet.seed
results += '\nStandby account created.'
leftResultField.value = results
} else {
rightAccountField.value = my_wallet.address
rightSeedField.value = my_wallet.seed
rightBalanceField.value = (await client.getXrpBalance(my_wallet.address))
results += '\nOperational account created.'
rightResultField.value = results
}
// --------------- Capture the seeds for both accounts for ease of reload.
seeds.value = leftSeedField.value + '\n' + rightSeedField.value
client.disconnect()
} // End of getAccount()
// *******************************************************
// ********** Get Accounts from Seeds ********************
// *******************************************************
async function getAccountsFromSeeds() {
let net = getNet()
const client = new xrpl.Client(net)
results = 'Connecting to ' + getNet() + '....'
leftResultField.value = results
await client.connect()
results += '\nConnected, finding wallets.\n'
leftResultField.value = results
// -------------------------------------------------Find the test account wallets.
var lines = seeds.value.split('\n')
const left_wallet = xrpl.Wallet.fromSeed(lines[0])
const right_wallet = xrpl.Wallet.fromSeed(lines[1])
// -------------------------------------------------------Get the current balance.
const left_balance = (await client.getXrpBalance(left_wallet.address))
const right_balance = (await client.getXrpBalance(right_wallet.address))
// ----------------------Populate the fields for Standby and Operational accounts.
leftAccountField.value = left_wallet.address
leftSeedField.value = left_wallet.seed
leftBalanceField.value = (await client.getXrpBalance(left_wallet.address))
rightAccountField.value = right_wallet.address
rightSeedField.value = right_wallet.seed
rightBalanceField.value = (await client.getXrpBalance(right_wallet.address))
client.disconnect()
} // End of getAccountsFromSeeds()

View File

@@ -0,0 +1,139 @@
body {
font-family: "Inter", sans-serif;
padding: 20px;
background: #abe2ff;
}
h1 {
font-weight: bold;
}
td {
padding-left: 25px;
vertical-align: top;
}
input,
button {
padding: 6px;
margin-bottom: 8px;
border: none
}
button {
font-weight: bold;
font-family: "Work Sans", sans-serif;
background-color: #006aff;
-webkit-text-fill-color: white;
}
td {
vertical-align: middle;
}
/* The switch - the box around the slider */
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
/* Hide default HTML checkbox */
.switch input {
opacity: 0;
width: 0;
height: 0;
}
/* The slider */
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked+.slider {
background-color: #2196F3;
}
input:focus+.slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked+.slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
.tooltip {
position: relative;
border-bottom: 1px dotted black;
}
.tooltip:before {
content: attr(tooltip-data);
position: absolute;
width: 250px;
background-color: #006aff;
color: #fff;
text-align: center;
padding: 15px;
line-height: 1.1;
border-radius: 5px;
z-index: 1;
opacity: 0;
transition: opacity .5s;
bottom: 125%;
left: 50%;
margin-left: -60px;
font-size: 0.70em;
visibility: hidden;
}
.tooltip:after {
content: "";
position: absolute;
bottom: 75%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
opacity: 0;
transition: opacity .5s;
border-color: #000 transparent transparent transparent;
visibility: hidden;
}
.tooltip:hover:before,
.tooltip:hover:after {
opacity: 1;
visibility: visible;
}

Binary file not shown.

View File

@@ -0,0 +1,211 @@
<html>
<head>
<title>Send MPT Test Harness</title>
<link href='https://fonts.googleapis.com/css?family=Work Sans' rel='stylesheet'>
<link href="mpt-generator.css" rel="stylesheet">
<script src='https://unpkg.com/xrpl@4.1.0/build/xrpl-latest.js'></script>
<script src='get-accounts.js'></script>
<script src='send-mpt.js'></script>
<script>
if (typeof module !== "undefined") {
const xrpl = require('xrpl')
}
</script>
</head>
<!-- ************************************************************** -->
<!-- ********************** The Form ****************************** -->
<!-- ************************************************************** -->
<body>
<h1>Send MPTs</h1>
<form id="theForm">
Choose your ledger instance:
&nbsp;&nbsp;
<input type="radio" id="tn" name="server"
value="wss://s.altnet.rippletest.net:51233" >
<label for="testnet">Testnet</label>
&nbsp;&nbsp;
<input type="radio" id="dn" name="server"
value="wss://s.devnet.rippletest.net:51233" checked>
<label for="devnet">Devnet</label>
<br/><br/>
<button type="button" onClick="getAccountsFromSeeds()">Get Accounts From Seeds</button>
<br/>
<textarea id="seeds" cols="40" rows= "2"></textarea>
<br/><br/>
<table>
<tr valign="top">
<td>
<table>
<tr valign="top">
<td>
<td>
<button type="button" onClick="getAccount('left')">Get New Left Account</button>
<table>
<tr valign="top">
<td align="right">
Left Account
</td>
<td>
<input type="text" id="leftAccountField" size="40"></input>
<br>
</td>
</tr>
<tr>
<td align="right">
Seed
</td>
<td>
<input type="text" id="leftSeedField" size="40"></input>
<br>
</td>
</tr>
<tr>
<td align="right">
XRP Balance
</td>
<td>
<input type="text" id="leftBalanceField" size="40"></input>
<br>
</td>
</tr>
<tr>
<td align="right">
Quantity
</td>
<td>
<input type="text" id="leftQuantityField" size="40"></input>
<br>
</td>
</tr>
<tr>
<td align="right">
Destination
</td>
<td>
<input type="text" id="leftDestinationField" size="40"></input>
<br>
</td>
</tr>
<tr>
<td align="right">
MPT Issuance ID
</td>
<td>
<input type="text" id="leftIssuanceIdField" size="40"></input>
</td>
</tr>
</table>
<p align="left">
<textarea id="leftResultField" cols="80" rows="20" ></textarea>
</p>
</td>
</td>
<td>
<table>
<tr valign="top">
<td align="center" valign="top">
<button type="button" onClick="MPTAuthorize()">Authorize MPTs</button>
<br/>
<button type="button" onClick="sendMPT()">Send MPT</button>
<br/>
<button type="button" onClick="getMPTs()">Get MPTs</button>
</td>
</tr>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>
<td>
<table>
<tr>
<td align="center" valign="top">
<button type="button" onClick="rightMPTAuthorize()">Authorize MPTs</button>
<br/>
<button type="button" onClick="rightSendMPT()">Send MPT</button>
<br/>
<button type="button" onClick="rightGetMPTs()">Get MPTs</button>
</td>
<td valign="top" align="right">
<button type="button" onClick="getAccount('right')">Get New Right Account</button>
<table>
<tr valign="top">
<td align="right">
Right Account
</td>
<td>
<input type="text" id="rightAccountField" size="40"></input>
<br>
</td>
</tr>
<tr>
<td align="right">
Seed
</td>
<td>
<input type="text" id="rightSeedField" size="40"></input>
<br>
</td>
</tr>
<tr>
<td align="right">
XRP Balance
</td>
<td>
<input type="text" id="rightBalanceField" size="40"></input>
<br>
</td>
</tr>
<tr>
<td align="right">
Quantity
</td>
<td>
<input type="text" id="rightQuantityField" size="40"></input>
<br>
</td>
</tr>
<tr>
<td align="right">
Destination
</td>
<td>
<input type="text" id="rightDestinationField" size="40"></input>
<br>
</td>
</tr>
<tr>
<td align="right">
MPT Issuance ID
</td>
<td>
<input type="text" id="rightIssuanceIdField" size="40"></input>
</td>
</tr>
</table>
<p align="right">
<textarea id="rightResultField" cols="80" rows="20" ></textarea>
</p>
</td>
</td>
</tr>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>

View File

@@ -0,0 +1,235 @@
// *******************************************************
// ********************* Send MPT ************************
// *******************************************************
async function sendMPT() {
let net = getNet()
const client = new xrpl.Client(net)
results = 'Connecting to ' + getNet() + '....'
document.getElementById('leftResultField').value = results
await client.connect()
results += '\nConnected.'
leftResultField.value = results
const left_wallet = xrpl.Wallet.fromSeed(leftSeedField.value)
const mpt_issuance_id = leftIssuanceIdField.value
const mpt_quantity = leftQuantityField.value
const send_mpt_tx = {
"TransactionType": "Payment",
"Account": left_wallet.address,
"Amount": {
"mpt_issuance_id": mpt_issuance_id,
"value": mpt_quantity,
},
"Destination": leftDestinationField.value,
}
const pay_prepared = await client.autofill(send_mpt_tx)
const pay_signed = left_wallet.sign(pay_prepared)
results += `\n\nSending ${mpt_quantity} ${mpt_issuance_id} to ${leftDestinationField.value} ...`
leftResultField.value = results
const pay_result = await client.submitAndWait(pay_signed.tx_blob)
if (pay_result.result.meta.TransactionResult == "tesSUCCESS") {
results += 'Transaction succeeded.\n\n'
results += JSON.stringify(pay_result.result, null, 2)
leftResultField.value = results
} else {
results += 'Transaction failed: See JavaScript console for details.'
results += JSON.stringify(pay_result.result, null, 2)
leftResultField.value = results
}
client.disconnect()
} // end of sendMPT()
// *******************************************************
// ******************** Get MPTs *************************
// *******************************************************
async function getMPTs() {
let net = getNet()
const client = new xrpl.Client(net)
results = 'Connecting to ' + getNet() + '....'
leftResultField.value = results
await client.connect()
const left_wallet = xrpl.Wallet.fromSeed(leftSeedField.value)
results += '\nConnected.'
leftResultField.value = results
const left_mpts = await client.request({
command: "account_objects",
account: left_wallet.address,
ledger_index: "validated",
type: "mptoken"
})
let JSONString = JSON.stringify(left_mpts.result, null, 2)
let JSONParse = JSON.parse(JSONString)
let numberOfMPTs = JSONParse.account_objects.length
console.log("length: " + numberOfMPTs)
let x = 0
while (x < numberOfMPTs){
results += "\n\nMPT Issuance ID: " + JSONParse.account_objects[x].MPTokenIssuanceID
+ "\nMPT Amount: " + JSONParse.account_objects[x].MPTAmount
x++
}
results += "\n\n" + JSONString
leftResultField.value = results
client.disconnect()
} // End of getMPTs()
// **********************************************************************
// ****** MPTAuthorize Transaction ***************************************
// **********************************************************************
async function MPTAuthorize() {
let net = getNet()
const client = new xrpl.Client(net)
results = 'Connecting to ' + getNet() + '....'
document.getElementById('leftResultField').value = results
await client.connect()
const left_wallet = xrpl.Wallet.fromSeed(leftSeedField.value)
const mpt_issuance_id = leftIssuanceIdField.value
const auth_mpt_tx = {
"TransactionType": "MPTokenAuthorize",
"Account": left_wallet.address,
"MPTokenIssuanceID": mpt_issuance_id,
}
const auth_prepared = await client.autofill(auth_mpt_tx)
const auth_signed = left_wallet.sign(auth_prepared)
results += `\n\nSending authorization...`
leftResultField.value = results
const auth_result = await client.submitAndWait(auth_signed.tx_blob)
if (auth_result.result.meta.TransactionResult == "tesSUCCESS") {
results += `Transaction succeeded`
leftResultField.value = results
} else {
results += 'Transaction failed: See JavaScript console for details.'
leftResultField.value = results
}
client.disconnect()
} // end of MPTAuthorize()
// **********************************************************************
// ****** Reciprocal Transactions ***************************************
// **********************************************************************
// *******************************************************
// ************* Right Send MPT ********
// *******************************************************
async function rightSendMPT() {
let net = getNet()
const client = new xrpl.Client(net)
results = 'Connecting to ' + getNet() + '....'
rightResultField.value = results
await client.connect()
results += '\nConnected.'
rightResultField.value = results
const right_wallet = xrpl.Wallet.fromSeed(rightSeedField.value)
const mpt_issuance_id = rightIssuanceIdField.value
const mpt_quantity = rightQuantityField.value
const send_mpt_tx = {
"TransactionType": "Payment",
"Account": right_wallet.address,
"Amount": {
"mpt_issuance_id": mpt_issuance_id,
"value": mpt_quantity,
},
"Destination": rightDestinationField.value,
}
const pay_prepared = await client.autofill(send_mpt_tx)
const pay_signed = right_wallet.sign(pay_prepared)
results += `\n\nSending ${mpt_quantity} ${mpt_issuance_id} to ${rightDestinationField.value} ...`
rightResultField.value = results
const pay_result = await client.submitAndWait(pay_signed.tx_blob)
if (pay_result.result.meta.TransactionResult == "tesSUCCESS") {
results += 'Transaction succeeded.\n\n'
results += JSON.stringify(pay_result.result, null, 2)
rightResultField.value = results
} else {
results += 'Transaction failed: See JavaScript console for details.'
results += JSON.stringify(pay_result.result, null, 2)
rightResultField.value = results
}
client.disconnect()
} // end of rightSendMPT()
// **********************************************************************
// ****** MPTAuthorize Transaction ***************************************
// **********************************************************************
async function rightMPTAuthorize() {
let net = getNet()
const client = new xrpl.Client(net)
results = 'Connecting to ' + getNet() + '....'
document.getElementById('rightResultField').value = results
await client.connect()
const right_wallet = xrpl.Wallet.fromSeed(rightSeedField.value)
const mpt_issuance_id = rightIssuanceIdField.value
const auth_mpt_tx = {
"TransactionType": "MPTokenAuthorize",
"Account": right_wallet.address,
"MPTokenIssuanceID": mpt_issuance_id,
}
const auth_prepared = await client.autofill(auth_mpt_tx)
const auth_signed = right_wallet.sign(auth_prepared)
results += `\n\nSending authorization...`
rightResultField.value = results
const auth_result = await client.submitAndWait(auth_signed.tx_blob)
console.log(JSON.stringify(auth_result.result, null, 2))
if (auth_result.result.meta.TransactionResult == "tesSUCCESS") {
results += `Transaction succeeded`
rightResultField.value = results
} else {
results += 'Transaction failed: See JavaScript console for details.'
rightResultField.value = results
}
client.disconnect()
} // end of rightMPTAuthorize()
// **********************************************************************
// ****** Right Get MPTs ***************************************
// **********************************************************************
async function rightGetMPTs() {
let net = getNet()
const client = new xrpl.Client(net)
results = 'Connecting to ' + getNet() + '....'
rightResultField.value = results
await client.connect()
const right_wallet = xrpl.Wallet.fromSeed(rightSeedField.value)
results += '\nConnected.'
rightResultField.value = results
const right_mpts = await client.request({
command: "account_objects",
account: right_wallet.address,
ledger_index: "validated",
type: "mptoken"
})
let JSONString = JSON.stringify(right_mpts.result, null, 2)
let JSONParse = JSON.parse(JSONString)
let numberOfMPTs = JSONParse.account_objects.length
let x = 0
while (x < numberOfMPTs){
results += "\n\nMPT Issuance ID: " + JSONParse.account_objects[x].MPTokenIssuanceID
+ "\nMPT Amount: " + JSONParse.account_objects[x].MPTAmount
x++
}
results += "\n\n" + JSONString
rightResultField.value = results
client.disconnect()
} // End of rightGetMPTs()