thirdweb

Connect to the blockchain and use our SDK

Published on:

February 6, 2022

Providers and Signers are used to connect with the blockchain. Find out how to do this to use our sdk

Intro

Connecting and interacting with a blockchain from the frontend works slightly different than you might be used too. In this guide we will dive deeper into what type of connection you need to perform certain interactions with a blockchain.

Provider

The provider is an easy way to connect to a block chain. When you just want to read data from the blockchain, the provider is the best way to connect your app to the blockchain.

Regardless of using thirdweb's sdk or another sdk, you will need a provider to establish this connection.

So a provider is a simple connection and this is what the connection looks like in terms of code:

import { ethers } from ethers const rpcUrl = "https://polygon-rpc.com/"; const provider = ethers.getDefaultProvider(rpcUrl);

A provider contains the information to tell our sdk which blockchain you want to use. A RPC url is the information the provider needs to decide which blockchain to retrieve data from.

RPC is a simple form of API interaction and stands for Remote Procedure Call. In other words we will use this url to make an API call to the chain.

Using a read only provider with thirdweb

The thirdwebs sdk can be used to interact with your modules. To use our sdk to read data from the blockchain we need to create a connection with a provider. Then we tell it which blockchain to target via the RPC url.

Make sure you have the packages installed. npm install @3rdweb/sdk for JavaScript or TypeScript and pip install thirdweb-sdk for Python.

import { ethers } from "ethers"; import { ThirdwebSDK } from "@3rdweb/sdk"; // This url indicates which chain you want to connect to const rpcUrl = "https://polygon-rpc.com/"; const provider = ethers.getDefaultProvider(rpcUrl); const sdk = new ThirdwebSDK(provider);

Here's an example of how to use the provider in an use case where you just want to retrieve data, like getting the number of NFTs you've minted so far. **

// Initialize nft module by passing in the module address const nftModuleAddress = "0xD9bC403529471515F16DA32eee652c0a6cEcBA78"; const nft = sdk.getNFTModule(nftModuleAddress); nft.balance();

You can find the addresses of your modules inside your dashboard.

By now you should have some modules deployed. If you haven't, check out this page to create and deploy a module. If you don't know what modules are, click here!

Here's another example, to get all the listings in our market we use the getAllListings method.

// Initialize market module by passing in the module address const marketplaceModuleAddress = "0xC06adC34097afa2085324D4192fbE9206059f8e0"; const marketplace = sdk.getMarketplaceModule(marketModuleAddress); // Get all the listings marketplace.getAllListings();

Signer

The Signer represents a connection established with a wallet. This connection has access to methods, which prompts the wallet to authorise and sign transactions, such as minting an NFT or transferring tokens.

If you want to mint NFTs or claim tokens, then you want to write data on the blockchain. In order to do this we need to enrich the Provider with a Signer. We explained in the previous step why we need the Provider.

The Provider doesn't hold any interface or connection to a wallet, it can only read from the blockchain and thus requires less security and care.

The Signer is necessary to authorize transactions. When you mint an NFT or claim a token, you perform a transaction on the blockchain. Every transaction needs to be authorized by the user. To authorize a transaction, the user needs to sign the transaction with their wallet. The Signer has access to methods that can prompt the connected wallet to authorize the relevant transaction

You might have seen this process while using your wallet on any web3 app. You want to buy an NFT or claim a token and then your wallet pops up in your browser to confirm the transaction.

Connect your users wallet with thirdweb

A wallet contains sensitive information and you do not want this data accessible by anyone except for the owner! At thirdweb we take security extremely seriously. That's why we developed a connect wallet flow for your users to sign transactions, without their sensitive data being exposed, by obtaining a Signer.

The user does not need to worry that we can see their private keys. That is not possible. Every transaction still needs to be approved by the user. The Signer just allows our sdk to handle the heavy lifting once the user authorizes the transaction from the frontend.

thirdweb has a component to get the necessary information from the wallet to provide context to the Provider , specifically the Signer, and we then use that provider to instantiate an sdk that can perform write methods.

Click here for a guide on how to obtain a Signer by including a Connect Wallet button in your app.

Check out this guide or this guide to see examples of using Signer with the thirdweb sdk.

Using your own signer with thirdweb

warning: this method is not suitable for frontend

There are cases where you might want to run a script or integrate our sdk on the backend. In this case the Connect Wallet frontend component cannot be used.

Another way to connect to the blockchain in this case, is by passing the Private Key of your wallet directly, when instantiating the sdk. To understand more about how to do this properly (i.e. making sure your Private Key is not exposed), check out this guide.

When opting for this method to establish a connection to the blockchain, you will need to explicitly pass the RPC url too, to tell the sdk which chain you want to connect.

Here's an example of doing this in TypeScript👇

// Instantiate thirdrdweb SDK const sdk = new ThirdwebSDK( new ethers.Wallet( // Your wallet private key process.env.PRIVATE_KEY as string, // RPC URL ethers.getDefaultProvider("https://rinkeby-light.eth.linkpool.io/") ) );

And here is one in Python 👇

# choose your network network = "https://rinkeby-light.eth.linkpool.io/" # create the object sdk = ThirdwebSdk(SdkOptions(), network) # use the '.env' file inside Python loadenv("./.env") PRIVATE_KEY = os.getenv('PRIVATE_KEY') # over here we connect our wallet via the private key stored in the '.env' file sdk.set_private_key(PRIVATE_KEY)

Ready to build your first web3 app? Get early access & add web3 features to your project today.

Contents

Intro

Provider

Using a read only provider with thirdweb

Signer

Connect your users wallet with thirdweb

Using your own signer with thirdweb