2

I've found this function in an assemblyscript project for a NEAR contract:

export function assert_single_promise_success(): void {
  const x = ContractPromise.getResults()
  assert(x.length == 1, "Expected exactly one promise result")
  assert(x[0].succeeded, "Expected PromiseStatus to be successful")
}

What does ContractPromise.getResults() do exactly? How should implement the same thing in rust?

2 Answers2

2

here is something similar in Rust from one of the examples in the SDK repo

require!(env::promise_results_count() == 2);

let data0: Vec<u8> = match env::promise_result(0) {
  PromiseResult::Successful(x) => x,
  _ => env::panic_str("Promise with index 0 failed"),
};
amgando
  • 1,563
  • 8
  • 12
1

I'm going to give an answer, comments taken directly from the implementation of ContractPromise.getResults(), which can be found here. The implementation also has an example on how to use the function, which may be useful.

Method to receive async (one or multiple) results from the remote contract in the callback.

@returns An array of results based on the number of promises the callback was created on. If the callback using then was scheduled only on one result, then one result will be returned.

John
  • 10,165
  • 5
  • 55
  • 71