SYMM App (Muon)
Introduction
The Muon app is designed to provide signed data for SYMM 3rd Party frontends (with Cloverfield used as an example in this text), a platform that enables users to trade assets and perpetual contracts (perps) on the Ethereum blockchain without the need for Know Your Customer (KYC) procedures. By utilizing their Ethereum wallet, users can participate in trading activities on Cloverfield.
Muon acts as an intermediary service, facilitating the generation and signing of data that is required for trading. This documentation will provide an overview of the app codebase, explaining its key components and functionalities.
Overview of the Muon App
The Muon app is designed to provide various calculations and information related to unrealized profit and loss (uPnl) and price data. It offers separate uPnl calculations for individual parties, namely "partyA" and "partyB," as well as combined uPnl calculations for both parties together. Additionally, the app allows fetching the prices of given quotes. The following sections provide a detailed explanation of each method provided by the Muon app, showcasing how it calculates uPnl for partyA and partyB separately and together, as well as how it retrieves price information for specified quotes.
Methods Overview
The Muon app has a set of methods of providing data for contracts:
uPnl_A
uPnl_A
The uPnl_A
method in the Muon app is designed to verify and validate the unrealized profit and loss (uPnl) for partyA based on a set tolerance level. This method ensures that the calculated uPnl does not deviate significantly from an expected value, safeguarding against discrepancies and ensuring accuracy in financial reporting and analysis.
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
partyA_overview
The partyA_overview
method in the Muon app serves to validate and provide a comprehensive financial overview for partyA. It checks unrealized profit and loss (uPnl) and loss against predefined tolerance levels, ensuring the reliability of trading data for partyA.
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-specific metadata ensuring the data's relevance to the correct network and point in time.
verify
verify
The verify
method in the Muon app is designed to confirm the authenticity and accuracy of liquidation-related data for a particular party (partyA) on the platform. This method plays a critical role in the validation process by packaging and preparing data for further cryptographic checks using Muon's network, specifically targeting the verification of 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, the primary focus of the verification.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
uPnl_A_withSymbolPrice
The uPnl_A_withSymbolPrice
method in the Muon app is designed to calculate the unrealized profit and loss (uPnl) for partyA, associated with a specific trading symbol, and validate the correctness of the symbol's price within predefined tolerance levels. This function is critical for ensuring accurate and timely financial reporting for trading activities.
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
uPnl_B
The uPnl_B
method within the Muon app calculates the unrealized profit and loss (uPnl) specifically 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, providing a snapshot of financial performance.timestamp
&chainId
: Metadata linking data to the correct blockchain network and timestamp.
uPnl
uPnl
The uPnl
method within the Muon app calculates and verifies the unrealized profit and loss (uPnl) for both partyA and partyB. This method facilitates a comprehensive assessment of financial performance for both parties involved in the trading activities on decentralized platforms.
Response Structure:
symmio
: Address of the Symmio diamond.partyB
andpartyA
: Addresses of the involved parties.nonceB
andnonceA
: Nonce values for both parties .uPnlB
anduPnlA
: The computed unrealized profit or loss for both parties.timestamp
&chainId
: Metadata linking data to the correct blockchain network and timestamp.
uPnlWithSymbolPrice
uPnlWithSymbolPrice
The uPnlWithSymbolPrice
method in the Muon app calculates and verifies the unrealized profit and loss (uPnl) for both partyA and partyB, incorporating the price of a specific symbol to enrich the financial data context.
Response Components:
symmio
: Address of the Symmio diamond.partyB
andpartyA
: Theaddresses of the involved parties.nonceB
andnonceA
: The nonces of partyA and partyBuPnlB
anduPnlA
: The computed unrealized profit or loss for both parties.symbolId
andprice
: The identifier for the specific trading symbol and its corresponding price.timestamp
&chainId
: Metadata linking data to the correct blockchain network and timestamp.
price
price
The price
method in the Muon app retrieves and validates the prices of multiple quotes, ensuring they align within specified tolerances. This method is essential for confirming real-time asset pricing accuracy on decentralized trading platforms.
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 in the Muon app calculates the unrealized profit and loss (uPnl) for partyA. Here's a breakdown of its implementation:
This function fetches the open positions and quote IDs for partyA, retrieves the nonce of partyA, and calculates the uPnl and notional value sum using 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 other relevant information such as nonce, quote IDs, open positions, prices map and markPrices.
uPnlPartyB
The uPnlPartyB
function in the Muon app calculates the unrealized profit and loss (uPnl) for partyB, given the associated partyA. Here's a breakdown of its implementation:
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 using 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 other relevant information such as nonce and quote IDs. The calculated uPnl is multiplied by -1 to represent partyB's perspective.
uPnlPartyB_FetchedData
The uPnlPartyB_FetchedData
function in the Muon app calculates the unrealized profit and loss (uPnl) for partyB using fetched data. Here's a breakdown of its implementation:
This function filters the mixed open positions to include only the positions associated with partyB. It then checks if there are any open positions remaining. If there are open positions, it retrieves the symbols associated with the quote IDs, creates a prices list using the symbols and prices map, and calculates the uPnl and notional value sum using the open positions and prices. If there are no open positions, it sets the uPnl and notional value sum to zero. Finally, it 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 in the Muon app calculates the unrealized profit and loss (uPnl) for both partyA and partyB and returns the results. Here's a breakdown of its implementation:
This function first checks if partyB and partyA are identical and throws an error if they are. Then, it calls the uPnlPartyA function to calculate the uPnl, nonce, notional value sum, prices map, prices, and quote IDs for partyA. Next, it calls the uPnlPartyB_FetchedData function to calculate the uPnl, nonce, notional value sum, prices, and quote IDs for partyB using fetched data. Finally, 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 in the Muon app is responsible for calculating the unrealized profit and loss (uPnl) and the notional value sum for a given set of open positions and their corresponding prices. Here's an explanation of how this function works:
This function iterates through each open position and calculates the uPnl for each position by multiplying the remaining amount of the position by the price difference. The uPnl is adjusted based on the position type (long or short) and scaled before being added to the total uPnl. Additionally, the notional value of each position is calculated by multiplying the remaining amount by the opened price and scaled before being added to the total notional value sum. Finally, the function returns the calculated uPnl and notional value sum as an object.
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 creates a map for convenient access within the Muon app. Here's a breakdown of its implementation:
This function is responsible for retrieving prices corresponding to a given set of quoteIds. It utilizes other helper methods to fetch symbols, obtain the latest prices, and create a map of prices. By combining these components, the method returns an object containing an array of prices and a map that associates symbols with their respective prices. This enables the Muon app to efficiently access and utilize the required price data for contract-related operations.
getPrices
In this implementation, the getPrices
function uses three separate functions (getBinancePrices
, getKucoinPrices
, and getMexcPrices
) to fetch prices from different exchanges asynchronously. The results are then stored in the markPrices object, which includes the prices for each exchange. The function also checks the retrieved prices using the checkPrices function to ensure their validity. Finally, the function returns an object containing the pricesMap and markPrices for further usage.
getBinancePrices
The getBinancePrices
function retrieves the latest price data for various symbols from binance API. It makes an HTTP request to the API endpoint and processes the response to extract the symbol-price mappings. The extracted data is stored in an object, which is then returned by the function. Here's a breakdown of its implementation:
This function implements a logic to fetch the latest price data for different symbols from an external API. It uses an HTTP request to the API endpoint and processes the response to extract the symbol-price mappings. The extracted data is stored in an object, which is returned by the function. This allows the application to access up-to-date price information for various symbols, enabling accurate calculations and analysis in the app.
checkPrices
The checkPrices
function is responsible for verifying the validity of the fetched prices. It compares the prices of different sources (Kucoin and Mexc) with the expected prices from Binance. The function iterates over each symbol and checks if the expected price exists for the symbol in the Binance prices. If not, an error is thrown. Then, it retrieves the prices for the symbol from each source and checks if they fall within the acceptable price tolerance range using the isPriceToleranceOk
function. If any price is outside the tolerance range, the function returns false
. Otherwise, if all prices pass the tolerance check, it returns true
, indicating that the prices are valid.
fetchPartyBsAllocateds
This function retrieves the allocated balances for multiple partyB
accounts with respect to a single partyA
. It queries the Symmio smart contract to obtain the amount of funds each partyB
has allocated towards trading with partyA
.
Returns an array of allocated balance amounts. Each element in the array 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 is used to segregate 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 specifiedpartyB
.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 typically used to ensure that price fluctuations remain within predefined limits, which can be crucial 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 crucial in financial analysis to ensure the uPnl remains 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's particularly useful in trading and financial platforms where understanding the market symbols related to specific trading quotes is necessary for data 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 an exhaustive 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 dynamically 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, making it a versatile tool 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 in the Muon app retrieves current cryptocurrency prices from the MEXC exchange. Here's a detailed breakdown of its implementation:
This function fetches the latest trading prices for various cryptocurrency pairs from the MEXC exchange. It processes the received data to ensure that prices are formatted correctly and returned in a map of symbol-to-price associations tailored 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 in the Muon app fetches current cryptocurrency prices from the Kucoin exchange. Here's a detailed breakdown of its implementation:
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.
Summary
To summarize the Muon app's capabilities and purpose, it offers a range of methods tailored specifically for contract-related scenarios. These methods play a vital role in providing the necessary signed data for SYMM 3rd party frontends (e.g. Cloverfield) smart contracts. By enabling functions such as fetching open positions, calculating uPnl, and retrieving external price data, the app facilitates seamless interaction with contracts, empowering users and developers to effectively manage and monitor their investments. With a strong emphasis on contract use cases, the Muon app serves as a valuable tool for securely integrating blockchain technology into various applications.
Last updated