发起交易指令时账户必须放一些亮片还是可以随机?

The accounts must be put with some sequence or can be random when initiate a TransactionInstruction?

在这里,我们在创建TransactionInstruction 时有一个帐户列表。 那么列表可以有一个随机顺序吗?还是必须遵循一些特定的顺序?

  const keys = [
    {
      pubkey: key1,
      isSigner: true,
      isWritable: true,
    },
    {
      pubkey: key2,
      isSigner: false,
      isWritable: true,
    },
    ...
  ];

  new TransactionInstruction({
    keys,
    programId: PROGRAM_ID,
    data: txnData,
  }),

帐户列表必须按程序预期的顺序排列。

例如,在 CreateAccount 指令期间,您有 from 帐户和 new 帐户,按以下顺序定义:https://github.com/solana-labs/solana/blob/c8f76b8bd05c9557123dd5695b3b809e01fe9ccd/web3.js/src/system-program.ts#L655

处理指令时,runtime会将第一个账户作为from账户,将第二个账户作为new账户:https://github.com/solana-labs/solana/blob/c8f76b8bd05c9557123dd5695b3b809e01fe9ccd/runtime/src/system_instruction_processor.rs#L284