Pooya KN
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Revised Order - Create Order Flow ✅ - Match Order Flow ✅ - Cancel Order Flow ✅ - User Registration Flow - Deposit Flow - Withdraw Flow - User Update Flow - User Delete Flow - Referral Flow ## Event and Command ### Command - Has reply - Has only one listener for the reply - Start with a imperative verb - cancel.order.command => (imperative verb + what + type) - - Is sequential #### Reply - Start with command name + type reply - cancel.order.reply ### Event - Is pub/sub and has *..n subscribers - Doesn't have reply - Is async - Publisher doesn't know about successful/unsuccessful execution of the rest of logic ## Order's Create Order Flow ```mermaid sequenceDiagram autonumber User ->> Order: create order Order ->> Asset: get order asset (validation data) Order ->> Order: validate order alt if not valid Order -->> User: error not valid end Order ->> Price: get market price Order ->> Order: validate market price alt if not valid Order -->> User: error not valid end Order ->> Account: check balance alt if not enough balance Order -->> User: error not enough balance end Order ->> Database: create order with NOT_EXECUTED status alt if database error Order -->> User: error end Order ->> Database: create freeze asset command alt if database error Order -->> User: error end Order ->> User: order created response loop Database -) Debezium: push events Debezium ->> Kafka: dispatch events end ``` ## Order's Create Order Flow After Asset is Frozen ```mermaid sequenceDiagram autonumber Order -) Account: freeze asset Account ->> Database: freeze user assets alt if not enough Account ->> Database: create error reply Database -) Debezium: push events Debezium ->> Kafka: dispatch events Kafka ->> Order: cannot freeze account else other error Account ->> Account: retry else no error Kafka ->> Order: freeze successful reply end Order ->> Database: find order with id from reply Order ->> Order: check status alt if other than NOT_EXECUTED Order ->> Order: ignore end alt if freeze has error Order ->> Order: reject order Order ->> Database: save rejected order Order ->> Database: publish order rejected event end alt if reply came late (> 3 minutes) Order ->> Order: set order status to TIMED_OUT Order ->> Database: save timed out order Order ->> Database: publish order timed out event Order ->> Database: publish unfreeze command Kafka -) Account: unfreeze Account ->> Database: unfreeze end Order ->> Book: get order asset's book Note over Book: PME alt if book not found Order ->> Order: retry end Order ->> Order: set order status to NEW Order ->> Database: publish order created event alt if database error Order -->> Order: retry end Order ->> Book: Process order loop Database -) Debezium: push events Debezium ->> Kafka: dispatch events end ``` ## Order's Match Orders Flow ```mermaid sequenceDiagram autonumber Order ->> Book: Process order alt order is not bot order Book ->> Book: add order to book end Book ->> Matcher: try match Matcher ->> Matcher: get opposite book Matcher ->> Matcher: find opposite order alt cannot find opposite order Note left of Matcher: TODO: raise error Matcher ->> Matcher: cannot match, add to book end Matcher ->> Matcher: can match alt if cannot match Note left of Matcher: TODO: send alert Matcher ->> Matcher: cannot match, add to book end Matcher ->> Book: remove both order Matcher ->> Matcher: create trade and update trade orders Matcher ->> Matcher: notify match listeners ``` ## Order's Match Listener Flow ```mermaid sequenceDiagram autonumber Matcher ->> MatchListener: Notify match listener loop for each match loop for both orders in match event MatchListener ->> Database: get order alt if cannot find order MatchListener ->> Matcher: return error end alt if order is cancelled MatchListener ->> Matcher: return error end MatchListener ->> Database: update order Note over MatchListener: calcualte total executed and deduct from original total, if less we have dust MatchListener ->> MatchListener: has dust? alt if has dust MatchListener ->> Database: publish unfreeze command for dust amount end alt if order is filled MatchListener ->> Database: publish order filled event else MatchListener ->> Database: publish order patially filled event end end MatchListener ->> Database: publish exchange command MatchListener ->> Database: create trade record MatchListener ->> Database: commit end loop Database -) Debezium: push events Debezium ->> Kafka: dispatch events end ``` ## Order's Cancel Order Flow ```mermaid sequenceDiagram autonumber User ->> Order: cancel order Order ->> Database: get order alt if not found Order ->> User: not found error end Order ->> Order: same owner? alt if not Order ->> User: ownership error end Order ->> Order: is pending cancel? alt if pending cancel Order ->> User: return successful cancel response end Note over Order: [FILLED, CANCELED, REJECTED, EXPIRED, TIMED_OUT] Order ->> Order: status is final? alt if final Order ->> User: cannot cancel error end Order ->> Book: set status to PENDING_CANCEL Order ->> Database: set status to PENDIG_CANCEL Order ->> Database: publish external me cancel order Database -) Debezium: cancel order event Debezium ->> Kafka: publish event Order ->> User: successful cancel response ``` ## External ME Cancel Order Flow ```mermaid sequenceDiagram autonumber Order -) ExternalME: cancel order ExternalME ->> Datbase: find order alt if not found ExternalME ->> Kafka: publish successful cancel end alt if filled ExternalME ->> Kafka: publish failed cancel end alt if anything other than [NEW, PARTIALLY_FILLED] ExternalME ->> Database: set status to cancelled ExternalME ->> Kafka: publish successful cancel end alt if new/partially filled ExternalME ->> LiquidityProvider: cancel order alt if not found Note over ExternalME: we cannot do anything ExternalME ->> Kafka: publish successful cancel else other error ExternalME ->> Kafka: publish failed cancel end ExternalME ->> Database: set status to pending cancel end loop check status job ExternalME ->> Database: get all pending cancel ExternalME ->> LiquidityProvider: get order alt if cancelled alt if order executed amount is more than our database after cancellation ExternalME ->> Kafka: publish match command Kafka --) ExternalME: order match reply received ExternalME ->> Kafka: publish successful cancel else ExternalME ->> Kafka: publish successful cancel end end ExternalME ->> Database: update order based on LP response end ``` ## Order Cancel Flow After External ME Cancellation ```mermaid sequenceDiagram autonumber ExternalME --) Order: publish external cancel reply Order ->> Database: get order alt if not found Order ->> Order: ignore event end alt if order is filled Order ->> Order: ignore event end alt if external cancel failed alt if order has fills Order ->> Book: Set status to PARTIALLY_FILLED Order ->> Database: Set status to PARTIALLY_FILLED else if order doesn't have fills Order ->> Book: Set status to NEW Order ->> Database: Set status to NEW end else if cancel was successufl Order ->> Book: remove order Order ->> Database: set statut to cancelled Order ->> Database: publish order cancelled event Order ->> Database: publish account unfreeze command end loop Database -) Debezium: push events Debezium ->> Kafka: dispatch events end ``` ### Order Statuses ``` type OrderStatus string const ( OrderStatusNotExecuted OrderStatus = "NOT_EXECUTED" OrderStatusNew OrderStatus = "NEW" OrderStatusPartiallyFilled OrderStatus = "PARTIALLY_FILLED" OrderStatusFilled OrderStatus = "FILLED" OrderStatusCanceled OrderStatus = "CANCELED" OrderStatusPendingCancel OrderStatus = "PENDING_CANCEL" OrderStatusRejected OrderStatus = "REJECTED" OrderStatusExpired OrderStatus = "EXPIRED" OrderStatusTimedOut OrderStatus = "TIMED_OUT" ) ``` ``` New Service: KYC Fiat Manager IPG Current Service: Auth => register with email/phone External or Internal Order Execution? Hot/Cold Wallet ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully