0

I am trying to call a function in a contract through another contract. So there are 3 contracts, (1) one we have which wants to get price on some action, (2) Price Provider contract which checks the exchange price and returns a uint, and (3) the Price feed which provides exchange price

(First Contract) request_price -> (Second Contract) get_price -> (Third Feed) get_latest_answer

From what I read, submitting a cross contract call returns a promise, which could be called when resolved. How would it work for 2 or more promises who depend on each other?

I tried https://www.near-sdk.io/cross-contract/callbacks, but I couldn't think of a way to make another call in the same call possible

Prasad
  • 31
  • 6

1 Answers1

0

It's slightly unclear based on the question of the flow you'd like to do. I assume you need the results of both get_price and get_latest_answer back in your contract?

If so, you can schedule both calls in parallel (if they don't depend on eachother) and then receive both results in a callback function defined in your contract.

Roughly in Rust-like pseudocode, would look like:

pub fn request_price() {
  get_price.and(get_latest_answer).then(some_callback)
}

#[private]
pub fn some_callback(#[callback] price, #[callback] latest) {
  // Can receive results from both in this function
}

I think I might be misunderstanding the expected flow of data, so let me know what assumptions are incorrect.

Also, feel free to link any code which causes issues and we would be happy to fix/fill in any gaps :)

Austin
  • 106
  • 3
  • Hi Austin, thank you for your answer. The flow I was looking for is as follow - I am building a function `request_price` in my contract, which calls `get_price` in the second contract (so that'd be one cross contract call), now the `get_price` function depends on `get_latest_answer` which is in the third contract. So I'm stuck in this two-hop call – Prasad Mar 31 '22 at 10:21
  • That's fine, `request_price` can receive the response from `get_latest_answer` or a combination of the two. On the above, you just remove the `.and(get_latest_answer)` and have `get_price` return what you'd like to the callback. Is this what you're trying to do? – Austin Apr 01 '22 at 16:29