gagliardetto / solana-go

Go SDK library and RPC client for the Solana Blockchain
Apache License 2.0
880 stars 257 forks source link

Create associated token account idempotent instruction #194

Open lucasoares opened 6 months ago

lucasoares commented 6 months ago

Currently solana-go only allows creating non-idempotent instruction for the associatedtokenaccount.NewCreateInstructionBuilder program instruction.

It would be nice to allow it to be created using the idempotent solution, which is achieved by having a [1] uint8 bytearray as the instruction data.

I do not understand the gagliardetto/binary library's internals and don't know how the BaseVariant works. The current binary module doesn't have much documentation, but if you guide me it appears to be an easy PR.

Loved the work you have done with this library. I will be using it a lot for personal projects, and I want to know more about it to contribute. Thanks!

quanghuy219 commented 18 hours ago

@lucasoares not sure if you need this anymore but i found a workaround to make create associated token account idempotent possible.

import (
    "github.com/gagliardetto/solana-go"
    associatedtokenaccount "github.com/gagliardetto/solana-go/programs/associated-token-account""
)

func BuildCreateATAIdempotentInstruction(payer, walletAddress, mint solana.PublicKey) *solana.GenericInstruction {
    createATAInstruction := associatedtokenaccount.NewCreateInstruction(
        payer,
        walletAddress,
        mint,
    ).Build()

    return solana.NewInstruction(createATAInstruction.ProgramID(), createATAInstruction.Accounts(), []byte{1})
}

The difference is at the data field, set it to 1 to enable idempotent mode