Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transaction fee is sufficient and simulate transaction success,but transaction is dropped. #236

Open
BillInUK opened this issue Aug 6, 2024 · 1 comment

Comments

@BillInUK
Copy link

BillInUK commented Aug 6, 2024

Hello, I recently encountered an issue that occurs with a relatively high frequency. When I send a transaction to Solana's mainnet, I ensure that my gas fee is sufficient and I simulate the transaction successfully before sending it. However, after sending the transaction and receiving the txID, I am unable to find it on the blockchain explorer. Could you please help me understand the reason for this?

here is my code to transfer the spl token:

func TransferSPLToken(rpcClient *rpc.Client, tokenMintAccount solana.PublicKey,
	fromPrivateKey solana.PrivateKey, toNativeAccount solana.PublicKey, amount uint64, decimals uint8, memo string) (*solana.Signature, error) {

	ctx := context.Background()

	fromNativeAccount := fromPrivateKey.PublicKey()

	recent, err := rpcClient.GetLatestBlockhash(context.Background(), rpc.CommitmentProcessed)
	if err != nil {
		return nil, err
	}

	//fmt.Printf("transfer spl token from token account %v\n", fromNativeAccount)
	//fmt.Printf("transfer spl token to token account %v\n", toNativeAccount)
	//fmt.Printf("transfer spl token token mint account %v\n", tokenMintAccount)
	//fmt.Printf("transfer spl token owner native account %v\n", fromNativeAccount)
	//fmt.Printf("transfer spl token amount %v\n", amount)
	//fmt.Printf("transfer spl token decimals %v\n", decimals)

	fromTokenAccount, _, err := solana.FindAssociatedTokenAddress(fromNativeAccount, tokenMintAccount)
	if err != nil {
		return nil, err
	}
	//fmt.Printf("get from token account %v \n", fromTokenAccount)

	toTokenAccountExist := true
	toTokenAccount, _, err := solana.FindAssociatedTokenAddress(toNativeAccount, tokenMintAccount)
	if err != nil {
		return nil, err
	}
	if _, err := GetSPLTokenAccountByNative(rpcClient, toNativeAccount, tokenMintAccount); err != nil {
		toTokenAccountExist = false
	}
	//fmt.Printf("get to token account %v \n", toTokenAccount)

	var instructions []solana.Instruction
	computeBudgetPriceInst := computebudget.NewSetComputeUnitPriceInstructionBuilder().SetMicroLamports(1000).Build()
	computeBudgetLimitInst := computebudget.NewSetComputeUnitLimitInstructionBuilder().SetUnits(600000).Build()

	if !toTokenAccountExist {
		associated := associatedtokenaccount.NewCreateInstruction(fromNativeAccount, toNativeAccount, tokenMintAccount).Build()
		instructions = append(instructions, associated)
	}

	transferInst := token.NewTransferCheckedInstructionBuilder().
		SetAmount(amount).
		SetSourceAccount(fromTokenAccount).
		SetDestinationAccount(toTokenAccount).
		SetDecimals(decimals).
		SetMintAccount(tokenMintAccount).
		SetOwnerAccount(fromNativeAccount).
		Build()

	instructions = append(instructions, computeBudgetPriceInst)
	instructions = append(instructions, computeBudgetLimitInst)
	instructions = append(instructions, transferInst)
	if memo != "" {
		programID := solana.MemoProgramID
		accounts := []*solana.AccountMeta{solana.Meta(fromNativeAccount).SIGNER().WRITE()}
		data := []byte(memo)
		memoInst := solana.NewInstruction(programID, accounts, data)
		instructions = append(instructions, memoInst)
	}

	tx, err := solana.NewTransaction(instructions, recent.Value.Blockhash, solana.TransactionPayer(fromNativeAccount))
	if err != nil {
		return nil, err
	}

	if _, err = tx.Sign(
		func(key solana.PublicKey) *solana.PrivateKey {
			if fromNativeAccount.Equals(key) {
				return &fromPrivateKey
			}
			return nil
		},
	); err != nil {
		return nil, err
	}

	// 模拟发送交易
	simulateResult, err := rpcClient.SimulateTransactionWithOpts(ctx, tx, &rpc.SimulateTransactionOpts{
		ReplaceRecentBlockhash: true,
	})
	if err != nil {
		return nil, fmt.Errorf("simulate transaction error %v", err)
	}
	if simulateResult.Value.Err != nil {
		return nil, fmt.Errorf("simulate transaction failed %v", simulateResult.Value.Err)
	}

	txId, err := rpcClient.SendTransactionWithOpts(ctx, tx, rpc.TransactionOpts{SkipPreflight: true})
	return &txId, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@BillInUK and others