【问题标题】:near-bindgen macro: unsupported argument typeNear-bindgen 宏:不支持的参数类型
【发布时间】:2021-01-11 04:33:03
【问题描述】:

我正在为 NEAR 区块链编写智能合约承诺接口。

我有如下界面:

#[ext_contract(token_receiver)]
pub trait ExtTokenReceiver {

    fn process_token_received(&self, sender_id: AccountId, amount: Balance, message: [u8]) -> Option<String>;
}

但是这会失败并出现以下错误:

error: Unsupported argument type.
  --> src/token.rs:32:1
   |
32 | #[ext_contract(token_receiver)]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
  • 如何调试 Near-bindgen 宏
  • 在这种情况下Unsupported argument type 是什么
  • 如何修复我的界面

【问题讨论】:

    标签: rust nearprotocol


    【解决方案1】:

    我相信不受支持的论点是 [u8]。我已将代码更改为使用 Vec 并且可以正常工作:

    use near_sdk::ext_contract;
    
    #[ext_contract(token_receiver)]
    pub trait ExtTokenReceiver {
        fn process_token_received(
            &self,
            sender_id: AccountId,
            amount: Balance,
            message: Vec<u8>,
        ) -> Option<String>;
    }
    
        Finished release [optimized] target(s) in 0.05s
    

    我认为编译器在编译时不知道静态数组的大小并抱怨的问题,而 Vec 因为它是一个很好的动态容器。

    【讨论】:

      【解决方案2】:

      对于第一个问题,它说:用-Z macro-backtrace 每晚运行,您可以通过编辑项目的build.sh 来做到这一点。在 rust 中调试宏的另一个工具是使用cargo-expand,它将从更改后的 AST 进行反编译。

      对于第二个和第三个问题:我的猜测是[u8],这是一个在编译时必须知道的数组。你应该使用Vec&lt;u8&gt;,它可以在需要的地方强制转换成&amp;[u8]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多