Cancel orders
This page details how to cancel active orders on-chain.
⚠️Warning
This package is in alpha and may be unstable.
📋Prerequisites
- Passport package setup
- Orderbook package setup
- Login
- Initialise provider and connect wallet
- Link all game contracts to the Immutable Hub to enable pre-approved transactions. For detailed instructions, please refer to the guide here.
💡Inventory
To display a gamer's NFT inventory, you can use the Unity zkEVM API package. For an example, refer to this page.
To cancel a listing on-chain:
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using Immutable.Orderbook.Api;
using Immutable.Orderbook.Client;
using Immutable.Orderbook.Model;
using Immutable.Passport;
using Immutable.Passport.Model;
using Newtonsoft.Json;
using UnityEngine;
public class CancelListingUseCase
{
private static readonly Lazy<CancelListingUseCase> s_Instance = new(() => new CancelListingUseCase());
private readonly OrderbookApi m_OrderbookApi = new(new Configuration { BasePath = "https://api.immutable.com" });
private CancelListingUseCase() { }
public static CancelListingUseCase Instance => s_Instance.Value;
/// <summary>
/// Cancels the specified listing.
/// </summary>
/// <param name="listingId">The unique identifier of the listing to cancel.</param>
public async UniTask CancelListing(string listingId)
{
try
{
var request = new CancelOrdersOnChainRequest(
accountAddress: SaveManager.Instance.WalletAddress, orderIds: new List<string> { listingId });
var response = await m_OrderbookApi.CancelOrdersOnChainAsync(request);
var transactionAction = response?.CancellationAction.PopulatedTransactions;
if (transactionAction?.To == null)
throw new Exception("Failed to cancel listing.");
var txResponse = await Passport.Instance.ZkEvmSendTransactionWithConfirmation(
new TransactionRequest
{
to = transactionAction.To,
data = transactionAction.Data,
value = "0"
});
if (txResponse.status != "1")
throw new Exception("Failed to cancel listing.");
}
catch (ApiException e)
{
Debug.LogError($"API Error: {e.Message} (Status: {e.ErrorCode})");
Debug.LogError(e.ErrorContent);
Debug.LogError(e.StackTrace);
throw;
}
}
}
The order will asynchronously transition to CANCELLED
once on-chain events have been registered by Immutable services.
💡Status polling
You can poll the Get Listing endpoint to check on status updates.
For more information, refer to this page.
You can also view a full example in the sample game here. This builds upon the concepts presented in the Build a game with Unity tutorial.