near-bindgen 宏:不支持的参数类型

near-bindgen macro: unsupported argument type

我正在为 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)

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

对于第二个和第三个问题:我的猜测是[u8],这是一个编译时必须知道的数组。您应该使用 Vec<u8>,它可以在需要时强制转换为 &[u8]

我认为不支持的参数是 [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 因为它是一个很好的动态容器。