Overview
Overview
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.jsEnvironment setup
Environment setup
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-tokenfor interacting with the Solana token program.bs58for converting a traditional string private key to aUint8Arrayin the account definition process.
index.js
Connecting to Solana
Connecting to Solana
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:1
Navigate to the Chainstack console
2
Deploy a Solana mainnet node
3
Open the newly deployed node
4
Copy the corresponding HTTPS & WSS endpoints
5
Paste them into your
Connection constructorindex.js
Importing a wallet
Importing a wallet
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.index.js
Preparing the mint
Preparing the mint
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 thewalletKeyPairobject.null, this is thefreezeAuthorityparameter, which can either benullor a public key.9, the decimal place location for the token being minted.undefined, filling in an optionalkeypairparameter.{}, filling in an optional transaction confirmation instruction parameter.splToken.TOKEN_PROGRAM_IDfor filling in theprogramIdparameter.
index.js
Creating the token account
Creating the token account
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 thecreateMintfunction.walletKeyPair.publicKey, specifically pulling the public key from thewalletKeyPairobject.
index.js
Minting the tokens
Minting the tokens
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 thecreateMintfunction.tokenAccount.address, this is the address of the associated token account we made; this is being used as the destination address.walletKeyPair.publicKeyis 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.
index.js