# Auctions

**URL:** https://heroiclabs.com/docs/hiro/unity/auctions/
**Keywords:** auctions, hiro
**Categories:** hiro, unity, auctions

---


# Auctions

Read more about the Auctions system in Hiro [here](../../concepts/auctions/).

## Initializing the auctions system

The auctions system relies on the [Nakama System](../getting-started/nakama-system) and an `ILogger`, both must be passed in as dependencies via the constructor.

```csharp
var auctionsSystem = new AuctionsSystem(logger, nakamaSystem);
systems.Add(auctionsSystem);
```

## Subscribing to changes in the auctions system

You can listen for changes in the auctions system so that you can respond appropriately, such as updating the UI, by implementing the `IObserver` pattern, or use the `SystemObserver<T>` type which handles it for you.

```csharp
var disposer = SystemObserver<AuctionsSystem>.Create(auctionsSystem, system => {
    Instance.Logger.Info($"System updated.");

    // Update UI elements etc as necessary here...
});
```

## Refreshing the auctions system

To ensure the auctions system has the latest information from Nakama you can refresh it.

```csharp
await auctionsSystem.RefreshAsync();
```

## Creating a New Auction

To create a new auction, use the `CreateAsync` function.

```csharp
var itemInstanceIds = new List<string> { "item_instance_001", "item_instance_002" };
string auctionId = "template_auction_001";
string conditionId = "condition_001";

var auction = await auctionsSystem.CreateAsync(auctionId, conditionId, itemInstanceIds);
Console.WriteLine($"Auction created with ID: {auction.Id}, Start Time: {auction.StartTimeSecDecoded}");
```

## Placing a Bid on an Auction

You can place a bid on an auction using the `BidAsync` function.

```csharp
var currencies = new Dictionary<string, long>
{
    { "gold", 1000 }
};

string auctionId = "auction_instance_123";
string version = "1.0";

var auction = await auctionsSystem.BidAsync(auctionId, version, currencies);
Console.WriteLine($"Bid placed on auction {auction.Id}. Current bid: {auction.BidDecoded}");
```

## Canceling an Auction

If an auction needs to be canceled before it ends, the `CancelAsync` method can be used.

```csharp
string auctionId = "auction_instance_123";

var canceledAuction = await auctionsSystem.CancelAsync(auctionId);
Console.WriteLine($"Auction {canceledAuction.Auction.Id} has been canceled.");
```

## Claiming a Winning Bid

Once an auction has ended, the winning bidder can claim their item using `ClaimBidAsync`.

```csharp
string auctionId = "auction_instance_123";

var claimBid = await auctionsSystem.ClaimBidAsync(auctionId);
Console.WriteLine($"Claimed item from auction {claimBid.Auction.Id}. Winning bid: {claimBid.BidAmount}");
```

## Listing Available Auctions

You can list the currently available auctions using the `ListAsync` function.

```csharp
string query = "active";
var sortCriteria = new List<string> { "end_time" };
int limit = 10;

var auctionList = await auctionsSystem.ListAsync(query, sortCriteria, limit);
foreach (var auction in auctionList.Auctions)
{
    Console.WriteLine($"Auction ID: {auction.Id}, Current Bid: {auction.BidDecoded}");
}
```
