Files
bch-dex/dev-docs/specification.md
T
2021-11-24 10:54:45 -08:00

6.0 KiB

ipfs-swap-service Specification

This document contains a high-level, human-readable specification for the four major architectural areas of the ipfs-swap-service:

  • Entities
  • Use Cases
  • Controllers (inputs)
  • Adapters (outputs)

This reflects the Clean Architecture design pattern.

Entities

Entities make up the core business concepts. If these entities change, they fundamentally change the entire app.

Order

An order is created from data passed to the app by the P2WDB webhook. It is destroyed when the UTXO described in the Signal has been detected as spent.

Order entities have the following properties:

  • tokenId - The unique ID that identifies the class of token being offered for sale.
  • buyOrSell - A string with a value buy or sell indicating which type of offer this is.
  • rateInSats - The rate in terms of tokens-per-currency-unit.
    • For Bitcoin, the min currency is sats.
    • For AVAX, the min currency is nano-Avax.
    • for eCash, the min currency is bits.
  • minSatsToExchange - The minimum order size accepted.
  • signature - A message signed by the address which created the order.
  • sigMsg - The clear-text message used to generate the signature.
  • utxoTxid - The TXID of the UTXO used in the order.
  • utxoVout - The vout of the UTXO used in the order.
  • numTokens - The maximum number of tokens offered for sale.
  • timestamp - The ISO time when the order was created.
  • localTimestamp - The localized time when the order was created.
  • p2wdbTxid - The TXID proof-of-burn used to add the order to the P2WDB.
  • p2wdbHash - The hash used to identify the order entry in the P2WDB.
  • lokadId - Not used. Provided for future functionality.
  • messageType - Not used. Provided for future functionality.
  • messageClass - Not used. Provided for future functionality.

Offer

An Offer Entity is nearly the same as an Order. But while an Order is generated by a webhook from P2WDB, the Offer Entity is created internally. It is used to track an Order generated by this application.

The Offer tracks the HD index address used to hold tokens or BCH for sale. This is the part of the app concerned with the custody of the funds. It creates a segregated UTXO to hold the offered asset. The Offer is automatically destroyed if the UTXO is accidentally spent, which is why it needs to be segregated from other wallet UTXOs.

Offer entities have the following properties:

  • offerIpfsId - The IPFS ID of the instance of ipfs-swap-service that is managing the offer.
  • offerBchAddr - The BCH address controlling the offer.
  • offerPubKey - The public key used to generate the BCH address, used for encryption.
  • tokenId - The unique ID that identifies the class of token being offered for sale.
  • buyOrSell - A string with a value buy or sell indicating which type of offer this is.
  • rateInSats - The rate in terms of tokens-per-currency-unit.
    • For Bitcoin, the min currency is sats.
    • For AVAX, the min currency is nano-Avax.
    • for eCash, the min currency is bits.
  • minSatsToExchange - The minimum order size accepted.
  • signature - A message signed by the address which created the order.
  • sigMsg - The clear-text message used to generate the signature.
  • utxoTxid - The TXID of the UTXO used in the order.
  • utxoVout - The vout of the UTXO used in the order.
  • numTokens - The maximum number of tokens offered for sale.
  • timestamp - The ISO time when the order was created.
  • localTimestamp - The localized time when the order was created.
  • p2wdbTxid - The TXID proof-of-burn used to add the order to the P2WDB.
  • p2wdbHash - The hash used to identify the order entry in the P2WDB.
  • lokadId - Not used. Provided for future functionality.
  • messageType - Not used. Provided for future functionality.
  • messageClass - Not used. Provided for future functionality.

Use Cases

Use cases are verbs or actions that is done to an Entity or between Entities.

Order

  • createOrder() - This method is triggered by a webhook from the P2WDB. It will take the data provided by the P2WDB and create a new Order entity in the local database.

Offer

  • ensureFunds() - Ensure that the wallet has enough BCH and tokens to complete the requested trade.
  • moveTokens() - Move the tokens indicated in the offer to a temporary holding address. This will generate the UTXO used in the webhook message. This function moves the funds and returns the UTXO information.
  • createOffer() - A macro command that leverages ensureFunds() and moveTokens(), to create a new Offer and submit it to the P2WDB.

Controllers

Controllers are inputs to the system. When a controller is activated, it causes the system to react in some way.

Orders

  • POST /order - This POST REST API endpoint will be triggered by a webhook generated by the P2WDB. This will notify the ipfs-swap-service that a new entry has been added to the P2WDB that matches the appId of swap-<chain>, where <chain> has a value of avax, bch, or ecash. It's a new entry that should be evaluated for inclusion in the ipfs-swap-service local database.

Offers

  • POST /offer - This POST REST API endpoint can be triggered by the Client or a simple curl call. It passes in the data needed for ipfs-swap-service to generate and track a new Offer, then submit the data to the P2WDB to generate an Order that is tracked by all other instances of ipfs-swap-service.

Adapters

Adapters are output libraries so that the business logic doesn't need to know any specific information about the I/O. They are essentially the output of the application.

  • localdb - An adapter for the local database (MongoDB).
  • ipfs - An adapter for IPFS and the ipfs-coord library. It allows the app to use the JSON-RPC over IPFS.