Trade in the DEX: edits per review

This commit is contained in:
mDuo13
2022-05-10 13:43:57 -07:00
parent bed6d7309d
commit 0da1f6e8b6
5 changed files with 53 additions and 36 deletions

View File

@@ -68,7 +68,8 @@ $("#look-up-offers").click( async (event) => {
for (const o of offers) {
if (o.quality <= proposed_quality) {
block.find(".output-area").append(
`<p>Matching Offer found, funded with ${o.owner_funds}</p>`)
`<p>Matching Offer found, funded with
${o.owner_funds} ${we_want.currency}</p>`)
running_total = running_total.plus(BigNumber(o.owner_funds))
if (running_total >= want_amt) {
block.find(".output-area").append("<p>Full Offer will probably fill.</p>")
@@ -81,22 +82,24 @@ $("#look-up-offers").click( async (event) => {
break
}
}
block.find(".output-area").append(`<p>Total matched: ${Math.min(running_total, want_amt)}</p>`)
block.find(".output-area").append(`<p>Total matched:
${Math.min(running_total, want_amt)} ${we_want.currency}</p>`)
if (running_total > 0 && running_total < want_amt) {
block.find(".output-area").append(`<p>Remaining ${want_amt - running_total} would probably be
placed on top of the order book.</p>`)
block.find(".output-area").append(`<p>Remaining
${want_amt - running_total} ${we_want.currency} would probably be
placed on top of the order book.</p>`)
}
}
if (running_total == 0) {
// If part of the Offer was expected to cross, then the rest would be placed
// at the top of the order book. If none did, then there might be other
// Offers going the same direction as ours already on the books with an
// equal or better rate. This code counts how much liquidity is likely to be
// above ours.
// If the Offer would be partly filled, then the rest would be placed
// at the top of the order book. If no part is filled, then there might be
// other Offers going the same direction as ours already on the books with
// an equal or better rate. This code counts how much liquidity is likely to
// be above ours.
// Unlike above, this time we check for Offers going the same direction as
// ours, so TakerGets and TakerPays are reversed from the previous
// Unlike above, this looks for Offers going the same direction as the
// proposed Offer, so TakerGets and TakerPays are reversed from the previous
// book_offers request.
let orderbook2_resp
try {
@@ -120,23 +123,30 @@ $("#look-up-offers").click( async (event) => {
const offered_quality = BigNumber(we_want.value) / BigNumber(we_spend.value)
const offers2 = orderbook2_resp.result.offers
let tally_currency = we_spend.currency
if (tally_currency == "XRP") { tally_currency = "drops of XRP" }
let running_total2 = BigNumber(0)
if (!offers2) {
block.find(".output-area").append(`<p>No similar Offers in the book. Ours would be the first.</p>`)
block.find(".output-area").append(`<p>No similar Offers in the book.
Ours would be the first.</p>`)
} else {
for (const o of offers2) {
if (o.quality <= offered_quality) {
block.find(".output-area").append(`<p>Existing offer found, funded with ${o.owner_funds}</p>`)
block.find(".output-area").append(`<p>Existing offer found, funded
with ${o.owner_funds} ${tally_currency}</p>`)
running_total2 = running_total2.plus(BigNumber(o.owner_funds))
} else {
block.find(".output-area").append(`<p>Remaining orders are below where ours would be placed.</p>`)
block.find(".output-area").append(`<p>Remaining orders are below where
ours would be placed.</p>`)
break
}
}
block.find(".output-area").append(`<p>Our Offer would be placed below at least ${running_total2} ${we_spend.currency}</p>`)
block.find(".output-area").append(`<p>Our Offer would be placed below at
least ${running_total2} ${tally_currency}</p>`)
if (running_total > 0 && running_total < want_amt) {
block.find(".output-area").append(`<p>Remaining ${want_amt - running_total} will probably be
placed on top of the order book.</p>`)
block.find(".output-area").append(`<p>Remaining
${want_amt - running_total} ${tally_currency} will probably be
placed on top of the order book.</p>`)
}
}
}

View File

@@ -1,5 +1,5 @@
# Trade in the Decentralized Exchange
This code demonstrates how to buy a (fungible) token on the XRP Ledger's decentralized exchange (DEX). For a detailed explanation of how to trade using the DEX, see <https://xrpl.org/trade-in-the-decentralized-exchange.html>.
This code demonstrates how to buy a fungible token on the XRP Ledger's decentralized exchange (DEX). For a detailed explanation of how to trade using the DEX, see <https://xrpl.org/trade-in-the-decentralized-exchange.html>.
The code is designed to run in-browser by loading `demo.html` and watching the console output, or in Node.js. For Node.js, you must first install the dependencies using your preferred package manager (such as `yarn` or `npm`).
The code is designed to run in a browser by loading `demo.html` and watching the console output, or in Node.js. For Node.js, you must first install the dependencies using your preferred package manager (such as `yarn` or `npm`).

View File

@@ -1,4 +1,4 @@
// In browsers, add the following <script> tags the HTML to load dependencies
// In browsers, add the following <script> tags to the HTML to load dependencies
// instead of using require():
// <script src="https://unpkg.com/xrpl@2.2.0/build/xrpl-latest-min.js"></script>
// <script src='https://cdn.jsdelivr.net/npm/bignumber.js@9.0.2/bignumber.min.js'></script>

View File

@@ -34,7 +34,8 @@ async function main() {
}
const we_spend = {
currency: "XRP",
value: xrpl.xrpToDrops(25*10*1.15) // 25 TST * 10 XRP per TST * 15% fx cost
// 25 TST * 10 XRP per TST * 15% financial exchange (FX) cost
value: xrpl.xrpToDrops(25*10*1.15)
}
// "Quality" is defined as TakerPays ÷ TakerGets. The lower the "quality"
// number, the better the proposed exchange rate is for the taker.
@@ -57,10 +58,9 @@ async function main() {
// If so, how much of it? (Partial execution is possible)
// If not, how much liquidity is above it? (How deep in the order book would
// other Offers have to go before ours would get taken?)
// Note: these estimates can be thrown off by rounding if the token issuer
// Note: These estimates can be thrown off by rounding if the token issuer
// uses a TickSize setting other than the default (15). In that case, you
// can increase the TakerGets amount of your final offer just a little bit to
// compensate.
// can increase the TakerGets amount of your final Offer to compensate.
const offers = orderbook_resp.result.offers
const want_amt = BigNumber(we_want.value)
@@ -71,7 +71,8 @@ async function main() {
} else {
for (const o of offers) {
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}
${we_want.currency}`)
running_total = running_total.plus(BigNumber(o.owner_funds))
if (running_total >= want_amt) {
console.log("Full Offer will probably fill")
@@ -84,10 +85,11 @@ async function main() {
break
}
}
console.log(`Total matched: ${Math.min(running_total, want_amt)}`)
console.log(`Total matched:
${Math.min(running_total, want_amt)} ${we_want.currency}`)
if (running_total > 0 && running_total < want_amt) {
console.log(`Remaining ${want_amt - running_total} would probably be
placed on top of the order book.`)
console.log(`Remaining ${want_amt - running_total} ${we_want.currency}
would probably be placed on top of the order book.`)
}
}
@@ -115,23 +117,27 @@ async function main() {
const offered_quality = BigNumber(we_want.value) / BigNumber(we_spend.value)
const offers2 = orderbook2_resp.result.offers
let tally_currency = we_spend.currency
if (tally_currency == "XRP") { tally_currency = "drops of XRP" }
let running_total2 = 0
if (!offers2) {
console.log(`No similar Offers in the book. Ours would be the first.`)
} else {
for (const o of offers2) {
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} ${tally_currency}`)
running_total2 = running_total2.plus(BigNumber(o.owner_funds))
} else {
console.log(`Remaining orders are below where ours would be placed.`)
break
}
}
console.log(`Our Offer would be placed below at least ${running_total2} ${we_spend.currency}`)
console.log(`Our Offer would be placed below at least
${running_total2} ${tally_currency}`)
if (running_total > 0 && running_total < want_amt) {
console.log(`Remaining ${want_amt - running_total} will probably be
placed on top of the order book.`)
console.log(`Remaining ${want_amt - running_total} ${tally_currency}
will probably be placed on top of the order book.`)
}
}
}
@@ -155,7 +161,8 @@ async function main() {
console.log("Sending OfferCreate transaction...")
const result = await client.submitAndWait(signed.tx_blob)
if (result.result.meta.TransactionResult == "tesSUCCESS") {
console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${signed.hash}`)
console.log(`Transaction succeeded:
https://testnet.xrpl.org/transactions/${signed.hash}`)
} else {
throw `Error sending transaction: ${result}`
}
@@ -164,7 +171,7 @@ async function main() {
// In JavaScript, you can use getBalanceChanges() to help summarize all the
// balance changes caused by a transaction.
const balance_changes = xrpl.getBalanceChanges(result.result.meta)
console.log("Total balance changes:", JSON.stringify(balance_changes, null, 2))
console.log("Total balance changes:", JSON.stringify(balance_changes, null,2))
// Helper to convert an XRPL amount to a string for display
function amt_str(amt) {

View File

@@ -30,7 +30,7 @@ This tutorial demonstrates how to buy a fungible token in the decentralized exch
| Currency Code | Issuer | Notes |
|---|---|---|
| TST | rP9jPyP5kyvFRb6ZiRghAGw5u8SGAmU4bd | A test token pegged to XRP at a rate of approximately 10 XRP per 1 TST. The issuer has existing Offers on the XRP Ledger Testnet to buy and sell these tokens for |
| TST | rP9jPyP5kyvFRb6ZiRghAGw5u8SGAmU4bd | A test token pegged to XRP at a rate of approximately 10 XRP per 1 TST. The issuer has existing Offers on the XRP Ledger Testnet to buy and sell these tokens. |
### {{n.next()}}. Connect to Network
@@ -57,7 +57,7 @@ To transact on the XRP Ledger, you need an address, a secret key, and some XRP.
{% include '_snippets/interactive-tutorials/generate-step.md' %}
When you're [building production-ready software](production-readiness.html), you should use an existing account, and manage your keys using a [secure signing configuration](set-up-secure-signing.html). The following code shows how to instantiate a class with the wallet instance:
When you're [building production-ready software](production-readiness.html), you should use an existing account, and manage your keys using a [secure signing configuration](set-up-secure-signing.html). The following code shows how to create a Wallet instance to use your keys:
<!-- MULTICODE_BLOCK_START -->