在NEARprotocol上,我想从合同(Rust)中调用另一个合同。我试过env::Promise,但无法检索呼叫应答。这里的正确方法是什么?另外,near-sdk机箱文档给出了示例&"bob_near".to_string(),这等同于id bob.near还是一个错误?
以下是我的代码的相关摘录:
near_sdk::setup_alloc!();
#[ext_contract]
pub trait AEXToken {
fn collect_premie(&mut self, account_id: ValidAccountId) -> bool;
fn get_min_balance(&self) -> u128;
}
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
pub struct Contract {
tokens: NonFungibleToken,
metadata: LazyOption<NFTContractMetadata>,
aex_tokenId: ValidAccountId,
}
#[near_bindgen]
impl Contract {
pub fn nft_mint(&mut self) -> Token {
let amount: Balance = env::attached_deposit();
aex_token::get_min_balance(&self.aex_tokenId.to_string(), 0, 1_000);
let min_balance: u128 = env::promise_return(1);
assert!(min_balance < amount);发布于 2022-02-20 17:00:16
我想你误解了这件事的运作方式。承诺不会在同样的方法中返回。它在随后的一些块中执行,尽可能快地执行,这取决于网络拥塞,通常在一个块内。
返回值将以回调的形式落在另一个方法中。请参阅此示例以及与SDK位于同一个examples文件夹中的其他几个
/// Call functions a, b, and c asynchronously and handle results with `handle_callbacks`.
pub fn call_all(fail_b: bool, c_value: u8) -> Promise {
let gas_per_promise = env::prepaid_gas() / 5;
ext::a(env::current_account_id(), 0, gas_per_promise)
.and(ext::b(fail_b, env::current_account_id(), 0, gas_per_promise))
.and(ext::c(c_value, env::current_account_id(), 0, gas_per_promise))
.then(ext::handle_callbacks(env::current_account_id(), 0, gas_per_promise))
}/// Receives the callbacks from the other promises called.
#[private]
pub fn handle_callbacks(
#[callback_unwrap] a: u8,
#[callback_result] b: Result<String, PromiseError>,
#[callback_result] c: Result<u8, PromiseError>,
) -> (bool, bool) {
require!(a == A_VALUE, "Promise returned incorrect value");
if let Ok(s) = b.as_ref() {
require!(s == "Some string");
}
(b.is_err(), c.is_err())
}这可能有助于更多的解释,https://www.near-sdk.io/cross-contract/callbacks
所以你的呼唤
aex_token::get_min_balance(&self.aex_tokenId.to_string(), 0, 1_000);应该使用then将其链接到执行此检查的回调。
let min_balance: u128 = env::promise_return(1);
assert!(min_balance < amount);https://stackoverflow.com/questions/71196296
复制相似问题