Interact With The Blockchain
One connected, embedded wallets can be used alongside the Contract SDK to interact with the blockchain.
View all of the wallet actions available on a wallet instance.
Initialize the SDK With Your Wallet
If you are working in React, React Native or Unity and have connected your wallet with the Connect Wallet Component you can skip this step - your wallet can already be used with the SDKs.
- TypeScript
- React
- React Native
To initialize the TypeScript SDK with your wallet, use the fromWallet
method:
import { ThirdwebSDK } from "@thirdweb-dev/sdk";
import { EmbeddedWallet } from "@thirdweb-dev/wallet";
const wallet = new EmbeddedWallet();
const sdk = ThirdwebSDK.fromWallet(wallet, "ethereum", {
clientId: "YOUR_CLIENT_ID", // Use client id if using on the client side, get it from dashboard settings
secretKey: "YOUR_SECRET_KEY", // Use secret key if using on the server, get it from dashboard settings
});
To initialize the React SDK with your wallet, use the getSigner
method to get the signer and pass it to the ThirdwebSDKProvider
which wraps your app:
import {
ThirdwebSDKProvider,
useSigner,
embeddedWallet,
useConnect,
} from "@thirdweb-dev/react";
function MyApp() {
const wallet = embeddedWallet();
const connect = useConnect();
await connect(wallet);
// this will only work if the user is already connected to your app with embedded wallet
const signer = useSigner();
return (
<ThirdwebSDKProvider
activeChain={"ethereum"}
// Example: Use ethers to get the signer from the window.ethereum object
signer={signer}
clientId="your-client-id"
>
<App />
</ThirdwebSDKProvider>
);
}
To initialize the React Native SDK with your wallet, use the getSigner
method to get the signer and pass it to the ThirdwebSDKProvider
which wraps your app:
import { ThirdwebSDKProvider } from "@thirdweb-dev/react";
import { EmbeddedWallet } from "@thirdweb-dev/wallet";
function MyApp() {
const wallet = new EmbeddedWallet();
const signer = wallet.getSigner();
return (
<ThirdwebSDKProvider
activeChain={"ethereum"}
// Example: Use ethers to get the signer from the window.ethereum object
signer={signer}
clientId="your-client-id"
>
<App />
</ThirdwebSDKProvider>
);
}
Execute a Write Function
- TypeScript
- React
- React Native
- Unity
const contract = await sdk.getContract("0x...");
const data = await contract.call(
"myFunctionName", // Name of your function as it is on the smart contract
// Arguments to your function, in the same order they are on your smart contract
[
"arg1", // e.g. Argument 1
"arg2", // e.g. Argument 2
],
);
import { useContractWrite, useContract, Web3Button } from "@thirdweb-dev/react";
// Your smart contract address
const contractAddress = "{{contract_address}}";
function App() {
const { contract } = useContract(contractAddress);
const { mutateAsync, isLoading, error } = useContractWrite(
contract,
"setName",
);
return (
<Web3Button
contractAddress={contractAddress}
// Calls the "setName" function on your smart contract with "My Name" as the first argument
action={() => mutateAsync({ args: ["My Name"] })}
>
Send Transaction
</Web3Button>
);
}
import {
useContractWrite,
useContract,
Web3Button,
} from "@thirdweb-dev/react-native";
// Your smart contract address
const contractAddress = "{{contract_address}}";
function App() {
const { contract } = useContract(contractAddress);
const { mutateAsync, isLoading, error } = useContractWrite(
contract,
"setName",
);
return (
<Web3Button
contractAddress={contractAddress}
// Calls the "setName" function on your smart contract with "My Name" as the first argument
action={() => mutateAsync({ args: ["My Name"] })}
>
Send Transaction
</Web3Button>
);
}
TransactionResult result = await contract.Write("functionName");
Get The Signer/Address
- TypeScript
- React
- React Native
//Get the wallet signer (Wallet SDK)
const signer = await wallet.getSigner();
//Get the wallet address (Wallet SDK)
const address = await wallet.getAddress();
import { useSigner, useAddress } from "@thirdweb-dev/react";
//Get the wallet signer
const signer = useSigner();
//Get the wallet address
const address = useAddress();
import { useSigner, useAddress } from "@thirdweb-dev/react-native";
//Get the wallet signer
const signer = useSigner();
//Get the wallet address
const address = useAddress();
View The Owned NFTs
- TypeScript
- React
- React Native
- Unity
// Address of the wallet to get the NFTs of
const contract = await sdk.getContract("0x...");
const address = wallet.getAddress(); // Optional - Defaults to the connected wallet
const nfts = await contract.erc721.getOwned(address);
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);
}
import {
useOwnedNFTs,
useContract,
useAddress,
} from "@thirdweb-dev/react-native";
// Your smart contract address
const contractAddress = "{{contract_address}}";
function App() {
const address = useAddress();
const { contract } = useContract(contractAddress);
const { data, isLoading, error } = useOwnedNFTs(contract, address);
}
var data = await contract.ERC721.GetOwned();
Full Reference
View everything you can do in the Contract SDK once you have connected your wallet: