Create your own token on Solana in less than 5 minutes!

Create your own token on Solana in less than 5 minutes!

Introduction

In this guide, we are going to use the fresh thirdweb Solana SDK to deploy a token!

Before we get started, below are some helpful resources where you can learn more about the tools we will use in this guide.

Let's get started!

Setup

Create a folder to create our project and cd into it-

mkdir solana-token
cd solana-token

We will use node.js to write and run a script that will deploy our token! So, initialize an empty node project-

npm init -y

Now, we need to install some packages that we will use, so install them-

npm i @thirdweb-dev/sdk dotenv # npm

yarn add @thirdweb-dev/sdk dotenv # yarn

Getting wallet private key

To deploy the contract from our wallet, we need the private key of our wallet. So, go to your phantom wallet (if you haven't already created one, check out this guide) and click on the top right badge and select your wallet. Then click on the export private key button:

image.png

Copy this private key, and paste it into a new file .env in the following format:

PRIVATE_KEY=<your-private-key>

Using private keys as an env variable is vulnerable to attacks and is not the best practice. We are doing it in this guide for the sake of brevity, but we strongly recommend using a secret manager to store your private key.

Writing the script

Now that we have got everything set up it is time to write our script! Create a new file called deploy-token.mjs in a scripts folder

Initializing the SDK

We need to initialize the thirdweb SDK with the private key that we just exported into our .env file so add the following code:

import { ThirdwebSDK } from "@thirdweb-dev/sdk/solana";
import { config } from "dotenv";
config();

// Instantiate the SDK and pass in a signer
const sdk = ThirdwebSDK.fromPrivateKey("devnet", process.env.PRIVATE_KEY);

Creating the token

For creating the token, we will use the SDK that we initialized and some metadata, so add the following:

// Define the metadata for your program
const metadata = {
  symbol: "TOK",
  description: "My token",
  name: "My Token",
  initialSupply: 100,
};

// And deploy a new program from the connected wallet
const address = await sdk.deployer.createToken(metadata);

console.log(address);
console.log("Contract deployed successfully! 🎉");

You need to change the details in the metadata object to your values.

Getting the token supply

We will also show the total token supply once the contract gets deployed so add this block as well:

const token = await sdk.getToken(address);
const supply = await token.totalSupply();

console.log(supply);

Running the script

Now, it's the moment of truth! We will run the script and see if our token get's deployed:

node scripts/deploy-token.mjs

Wait for a second for the script to run 🥁 🥁 anddd it works!

image.png

We now have our own token deployed on Solana! How cool is that? You can even see it in your phantom wallet

image.png

Conclusion

In this guide, we learned how to use the Solana SDK with node.js and deploy a token. If you made your own token pat yourself on the back and share it with us on the thirdweb discord! If you want to take a look at the code, check out the GitHub Repository!