metaplex-foundation / solita

Genrates an SDK API from solana contract IDL.
Apache License 2.0
140 stars 32 forks source link

feat: anchor remaining accounts #79

Closed thlorenz closed 2 years ago

thlorenz commented 2 years ago

Summary

For anchor programs solita now renders an optional anchorRemainingAccounts property for the instruction accounts, allowing users to specify them.

This can be turned off by setting anchorRemainingAccounts = false in the solita config.

Renders code outlined in this issue and thus fixes it.

Sample Code Rendered

export type ExecuteSaleInstructionAccounts = {
  buyer: web3.PublicKey
  seller: web3.PublicKey
  tokenAccount: web3.PublicKey
  tokenMint: web3.PublicKey
  [ .. many more .. ]
  programAsSigner: web3.PublicKey
  rent?: web3.PublicKey
  anchorRemainingAccounts?: web3.AccountMeta[]
}

[ .. ]

export function createExecuteSaleInstruction(
  accounts: ExecuteSaleInstructionAccounts,
  args: ExecuteSaleInstructionArgs,
  programId = new web3.PublicKey('hausS13jsjafwWwGqZTUQRmWyvyxn9EQpqMwV1PBBmk')
) {
  const [data] = executeSaleStruct.serialize({
    instructionDiscriminator: executeSaleInstructionDiscriminator,
    ...args,
  })
  const keys: web3.AccountMeta[] = [
    {
      pubkey: accounts.buyer,
      isWritable: true,
      isSigner: false,
    },
    {
      pubkey: accounts.seller,
      isWritable: true,
      isSigner: false,
    },
   [ .. many more .. ]
  }

  if (accounts.anchorRemainingAccounts != null) {
    for (const acc of accounts.anchorRemainingAccounts) {
      keys.push(acc)
    }
  }

  const ix = new web3.TransactionInstruction({
    programId,
    keys,
    data,
  })
  return ix