Skip to main content

Fill orders

This page explains how to fill an order using the Immutable Unity Orderbook package. For fulfilment to occur, the gamer must have sufficient funds to cover the asking price of the order.


⚠️Warning
This package is in alpha and may be unstable.
📋Prerequisites
💡Marketplace
To display active listings in your in-game marketplace, you can utilise the Unity zkEVM API package. For an example, refer to this page.

To fulfil an order or buy an NFT, you need to send 1–2 transactions:

  1. An approval transaction (if required, as determined by the API).
  2. A fulfilment transaction to complete the order.
💡Fees
Read more about fees here
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 FulfilOrderUseCase
{
private static readonly Lazy<FulfilOrderUseCase> s_Instance = new(() => new FulfilOrderUseCase());

private readonly OrderbookApi m_OrderbookApi = new(new Configuration { BasePath = "https://api.immutable.com" });

private FulfilOrderUseCase() { }

public static FulfilOrderUseCase Instance => s_Instance.Value;

/// <summary>
/// Executes an order by fulfilling a listing and optionally confirming its status.
/// </summary>
/// <param name="listingId">The unique identifier of the listing to fulfil.</param>
/// <param name="fees">The taker fees</param>
public async UniTask ExecuteOrder(string listingId, List<FulfillOrderRequestTakerFeesInner> fees)
{
try
{
var request = new FulfillOrderRequest(
takerAddress: SaveManager.Instance.WalletAddress,
listingId: listingId,
takerFees: fees);

var createListingResponse = await m_OrderbookApi.FulfillOrderAsync(request);

if (createListingResponse.Actions.Count > 0)
{
foreach (var transaction in createListingResponse.Actions)
{
var transactionHash = await Passport.Instance.ZkEvmSendTransaction(new TransactionRequest
{
to = transaction.PopulatedTransactions.To,
data = transaction.PopulatedTransactions.Data,
value = "0"
});
Debug.Log($"Transaction hash: {transactionHash}");
}
}
}
catch (ApiException e)
{
Debug.LogError($"API Error: {e.Message} (Status: {e.ErrorCode})");
Debug.LogError(e.ErrorContent);
Debug.LogError(e.StackTrace);
throw;
}
}
}

The fulfilment transaction is now being processed. The order will asynchronously transition to FILLED once on-chain events are 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.