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?