solana-labs / solana-program-library

A collection of Solana programs maintained by Solana Labs
https://solanalabs.com
Apache License 2.0
3.46k stars 2.03k forks source link

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

Closed CoachChuckFF closed 4 months ago

CoachChuckFF commented 4 months ago

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 commented 4 months ago

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

joncinque commented 4 months ago

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?

CoachChuckFF commented 4 months ago

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!

joncinque commented 4 months ago

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