Solana: fetch Mint Account details using web.3js V2

I can provide an example article on how to get mint account details using Web3.js V2 and the @solana-program/token-programs library.

Fetching Mint Account Details Using Web3.js V2 and @solana-program/token-programs

In this article, we will explore how to get mint account details using the new Solana web3.js V2 and the @solana-program/token-programs library. We will cover the steps to create a token program, deploy it to Solana, and then use Web3.js to get mint account details.

Prerequisites

  • Solana CLI installed
  • Node.js installed
  • Web3.js installed

Step 1: Create a Token Program

Solana: fetch Mint Account details using web.3js V2

A token program is an extension to the Solana blockchain that allows you to create custom tokens. To create a token program, we will use the solana-program/token-programs library.

Create a new directory for your project and initialize it with npm:

mkdir solana-token-example-program

cd solana-token-example-program

npm initialize -y

Install the required dependencies:

npm install @solana-program/token-programs

Create a file called token-program.json and add the following code:

{

"name": "MyToken",

"author": {

"id": "username"

},

"description": "A simple example token program"

}

This will create a new token program with a default name, author, and description.

Step 2: Deploy the Token Program

To deploy the token program to Solana, we will use the solana-program/token-programs library. Create a file called deploy.sh and add the following code:

#!/bin/sh

echo "Deploying token program..."

run solo --type script deploy --name MyToken \

solana-program(token-programs) --datafile token-program.json \

--account-name accounts/0 --script-script my-token-script.js

This will create a new deployment of the token program to Solana.

Step 3: Create an Account

To get the mint account details, we need to create a custom account that contains the tokens. We will use the @solana-program/token-programs library again.

Create a file called create-account.sh and add the following code:

#!/bin/sh

echo "Creating custom account..."

run solo --type script deploy \

solana program (token programs) --name MyAccount --datafile create-account.json \

--account-name accounts/1 --script-script my-account-script.js

This will create a new custom account that contains the tokens.

Step 4: Fetch Mint Account Details

To get mint account details, we will use Web3.js to interact with Solana. Create a file called fetch-mint-account.sh and add the following code:

#!/bin/sh

echo "Fetching mint account details..."

const web3 = require('web3');

const accounts = await web3.eth.accounts.get();

const tokenProgramAddress = 'some-token-program-address'; // Replace with your token program address

const transaction = {

from: 'your-username',

to: accounts[0].address, // Replace with your custom account address

value: web3.utils.toWei(1, 'ether'), // Replace with the amount of tokens you want to transfer

gas limit: 20,000,

};

web3.eth.sendTransaction(transaction)

.then((transactionReceipt) => {

console.log(Mint Account Details:, transactionReceipt);

})

.catch((error) => {

console.error('Error fetching mint account details:', error);

});

This will create a new transaction to fetch the mint account details and sign it using Web3.js.

Putting it all together

Here’s an example of how you can put it all together:

./create-account.sh

./fetch-mint-account.sh

Replace MyAccount with your custom account name, some-token-program-address with your token program address, and your-username with your Solana username.

This will create a new custom account that contains the tokens and then fetch the mint account details using Web3.js.

METAMASK STATE UPDATE METAMASK

Comments are closed.