Tokens
Connect to your Token program by providing the on-chain address of the program:
- React
- Javascript
import { useProgram } from "@thirdweb-dev/react/solana"
export default function Component() {
const { program } = useProgram("{{program_address}}", "token")
...
}
import { ThirdwebSDK } from "@thirdweb-dev/sdk/solana";
const sdk = ThirdwebSDK.fromNetwork("devnet");
sdk.wallet.connect(signer);
// Get the interface for your token program
const program = await sdk.getProgram("{{program_address}}", "token");
Get Token Metadata
- React
- Javascript
const metadata = await program.getMetadata();
console.log(metadata.supply);
console.log(metadata.decimals);
const metadata = await program.getMetadata();
console.log(metadata.supply);
console.log(metadata.decimals);
Get Token Balance
- React
- Javascript
import { useProgram, useTokenBalance } from "@thirdweb-dev/react/solana";
export default function Component() {
const { program } = useProgram("{{program_address}}");
const { data: balance, isLoading } = useTokenBalance(program, "{{wallet_address}}");
return (
<p>{balance}</p>
)
}
const address = "..."
const balance = await program.balanceOf(address);
console.log(balance.displayValue);
Mint Tokens
- React
- Javascript
import { useProgram, useMintToken } from "@thirdweb-dev/react/solana";
export default function Component() {
const { program } = useProgram("{{program_address}}");
const { mutateAsync: mint, isLoading, error } = useMintToken(program);
return (
<button onClick={() => mint({ to: "{{wallet_address}}", amount: 1 })}>
Mint
</button>
)
}
// Specify the address to mint tokens to
const address = "{{wallet_address}}"";
// And the amount of tokens to mint
const amount = 1;
// And then you can make a mint transaction
const tx = await program.mintTo(address, 1);
Transfer Tokens
- React
- Javascript
import { useProgram, useTransferToken } from "@thirdweb-dev/react/solana";
export default function Component() {
const { program } = useProgram("{{program_address}}");
const { mutateAsync: transfer, isLoading, error } = useTransferToken(program);
return (
<button onClick={() => transfer({ to: "{{wallet_address}}", amount: 1 })}>
Transfer
</button>
)
}
// Specify the address to transfer tokens to
const to = "...";
// And the amount of tokens to transfer
const amount = 1;
// And then you can make the transfer transaction
const tx = await program.transfer(to, amount);