When chaining multiple executions with callbacks how to allocate gas

Viewed 48

If there is a chain of actions that need to be executed inside NEAR's smart contract and is chained via callbacks from given contract - how to allocate gas for them.

For example:

pub fn some_method(&mut self, ...) -> Promise {
   ...
   Promise::new(other_contract).function_call(...).then(
      ext_self::callback(env::current_account_id(), 0, ????)
   )
}

pub fn callback(&mut self) -> PromiseOrValue<bool> {
   if some_condition {
      self.some_method(...)
   } else {
      PromiseOrValue::Value(true)
   }
}

What should be ???? to correctly allocate gas for this chain of actions?

1 Answers

The correct amount of gas allocation depends on the remote method. The amount of gas is just the amount of prepaid gas that will be available at the remote function call. It doesn't include fees for calling the contract or creating a promise. They will charged separately.

The fees can be vary based on what the remote method is going to do.

Let's say it's just a simple local method without external promises. If it doesn't do a lot of compute and doesn't access a lot of storage, then the execution of it going to be fairly cheap. So you may attach about 10**13 gas.

But if the remote method is going to call other promises and make external calls, or has heavier compute, then you need to account for this.

It's also the case for the local callback that you attach using .then. Use the same logic when estimating the amount of prepaid gas needed.

Related