Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unity SDK Primary Sales Guide #354

Merged
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/pages/guides/primary-sales.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ When you're ready, replace the contents of `.env` with your project's informatio

## Configure your own Primary Sales Contracts in the repository

We provide a few example contracts and variables in order to get you started. However, you will likely wnat to use your own contracts. In order to configure this simply following the steps below:
We provide a few example contracts and variables in order to get you started. However, you will likely want to use your own contracts. In order to configure this simply following the steps below:

::::steps
### Deploy a Primary Sales Contract in Sequence Builder.
Expand Down
134 changes: 134 additions & 0 deletions docs/pages/guides/unity-primary-sales.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
---
title: How to sell On-Chain Items in your Game Client
description: Intro to Jelly Forest - Unity Game Guide introduces a 2D runner game with blockchain features like social sign-in, upgrades, and cosmetic items stored in a smart contract wallet.
JamesLawton marked this conversation as resolved.
Show resolved Hide resolved
---

# How to sell On-Chain Items in Unity
JamesLawton marked this conversation as resolved.
Show resolved Hide resolved

Accelerate your game growth by selling items directly to your players. In this guide, we will go over the steps how
to deploy a Primary Sale contract using any custom or existing currency for an In-Game Shop that utilizes game items
from a ERC1155 contract. We will use the following technologies from the Sequence platform:

- [Primary Sales Contract](/solutions/collectibles/contracts/deploy-primary-sales-contract/): How to set up and deploy contracts for launching a primary sale — suitable for an Web Shop, NFT Drop, and more.
- [Embedded Wallet](/solutions/wallets/embedded-wallet/overview): Use Sequence Kit and Sequence Embedded Wallet to authenticate a user.
JamesLawton marked this conversation as resolved.
Show resolved Hide resolved
- [Sequence Indexer](solutions/builder/indexer): Leveraging the Sequence Indexer to query NFT metadata and user's wallet assets.

## 1. Deploy your own Token- and Sale Contract in Sequence Builder

In the builder you have to create your own token contract and a sale contract specific to your
JamesLawton marked this conversation as resolved.
Show resolved Hide resolved
We first need a Primary Sales Contract along with an ERC1155 contract that will contain our game items we want to sell.
To do that, please follow the [guide](/solutions/collectibles/contracts/deploy-primary-sales-contract) here.

## 2. Use the Primary Sales Demo inside Sequence's Unity SDK

To get started, import [Sequence's Unity SDK](https://github.com/0xsequence/sequence-unity/releases) into your project.
Then, navigate to the `Demo.unity` scene located `Sequence Embedded Wallet SDK/Sequence/Samples/DemoScene`.
In this scene, you’ll find the `PrimarySalePage` object, which serves as a useful reference.

## 3. Set your Sale Configurations in your Project

To configure your sale, use the `PrimarySalePage` object to input your sale information.
In the Configuration section, you’ll find the following options:

- Chain: Select the chain where your contract is deployed.
- Token & Sale Contract Address: Enter the contract addresses provided by Sequence's Builder.
- Items For Sale: List all the Token IDs you wish to sell.

## 4. Implement Custom Code to Interact with the SDK

Let's create a custom class to manage the state for our sale. This class will gather all the necessary data,
allowing us to display this information to users effectively.
In the PrimarySalePage.cs demo, the data is sourced from the Configuration section.

```csharp
class ERC1155SaleState
{
public ERC1155SaleState(IWallet wallet, string tokenContractAddress, string saleContractAddress, Chain chain, int[] itemsForSale)
{
_tokenContractAddress = tokenContractAddress;
_saleContract = new ERC1155Sale(saleContractAddress);
_client = new SequenceEthClient(chain);
_wallet = wallet;
_chain = chain;
_itemsForSale = itemsForSale;
}
}
```

## 5. Retrieve Your Primary Sale Details

Next, use the `ERC1155Sale.cs` reference to obtain sale details and the payment token from the contract.
This information can be used, for instance, to locally verify if the user has sufficient balance for the specified payment token.

```csharp
public async Task<bool> UpdateSaleDetailsAsync()
{
string paymentToken = await _saleContract.GetPaymentTokenAsync(_client);

ERC1155Sale.SaleDetails globalSaleDetails = await _saleContract.GetGlobalSaleDetailsAsync(_client);
BigInteger cost = globalSaleDetails.Cost;
BigInteger supplyCap = globalSaleDetails.SupplyCap;
int startTime = globalSaleDetails.StartTime;
int endTime = globalSaleDetails.EndTime;
}
```

## 6. Fetching Token Metadata to Display Items to Users

We’ll use the Indexer API to retrieve token supplies for the specified token contract address.
Be sure to use the address of the ERC1155 contract, not the sales contract.

```csharp
public async Task UpdateTokenSuppliesAsync()
{
Dictionary<BigInteger, TokenSupply> tokenSupplies = new Dictionary<BigInteger, TokenSupply>();
GetTokenSuppliesArgs supplyArgs = new GetTokenSuppliesArgs(_tokenContractAddress, true);
GetTokenSuppliesReturn suppliesReturn = await Indexer.GetTokenSupplies((int) _chain, supplyArgs);

foreach (int tokenId in _itemsForSale)
{
TokenSupply supply = Array.Find(suppliesReturn.tokenIDs, t => t.tokenID == tokenId);
if (supply == null)
continue;

tokenSupplies.Add(supply.tokenID, supply);
}
}
```

For example, you can use the `supply.tokenMetadata.image` variable from the token supplies mentioned above
to display your items to the user.

```csharp
[SerializeField] private Image _image;

public async void RenderTokenImage(TokenSupply supply)
{
_image.sprite = await AssetHandler.GetSpriteAsync(supply.tokenMetadata.image);
}
```

:::warning
Ensure that you implement your own `AssetHandler` class to handle image downloads from remote URLs.
:::

## 7. Purchase an Item from the Store

This will call the mint function from the Sales Contract. By specifying the user’s wallet address as the `to` parameter,
the contract will mint the item to that user. You can use the `ERC1155Sale.cs` class to create a `CallContractFunction`
reference, which allows you to send a transaction to your sales contract using the user's wallet.

```csharp
public async Task<bool> PurchaseAsync(BigInteger tokenId, int amount)
{
string to = _wallet.GetWalletAddress();
byte[] defaultProof = Array.Empty<byte>();

CallContractFunction contractCall = _saleContract.Mint(to, new[] {tokenId},
new[] {new BigInteger(amount)}, null, PaymentToken, new BigInteger(1), defaultProof);

Transaction[] transactions = new Transaction[] { new RawTransaction(contractCall) };
TransactionReturn result = await _wallet.SendTransaction(_chain, transactions);
return result is SuccessfulTransactionReturn;
}
```
JamesLawton marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,11 @@ export const sidebar = {
collapsed: true,
link: '/guides/building-transaction-heavy-games-with-unity',
},
{
text: 'How to sell On-Chain Items in Unity',
collapsed: true,
link: '/guides/unity-primary-sales',
},
{
text: 'Build a Collectible Minting Service',
collapsed: true,
Expand Down