【问题标题】:Rust Anchor-Spl errors from too many "fallback functions"?Rust Anchor-Spl 错误来自太多“后备函数”?
【发布时间】:2022-06-16 13:40:42
【问题描述】:

我正在尝试构建一个基线锚智能合约,其中我尝试构建具有以下(精简)布局的结构

#[program]
pub mod introToAnchor {
    use super::*;
    
    pub fn setData(ctx: UpdateData, data: u64) -> ProgramResult {
    .
    .
    .
    }
    pub fn terminateData(ctx: TerminateData) -> ProgramResult {
    .
    .
    .
    }
    (Function Format repeated)
}

但我收到错误 More than one fallback function found?什么是后备功能,我怎么会有太多?

【问题讨论】:

  • 你能分享重现问题和github上的代码吗

标签: rust solana anchor-solana


【解决方案1】:

解析器无法根据函数的签名生成指令处理器,但有几件事可能会出错。

  1. 您可能需要让它返回anchor_lang::Result,而不是返回ProgramResult

  2. 尝试将预期的数据包装在 Context 中。

这两个一起给出:

use anchor_lang::prelude::*;
#[program]
pub mod introToAnchor {
    use super::*;
    
    pub fn setData(ctx: Context<UpdateData>, data: u64) -> Result<()> {
    .
    .
    .
    }
    pub fn terminateData(ctx: Context<TerminateData>) -> Result<()> {
    .
    .
    .
    }
    (Function Format repeated)
}

本文摘自官方教程:https://project-serum.github.io/anchor/tutorials/tutorial-1.html#defining-a-program

【讨论】:

    【解决方案2】:

    Jon C 的回答应该能让你继续前进,但我可以为你提供有关错误和后备功能的背景信息。

    Anchor 的一个没有很好记录的特性是在传入指令与任何已实现的程序方法不匹配时调用的后备方法。

    Anchor 决定方法是 后备方法的方式相当迟钝。基本上,任何具有Context&lt;&gt;参数的方法都被视为后备方法。

    您的原始代码有两个没有Context&lt;&gt; 参数的方法,因此它看到了两个备用方法。一个程序在任何时候都只能有一个回退方法。

    不幸的是,我可以为您提供此功能的唯一参考是此问题及其相应的 PR。 https://github.com/project-serum/anchor/issues/409

    【讨论】:

      猜你喜欢
      • 2022-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多