【问题标题】:Calling specific endpoint on Solana smart contract在 Solana 智能合约上调用特定端点
【发布时间】:2021-11-06 12:40:23
【问题描述】:

我正在尝试使用@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

【问题讨论】:

  • 我认为您遗漏了一些代码。因此,流程就像您从客户端沿事务传递一些整数。然后在 rust 程序中读取这个整数并调用相应的指令。我建议您查看Anchor。这将使编写 solana 程序变得容易得多。

标签: blockchain solana


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-15
    • 2022-08-04
    • 2022-06-17
    • 1970-01-01
    • 2023-01-15
    • 1970-01-01
    • 2022-06-16
    • 2021-12-28
    相关资源
    最近更新 更多