lib/order/structure/maker/withMakerTxns.js

  1. const getMakerTxns = require('./getMakerTxns');
  2. /**
  3. * # 🔗 withMakerTxns
  4. * Accepts an [Order]{@link Order} with execution type [Maker]{@tutorial Maker} and generates the relevant transactions.
  5. *
  6. *
  7. * ### Existing Escrow (Only relevant for [makePlaceAlgo]{@link module:txns/buy.makePlaceAlgo} )
  8. * 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.
  9. * * If that order exists, we generate a set of transactions that adds to the existing order.
  10. * * If the order does not exist, we generate a set of transactions to create a new maker order.
  11. *
  12. * 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.
  13. *
  14. * ### When is it used?
  15. * This method and the corresponding Factory is used anytime a Maker order is added to the [Algodex Orderbook]{@tutorial Orderbook}.
  16. *
  17. * This method is the maker leg of [getMakerTakerTxns]{@link module:order/structures.getMakerTakerTxns}.
  18. *
  19. * This method would be ideal for use in algorithmic trading strategies and for programmaticaly providing liquidity to the [Algodex Orderbook]{@tutorial Orderbook}
  20. *
  21. *
  22. *
  23. *
  24. * @example
  25. * const [AlgodexAPI]{@link AlgodexApi} = require(@algodex/algodex-sdk)
  26. * const api = new [AlgodexAPI]{@link AlgodexApi}(require('../config.json'))
  27. * const order = {
  28. * "client": api.algod,
  29. * "indexer": api.indexer,
  30. * "asset": {
  31. * "id": 15322902,
  32. * "decimals": 6,
  33. * },
  34. * "address": "TJFFNUYWHPPIYDE4DGGYPGHWKGAPJEWP3DGE5THZS3B2M2XIAPQ2WY3X4I",
  35. * "price": 2.22,
  36. * "amount": 1,
  37. * "total": 2,
  38. * "execution": "maker",
  39. * "type": "buy",
  40. * "appId": 22045503,
  41. * "version": 6
  42. * }
  43. * const {[compile]{@link module:order/compile}} = require(@algodex/algodex-sdk/order)
  44. *
  45. * //order.execution === 'maker'
  46. * let res = await withMakerTxns(api, await [compile]{@link module:order/compile}(order))
  47. * console.log(res.contract.txns)
  48. * //Outputs:
  49. * [makePlaceAlgoTxns]{@link module:txns/buy.makePlaceAlgoTxns} || [makePlaceAssetTxns]{@link module:txns/sell.makePlaceAssetTxns}
  50. *
  51. * @param {Api} api Instance of AlgodexApi
  52. * @param {Order} order The User's Order
  53. * @return {Promise<Order>}
  54. * @memberOf module:order/structure
  55. * @see [getMakerTxns]{@link getMakerTxns} || [makePlaceAlgoTxns]{@link module:txns/buy.makePlaceAlgoTxns} || [makePlaceAssetTxns]{@link module:txns/sell.makePlaceAssetTxns}
  56. */
  57. async function withMakerTxns(api, order) {
  58. const isExistingEscrow = await api.getIsExistingEscrow(order);
  59. if (isExistingEscrow) {
  60. order.contract = {
  61. ...order.contract,
  62. creator: order.address,
  63. };
  64. }
  65. return {
  66. ...order,
  67. contract: {
  68. ...order.contract,
  69. txns: await getMakerTxns(order, isExistingEscrow),
  70. creator: order.address,
  71. },
  72. };
  73. }
  74. module.exports = withMakerTxns;
  75. JAVASCRIPT
    Copied!