solana-program-library icon indicating copy to clipboard operation
solana-program-library copied to clipboard

Feature: Add `createMemoInstruction` helper function to help with the required memo extension

Open CoachChuckFF opened this issue 1 year ago • 4 comments

Creating a memo to send to satisfy the required memo extension could be easier with the addition of a createMemoInstruction helper function.

What is required right now:

const message = "Hello, Solana"

const transaction = new Transaction().add(
  new TransactionInstruction({
    keys: [{ pubkey: payer.publicKey, isSigner: true, isWritable: true }],
    data: Buffer.from(message, "utf-8"), // Memo message. In this case it is "Hello, Solana"
    programId: new PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"), // Memo program that validates keys and memo message
  }),

  createTransferInstruction(
    ourTokenAccount,
    otherTokenAccount, // Has required memo
    payer.publicKey,
    amountToTransfer,
    undefined,
    TOKEN_2022_PROGRAM_ID
  )
);
await sendAndConfirmTransaction(connection, transaction, [payer]);

Proposed helper function:

function createMemoInstruction(
    payer: PublicKey,
    message: string,
    memoProgramId?: PublicKey
){
    return new TransactionInstruction({
        keys: [{ pubkey: payer, isSigner: true, isWritable: true }],
        data: Buffer.from(message, "utf-8"),
        programId: memoProgramId ?? new PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),
    });
}

CoachChuckFF avatar Apr 29 '24 15:04 CoachChuckFF

Added a PR: https://github.com/solana-labs/solana-program-library/pull/6656

CoachChuckFF avatar Apr 29 '24 16:04 CoachChuckFF

Thanks for your contribution and your idea.

The concept is for people to use the @solana/spl-memo package if they want to create memo instructions: https://github.com/solana-labs/solana-program-library/tree/master/memo/js

You can see how that's done in the spl-token JS tests starting at https://github.com/solana-labs/solana-program-library/blob/5feb1170d7d17bb78247a82c2613468b9788a6c5/token/js/test/e2e-2022/memoTransfer.test.ts#L7

While it is only one function, we've deliberately avoided copying it from the spl-memo package, so we would not accept that contribution unfortunately. Maybe we actually need better documentation to make that intention clear. What do you think?

joncinque avatar Apr 30 '24 12:04 joncinque

I totally get it! I'm also guessing you would not want to depend on the @solana/spl-memo library.

I honestly did not know the library existed, and it seems that no other reference material did either.

I would suggest adding a "transfer" section to the Token22 required memo documentation. To show that there is a library that takes care of this!

Thanks for the consideration!

CoachChuckFF avatar Apr 30 '24 16:04 CoachChuckFF

Ah darn, yeah you're right. I put in a couple of PRs to try to make that better -- if you have a moment, could you check that they satisfy your use case? They're https://github.com/solana-foundation/developer-content/pull/194 and https://github.com/solana-labs/solana-program-library/pull/6668

joncinque avatar Apr 30 '24 20:04 joncinque