Minting SPL tokens with solana-web3.js
Leveraging solana-web3.js and spl-token to mint 1000 SPL tokens on Solana.
const solanaWeb3 = require('@solana/web3.js');
const splToken = require('@solana/spl-token');
const bs58 = require("bs58");
async function main() {
const connection = new solanaWeb3.Connection("CHAINSTACK_HTTPS_ENDPOINT", {wsEndpoint:"CHAINSTACK_WSS_ENDPOINT"});
const walletKeyPair = solanaWeb3.Keypair.fromSecretKey(new Uint8Array(bs58.decode(process.env.PRIVATE_KEY)));
const mint = await splToken.createMint(
connection,
walletKeyPair,
walletKeyPair.publicKey,
null,
9,
undefined,
{},
splToken.TOKEN_PROGRAM_ID,
);
const tokenAccount = await splToken.getOrCreateAssociatedTokenAccount(
connection,
walletKeyPair,
mint,
walletKeyPair.publicKey,
);
await splToken.mintTo(
connection,
walletKeyPair,
mint,
tokenAccount.address,
walletKeyPair.publicKey,
1000000000000,
)
solana-web3.js (@solana/web3.js
) is a powerful library that exists as a key tool within the typical Solana developer’s tech stack.
Having already covered sending a simple transaction with solana-web3.js, we’re going to dive into a slightly more complex task: Minting an SPL token natively in solana-web3.js
If you haven’t already, ensure node.js is installed.
You’ll need to install three main dependencies here:
@solana/web3.js
, self-explanatory, this is what you’ll be using to connect and interact with Solana.@solana/spl-token
for interacting with the Solana token program.bs58
for converting a traditional string private key to aUint8Array
in the account definition process.
const solanaWeb3 = require('@solana/web3.js');
const splToken = require('@solana/spl-token');
const bs58 = require("bs58");
To connect to the Solana blockchain, like any other Web3 application, you’ll need to create a master variable with an RPC endpoint.
We’ve defined this in a variable called connection
, and passed in both a Chainstack HTTPS Solana endpoint and a Chainstack WSS Solana endpoint. To retrieve these, do the following:
Navigate to the Chainstack console
Deploy a Solana mainnet node
Open the newly deployed node
Copy the corresponding HTTPS & WSS endpoints
Paste them into your Connection
constructor
const connection = new solanaWeb3.Connection("CHAINSTACK_HTTPS_ENDPOINT", {wsEndpoint:"CHAINSTACK_WSS_ENDPOINT"});
We’ll now need a funded Solana wallet. You can either import an existing one, as we do here, or create a fresh one with solanaWeb3.Keypair.generate()
.
For importing an existing wallet, you’ll need to leverage the fromSecretKey
function on Keypair
. Within this function, you’ll need to pass in your private key converted to a Uint8Array
, which can be achieved through using bs58.decode
in a new Uint8Array
.
const walletKeyPair = solanaWeb3.Keypair.fromSecretKey(new Uint8Array(bs58.decode(process.env.PRIVATE_KEY)));
To build the mint in preparation for pushing it to Solana, we’ll need to use splToken.createMint
. Within this snippet, it’s been saved to a variable called mint
.
Within createMint
, we’ve passed the following parameters:
connection
, our previously defined connection to Solana that leverages Chainstack.walletKeyPair
, the previously defined object containing both your public and private key.walletKeyPair.publicKey
, specifically pulling the public key from thewalletKeyPair
object.null
, this is thefreezeAuthority
parameter, which can either benull
or a public key.9
, the decimal place location for the token being minted.undefined
, filling in an optionalkeypair
parameter.{}
, filling in an optional transaction confirmation instruction parameter.splToken.TOKEN_PROGRAM_ID
for filling in theprogramId
parameter.
const mint = await splToken.createMint(
connection,
walletKeyPair,
walletKeyPair.publicKey,
null,
9,
undefined,
{},
splToken.TOKEN_PROGRAM_ID,
);
We’ll now need to create a corresponding token account for our SPL token. This can be done through the getOrCreateAssociatedTokenAccount
function.
Within this function, we’ve used the following parameters:
connection
, our previously defined connection to Solana that leverages Chainstack.walletKeyPair
, the previously defined object containing both your public and private key.mint
, the previously defined mint object derived from thecreateMint
function.walletKeyPair.publicKey
, specifically pulling the public key from thewalletKeyPair
object.
const tokenAccount = await splToken.getOrCreateAssociatedTokenAccount(
connection,
walletKeyPair,
mint,
walletKeyPair.publicKey,
);
For the last step, we’ll need to take the mint
object, and the tokenAccount
object and actually mint the SPL tokens.
In this snippet, we’re calling the mintTo
function alone without variable assignment. Within this function, we’re using the following variables:
connection
, our previously defined connection to Solana that leverages Chainstack.walletKeyPair
, the previously defined object containing both your public and private key, this is our payer.mint
, the previously defined mint object derived from thecreateMint
function.tokenAccount.address
, this is the address of the associated token account we made; this is being used as the destination address.walletKeyPair.publicKey
is the public key of our wallet, specifically being used here as the minting authority.1000000000000
, the number of tokens being minted. In this case, this’ll result in 1000 tokens being minted, due to the previous decimal configuration of9
.
await splToken.mintTo(
connection,
walletKeyPair,
mint,
tokenAccount.address,
walletKeyPair.publicKey,
1000000000000,
)
const solanaWeb3 = require('@solana/web3.js');
const splToken = require('@solana/spl-token');
const bs58 = require("bs58");
async function main() {
const connection = new solanaWeb3.Connection("CHAINSTACK_HTTPS_ENDPOINT", {wsEndpoint:"CHAINSTACK_WSS_ENDPOINT"});
const walletKeyPair = solanaWeb3.Keypair.fromSecretKey(new Uint8Array(bs58.decode(process.env.PRIVATE_KEY)));
const mint = await splToken.createMint(
connection,
walletKeyPair,
walletKeyPair.publicKey,
null,
9,
undefined,
{},
splToken.TOKEN_PROGRAM_ID,
);
const tokenAccount = await splToken.getOrCreateAssociatedTokenAccount(
connection,
walletKeyPair,
mint,
walletKeyPair.publicKey,
);
await splToken.mintTo(
connection,
walletKeyPair,
mint,
tokenAccount.address,
walletKeyPair.publicKey,
1000000000000,
)
const solanaWeb3 = require('@solana/web3.js');
const splToken = require('@solana/spl-token');
const bs58 = require("bs58");
async function main() {
const connection = new solanaWeb3.Connection("CHAINSTACK_HTTPS_ENDPOINT", {wsEndpoint:"CHAINSTACK_WSS_ENDPOINT"});
const walletKeyPair = solanaWeb3.Keypair.fromSecretKey(new Uint8Array(bs58.decode(process.env.PRIVATE_KEY)));
const mint = await splToken.createMint(
connection,
walletKeyPair,
walletKeyPair.publicKey,
null,
9,
undefined,
{},
splToken.TOKEN_PROGRAM_ID,
);
const tokenAccount = await splToken.getOrCreateAssociatedTokenAccount(
connection,
walletKeyPair,
mint,
walletKeyPair.publicKey,
);
await splToken.mintTo(
connection,
walletKeyPair,
mint,
tokenAccount.address,
walletKeyPair.publicKey,
1000000000000,
)