Home

Framework Quickstarts

Use Openfort with CSharp

Learn how to get started with Openfort and mint an asset with CSharp.

What you'll learn:#

  • How to manage third-party dependencies using the .NET Core CLI, NuGet CLI or the Package Manager Console
  • How to install the latest Openfort CSharp SDK.
  • How to send your first SDK request.
1

Set up a Openfort CSharp SDK

Open your project in the Openfort Dashboard.

After your project is ready, grab your secret_key and public_key from the project.

Terminal

_10
mkdir openfort-tutorial
_10
cd openfort-tutorial
_10
dotnet new console
_10
dotnet add package Openfort.SDK

2

Add a contract to Openfort

In this tutorial, we'll use a simple ERC-721 contract on the Amoy network deployed at 0x380...AC0.

Once added, Openfort will return a contract id that you can use to interact with the contract. It starts with con_.

.env

_10
YOUR_SECRET_KEY=sk_test_...

Terminal

_10
curl https://api.openfort.xyz/v1/contracts \
_10
-H "Authorization: Bearer $YOUR_SECRET_KEY" \
_10
-d chainId=80002 \
_10
-d address="0xbed6a05ce8719bc00db1cc81a814192c82be1bb1" \
_10
-d name="Simple NFT"

3

Prepare gas sponsorship

First, we'll create a policy to sponsor the gas fees for the contract.

Thebn, we'll create a policy rule to define the contract functions we want to sponsor. In this example, we create a simple policy rule to pay for all user's interactions with the contract.

Grab your contract id and the policy id to include on your call.

Terminal

_10
curl https://api.openfort.xyz/v1/policies \
_10
-H "Authorization: Bearer $YOUR_SECRET_KEY" \
_10
-d chainId=80002 \
_10
-d name="simple sponsor" \
_10
-d "strategy[sponsorSchema]=pay_for_user"

Terminal

_10
curl https://api.openfort.xyz/v1/policies/:id/policy_rules \
_10
-H "Authorization: Bearer $YOUR_SECRET_KEY" \
_10
-d type="contract_functions" \
_10
-d functionName="All functions" \
_10
-d contract="con_..."

4

Mint an NFT

You're all set! Now you can mint an NFT using the contract id and the policy id that you created in the previous steps.

Openfort will encode the transaction based on the provided information in the interaction.

An account will be created and deployed automatically for you on the Amoy network.

Because optimistic is set to false, the response from creating the transactionIntents will contain a response.

Go ahead and replace the code in Program.cs with the following code.

Remember to replace the policy, contract and player ids with your own.

Program.cs

_41
using Openfort.SDK.Model;
_41
_41
class Program
_41
{
_41
static async Task Main()
_41
{
_41
var client = new Openfort.SDK.OpenfortClient("sk_test_...");
_41
string policyId = "pol_...";
_41
string contractId = "con_...";
_41
int chainId = 80002;
_41
bool optimistic = false;
_41
_41
var playerRequest = new CreatePlayerRequest
_41
(
_41
name: "John Doe"
_41
);
_41
PlayerResponse player = await client.Players.Create(playerRequest);
_41
Console.Write("Success! Here is your player id: {0}\n", player.Id);
_41
string TokenId = "1"; // This should be a unique token id
_41
var interactionMint = new Interaction
_41
(
_41
contract: contractId,
_41
functionName: "mint",
_41
functionArgs: new List<object> { player.Id, TokenId }
_41
);
_41
_41
var transactionIntentRequest = new CreateTransactionIntentRequest
_41
(
_41
player: player.Id,
_41
chainId: chainId,
_41
policy: policyId,
_41
externalOwnerAddress: null!,
_41
optimistic: optimistic,
_41
confirmationBlocks: 0,
_41
interactions: new List<Interaction> { interactionMint }
_41
);
_41
_41
TransactionIntentResponse transactionIntent = await client.TransactionIntents.Create(transactionIntentRequest);
_41
Console.Write("Success! Here is your transaction id: {0}\n", transactionIntent.Id);
_41
}
_41
}

5

Run the script

Run the program.

Terminal

_10
dotnet run