[][src]Function hdk::api::call

pub fn call<S: Into<String>>(
    instance_handle: S,
    zome_name: S,
    cap_token: Address,
    fn_name: S,
    fn_args: JsonString
) -> ZomeApiResult<JsonString>

Call an exposed function from another zome or another (bridged) instance running on the same agent in the same conductor. Arguments for the called function are passed as JsonString. Returns the value that's returned by the given function as a json str.

Examples

In order to utilize call, you must have at least two separate Zomes. Here are two Zome examples, where one performs a call into the other.

This first one, is the one that is called into, with the Zome name summer.



fn handle_sum(num1: u32, num2: u32) -> JsonString {
    let sum = num1 + num2;
    json!({"sum": sum.to_string()}).into()
}

define_zome! {
    entries: []

    genesis: || {
        Ok(())
    }

    functions: [
            sum: {
                inputs: |num1: u32, num2: u32|,
                outputs: |sum: JsonString|,
                handler: handle_sum
            }
    ]

    traits: {
        hc_public [sum]
    }
}

This second one, is the one that performs the call into the summer Zome.




fn handle_check_sum(num1: u32, num2: u32) -> ZomeApiResult<JsonString> {
    #[derive(Serialize, Deserialize, Debug, DefaultJson)]
    struct SumInput {
        num1: u32,
        num2: u32,
    };
    let call_input = SumInput {
        num1: num1,
        num2: num2,
    };
    hdk::call(hdk::THIS_INSTANCE, "summer", Address::from(hdk::PUBLIC_TOKEN.to_string()), "sum", call_input.into())
}

define_zome! {
    entries: []

    genesis: || {
        Ok(())
    }

    functions: [
            check_sum: {
                inputs: |num1: u32, num2: u32|,
                outputs: |sum: ZomeApiResult<JsonString>|,
                handler: handle_check_sum
            }
    ]

    traits: {
        hc_public [sum]
    }
}