2

I have two questions.

import { createAssociatedTokenAccount } from "@solana/spl-token"
import { Connection, Keypair, PublicKey, clusterApiUrl } from "@solana/web3.js"
import { getWalletBalanceWithUSD } from "../get-wallet-balance"
import addTokenAccountRecord from "../../../db-operations/write/token-account/add-token-account-record"

export default async function createTokenAccountHelper(
    feePayerWallet: Keypair,
    splId: number,
    splTokenPublicKey: PublicKey,
    userPublicKey: PublicKey,
    userWalletIdIdb: number
): Promise<{ tokenAccountIdDb: number, accountAddress: PublicKey }> {
    try {
        const connection = new Connection(clusterApiUrl("devnet"), "confirmed")
        const initialWalletBalance = await getWalletBalanceWithUSD(feePayerWallet.publicKey)

        const tokenAccountAddress = await createAssociatedTokenAccount(
            connection,
            feePayerWallet,
            splTokenPublicKey,
            userPublicKey
        )

        const secondWalletBalance = await getWalletBalanceWithUSD(feePayerWallet.publicKey)
        console.log("sol price diff", initialWalletBalance.balanceInSol - secondWalletBalance.balanceInSol)

        console.log("usd price diff", initialWalletBalance.balanceInUsd - secondWalletBalance.balanceInUsd)
        const tokenAccountDB = await addTokenAccountRecord(
            splId,
            userWalletIdIdb,
            tokenAccountAddress,
            initialWalletBalance.balanceInSol - secondWalletBalance.balanceInSol,
            initialWalletBalance.balanceInUsd - secondWalletBalance.balanceInUsd
        )

        return {
            tokenAccountIdDb: tokenAccountDB.token_account_id,
            accountAddress: tokenAccountAddress
        }
    } catch (error) {
        console.error(error)
        throw error
    }
}

First, is there a less hacky way of determining the transaction cost associated with opening a new account? Currently, I'm getting the before and after wallet balance. I tried passing in the token account address into this function:

export default async function calculateTransactionFee(signature: string): Promise<number> {
    try {
        const connection = new Connection(clusterApiUrl("devnet"), "confirmed")
        const transactionDetails = await connection.getTransaction(
            signature,
            { commitment: "confirmed", maxSupportedTransactionVersion: 0 }
        )

        if (_.isNull(transactionDetails) || _.isNull(transactionDetails.meta)) {
            console.error("Unable to retrieve transaction details")
            throw Error("Unable to retrieve transaction details")
        }

        const fee = transactionDetails.meta.fee
        const feeInSol = fee / LAMPORTS_PER_SOL

        return feeInSol
    } catch (error) {
        console.error(error)
        throw error
    }
}

But got this error: SolanaJSONRPCError: failed to get transaction: Invalid param: WrongSize.

My second question is around the cost it takes to create the very first associated token account for a new SPL. Here are some logs when creating 3 new Associated Token Accounts:

sol price diff 0.01766599999999996
usd price diff 3.1431347199999777
sol price diff 0.0020442800000000094
usd price diff 0.36371829759997354
sol price diff 0.0020442800000000094
usd price diff 0.3637182976000304

Why is the price of creating the first Token Account so high, and why is the cost of the other transactions double the rent-exempt amount?

1 Answer 1

1

If you're using @solana/spl-token, you can get the amount required to create a token account using getMinimumBalanceForRentExemptAccount https://github.com/solana-labs/solana-program-library/blob/d4bbd51b5167d3f0c8a247b5f304a92e6482cd6f/token/js/src/state/account.ts#L133

And it's tough to tell what's going on for your second question without seeing at least the mint ids that you're using. That's a strangely huge amount, so I wonder if there's an issue elsewhere.

2
  • When I use getMinimumBalanceForRentExemptAccount, here's what gets logged: wallet dif 0.01766599999999996 rentExemptAmount 0.00203928 wallet dif 0.0020442800000000094 rentExemptAmount 0.00203928 For the following code: const secondWalletBalance = await getWalletBalanceWithUSD(feePayerWallet.publicKey) console.log("wallet dif", initialWalletBalance.balanceInSol - secondWalletBalance.balanceInSol) const rentExemptAmount = await getMinimumBalanceForRentExemptAccount(connection, "confirmed") console.log("rentExemptAmount", rentExemptAmount / LAMPORTS_PER_SOL)
    – Ariel
    Commented May 28 at 15:46
  • Once again, it looks like the creation of the first Associated Token Account takes up significantly more resources/cost than the others. @Jon, I appreciate the help
    – Ariel
    Commented May 28 at 15:50

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.