Let's first look at how we can fetch the markets from the hedger, which contains the autoSlippage for a specific pair.
Fetching the Market by SymbolId
The fetchMarketSymbolId() function is designed to asynchronously fetch market data from specified URLs by the Symbol ID. Here we're simply querying the hedger for symbols, then filtering them by the provided symbol. At the end we return the market object. The 'error_codes' API endpoint helps us understand the meaning of error codes.
url (String): The base URL from which the contract-symbols and error_codes endpoints can be constructed.
symbol (String): The market symbol for which detailed information is requested.
Returns:
filteredMarkets (Object): The information related to the symbol.
errorMessages(String): Any error messages associated with the hedger query
For more information on the returned data, consult the documentation for this API query.
Fetching the Locked Parameters
The fetchLockedParams() function asynchronously retrieves the locked values required to enter a position for a trading pair. This is the cva, the lf, and the mm. These variables are percentages of the notional value and will fluctuate depending on the leverage and the pair.
pair (String): The currency pair for which to fetch locked parameters (e.g., "BTCUSD").
leverage (Number): The leverage level for which the parameters are to be fetched.
Returns:
An object containing the following keys with their respective values from the fetched data:
cva (Number): The Credit valuation adjustment. This is
partyAmm (Number): PartyA's maintenance margin.
lf (Number): The liquidation fee awarded to liquidators.
leverage (Number): The leverage.
partyBmm (Number): PartyA's maintenance margin.
Now that we've declared these required functions, we can move onto defining the sendQuote() function. For this example we'll be sending a MARKET order.
Executing a MARKET Order
First we need to determine the requestedPrice of the asset. This is the market price from the Muon Signature +- the slippage (either from the hedger or determined by the user).
asyncfunctionexecuteSendQuoteMarket(subAccount, positionType, quantity, slippage) {const { markets } =awaitfetchMarketSymbolId(config.HEDGER_URL,userConfig.SYMBOL);constlockedParams=awaitfetchLockedParams(markets[0].name,userConfig.LEVERAGE);constautoSlippage= markets[0].autoSlippage;constsignatureResult=awaitgetMuonSigImplementation(subAccount);let adjustedPrice =BigInt(signatureResult.signature.price); //Getting Price from the Muon Signaturelet numericSlippage;if (signatureResult.success) {if (slippage ==="auto") {constautoSlippageNumerator=BigInt(Math.floor(autoSlippage *1000));constautoSlippageDenominator=BigInt(1000); adjustedPrice = positionType ===1? (adjustedPrice * autoSlippageDenominator) / autoSlippageNumerator: (adjustedPrice * autoSlippageNumerator) / autoSlippageDenominator; } else { numericSlippage =Number(slippage); if (isNaN(numericSlippage)) {console.error("Slippage must be a number or 'auto'");return; }constspSigned= positionType ===1? numericSlippage :-numericSlippage;constslippageFactored= (100- spSigned) /100;constslippageFactorBigInt=BigInt(Math.floor(slippageFactored *100)); adjustedPrice = (adjustedPrice * slippageFactorBigInt) /BigInt(100); }constrequestedPrice= adjustedPrice;
The next step is to format the signature we received from Muon so it can be processed by the smart contract. This is done by converting the reqId and gatewaySignature to bytes arrays, and converting the other values to strings:
const { reqId,timestamp,upnl,price,gatewaySignature,sigs } =signatureResult.signature;console.log("Price of asset: ", price);if (typeof reqId ==='undefined'||!reqId.startsWith('0x')) {console.error("reqId is undefined or not a hex string:", reqId); }if (typeof gatewaySignature ==='undefined'||!gatewaySignature.startsWith('0x')) {console.error("gatewaySignature is undefined or not a hex string:", gatewaySignature); }constupnlSigFormatted= { reqId:web3.utils.hexToBytes(reqId), timestamp:timestamp.toString(), upnl:upnl.toString(), price:price.toString(), gatewaySignature:web3.utils.hexToBytes(gatewaySignature), sigs: { signature:sigs.signature.toString(), owner:sigs.owner, nonce:sigs.nonce, } };constpartyBsWhiteList= [config.PARTY_B_WHITELIST];constsymbolId= markets[0].id;constorderType=1; // This will be a MARKET order
Using the Locked Values
The locked values provided by the hedger should be implemented by applying them to the notional value of the quote. This can be done with the following logic:
To finalize the transaction, first encode it using the ABI from the sendQuote() function within the Diamond Facet. Then, leverage the _call function from the MultiAccount contract to pass the encoded data (_callData).