OrderBook Blueprint

The OrderBook Blueprint is an extended standard template built on top of the Basic Blueprint. It is designed to help developers create custom Agents with order book functionality. In addition to inheriting all features of the Basic Blueprint, the OrderBook Blueprint introduces several core functionalities to support the creation, management, and settlement of orders.

OrderBook Blueprint Analysis

New Core Features

  • Make Order: Allows the Agent to create Notes (orders).

  • Execute Settlement: The core operation during the matching and settlement phases, enabling the actual transfer of assets.

  • Cancel Order: Cancels published Notes that have not yet been settled.

Detailed Code Explanation

1. Make Order

code
Handlers.add('ffp.makeOrder', 'FFP.MakeOrder',
  function(msg)
    assert(msg.From == Owner or msg.From == ao.id, 'Only owner can make order')
    assert(type(msg.AssetID) == 'string', 'AssetID is required')
    assert(type(msg.Amount) == 'string', 'Amount is required')
    assert(bint.__lt(0, bint(msg.Amount)), 'Amount must be greater than 0')
    assert(type(msg.HolderAssetID) == 'string', 'HolderAssetID is required')
    assert(type(msg.HolderAmount) == 'string', 'HolderAmount is required')
    assert(bint.__lt(0, bint(msg.HolderAmount)), 'HolderAmount must be greater than 0')

    local expireDate = msg.ExpireDate
    if expireDate and tonumber(expireDate) < msg.Timestamp then
      msg.reply({Error = 'err_invalid_expire_date', ['X-FFP-MakeOrderID'] = msg.Id})
      return
    end
    if not expireDate then expireDate = '' end

    local res = Send({
      Target = FFP.Settle,
      Action = 'CreateNote',
      AssetID = msg.AssetID,
      Amount = msg.Amount,
      HolderAssetID = msg.HolderAssetID,
      HolderAmount = msg.HolderAmount,
      IssueDate = tostring(msg.Timestamp),
      ExpireDate = expireDate,
      Version = FFP.SettleVersion
    }).receive()
    local noteID = res.NoteID
    local note = json.decode(res.Data)
    FFP.Notes[noteID] = note
    FFP.MakeTxToNoteID[msg.Id] = noteID
    msg.reply({Action = 'OrderMade-Notice', NoteID = note.NoteID, Data = json.encode(note)})
  end

This feature enables the Agent to publish trading intentions using Notes, which specify trading conditions (e.g., asset type and quantity), supporting the creation of orders in the order book.

Feature Description:

  • Allows the Agent owner to create new Notes by calling this interface.

Key Code Logic:

  • Validate the legality of input parameters (e.g., asset type, quantity, expiration time).

  • Call the FFP protocol’s CreateNote interface to generate a Note and store it in the Agent’s internal data table.

2. Execute Settlement

code

Feature Description:

  • When Notes issued by the Agent enter the settlement phase, the FFP settlement center triggers the Agent to execute asset transfer operations.

  • Supports the Agent in handling asset delivery for Notes it created during settlement.

Key Code Logic:

  • Validate the Note’s legitimacy (e.g., ensure it exists, was issued by the Agent, and has an Open status).

  • Update the Note’s status to Executed.

  • Transfer the corresponding assets to the FFP settlement center.

3. Cancel Orders

code

Feature Description:

  • Allows the Agent owner to cancel Notes that have not yet been settled.

  • Only Notes in the Open status can be canceled; other statuses (e.g., Executed or Settled) cannot be canceled.

Key Code Logic:

  • Verify that the Note belongs to the current Agent and ensure its status is Open.

  • Call the FFP protocol’s Cancel interface to cancel the Note.

  • Update the local Note status to Canceled.

Complete Code

orderbook.lua
utils.lua

Before development, copy orderbook.lua and utils.lua into your development environment.

Summary

The OrderBook Blueprint is an upgraded version of the Basic Blueprint, designed for developers requiring advanced transaction and order management logic. It provides standardized foundational features for building custom order book Agents, enabling developers to quickly get started and focus on optimizing and implementing their business logic.

Last updated

Was this helpful?