lib/order/compile/compile.js

  1. const algosdk = require('algosdk');
  2. const withUnits = require('./withUnits');
  3. const withLogicSigAccount = require('./withLogicSigAccount.js');
  4. const withOrderbookEntry = require('./withOrderbookEntry.js');
  5. const logger = require('../../logger');
  6. /**
  7. * ## 🏗 [Compile Order](#compile)
  8. *
  9. * Takes an {@link Order} and compiles it into the required shape for the sdk.
  10. * This includes converting floats to {@link BaseUnits} with Numerator and
  11. * Denominator. The {@link BaseUnits} properties are used to compile the
  12. * delegate template. Then it constructs a {@link LogicSigAccount} which is
  13. * the main output of the compile step.
  14. *
  15. * @example
  16. * const {order: compile} = require('@algodex/sdk')
  17. * const order = compile({
  18. * "client": new algosdk.Algodv2(),
  19. * "asset": {
  20. * "id": 15322902,
  21. * "decimals": 6,
  22. * },
  23. * "address": "TJFFNUYWHPPIYDE4DGGYPGHWKGAPJEWP3DGE5THZS3B2M2XIAPQ2WY3X4I",
  24. * "price": 2.22,
  25. * "amount": 1,
  26. * "total": 2,
  27. * "execution": "maker",
  28. * "type": "buy",
  29. * "appId": 22045503,
  30. * "version": 6
  31. * })
  32. * console.log(order.contract.lsig)
  33. * // Outputs LogicSigAccount
  34. *
  35. * @param {Order} o The Order Object
  36. * @return {Promise<Order>} Returns a composed order with ContractState
  37. * @memberOf module:order/compile
  38. */
  39. async function compile( o) {
  40. logger.debug(`compile(Order) as ${o.execution}`);
  41. if (!(o?.client instanceof algosdk.Algodv2)) {
  42. throw new TypeError('Invalid Algod Client!');
  43. }
  44. if (typeof o?.asset === 'undefined' || (typeof o?.asset?.decimals !== 'number' && typeof o?.contract?.N === 'undefined')) {
  45. throw new TypeError('Invalid Asset!!');
  46. }
  47. return await withLogicSigAccount(withOrderbookEntry(withUnits(o)));
  48. }
  49. module.exports = compile;
  50. JAVASCRIPT
    Copied!