Examples

Complete Examples

Opening a Position and interacting with it

This example demonstrates how to open a position. You can find the complete runnable code in the SDK examples repository (opens in a new tab).

    // Initialize client with testnet configuration
  const config = new MirageConfig({ 
    deployment: Deployment.APTOS_TESTNET 
  })
  const mirageClient = new MirageClient(config)
  const aptosClient = new Aptos(
    new AptosConfig({
      fullnode: getDefaultFullnodeUrl(Deployment.APTOS_TESTNET),
      indexer: getDefaultIndexerUrl(Deployment.APTOS_TESTNET),
    })
  )
  
  // Create account from private key
  // WARNING: In production, use secure key management
  const account = await Account.fromPrivateKey({ 
    privateKey: new Ed25519PrivateKey(process.env.PRIVATE_KEY || '') 
  })
  console.log("Account:", account.accountAddress.toString())
  
  // Example market parameters
  const perpSymbol = "BTCPERP"
  const marginSymbol = "mUSD"
  const marginAmount = 1000
  const positionSize = 0.1
  const entryPrice = 101000
  const maxSlippage = 1000
  const takeProfitPrice = 105000
  const stopLossPrice = 95000
  
  // Create position with take profit and stop loss
  console.log("Opening position with take profit and stop loss...")
  const txPayload = await mirageClient.market.transactions.getCreatePositionWithOrderPayload(
    perpSymbol,
    marginSymbol,
    OrderType.MARKET,
    marginAmount,
    positionSize,
    PositionSide.LONG,
    entryPrice,
    maxSlippage,
    {
      takeProfit: takeProfitPrice,
      stopLoss: stopLossPrice
    }
  )
 
  const tx = await aptosClient.transaction.build.simple({
    sender: account.accountAddress,
    data: txPayload as any
  })
 
  console.log(tx)
  console.log(txPayload)
  
  // Submit transaction
  const submittedTx = await aptosClient.signAndSubmitTransaction(
    {
      signer: account,
      transaction: tx as any
    }
  )
 
  await aptosClient.waitForTransaction({
    transactionHash: submittedTx.hash
  })
 
  console.log(
    `Transaction: https://explorer.aptoslabs.com/txn/${submittedTx.hash}?network=testnet`
  )