Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap backend #447

Open
2 tasks
Tracked by #440
joel-medicala-yral opened this issue Nov 6, 2024 · 1 comment · May be fixed by #448
Open
2 tasks
Tracked by #440

Swap backend #447

joel-medicala-yral opened this issue Nov 6, 2024 · 1 comment · May be fixed by #448

Comments

@joel-medicala-yral
Copy link
Contributor

joel-medicala-yral commented Nov 6, 2024

  • Users should be able to send a swap request to a token creator
  • Swap request can be accepted or declined by a token creator
  • Request is valid for a week
  • Token creator can accept or decline the swap request
  • If accepted swap each other's tokens
  • Check if the swap requester's token has a value in the market and update the token creator's price
  • Swap requester can cancel their request whenever they want to

Overview

flowchart TD
   %% Initiation by the requester
   A[Requester initiates swap via Frontend] --> B[Leptos Server Function]

   %% Approval process from requester to token creator canister
   B --> C[Send icrc2 approve call to Token Creator Canister, e.g. approve 20 USDC]
   
   %% Token creator side checks and setup
   C --> D[Token Creator Canister verifies allocation and token details, last swap price, etc.]
   
   %% For tokens that require external price data (BTC, DOLR, etc.)
   B --> L[Check if token requires external pricing]
   L -- Yes --> M[Invoke External CF Worker]
   M --> N[CF Worker fetches and caches price from Binance, Coinbase, Kongswap]
   N --> D

   %% Swap request action in the token creator canister
   D --> E[Invoke swap_request_action with swap details]

   %% Outcome decision point
   E --> F{Swap Outcome?}
   
   %% Accepted swap flow
   F -- "Accepted" --> G[Execute swap: Exchange tokens and update last swap price, e.g. DOG token new price = 2 USD]
   
   %% Requester cancels swap flow
   F -- "Requester Cancels" --> H[Requester cancels the swap]
   H --> I[Trigger icrc2 approve call with amount = 0, reset/revoke approval]
   
   %% Token creator rejects swap flow
   F -- "Token Creator Rejects" --> J[Token Creator rejects swap request]
   J --> K[Notify requester, approval reset to 0 on notification]

Loading

Caller Callee Flow

flowchart TD
    Requester[Requester Principal] --> icrc2_approve --> Creator[Creator Canister]

    CreatorPrincipal[Creator Principal] --> swap_request_action --> Creator
Loading

swap_request_action(Canister side)

  • Swapping is the transfer of token_a from requester_princiapl to the caller and token_b from caller to requester_principal
flowchart TD
    Caller[Caller = token_creator] --> swap_request_action{SwapRequestActions} --> Accept
    Accept --> token_a
    Accept --> token_b
    Accept --> requester_principal

    token_a --> Swap
    token_b --> Swap
    requester_principal --> Swap

    Swap --> get_token_prices{get_token_prices} --> Priced -->UpdateLastSwappedPrice[Update Last Swap Price]
    get_token_prices --> NotPriced

Loading

get_token_price (CF Worker)

flowchart TD
   D{"Is Token Owner? (Call deployed_cdao for each ID)"} -- Not Owner --> E["Error: Not Owner"]
   E --> F["Abort Process"]
   J{"Token Type"} -- DOLR or BTC --> K["Check KV Cache for Price"]
   K -- Cached Price Found --> L["Return Cached Price"]
   K -- No Cached Price --> M{"Token Type"}
   M -- BTC --> N["Fetch Price from Coinbase & Binance, Calculate Average"]
   M -- DOLR --> O["Fetch Price from Kong Swap"]
   N --> P["Store Fetched Price in KV Cache"]
   O --> P
   P --> Q["Return Fetched Price"]
   J -- "Other Token (e.g. ABC)" --> R["Obtain Token Creator via sns_init Params"]
   R --> S@{ label: "Call Token Creator's Canister to Retrieve Price" }
   S --> T@{ label: "Return Token Price from Token Creator's Canister" }
   D --> J

   S@{ shape: rect}
   T@{ shape: rect}
Loading

cancel_swap_request (Server Function Side)

flowchart TD
    Caller[Caller = requester_principal] -->
    token_a --> |Deallocate by calling icrc2_approve again and setting the amount to 0|icrc2_approve

Loading
@siyara-m-yral
Copy link

  • Price fetching part can be delegated
  • Backend APIs are done but bug fixing (60% done)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants