thirdweb SDK

... About 2 min

Thirdweb SDK is a library that enables developers to build web3 applications and interact with any EVM-compatible blockchain.

You can use the Thirdweb SDK to build apps and interact with smart contracts deployed on the Rollux network.

The thirdweb SDK is available in various programming languages, including: React (opens new window), React Native (opens new window), TypeScript (opens new window), Python (opens new window), Go (opens new window), and Unity (opens new window).

Visit the Thirdweb documentation (opens new window) for more instructions on using the Thirdweb SDKs.

# Install

To install the Thirdweb SDK, run:

npm install @thirdweb-dev/sdk ethers@5
1

# Initialize the SDK with Rollux

Here's how to initialize the SDK with the Rollux mainnet network and get a contract:

import { Rollux } from "@thirdweb-dev/chains";
import { ThirdwebSDK } from "@thirdweb-dev/sdk";

// If used on the FRONTEND pass your 'clientId'
const sdk = new ThirdwebSDK(Rollux, {
  clientId: "YOUR_CLIENT_ID",
});
// --- OR ---
// If used on the BACKEND pass your 'secretKey'
const sdk = new ThirdwebSDK(Rollux, {
  secretKey: "YOUR_SECRET_KEY",
});

const contract = await sdk.getContract("0x0000000000000000000000000000000000000000");
1
2
3
4
5
6
7
8
9
10
11
12
13
14

You will need to pass a client ID/secret key to use Thirdweb's infrastructure services. If you don't have any API keys yet you can create one for free from your Thirdweb dashboard settings (opens new window).

# Interact with smart contracts

Once you initialize the SDK and connect to a smart contract deployed to Rollux, you can start calling functions on it using the SDK.

Info

Any interaction you make with a smart contract will be made from the connected wallet automatically.

# Use contract extension functions

The thirdweb SDK provides convenience functions when your smart contract uses extensions (opens new window). This is the easiest way to read data and write transactions with your smart contracts.

For example, if your contract implements the ERC721 (opens new window) extension, you can utilize all of the functions of the corresponding erc721 standard (opens new window) in the SDK.

As an example, below is a code snippet that uses useOwnedNFTs (opens new window) hook to get a list of NFTs owned by a single wallet address:

import { useOwnedNFTs } from '@thirdweb-dev/react';

const { data, isLoading, error } = useOwnedNFTs(contract, '{{wallet_address}}');
1
2
3

# Usage

import { useOwnedNFTs, useContract, useAddress } from '@thirdweb-dev/react';

// Your smart contract address
const contractAddress = '{{contract_address}}';

function App() {
  const address = useAddress();
  const { contract } = useContract(contractAddress);
  const { data, isLoading, error } = useOwnedNFTs(contract, address);
}
1
2
3
4
5
6
7
8
9
10

For more examples on using contract extension functions, visit the Thirdweb developer documentation (opens new window).

# Read contract data

If your contract doesn’t use any extensions (opens new window), or you want to directly call functions on your smart contract to read data, you can use the useContractRead (opens new window) hook.

Read data on your contract from a connected wallet:

const { contract } = useContract('{{contract_address}}');
const { data: myData, isLoading } = useContractRead(contract, 'myFunction');
1
2

# Write to contracts

If your contract doesn’t use any extensions, or you want to directly call functions on your smart contract to write data, you can use the useContractWrite (opens new window) hook.

Make transactions on your contract from a connected wallet:

const { contract } = useContract('{{contract_address}}');
const { mutateAsync: myFunctionAsync } = useContractWrite(contract, 'myFunction');
const tx = await myFunctionAsync(['argument1', 'argument2']); // Call the function
1
2
3

Info

For guides on deploying contracts on Rollux, see the tutorial (opens new window).