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

4. Opening/Closing a Position On-Chain

4. Opening/Closing a Position On-Chain

Opening a position

At this stage, the solver calls openPosition() or a combined function (lockAndOpenQuote()).

  • Partial fills are possible: if the hedger only wants to open half the quantity, the remainder can be reposted as a new intent.

  • The contract runs a final solvency check via Muon. If either party is insolvent, the contract disallows opening.

Before a hedger can lockQuote() or openPosition(), enough collateral must be allocated to PartyB. This allocation ensures PartyB meets the margin requirements for the position. If the allocated balance is insufficient, the contract rejects the lockQuote or openPosition transaction.

Allocating collateral for PartyB

A short script showing how a solver allocates collateral to a PartyA. There's more here.

async function allocateForPartyB(amount, partyB, partyA) {
  try {
    console.log(`Allocating ${amount} tokens for PartyB with address ${partyA} from ${partyB}...`);

    // Estimate gas for the transaction
    const allocateGasEstimate = await diamondContract.methods.allocateForPartyB(amount, partyA).estimateGas({ from: partyB });
    console.log("Estimated Gas: ", allocateGasEstimate);

    // Fetch current gas price
    const allocateGasPrice = await web3.eth.getGasPrice();
    console.log("Current Gas Price: ", allocateGasPrice);

    // Execute the allocation transaction
    const receipt = await diamondContract.methods.allocateForPartyB(amount, partyA).send({
      from: partyB,
      gas: allocateGasEstimate,
      gasPrice: allocateGasPrice,
    });

    console.log("Allocation successful!");
    return { success: true, receipt: receipt };
  } catch (error) {
    console.error("Error during allocation:", error);
    return { success: false, error: error.toString() };
  }
}

Locking the quote

Locking the quote ensures no other hedger can act on the same quoteId. It requires a valid Muon signature for the position's uPnl (uPnl_B); there's more on that method here. Pass the formatted signature and quoteId to lockQuote().

Opening the position

Once the quote is locked, the solver calls openPosition() to finalize the transaction. This method runs a solvency check before opening.

The Muon uPnlWithSymbolPrice method is used for solvency verification. On success, the positionState changes to OPENED. If either party is insolvent, the contract rejects the transaction.

Combined method: lockAndOpenQuote()

For efficiency, solvers can use combined methods like lockAndOpenQuote(), which run the locking and opening steps in a single transaction.

Both a uPnlSig (from uPnL_B) and a pairUpnlSig (from uPnlWithSymbolPrice) are required for this group action.

Once opened, the position stays active until fully closed or liquidated. PartyA can place limit or market close requests, which the solver fills. If the solver doesn't respond within a certain timeframe, PartyA can force close or force cancel.

Closing a position

PartyA calls requestToCloseQuote() with the parameters (quantity, price if limit, and so on). The solver can close its corresponding off-chain hedge, then call fillCloseRequest() on-chain.

Closing a position on Symmio means calling fillCloseRequest(). The solver (PartyB) can close an open position partially or fully, depending on the request type and amount.

How fillCloseRequest works

Parameters:

  • quoteId: the unique identifier of the quote to close.

  • filledAmount: the amount being closed.

  • closedPrice: the final price at which the position closes.

  • upnlSig: a Muon signature that ensures solvency and validates the close.

LIMIT requests can be closed incrementally in multiple steps; MARKET requests must be closed in a single transaction.

Preconditions for closing

  • The hedger (PartyB) must be authorized to act on the quote. If not, contact the SYMMIO developers about whitelisting.

  • A Muon signature validates solvency for both parties before the close is processed.

  • The position must not be in the LIQUIDATED state, and PartyA must be solvent after the close.

Closing the position script

This script lets PartyB close a trade initiated by PartyA via fillCloseRequest() on the smart contract. The Muon signature is used for solvency validation; the correct Muon method here is also uPnlWithSymbolPrice.

Last updated