mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-20 11:45:50 +00:00
DEX trade code: interpret metadata
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<title>Code Sample - Trade in the Decentralized Exchange</title>
|
<title>Code Sample - Trade in the Decentralized Exchange</title>
|
||||||
<script src="https://unpkg.com/xrpl@2.0.0/build/xrpl-latest-min.js"></script>
|
<script src="https://unpkg.com/xrpl@2.0.0/build/xrpl-latest-min.js"></script>
|
||||||
|
<script src='https://cdn.jsdelivr.net/npm/bignumber.js@9.0.2/bignumber.min.js'></script>
|
||||||
<script type="application/javascript" src="trade-in-the-dex.js"></script>
|
<script type="application/javascript" src="trade-in-the-dex.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>Open your browser's console (F12) to see the logs.</body>
|
<body>Open your browser's console (F12) to see the logs.</body>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"xrpl": "^2.1.0"
|
"xrpl": "^2.1.0",
|
||||||
|
"bignumber.js": "^9.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
if (typeof module !== "undefined") {
|
if (typeof module !== "undefined") {
|
||||||
// Use var here because const/let are block-scoped to the if statement.
|
// Use var here because const/let are block-scoped to the if statement.
|
||||||
var xrpl = require('xrpl')
|
var xrpl = require('xrpl')
|
||||||
|
var BigNumber = require('bignumber.js')
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect ---------------------------------------------------------------------
|
// Connect ---------------------------------------------------------------------
|
||||||
@@ -15,10 +16,10 @@ async function main() {
|
|||||||
await client.connect()
|
await client.connect()
|
||||||
|
|
||||||
// Get credentials from the Testnet Faucet -----------------------------------
|
// Get credentials from the Testnet Faucet -----------------------------------
|
||||||
// console.log("Requesting address from the Testnet faucet...")
|
console.log("Requesting address from the Testnet faucet...")
|
||||||
// const wallet = (await client.fundWallet()).wallet
|
const wallet = (await client.fundWallet()).wallet
|
||||||
// console.log(`Got address ${wallet.address}.`)
|
console.log(`Got address ${wallet.address}.`)
|
||||||
const wallet = xrpl.Wallet.fromSeed("SEED VALUE HERE") // temp for testing: don't fund every time
|
//const wallet = xrpl.Wallet.fromSeed("SEED VALUE HERE") // temp for testing: don't fund every time
|
||||||
|
|
||||||
// Define the proposed trade.
|
// Define the proposed trade.
|
||||||
const we_want = {
|
const we_want = {
|
||||||
@@ -59,7 +60,7 @@ async function main() {
|
|||||||
console.log(`No Offers in the matching book.
|
console.log(`No Offers in the matching book.
|
||||||
Offer probably won't execute immediately.`)
|
Offer probably won't execute immediately.`)
|
||||||
} else {
|
} else {
|
||||||
for (o of offers) {
|
for (const o of offers) {
|
||||||
if (o.quality <= proposed_quality) {
|
if (o.quality <= proposed_quality) {
|
||||||
console.log(`Matching Offer found, funded with ${o.owner_funds}`)
|
console.log(`Matching Offer found, funded with ${o.owner_funds}`)
|
||||||
running_total += Number(o.owner_funds)
|
running_total += Number(o.owner_funds)
|
||||||
@@ -109,7 +110,7 @@ async function main() {
|
|||||||
if (!offers2) {
|
if (!offers2) {
|
||||||
console.log(`No similar Offers in the book. Ours would be the first.`)
|
console.log(`No similar Offers in the book. Ours would be the first.`)
|
||||||
} else {
|
} else {
|
||||||
for (o of offers2) {
|
for (const o of offers2) {
|
||||||
if (o.quality <= offered_quality) {
|
if (o.quality <= offered_quality) {
|
||||||
console.log(`Existing offer found, funded with ${o.owner_funds}`)
|
console.log(`Existing offer found, funded with ${o.owner_funds}`)
|
||||||
running_total2 += Number(o.owner_funds)
|
running_total2 += Number(o.owner_funds)
|
||||||
@@ -153,6 +154,79 @@ async function main() {
|
|||||||
throw `Error sending transaction: ${result}`
|
throw `Error sending transaction: ${result}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Interpret metadata --------------------------------------------------------
|
||||||
|
for (const affnode of result.result.meta.AffectedNodes) {
|
||||||
|
if (affnode.hasOwnProperty("ModifiedNode")) {
|
||||||
|
const mn = affnode.ModifiedNode
|
||||||
|
if (mn.LedgerEntryType == "AccountRoot") {
|
||||||
|
if (mn.FinalFields.Account == wallet.address) {
|
||||||
|
// Found our account in the metadata
|
||||||
|
const final_xrp_bal = mn.FinalFields.Balance
|
||||||
|
const prev_xrp_bal = mn.PreviousFields.Balance
|
||||||
|
if (prev_xrp_bal === undefined) {
|
||||||
|
console.log(`Total XRP balance unchanged. This usually means the
|
||||||
|
transaction acquired exactly enough XRP to offset the XRP burned
|
||||||
|
as a transaction cost. Balance = ${xrpl.dropsToXrp(final_xrp_bal)} XRP`)
|
||||||
|
} else {
|
||||||
|
const diff_drops = BigNumber(final_xrp_bal) - BigNumber(prev_xrp_bal)
|
||||||
|
console.log(`Changed XRP balance by ${xrpl.dropsToXrp(diff_drops)}
|
||||||
|
to a new value of ${xrpl.dropsToXrp(final_xrp_bal)} XRP. (This
|
||||||
|
includes the net effect of burning XRP as a transaction cost.)`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (mn.LedgerEntryType == "RippleState") {
|
||||||
|
if (mn.FinalFields.HighLimit.issuer == wallet.address ||
|
||||||
|
mn.FinalFields.LowLimit.issuer == wallet.address) {
|
||||||
|
// Modified a trust line we already had
|
||||||
|
const we_are_low = (mn.FinalFields.LowLimit.issuer == wallet.address)
|
||||||
|
const currency = mn.FinalFields.Balance.currency
|
||||||
|
let counterparty
|
||||||
|
if (we_are_low) {
|
||||||
|
counterparty = mn.FinalFields.HighLimit.issuer
|
||||||
|
} else {
|
||||||
|
counterparty = mn.FinalFields.LowLimit.issuer
|
||||||
|
}
|
||||||
|
const final_token_bal = mn.FinalFields.Balance.value
|
||||||
|
const prev_token_bal = mn.PreviousFields.Balance.value
|
||||||
|
if (prev_token_bal === undefined) {
|
||||||
|
console.log(`Balance of ${currency}.${counterparty} unchanged.`)
|
||||||
|
} else {
|
||||||
|
let final_bal = BigNumber(final_token_bal)
|
||||||
|
let diff_tokens = final_bal - BigNumber(prev_token_bal)
|
||||||
|
if (!we_are_low) {
|
||||||
|
// RippleState objects store the balance from the low account's
|
||||||
|
// perspective, so if we are the high account, we negate it to get
|
||||||
|
// the balance from our perspective instead.
|
||||||
|
final_bal = -final_bal
|
||||||
|
diff_tokens = -diff_tokens
|
||||||
|
}
|
||||||
|
console.log(`Balance of ${currency}.${counterparty} changed by
|
||||||
|
${diff_tokens} to a final value of ${final_bal}.`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (affnode.hasOwnProperty("CreatedNode")) {
|
||||||
|
const cn = affnode.CreatedNode
|
||||||
|
if (cn.LedgerEntryType == "Offer") {
|
||||||
|
console.log(`Created Offer with TakerPays=${cn.NewFields.TakerPays}
|
||||||
|
and TakerGets=${cn.NewFields.TakerGets}.`)
|
||||||
|
} else if (cn.LedgerEntryType == "RippleState") {
|
||||||
|
if (cn.NewFields.HighLimit.issuer == wallet.address ||
|
||||||
|
cn.NewFields.LowLimit.issuer == wallet.address) {
|
||||||
|
const we_are_low = (cn.NewFields.LowLimit.issuer == wallet.address)
|
||||||
|
let balance = cn.NewFields.Balance.value
|
||||||
|
if (!we_are_low) {
|
||||||
|
balance = -balance
|
||||||
|
}
|
||||||
|
console.log(`Created ${cn.NewFields.Balance.currency} trust line
|
||||||
|
between ${cn.NewFields.LowLimit.issuer} and
|
||||||
|
${cn.NewFields.HighLimit.issuer} with starting balance
|
||||||
|
${balance}.`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check balances ------------------------------------------------------------
|
// Check balances ------------------------------------------------------------
|
||||||
console.log("Getting address balances...")
|
console.log("Getting address balances...")
|
||||||
const balances = await client.request({
|
const balances = await client.request({
|
||||||
|
|||||||
Reference in New Issue
Block a user