Skip to content

Commit

Permalink
chore: suggestions by code review
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksuss committed Oct 11, 2023
1 parent b777c0b commit fafd4cc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
19 changes: 14 additions & 5 deletions engine-test-doubles/src/promise.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use aurora_engine_sdk::promise::PromiseHandler;
use aurora_engine_sdk::promise::PromiseId;
use aurora_engine_types::parameters::{PromiseBatchAction, PromiseCreateArgs};
use aurora_engine_types::parameters::{
NearPromise, PromiseBatchAction, PromiseCreateArgs, SimpleNearPromise,
};
use aurora_engine_types::types::PromiseResult;
use std::collections::HashMap;

Expand All @@ -13,6 +15,7 @@ pub enum PromiseArgs {
callback: PromiseCreateArgs,
},
Batch(PromiseBatchAction),
Recursive(NearPromise),
}

/// Doesn't actually schedule any promises, only tracks what promises should be scheduled
Expand Down Expand Up @@ -53,10 +56,16 @@ impl PromiseHandler for PromiseTracker {
}

unsafe fn promise_create_and_combine(&mut self, args: &[PromiseCreateArgs]) -> PromiseId {
args.iter()
.map(|arg| self.promise_create_call(arg))
.last()
.unwrap_or_else(|| PromiseId::new(0))
let id = self.take_id();
self.scheduled_promises.insert(
id,
PromiseArgs::Recursive(NearPromise::And(
args.iter()
.map(|p| NearPromise::Simple(SimpleNearPromise::Create(p.clone())))
.collect(),
)),
);
PromiseId::new(id)
}

unsafe fn promise_attach_callback(
Expand Down
10 changes: 7 additions & 3 deletions engine/src/contract_methods/connector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub mod internal;
pub const ERR_NOT_ENOUGH_BALANCE_FOR_FEE: &str = "ERR_NOT_ENOUGH_BALANCE_FOR_FEE";
/// Indicate zero attached balance for promise call
pub const ZERO_ATTACHED_BALANCE: Yocto = Yocto::new(0);
/// Amount of attached gas for read-only promises.
const READ_PROMISE_ATTACHED_GAS: NearGas = NearGas::new(5_000_000_000_000);

/// Create new eth-connector;
pub fn new_eth_connector<I: IO + Copy, E: Env>(io: I, env: &E) -> Result<(), ContractError> {
Expand Down Expand Up @@ -470,15 +472,15 @@ pub fn mirror_erc20_token<I: IO + Env + Copy, H: PromiseHandler>(
.try_to_vec()
.map_err(|_| crate::errors::ERR_SERIALIZE)?,
attached_balance: Yocto::new(0),
attached_gas: NearGas::new(5_000_000_000_000),
attached_gas: READ_PROMISE_ATTACHED_GAS,
},
PromiseCreateArgs {
target_account_id: args.contract_id,
method: "get_erc20_metadata".into(),
args: serde_json::to_vec(&Erc20Identifier::from(args.nep141))
.map_err(|_| crate::errors::ERR_SERIALIZE)?,
attached_balance: Yocto::new(0),
attached_gas: NearGas::new(5_000_000_000_000),
attached_gas: READ_PROMISE_ATTACHED_GAS,
},
];

Expand All @@ -487,8 +489,10 @@ pub fn mirror_erc20_token<I: IO + Env + Copy, H: PromiseHandler>(
method: "mirror_erc20_token_callback".to_string(),
args: input,
attached_balance: Yocto::new(0),
attached_gas: NearGas::new(5_000_000_000_000),
attached_gas: READ_PROMISE_ATTACHED_GAS,
};
// Safe because these promises are read-only calls to the main engine contract
// and this transaction could be executed by the owner of the contract only.
let promise_id = unsafe {
let promise_id = handler.promise_create_and_combine(&promise);
handler.promise_attach_callback(promise_id, &callback)
Expand Down

0 comments on commit fafd4cc

Please sign in to comment.