Thirdweb 1.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Thirdweb --version 1.3.0                
NuGet\Install-Package Thirdweb -Version 1.3.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Thirdweb" Version="1.3.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Thirdweb --version 1.3.0                
#r "nuget: Thirdweb, 1.3.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Thirdweb as a Cake Addin
#addin nuget:?package=Thirdweb&version=1.3.0

// Install Thirdweb as a Cake Tool
#tool nuget:?package=Thirdweb&version=1.3.0                

net-banner

<img alt="NuGet Version" src="https://img.shields.io/nuget/v/Thirdweb?logo=nuget&style=for-the-badge" height="30"> <img alt="NuGet Downloads" src="https://img.shields.io/nuget/dt/Thirdweb?logo=nuget&style=for-the-badge" height="30"> <img alt="Codecov" src="https://img.shields.io/codecov/c/github/thirdweb-dev/thirdweb-dotnet?logo=codecov&style=for-the-badge" height="30">

Overview

The Thirdweb .NET SDK is a comprehensive library that allows developers to interact with the blockchain using the .NET framework. It simplifies the integration of Web3 functionality into your .NET applications with a robust set of methods and classes and a minimal amount of dependencies.

Features

  • Connect to any EVM network: Easily connect to Ethereum and other EVM-compatible networks.
  • Query blockchain data: Use Thirdweb RPC to fetch blockchain data efficiently.
  • Interact with smart contracts: Simplified read and write operations for smart contracts, with various out-of-the-box extensions provided.
  • In-App Wallets: Integrate user-friendly wallets within your applications, supporting email, phone, and OAuth login.
  • Account Abstraction: Simplify complex account management tasks with smart wallets.
  • Gasless Transactions: Enable transactions without requiring users to pay gas fees.
  • Storage Solutions: Download and upload files using IPFS.
  • Transaction Builder: Easily build and send transactions.
  • Session Keys: Advanced control for smart wallets to manage permissions and session durations.
  • Thirdweb Pay: Easily integrate fiat onramps and cross-chain crypto purchases.
  • Unity Compatibility: This SDK has been tested successfully in Unity 2022.3+ (Standalone, Mobile and WebGL).
  • Godot Compatibility: This SDK has been tested successfully in Godot .NET

Installation

To use the Thirdweb .NET SDK in your project, you can either download the source code and build it manually, or install it via NuGet package manager.

Run the following command to install:

dotnet add package Thirdweb

Usage

You can access the full documentation at https://portal.thirdweb.com/dotnet

Full API reference also available here.

Getting Started

Initialize the Thirdweb client to connect to the blockchain.

For frontend applications:

var client = ThirdwebClient.Create(clientId: "myClientId", bundleId: "com.my.bundleid");

For backend applications:

var secretKey = Environment.GetEnvironmentVariable("THIRDWEB_SECRET_KEY");
var client = ThirdwebClient.Create(secretKey: secretKey);

Interacting with Smart Contracts

You can interact with smart contracts by creating a contract instance and calling read/write methods.

Reading Data

var contract = await ThirdwebContract.Create(client: client, address: "0x81ebd23aA79bCcF5AaFb9c9c5B0Db4223c39102e", chain: 421614);
var readResult = await contract.Read<string>("name");
Console.WriteLine($"Contract read result: {readResult}");

Writing Data

var writeResult = await contract.Write(smartWallet, "mintTo", 0, await smartWallet.GetAddress(), 100);
Console.WriteLine($"Contract write result: {writeResult}");

Using Extensions

Thirdweb comes with very handy prebuilt extensions so you don't have to rely on dynamic parameters, available for any contract object.

// ERC20 balanceOf
var balance = await contract.ERC20_BalanceOf(ownerAddress: "0xOwner");

// DropERC20 (Thirdweb Prebuilt Contract) claim
var claimTx = await contract.DropERC20_Claim(wallet: privateKeyWallet, receiverAddress: "0xReceiver", amount: "1.5");

// Miscellaneous
var nativeContractBalance = await contract.GetBalance(); // Can also take in ERC20 address
var nfts = await contract.ERC721_GetAllNFTs(); // Fetches all NFTs of a contract
var nftImageBytes = await nfts[0].GetNFTImageBytes(client); // NFT type extension to get image bytes

Extensions exist for various common standards, thirdweb-specific prebuilt contracts and much more!

Wallet Interactions

In-App Wallets

In-app wallets facilitate user authentication and transactions with support for email, phone, and OAuth logins.

Email Login

var inAppWallet = await InAppWallet.Create(client: client, email: "email@example.com");

if (!await inAppWallet.IsConnected()) {
    await inAppWallet.SendOTP();
    Console.WriteLine("Please submit the OTP.");
    var otp = Console.ReadLine();
    (var inAppWalletAddress, var canRetry) = await inAppWallet.SubmitOTP(otp);
    if (inAppWalletAddress == null && canRetry) {
        Console.WriteLine("Please submit the OTP again.");
        otp = Console.ReadLine();
        (inAppWalletAddress, _) = await inAppWallet.SubmitOTP(otp);
    }
    if (inAppWalletAddress == null) {
        Console.WriteLine("OTP login failed. Please try again.");
        return;
    }
}

Console.WriteLine($"InAppWallet: {await inAppWallet.GetAddress()}");

OAuth Login

var inAppWallet = await InAppWallet.Create(client, oauthProvider: OAuthProvider.Google);

// Windows console app example
var address = await inAppWallet.LoginWithOauth(
    isMobile: false,
    browserOpenAction: (url) =>
    {
        var psi = new ProcessStartInfo { FileName = url, UseShellExecute = true };
        _ = Process.Start(psi);
    },
);

// Godot standalone example
var address = await ThirdwebManager.Instance.InAppWallet.LoginWithOauth(
        isMobile: OS.GetName() == "Android" || OS.GetName() == "iOS",
        browserOpenAction: (url) => OS.ShellOpen(url),
        mobileRedirectScheme: "thirdweb://"
);
Smart Wallets

Smart wallets offer advanced functionalities such as gasless transactions and session keys.

Creating a Smart Wallet

var smartWallet = await SmartWallet.Create(personalWallet: inAppWallet, gasless: true, chainId: 421614);

Console.WriteLine($"Smart Wallet: {await smartWallet.GetAddress()}");

Gasless Transactions

var writeResult = await contract.Write(smartWallet, "mintTo", 0, await smartWallet.GetAddress(), 100);
Console.WriteLine($"Gasless transaction result: {writeResult}");

Session Key Creation

Session keys provide temporary keys for smart wallets with specific permissions and durations. This is useful for granting limited access to a wallet.

var sessionKey = await smartWallet.CreateSessionKey(
    signerAddress: await privateKeyWallet.GetAddress(),
    approvedTargets: new List<string>() { Constants.ADDRESS_ZERO },
    nativeTokenLimitPerTransactionInWei: "0",
    permissionStartTimestamp: "0",
    permissionEndTimestamp: (Utils.GetUnixTimeStampNow() + 86400).ToString(),
    reqValidityStartTimestamp: "0",
    reqValidityEndTimestamp: Utils.GetUnixTimeStampIn10Years().ToString()
);

You may then connect to a specific smart wallet address by passing an account override.

var smartWallet = await SmartWallet.Create(...same parameters with new signer, accountAddressOverride: "0xInitialSmartWalletAddress");
Using Private Key Wallets

Private key wallets allow you to interact with the blockchain using a private key. This is useful for server-side applications.

var privateKey = Environment.GetEnvironmentVariable("PRIVATE_KEY");
var privateKeyWallet = await PrivateKeyWallet.Create(client: client, privateKeyHex: privateKey);
Console.WriteLine($"PrivateKey Wallet: {await privateKeyWallet.GetAddress()}");

// or generate a private key wallet
var generatedPrivateKeyWallet = await PrivateKeyWallet.Generate(client);

Advanced Features

RPC Direct Access

Directly interact with the blockchain using the RPC instance. This allows for low-level access to blockchain data and functions.

var rpc = ThirdwebRPC.GetRpcInstance(client, 421614);
var blockNumber = await rpc.SendRequestAsync<string>("eth_blockNumber");
Console.WriteLine($"Block number: {blockNumber}");

ZkSync Native Account Abstraction

ZkSync 0x71 (113) type transactions are supported through the Transaction Builder (DIY) or Smart Wallets (Managed).

DIY Approach

var tx = await ThirdwebTransaction.Create(
    wallet: privateKeyWallet,
    txInput: new ThirdwebTransactionInput()
    {
        From = await privateKeyWallet.GetAddress(),
        To = await privateKeyWallet.GetAddress(),
        Value = new HexBigInteger(BigInteger.Zero),
    },
    chainId: 300
);
tx.SetZkSyncOptions(
    new ZkSyncOptions(
        paymaster: "0xMyGaslessPaymaster",
        paymasterInput: "0x8c5a344500000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"
    )
);
var txHash = await ThirdwebTransaction.Send(transaction: tx);
Console.WriteLine($"Transaction hash: {txHash}");

Managed Approach

With ZkSync, you don't need to pass an account factory address, and the rest works the same.

var zkSyncWallet = await SmartWallet.Create(personalWallet: inAppWallet, gasless: true, chainId: 300);

Console.WriteLine($"ZkSync Smart Wallet: {await zkSyncWallet.GetAddress()}");

var zkSyncWriteResult = await contract.Write(zkSyncWallet, "mintTo", 0, await zkSyncWallet.GetAddress(), 100);
Console.WriteLine($"ZkSync gasless transaction result: {zkSyncWriteResult}");

Storage Solutions

Download and upload files using IPFS. This is useful for decentralized storage solutions.

var downloadResult = await ThirdwebStorage.Download<string>(client: client, uri: "ipfs://exampleUri");
Console.WriteLine($"Download result: {downloadResult}");

var uploadResult = await ThirdwebStorage.Upload(client: client, path: "path/to/file");
Console.WriteLine($"Upload result preview: {uploadResult.PreviewUrl}");

Thirdweb Pay

Easily integrate fiat onramps and cross-chain crypto purchases.

Buy With Crypto

// Swap Polygon MATIC to Base ETH
var swapQuoteParams = new BuyWithCryptoQuoteParams(
    fromAddress: walletAddress,
    fromChainId: 137,
    fromTokenAddress: Thirdweb.Constants.NATIVE_TOKEN_ADDRESS,
    toTokenAddress: Thirdweb.Constants.NATIVE_TOKEN_ADDRESS,
    toChainId: 8453,
    toAmount: "0.1"
);
var swapQuote = await ThirdwebPay.GetBuyWithCryptoQuote(client, swapQuoteParams);
Console.WriteLine($"Swap quote: {JsonConvert.SerializeObject(swapQuote, Formatting.Indented)}");

// Initiate swap
var txHash = await ThirdwebPay.BuyWithCrypto(wallet: privateKeyWallet, buyWithCryptoQuote: swapQuote);
Console.WriteLine($"Swap transaction hash: {txHash}");

// Poll for status
var currentSwapStatus = SwapStatus.NONE;
while (currentSwapStatus is not SwapStatus.COMPLETED and not SwapStatus.FAILED)
{
    var swapStatus = await ThirdwebPay.GetBuyWithCryptoStatus(client, txHash);
    currentSwapStatus = Enum.Parse<SwapStatus>(swapStatus.Status);
    Console.WriteLine($"Swap status: {JsonConvert.SerializeObject(swapStatus, Formatting.Indented)}");
    await Task.Delay(5000);
}

Buy With Fiat

// Find out more about supported FIAT currencies
var supportedCurrencies = await ThirdwebPay.GetBuyWithFiatCurrencies(client);
Console.WriteLine($"Supported currencies: {JsonConvert.SerializeObject(supportedCurrencies, Formatting.Indented)}");

// Get a Buy with Fiat quote
var fiatQuoteParams = new BuyWithFiatQuoteParams(
    fromCurrencySymbol: "USD", 
    toAddress: walletAddress, 
    toChainId: "137", 
    toTokenAddress: Thirdweb.Constants.NATIVE_TOKEN_ADDRESS, 
    toAmount: "20"
);
var fiatOnrampQuote = await ThirdwebPay.GetBuyWithFiatQuote(client, fiatQuoteParams);
Console.WriteLine($"Fiat onramp quote: {JsonConvert.SerializeObject(fiatOnrampQuote, Formatting.Indented)}");

// Get a Buy with Fiat link
var onRampLink = ThirdwebPay.BuyWithFiat(fiatOnrampQuote);
Console.WriteLine($"Fiat onramp link: {onRampLink}");

// Open onramp link to start the process (use your framework's version of this)
var psi = new ProcessStartInfo { FileName = onRampLink, UseShellExecute = true };
_ = Process.Start(psi);

// Poll for status
var currentOnRampStatus = OnRampStatus.NONE;
while (currentOnRampStatus is not OnRampStatus.ON_RAMP_TRANSFER_COMPLETED and not OnRampStatus.ON_RAMP_TRANSFER_FAILED)
{
    var onRampStatus = await ThirdwebPay.GetBuyWithFiatStatus(client, fiatOnrampQuote.IntentId);
    currentOnRampStatus = Enum.Parse<OnRampStatus>(onRampStatus.Status);
    Console.WriteLine($"Fiat onramp status: {JsonConvert.SerializeObject(onRampStatus, Formatting.Indented)}");
    await Task.Delay(5000);
}

For more information, please refer to the official documentation.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.8.1 0 11/7/2024
2.7.1 0 11/7/2024
2.7.0 65 11/6/2024
2.6.1 82 10/24/2024
2.6.0 77 10/24/2024
2.5.2 72 10/24/2024
2.5.1 93 10/10/2024
2.5.0 78 10/9/2024
2.4.0 83 10/3/2024
2.3.1 86 10/2/2024
2.3.0 94 9/27/2024
2.2.2 97 9/23/2024
2.2.1 75 9/21/2024
2.2.0 82 9/20/2024
2.1.1 93 9/13/2024
2.1.0 121 9/11/2024
2.0.2 97 9/10/2024
2.0.1 102 9/9/2024
2.0.0 116 9/5/2024
1.5.0 124 8/20/2024
1.4.0 818 8/12/2024
1.3.0 94 8/7/2024
1.2.0 92 8/7/2024
1.1.0 79 8/2/2024
1.0.2 81 7/31/2024
1.0.1 95 7/16/2024
1.0.0 137 7/1/2024
0.4.0 146 6/1/2024
0.3.1 118 5/29/2024
0.3.0 120 5/23/2024
0.2.0 145 5/15/2024
0.1.1 150 4/15/2024
0.1.0 104 4/12/2024
0.0.13 103 4/10/2024
0.0.12 105 4/10/2024
0.0.11 96 4/10/2024
0.0.10 107 4/10/2024
0.0.9 117 4/10/2024
0.0.8 112 4/9/2024
0.0.7 101 4/9/2024
0.0.6 117 4/8/2024
0.0.5 118 4/5/2024
0.0.4 109 4/3/2024
0.0.3 101 4/3/2024
0.0.1 133 3/26/2024
0.0.0 134 3/26/2024