[elixir] 거래소에서의 거래 실행 및 확정 로직

거래소에서 거래가 발생하고 이를 실행하고 확정하는 과정을 알아봅시다.

1. 거래 발생

거래는 구매자와 판매자 간의 주문이 일치함으로써 발생합니다. 구매자가 주문한 수량과 가격이 판매자의 판매 조건과 일치할 때 거래가 성립됩니다.

defmodule Trade do
  def execute_trade(buyer, seller, price, quantity) do
    # Check if buyer's order matches seller's offer
    if buyer.price >= seller.price and buyer.quantity >= seller.quantity do
      # Execute the trade
      execute(buyer, seller, price, quantity)
    else
      # Handle unmatched orders
      handle_unmatched_orders(buyer, seller)
    end
  end
end

2. 거래 실행

거래가 발생하면 거래소 시스템은 구매자의 계정에서 판매자의 계정으로 자산을 이체하고, 거래가 완료되었다는 메시지를 양 측에 전달합니다.

defmodule Trade do
  def execute(buyer, seller, price, quantity) do
    # Transfer assets from buyer to seller
    transfer_assets(buyer, seller, price, quantity)
  
    # Notify both parties about the trade execution
    notify_trade_execution(buyer, seller, price, quantity)
  end
end

3. 거래 확정

거래 실행 이후에는 거래가 확정되어야 합니다. 이는 거래가 취소되지 않고 완료되었음을 보장합니다.

defmodule Trade do
  def confirm_trade(trade_id) do
    # Fetch the trade from the database
    trade = TradeRepo.get_trade(trade_id)
  
    # Confirm the trade
    confirm(trade)
  end
end

이와 같은 방식으로 거래소에서의 거래 실행과 확정 로직이 이뤄집니다.

참고 문헌

  1. Elixir 공식 문서: https://elixir-lang.org/docs.html
  2. Programming Phoenix: Productive > Reliable > Fast, by Chris McCord, Bruce Tate, and José Valim