For the complete documentation index, see llms.txt. This page is also available as Markdown.

SYMMIO App (Muon)

Introduction

The Muon app provides signed data for SYMM third-party frontend applications (Cloverfield is used as the example here), a platform for trading assets and perpetual contracts (perps) on the Ethereum blockchain without KYC. Users trade on Cloverfield with their Ethereum wallet.

Muon is an intermediary service that generates and signs the data required for trading. This page describes the app codebase and its main components.

Overview of the Muon App

The Muon app provides calculations and data for unrealized profit and loss (uPnl) and prices. It computes uPnl separately for partyA and partyB, and combined for both parties, and it fetches prices for given quotes. The following sections describe each method: how it calculates uPnl for partyA and partyB separately and together, and how it retrieves prices for specified quotes.

Methods Overview

The Muon app has a set of methods that provide data for contracts:

uPnl_A

The uPnl_A method verifies the unrealized profit and loss (uPnl) for partyA against a set tolerance. It checks that the calculated uPnl does not deviate significantly from an expected value.

Returned Data Structure:

  • ssymmio: Address of the Symmio diamond.

  • partyA: The address of partyA.

  • nonce: Unique nonce for the transaction to prevent replay attacks.

  • uPnl: The verified uPnl value for partyA as obtained from the request data.

  • timestamp: The blockchain timestamp at which the request data was generated.

  • chainId: The chain identifier.

partyA_overview

The partyA_overview method validates and provides a financial overview for partyA. It checks unrealized profit and loss (uPnl) and loss against predefined tolerances.

Returned Data Structure:

  • liquidationId: Identifier for any liquidation processes that might be relevant to the account.

  • symmio: Address of the Symmio diamond.

  • partyA: The address of partyA.

  • nonce: Unique nonce for the transaction to prevent replay attacks.

  • uPnl and loss: Verified financial metrics showing unrealized profit and losses.

  • symbolIds: Array of symbol IDs related to partyA's positions.

  • symbolIdsPrices: Prices corresponding to the symbol IDs involved.

  • timestamp and chainId: Blockchain metadata identifying the network and point in time.

verify

The verify method confirms the authenticity and accuracy of liquidation-related data for partyA. It packages and prepares data for cryptographic checks on Muon's network, specifically to verify liquidation signatures.

Returns:

  • liquidationId: The id of the liquidation.

  • symmio: Address of the Symmio diamond.

  • verifyLiquidationSig: A string indicating the specific verification function to be used.

  • partyA: Ethereum address of the subject party.

  • nonce: Unique nonce for the transaction to prevent replay attacks.

  • uPnl and loss: Unrealized profits and losses.

  • symbolIds and prices: Lists of trading symbols and their corresponding prices.

  • timestamp & chainId: Metadata linking data to the correct blockchain network and timestamp.

uPnl_A_withSymbolPrice

The uPnl_A_withSymbolPrice method calculates the unrealized profit and loss (uPnl) for partyA for a specific trading symbol, and validates the symbol's price against predefined tolerances.

Returns:

  • symmio: Address of the Symmio diamond.

  • partyA: Address of the trader whose uPnl is being computed.

  • nonce: Unique nonce for the transaction to prevent replay attacks.

  • uPnl: Calculated unrealized profit or loss for the specified positions of partyA.

  • symbolId: Identifier for the trading symbol whose price is verified.

  • price: Verified price of the symbol.

  • timestamp & chainId: Metadata linking data to the correct blockchain network and timestamp.

uPnl_B

The uPnl_B method calculates the unrealized profit and loss (uPnl) for partyB in relation to partyA.

Response Structure:

  • symmio: Address of the Symmio diamond.

  • partyB: Address of the solver (partyB) whose uPnl is evaluated.

  • partyA: Address of the associated party (partyA) involved in the transaction.

  • nonce: Unique nonce for the transaction to prevent replay attacks.

  • uPnl: The computed unrealized profit or loss for partyB's positions.

  • timestamp & chainId: Metadata linking data to the correct blockchain network and timestamp.

uPnl

The uPnl method calculates and verifies the unrealized profit and loss (uPnl) for both partyA and partyB.

Response Structure:

  • symmio: Address of the Symmio diamond.

  • partyB and partyA: Addresses of the involved parties.

  • nonceB and nonceA: Nonce values for both parties.

  • uPnlB and uPnlA: The computed unrealized profit or loss for both parties.

  • timestamp & chainId: Metadata linking data to the correct blockchain network and timestamp.

uPnlWithSymbolPrice

The uPnlWithSymbolPrice method calculates and verifies the unrealized profit and loss (uPnl) for both partyA and partyB, including the price of a specific symbol.

Response Components:

  • symmio: Address of the Symmio diamond.

  • partyB and partyA: Addresses of the involved parties.

  • nonceB and nonceA: The nonces of partyA and partyB.

  • uPnlB and uPnlA: The computed unrealized profit or loss for both parties.

  • symbolId and price: The identifier for the specific trading symbol and its corresponding price.

  • timestamp & chainId: Metadata linking data to the correct blockchain network and timestamp.

price

The price method retrieves and validates the prices of multiple quotes, checking that they fall within specified tolerances.

Response Components:

  • symmio: Address of the Symmio diamond.

  • quoteIds: An array of quote ids.

  • prices: The validated list of prices corresponding to the quote IDs.

  • timestamp: The timestamp marking the retrieval of the price data.

  • chainId: The chain identifier.

Functions Breakdown

uPnlPartyA

The uPnlPartyA function calculates the unrealized profit and loss (uPnl) for partyA.

This function fetches the open positions and quote IDs for partyA, retrieves the nonce of partyA, and calculates the uPnl and notional value sum from the open positions and corresponding prices. If there are no open positions, it returns a result with zero uPnl and notional value sum, along with the nonce, quote IDs, open positions, prices map and markPrices.

uPnlPartyB

The uPnlPartyB function calculates the unrealized profit and loss (uPnl) for partyB, given the associated partyA.

This function fetches the open positions and quote IDs for partyB with the associated partyA, retrieves the nonce of partyB for the given partyA, and calculates the uPnl and notional value sum from the open positions and corresponding prices. If there are no open positions, it returns a result with zero uPnl and notional value sum, along with the nonce and quote IDs. The calculated uPnl is multiplied by -1 to represent partyB's perspective.

uPnlPartyB_FetchedData

The uPnlPartyB_FetchedData function calculates the unrealized profit and loss (uPnl) for partyB using fetched data.

This function filters the mixed open positions to include only the positions associated with partyB, then checks whether any open positions remain. If there are open positions, it retrieves the symbols associated with the quote IDs, creates a prices list from the symbols and prices map, and calculates the uPnl and notional value sum from the open positions and prices. If there are no open positions, it sets the uPnl and notional value sum to zero. It then retrieves the nonce of partyB for the given partyA and returns the result with the calculated uPnl, notional value sum, nonce, prices, and quote IDs.

uPnlParties

The uPnlParties function calculates the unrealized profit and loss (uPnl) for both partyA and partyB and returns the results.

This function first checks whether partyB and partyA are identical and throws an error if they are. It calls uPnlPartyA to calculate the uPnl, nonce, notional value sum, prices map, prices, and quote IDs for partyA. It then calls uPnlPartyB_FetchedData to calculate the uPnl, nonce, notional value sum, prices, and quote IDs for partyB using fetched data. It returns the results with the adjusted uPnl for partyB (multiplied by -1), uPnl for partyA, notional value sum for partyB and partyA, nonces for partyB and partyA, prices map, prices for partyB and partyA, and quote IDs for partyB and partyA.

calculateUpnl

The calculateUpnl function calculates the unrealized profit and loss (uPnl) and the notional value sum for a given set of open positions and their corresponding prices.

This function iterates through each open position and calculates the uPnl for each position by multiplying the remaining amount by the price difference. The uPnl is adjusted for the position type (long or short) and scaled before being added to the total uPnl. The notional value of each position is the remaining amount multiplied by the opened price, scaled and added to the total notional value sum. The function returns the calculated uPnl and notional value sum.

fetchPrices

The fetchPrices method obtains the latest prices for a set of quoteIds by fetching the symbols and corresponding prices. It returns the prices as an array and a map for access within the Muon app.

This function retrieves prices for a given set of quoteIds. It uses helper methods to fetch symbols, obtain the latest prices, and create a map of prices. It returns an object containing an array of prices and a map associating symbols with their prices.

getPrices

The getPrices function uses three functions (getBinancePrices, getKucoinPrices, and getMexcPrices) to fetch prices from different exchanges asynchronously. The results are stored in the markPrices object, which holds the prices for each exchange. The function checks the retrieved prices with the checkPrices function, then returns an object containing the pricesMap and markPrices.

getBinancePrices

The getBinancePrices function retrieves the latest price data for various symbols from the Binance API. It makes an HTTP request to the API endpoint and processes the response to extract the symbol-price mappings, storing them in an object that the function returns.

This function fetches the latest price data for different symbols from an external API. It makes an HTTP request to the API endpoint and processes the response to extract the symbol-price mappings, stored in an object and returned by the function.

checkPrices

The checkPrices function verifies the validity of the fetched prices. It compares the prices from Kucoin and Mexc against the expected prices from Binance. The function iterates over each symbol and checks whether the expected price exists for the symbol in the Binance prices; if not, an error is thrown. It then retrieves the price for the symbol from each source and checks whether it falls within the acceptable price tolerance using the isPriceToleranceOk function. If any price is outside the tolerance, the function returns false. If all prices pass, it returns true.

fetchPartyBsAllocateds

This function retrieves the allocated balances for multiple partyB accounts with respect to a single partyA. It queries the Symmio smart contract for the amount each partyB has allocated toward trading with partyA.

Returns an array of allocated balance amounts. Each element corresponds to the allocated balance of a partyB as indexed in the partyBs array.

filterPositions

This function filters a mixed array of open trading positions to extract those that involve a specific partyB. It segregates positions by the counterparty involved, making it easier to manage or analyze the trading activities related to a particular partyB.

Returns: An object containing two properties:

  • openPositions (Array): An array of all positions that match the specified partyB.

  • quoteIds (Array): An array of quoteIds corresponding to the positions.

isPriceToleranceOk

This function evaluates whether the deviation of a given price from an expected price falls within an acceptable tolerance range. It is used to keep price fluctuations within predefined limits, which matters for trading strategies that require price accuracy and stability.

Returns:

  • Object: An object containing:

    • isOk (Boolean): A boolean indicating whether the actual price is within the specified tolerance of the expected price.

    • priceDiffPercentage (Float): The calculated price difference as a percentage, scaled to two decimal places for readability.

isUpnlToleranceOk

This function determines whether the difference between an actual unrealized profit and loss (uPnl) and an expected uPnl is within a specified tolerance, relative to the notional value of the position. It is used to keep the uPnl within acceptable risk parameters.

Returns: An object containing:

  • isOk (Boolean): A boolean indicating whether the uPnl is within the specified tolerance relative to the notional value.

  • uPnlDiffInNotionalValue (BigNumber): The uPnl difference expressed as a percentage of the notional value, adjusted for readability (multiplied by 100 for percentage format).

getSymbols

This function retrieves the symbols associated with given quote IDs by querying the symmio contract. It is used where the market symbols related to specific trading quotes are needed for display or further processing.

  • Returns:

  • Array (String[]): Returns an array of symbol names corresponding to the quote IDs provided. Each symbol in the array aligns with the quote ID at the same index in the input array.

fetchOpenPositions

This function retrieves all open trading positions for a specified party (partyA or partyB) from the Symmio platform, spanning potentially multiple pages of data. It compiles a list of positions, including corresponding quote IDs, symbol IDs, and involved party B addresses.

This function returns an object containing arrays of open positions, quote IDs, symbol IDs, and party B addresses:

getOpenPositions

This function retrieves a list of open trading positions for a specified party (partyA or partyB) from the Symmio platform. It selects the appropriate Symmio contract method based on whether the positions for partyA or partyB are requested.

This function supports pagination through the start and size parameters and accommodates historical data queries with the blockNumber parameter.

getPositionsCount

This function retrieves the count of open positions for a specified party (partyA or partyB) on the Symmio platform. It determines which function to call based on the side (A or B) indicated, for querying position counts in a decentralized trading environment.

Returns: Number, the count of open positions for the specified party at the given block number.

createPricesList

This function creates a list of prices for specified cryptocurrency symbols based on a mapping of symbol-to-price

Returns: prices (Array), an array containing the price values as strings for the requested symbols.

getMexcPrices

The getMexcPrices function retrieves current cryptocurrency prices from the MEXC exchange.

This function fetches the latest trading prices for various cryptocurrency pairs from the MEXC exchange. It formats the received data and returns it as a map of symbol-to-price associations for further processing and display.

Returns: pricesMap, an object containing key-value pairs where keys are cryptocurrency symbols (e.g., BTCUSDT, ETHUSDT) and values are their corresponding prices, adjusted and scaled appropriately.

getKucoinPrices

The getKucoinPrices function fetches current cryptocurrency prices from the Kucoin exchange.

Returns: An object (pricesMap) containing key-value pairs where keys are the cryptocurrency symbols (e.g., BTCUSDT, ETHUSDT) and values are their corresponding prices, adjusted and scaled as necessary.

formatKucoinSymbol

This function formats a cryptocurrency symbol from Kucoin to standardize it for use within the application

Returns: symbol (String), a string representing the formatted cryptocurrency symbol without the last character.

Last updated