0

我正在尝试使用@solana-web3 调用 Solana 智能合约上的特定端点,但我不知道如何做到这一点。我已经看到它是这样完成的:

const buffer = Buffer.from("hello");
  const instruction = new web3.TransactionInstruction({
    keys: [{ pubkey: fromWallet.publicKey, isSigner: false, isWritable: true }],
    programId: new web3.PublicKey(metaDataProgramID),
    data: buffer,
  });
  const confirmation = await web3.sendAndConfirmTransaction(
    connection,
    new web3.Transaction().add(instruction),
    [fromWallet],
    {
      commitment: "singleGossip",
      preflightCommitment: "singleGossip",
    }
  );`

但我不确定如何处理这样的具有多个端点的智能合约。因为如何告诉智能合约使用哪个端点以及将什么数据传递给该端点?:

pub fn process_instruction<'a>(
    program_id: &'a Pubkey,
    accounts: &'a [AccountInfo<'a>],
    input: &[u8],
) -> ProgramResult {
    let instruction = MetadataInstruction::try_from_slice(input)?;
    match instruction {
        MetadataInstruction::CreateMetadataAccount(args) => {
            msg!("Instruction: Create Metadata Accounts");
            process_create_metadata_accounts(
                program_id,
                accounts,
                args.data,
                false,
                args.is_mutable,
            )
        }
        MetadataInstruction::UpdateMetadataAccount(args) => {
            msg!("Instruction: Update Metadata Accounts");
            process_update_metadata_accounts(
                program_id,
                accounts,
                args.data,
                args.update_authority,
                args.primary_sale_happened,
            )
        }
        MetadataInstruction::DeprecatedCreateMasterEdition(args) => {
            msg!("Instruction: Deprecated Create Master Edition");
            process_deprecated_create_master_edition(program_id, accounts, args.max_supply)
        }
        MetadataInstruction::DeprecatedMintNewEditionFromMasterEditionViaPrintingToken => {
            msg!("Instruction: Deprecated Mint New Edition from Master Edition Via Token");
            process_deprecated_mint_new_edition_from_master_edition_via_printing_token(
                program_id, accounts,
            )
        }
        MetadataInstruction::UpdatePrimarySaleHappenedViaToken => {
            msg!("Instruction: Update primary sale via token");
            process_update_primary_sale_happened_via_token(program_id, accounts)
        }
        MetadataInstruction::MintNewEditionFromMasterEditionViaVaultProxy(args) => {
            msg!("Instruction: Mint New Edition from Master Edition Via Vault Proxy");
            process_mint_new_edition_from_master_edition_via_vault_proxy(
                program_id,
                accounts,
                args.edition,
            )
        }
        MetadataInstruction::PuffMetadata => {
            msg!("Instruction: Puff Metadata");
            process_puff_metadata_account(program_id, accounts)
        }
    }
}

这是智能合约的完整代码:https ://github.com/metaplex-foundation/metaplex/tree/master/rust/token-metadata/program

4

1 回答 1

2

在 Solana 中调用智能合约的“端点”的选择取决于您作为指令buffer传入的那个。data给定这些数据,程序决定调用哪个指令处理器。

在您的示例中,指令的选择可能基于指令的第一个字节,因此0可能对应于MetadataInstruction::CreateMetadataAccount.

您最好的选择是使用程序开发人员提供的软件包。对于元数据程序,指令和类型定义位于:https ://github.com/solana-labs/oyster/blob/e79c5a66ed5290d3fb752f4cd6b4e90f7974ec94/packages/common/src/actions/metadata.ts

于 2021-09-13T11:13:01.443 回答