gpt4 book ai didi

nearprotocol - 跨合约调用是原子的吗?

转载 作者:行者123 更新时间:2023-12-04 07:53:19 37 4
gpt4 key购买 nike

以这段代码为例:

    #[payable]
pub fn add_liquidity(&mut self, min_liquidity: u128, max_tokens: u128) -> U128 {
let deposit = env::attached_deposit();
let contract_near_balance = env::account_balance();
let user_address = env::predecessor_account_id();
let contract_address = env::current_account_id();

let token_amount = max_tokens;
println!("{}", token_amount);
let initial_liquidity = contract_near_balance;
println!("initial liquidity{}", initial_liquidity);

self.uni_totalsupply = initial_liquidity;
let balance_option = self.uni_balances.get(&user_address);
match balance_option {
Some(_) => {
self.uni_balances.insert(&user_address, &initial_liquidity);
}
None => {
self.uni_balances.insert(&user_address, &initial_liquidity);
}
}
Promise::new(self.avrit_token_id.clone()).function_call(
b"transfer_from".to_vec(),
json!({"owner_id":contract_address, "new_owner_id":"avrit.testnet", "amount": U128(token_amount)}).to_string().as_bytes().to_vec(),
DEPOSIT,
env::prepaid_gas() - GAS_FOR_SWAP,
);

initial_liquidity.into()

}
即使 promise 失败,它会在存储中设置 uni_balances 吗?如何使事务原子化?

最佳答案

合约调用不是原子的。为了使 promise 链原子化是使用 then在初始 promise 之后调用的回调。在回调函数中,您可以像 here 一样检查之前的 promise 是否成功。 :

pub fn check_promise(&mut self) {
match env::promise_result(0) {
PromiseResult::Successful(_) => {
env::log(b"Check_promise successful");
self.checked_promise = true;
}
_ => panic!("Promise with index 0 failed"),
};
}
此时,您可以进行最终的状态更改,并且只有在整个事务成功时才会发生。

关于nearprotocol - 跨合约调用是原子的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66830988/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com