Search code examples
c#solana

solnet problem in response for making Token : Blockhash not found


I use Solnet to communicate with solana, I got this error in response of transaction :

Transaction simulation failed: Attempt to debit an account but found no record of a prior credit.

I did it without any problem in solana-cli. I thought problem is in GetRecentBlockHash or GetLatestBlockHash both they returned blockhash successfully.

I got this error in this line of code: Examples.SubmitTxSendAndLog(createAndInitializeMintToTx);

 Wallet wallet = new Wallet(MnemonicWords);

IRpcClient RpcClient = ClientFactory.GetClient(Cluster.DevNet);

#region Create and Initialize a token Mint Account


RequestResult<ResponseValue<LatestBlockHash>> blockHash = RpcClient.GetLatestBlockHash(Solnet.Rpc.Types.Commitment.Finalized);


ulong minBalanceForExemptionAcc =
    RpcClient.GetMinimumBalanceForRentExemption(TokenProgram.TokenAccountDataSize).Result;
ulong minBalanceForExemptionMint =
    RpcClient.GetMinimumBalanceForRentExemption(TokenProgram.MintAccountDataSize).Result;

Console.WriteLine($"MinBalanceForRentExemption Account >> {minBalanceForExemptionAcc}");
Console.WriteLine($"MinBalanceForRentExemption Mint Account >> {minBalanceForExemptionMint}");

Account ownerAccount = wallet.GetAccount(10);
Account mintAccount = wallet.GetAccount(11);
Account initialAccount = wallet.GetAccount(12);
Console.WriteLine($"OwnerAccount: {ownerAccount}");
Console.WriteLine($"MintAccount: {mintAccount}");
Console.WriteLine($"InitialAccount: {initialAccount}");

byte[] createAndInitializeMintToTx = new TransactionBuilder().
    SetRecentBlockHash(blockHash.Result.Value.Blockhash).
    SetFeePayer(ownerAccount).
    AddInstruction(SystemProgram.CreateAccount(
        ownerAccount,
        mintAccount,
        minBalanceForExemptionMint,
        TokenProgram.MintAccountDataSize,
        TokenProgram.ProgramIdKey)).
    AddInstruction(TokenProgram.InitializeMint(
        mintAccount.PublicKey,
        2,
        ownerAccount.PublicKey,
        ownerAccount.PublicKey)).
    AddInstruction(SystemProgram.CreateAccount(
        ownerAccount,
        initialAccount,
        minBalanceForExemptionAcc,
        TokenProgram.TokenAccountDataSize,
        TokenProgram.ProgramIdKey)).
    AddInstruction(TokenProgram.InitializeAccount(
        initialAccount.PublicKey,
        mintAccount.PublicKey,
        ownerAccount.PublicKey)).
    AddInstruction(TokenProgram.MintTo(
        mintAccount.PublicKey,
        initialAccount.PublicKey,
        1_000_000,
        ownerAccount)).
    AddInstruction(MemoProgram.NewMemo(initialAccount, "Hello from Sol.Net")).
    Build(new List<Account> { ownerAccount, mintAccount, initialAccount });

string createAndInitializeMintToTxSignature = Examples.SubmitTxSendAndLog(createAndInitializeMintToTx);


if (!string.IsNullOrEmpty(createAndInitializeMintToTxSignature))
    Examples.PollConfirmedTx(createAndInitializeMintToTxSignature);



      public static string SubmitTxSendAndLog(byte[] tx)
    {
        Console.WriteLine($"Tx Data: {Convert.ToBase64String(tx)}");

        RequestResult<ResponseValue<SimulationLogs>> txSim = RpcClient.SimulateTransaction(tx);
        string logs = PrettyPrintTransactionSimulationLogs(txSim.Result.Value.Logs);
        Console.WriteLine($"Transaction Simulation:\n\tError: {txSim.Result.Value.Error}\n\tLogs: \n" + logs);

        RequestResult<string> txReq = RpcClient.SendTransaction(tx);
        Console.WriteLine($"Tx Signature: {txReq.Result}");

        Console.WriteLine(txReq.Reason);

        return txReq.Result;
    }

Solution

  • When we perform transactions in different clusters, this issue arises. for instance, send a transaction in Testnet and get the most recent hash in Devnet.

    Your balance zero value might be another issue.