lib/order/structure/maker/withMakerTxns.js

  1. /*
  2. * Copyright (C) 2021-2022 Algodex VASP (BVI) Corp.
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. const getMakerTxns = require('./getMakerTxns');
  9. /**
  10. * # 🔗 withMakerTxns
  11. * Accepts an [Order]{@link Order} with execution type [Maker]{@tutorial Maker} and generates the relevant transactions.
  12. *
  13. *
  14. * ### Existing Escrow (Only relevant for [makePlaceAlgo]{@link module:txns/buy.makePlaceAlgo} )
  15. * When a user places a maker order, we first check to see if the [Algodex Orderbook]{@tutorial Orderbook} already has an Order entry at that price, from that User.
  16. * * If that order exists, we generate a set of transactions that adds to the existing order.
  17. * * If the order does not exist, we generate a set of transactions to create a new maker order.
  18. *
  19. * You can learn more about the differences between the two types of transactions in the trasnaction table for [makePlaceAlgoTxns]{@link module:txns/buy.makePlaceAlgoTxns} factory.
  20. *
  21. * ### When is it used?
  22. * This method and the corresponding Factory is used anytime a Maker order is added to the [Algodex Orderbook]{@tutorial Orderbook}.
  23. *
  24. * This method is the maker leg of [getMakerTakerTxns]{@link module:order/structures.getMakerTakerTxns}.
  25. *
  26. * This method would be ideal for use in algorithmic trading strategies and for programmaticaly providing liquidity to the [Algodex Orderbook]{@tutorial Orderbook}
  27. *
  28. *
  29. *
  30. *
  31. * @example
  32. * const [AlgodexAPI]{@link AlgodexApi} = require(@algodex/algodex-sdk)
  33. * const api = new [AlgodexAPI]{@link AlgodexApi}(require('../config.json'))
  34. * const order = {
  35. * "client": api.algod,
  36. * "indexer": api.indexer,
  37. * "asset": {
  38. * "id": 15322902,
  39. * "decimals": 6,
  40. * },
  41. * "address": "TJFFNUYWHPPIYDE4DGGYPGHWKGAPJEWP3DGE5THZS3B2M2XIAPQ2WY3X4I",
  42. * "price": 2.22,
  43. * "amount": 1,
  44. * "total": 2,
  45. * "execution": "maker",
  46. * "type": "buy",
  47. * "appId": 22045503,
  48. * "version": 6
  49. * }
  50. * const {[compile]{@link module:order/compile}} = require(@algodex/algodex-sdk/order)
  51. *
  52. * //order.execution === 'maker'
  53. * let res = await withMakerTxns(api, await [compile]{@link module:order/compile}(order))
  54. * console.log(res.contract.txns)
  55. * //Outputs:
  56. * [makePlaceAlgoTxns]{@link module:txns/buy.makePlaceAlgoTxns} || [makePlaceAssetTxns]{@link module:txns/sell.makePlaceAssetTxns}
  57. *
  58. * @param {Api} api Instance of AlgodexApi
  59. * @param {Order} order The User's Order
  60. * @return {Promise<Order>}
  61. * @memberOf module:order/structure
  62. * @see [getMakerTxns]{@link getMakerTxns} || [makePlaceAlgoTxns]{@link module:txns/buy.makePlaceAlgoTxns} || [makePlaceAssetTxns]{@link module:txns/sell.makePlaceAssetTxns}
  63. */
  64. async function withMakerTxns(api, order) {
  65. const isExistingEscrow = await api.getIsExistingEscrow(order);
  66. if (isExistingEscrow) {
  67. order.contract = {
  68. ...order.contract,
  69. creator: order.address,
  70. };
  71. }
  72. return {
  73. ...order,
  74. contract: {
  75. ...order.contract,
  76. txns: await getMakerTxns(order, isExistingEscrow),
  77. creator: order.address,
  78. },
  79. };
  80. }
  81. module.exports = withMakerTxns;
  82. JAVASCRIPT
    Copied!