Skip to content

cross contract calls: document the non panicking version #413

Closed

Description

@tomerweller

Our current cross contract call example panics when the called contract panics.

However, often times the calling contract would be interested in handling the error, rather than panicing. It would be beneficial to either add this to our existing cross contract how-to or potentially somewhere else.

For example, instead of this call, which will panic when add() panics:

let client = contract_a::ContractClient::new(&env, contract_id);
client.add(&x, &y)

We can introduce something like this call, which will return 0 when add() panics:

let client = contract_a::ContractClient::new(&env, contract_id);
client.try_add(&x, &y).unwrap_or(Ok(0)).unwrap()

Activity

  1. leighmcculloch commented on May 3, 2023

    @leighmcculloch
    Member

    When we demonstrate it in the docs it'd be good to demonstrate the Result destructing a little more verbosely. There's nothing wrong with using unwrap_or, but we should show contracts how to take different actions for different errors.

    For example:

    let client = contract_a::ContractClient::new(&env, contract_id);
    match client.try_add(&x, &y) {
        // the contract returned a value
        Ok(Ok(number)) => todo!("do something with the number returned"),
        Ok(Err(ConversionError)) => todo!("got a value back, but it wasn't a number like we expected"),
    
        // the contract errored
        Err(Ok(Error::AnError)) => todo!("do something when an error occurs that the contract included in its contract spec"),
        Err(Err(status)) => todo!("do something when an unrecognized error, or system error, occurs"),
    }
  2. added
    bug
    and removed
    bug
    on Sep 20, 2023
  3. moved this from Backlog to To Do in Dev Docson Apr 25, 2024
  4. tupui commented on Jul 29, 2024

    @tupui
    Contributor

    This 👍 And also if I can add something (was not sure about making a new issue for it), it would be good to show usages of invoke_contract and try_invoke_contract by extension. Plus a discussion on pros/cons like fees.

    (I was looking if there was a bounty entry for cross contract calls as I now spent some time playing around and could have tried to contribute. Regardless, happy to try and take a stab at it if wanted cc @briwylde08 😃)

  5. briwylde08 commented on Jul 31, 2024

    @briwylde08
    Contributor

    @janewang, I see this has been labeled as part of the Horizon scrum, do you know if this is being worked on internally?

  6. janewang commented on Jul 31, 2024

    @janewang
    Contributor

    It does not appear to be. I added to the Dev X docpocalyse so perhaps we could get to it this week.

  7. briwylde08 commented on Aug 1, 2024

    @briwylde08
    Contributor

    @tupui: feel free to take this one! We'll offer $150 worth of XLM for the docs bounty!

  8. tupui commented on Aug 1, 2024

    @tupui
    Contributor

    Thanks! Starting right away

  9. tupui commented on Aug 2, 2024

    @tupui
    Contributor

    I opened a PR 😃 #853 Thanks again all!

  10. deleted a comment from gre3n3yes34 on Aug 5, 2024
  11. moved this from To Do to In Review in Dev Docson Aug 5, 2024
  12. moved this from In Review to Done in Dev Docson Aug 12, 2024
  13. briwylde08 commented on Aug 12, 2024

    @briwylde08
    Contributor

    Thanks again for your contribution @tupui! Your bounty submission has qualified for $150 worth of XLM. Is the previously summitted information still valid for your award payment?

  14. tupui commented on Aug 13, 2024

    @tupui
    Contributor

    All is still valid. Thanks again everyone, that was a fun program to participate in 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

  • Status
    No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions