2

I tried to run the script below but got this error and was wondering where I may have made a mistake?

bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)
The token was created, here is the address: 7RELwCrQm1XZa7BtpAQbYUxrMmpssy2MF4TkvY1YE2kQ
Created an account for the owner of the token, here is the account AGe3KSgfyxhM3R6MNextG4MTCXs9nM4H4CoK9Eu7cG2r
Initially the account had 0 tokens
The account has 10000000000000 tokens after minting
Error: TypeError: Cannot read properties of undefined (reading 'toBuffer')
PS C:\Users\Student\Documents\Creating_SPL_Token\Solana_Token>
const { Connection, clusterApiUrl, Keypair, LAMPORTS_PER_SOL, PublicKey, amount } = require('@solana/web3.js');
require('dotenv').config();
const base58 = require('bs58');

const connection = new Connection(clusterApiUrl('devnet'), 'confirmed');

const privateKey = base58.decode(process.env.PRIVATE_KEY);
const wallet = Keypair.fromSecretKey(privateKey);

async function fundWallet() {
    const signature = await connection.requestAirdrop(wallet.publicKey, LAMPORTS_PER_SOL);
    await connection.confirmTransaction({ signature })
}

async function getAccountBalance(accountAddress) {
    const tokenAccountInfo = await getAccount(connection, accountAddress);
    return tokenAccountInfo.amount;
}

async function transferTokens(recipient, token, fromTokenAccount) {
    const toPublicKey = new PublicKey(recipient)
    const toTokenAccount = await getOrCreateAssociatedTokenAccount(connection, wallet, token, amount);

    const signature = await transfer(
        connection, // connection to Solana
        wallet, // the fee payer 
        fromTokenAccount.address, // the account that sends the tokens
        toTokenAccount.address, //  the account that receives the tokens 
        wallet.publicKey, // the wallet that send the transaction
        amount * LAMPORTS_PER_SOL // the amount of tokens to send during the transaction 
    );
    await connection.confirmTransaction({ signature });

    return signature;
}

async function disableMint() {
    await setAuthority(
        connection, // the connection to Solana
        wallet, // the fee payer
        tokenAccount.address, // the token
        wallet, // the current authority
        AuthorityType.MintTokens, // the type of authority to set
        null // this sets the mint authority to null so nobody can mint
    );
}

async function burnTokens(tokenAccount, token, amount) {
    await burn(
        connection,
        wallet,
        tokenAccount,
        token,
        wallet.publicKey,
        amount * LAMPORTS_PER_SOL
    );
}

async function createToken() {
    const balance = await connection.getBalance(wallet.publicKey);

    if (balance < LAMPORTS_PER_SOL) {
        await fundWallet();
    }

    const token = await createMint(
        connection, // connection to Solana 
        wallet, // fee payer 
        wallet.publicKey, // the mint authority 
        wallet.publicKey, // the freeze authority
        9 // the number of decimals of precision our token uses (the deafault is 9)
    );

    const tokenAddress = token.toString();
    console.log(`The token was created, here is the address: ${tokenAddress}`);

    const tokenAccount = await getOrCreateAssociatedTokenAccount(connection, wallet, token, wallet.publicKey);

    const accountAddress = tokenAccount.address.toString();
    console.log(`Created an account for the owner of the token, here is the account ${accountAddress}`);


    const initialBalance = await getAccountBalance(tokenAccount.address);
    console.log(`Initially the account had ${initialBalance} tokens`);

    await mintTo(
        connection, // connection to Solana
        wallet, // the fee payer 
        token, // the token to mint 
        tokenAccount.address, // the account that will receive the tokens
        wallet.publicKey, // the mint authority 
        10000 * LAMPORTS_PER_SOL // the amount of tokens that we want to mint 
    );

    const balanceAfterMint = await getAccountBalance(tokenAccount.address);
    console.log(`The account has ${balanceAfterMint} tokens after minting`);

    const transferSignature = await transferTokens(
        "GcovasTHokbR3PtA5WJ66f828fAenwSvUnv9Ju55MgCg",
        token,
        tokenAccount,
        100
    );

    console.log(
        `Transferred 100 tokens to GcovasTHokbR3PtA5WJ66f828fAenwSvUnv9Ju55MgCg, here is the signature of the transaction: ${transferSignature}`
    );

    await burnTokens(tokenAccount.address, token, 10);
    console.log(`Just burned 10 tokens`);

    const balanceAfterBurn = await getAccountBalance(tokenAccount.address);
    console.log(`The account has ${balanceAfterBurn} tokens after minting`);
}

createToken()
    .then(() => {
        console.log("Done!");
    })
    .catch((err) => {
        console.log(`Error: ${err}`);
    });

1 Answer 1

2

In fn transferTokens the toTokenAccount might be wrong.

You passed the amount into getOrCreateAssociatedTokenAccount you need to change it to toTokenAccount.

1
  • Thanks for the help
    – johnny
    Jan 20 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.