Leveraging solana-web3.js and spl-token to mint 1000 SPL tokens on Solana.
Overview
@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
@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 a Uint8Array
in the account definition process.Connecting to Solana
connection
, and passed in both a Chainstack HTTPS Solana endpoint and a Chainstack WSS Solana endpoint. To retrieve these, do the following:Connection
constructorImporting a wallet
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
.Preparing the mint
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 the walletKeyPair
object.null
, this is the freezeAuthority
parameter, which can either be null
or a public key.9
, the decimal place location for the token being minted.undefined
, filling in an optional keypair
parameter.{}
, filling in an optional transaction confirmation instruction parameter.splToken.TOKEN_PROGRAM_ID
for filling in the programId
parameter.Creating the token account
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 the createMint
function.walletKeyPair.publicKey
, specifically pulling the public key from the walletKeyPair
object.Minting the tokens
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 the createMint
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 of 9
.