Obtaining Muon Signatures
For sensitive functions like opening and closing a trade, Muon signatures are required to be passed and verified on the contracts to verify the user's upnl and the price of an asset. This is done through a series of processes. For a trading bot, we'll structure Muon queries into three main parts: HTTP utility functions, a base client for handling requests to the Muon network, and a specialized client for fetching quotes and signatures.
Making a HTTP Request
The HTTP utility module provides a basic functionality to make HTTP GET requests using axios
, a promise-based HTTP client.
This code snippet sends an HTTP GET request to a specified URL and returns the response data. We'll export this module and use it in the base client.
Base Client
Overview
MuonClient
serves as the base class for making requests to the Muon network. It constructs URLs with appropriate parameters for the network requests. Here's how you can set up a base client, which will be used by extended classes to carry out specific queries to Muon.
Parameters:
options
(Object): Configuration options for the Muon client. IncludesAPP_METHOD
to specify the method for the Muon application.
Here's a full list of the App Methods you can use to query Muon:
'uPnl_A':
Returns the uPnl of partyA.**
'partyA_overview':
**Returns an the prices of partyA's assets upon liquidation.'verify':
Returns liquidation signatures.**
'uPnl_A_withSymbolPrice':
**Returns partyA's uPnl with the price of a symbol.**
'uPnl_B':
**Returns partyB's uPnl.**
'uPnl':
**Returns the uPnl of both parties at a timestamp.'uPnlWithSymbolPrice':
Returns both parties' uPnl with a symbol price.'price':
Returns an array of prices from an array of quotes.
Sending a Request
The _sendRequest()
function constructs a request URL with the given parameters and sends an HTTP GET request to the Muon network.
Parameters:
baseUrl
(String): The base URL for the Muon network endpoint.appName
(String): The name of the application making the request ("symmio"
)requestParams
(Array): A list of parameters required for the request, structured as key-value pairs.
Returns: An object indicating the success of the request and containing the result or error information.
Finally we'll export this class:
Creating the Quotes Client
The QuotesClient
class extends MuonClient
to interact with the Muon network for the purpose of fetching the price of a symbol with the upnl of partyA
. In order to send a quote, the signature received must be verified on SYMM's contracts.
Constructor
Initializes the QuotesClient
with a predefined application method.
APP_METHOD
: Set to"uPnl_A_withSymbolPrice"
, specifying the Muon network method to be used for fetching quotes and signatures related to unrealized profit and loss (uPnl) and symbol price.
Creating an Instance
A static factory method to conditionally create an instance of QuotesClient
.
Parameters:
isEnabled
(Boolean): A flag to determine whether an instance should be created.
Returns:
An instance of
QuotesClient
ifisEnabled
istrue
.null
ifisEnabled
isfalse
.
Obtaining the Muon Signature
The getMuonSig()
function fetches a cryptographic signature from the Muon network based on provided parameters. It formats the generated signature so it can be passed to the contract without causing errors.
Parameters:
account
(String): ThesubAccount
address.appName
(String): The name of the Muon app ("symmio"
)urls
(Array): An array of URLs for the Muon network endpoints to try in sequence.chainId
(Number): The blockchain chain ID.contractAddress
(String): The address of the Diamond contract.marketId
(Number): A unique identifier for the market.
Returns: A promise that resolves to an object with two properties:
success
(Boolean): Indicates whether the request was successful.signature
(Object): The signature data, available only ifsuccess
istrue
.error
(String): An error message, available only ifsuccess
isfalse
.
Getting Request Params
This is a helper function to make sure all fields for the Muon request are filled. It constructs the request parameters required by the Muon network.
Parameters:
The same as for
getMuonSig
.
Returns:
An array of key-value pairs representing the request parameters.
An
Error
object if any parameter is missing.
Finally we'll export the QuotesClient
so it can be used in our main code:
Implementing for Sending a Quote
Here's how you can implement the getMuonSig()
function in the main bot (using pre-defined values).
Last updated