[][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 in the same conductor. Arguments for the called function are passed and resturned as JsonString.

Examples

Here are two example Zomes, where one performs a call into the other.

This first zome is the "callee"; i.e., the zome that receives the call, and is named summer. because the call sums two numbers.


#[no_mangle]
#[no_mangle]

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

define_zome! {
    entries: []

    init: || {
        Ok(())
    }

    validate_agent: |validation_data : EntryValidationData::<AgentId>| {
        Ok(())
    }

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

    traits: {
        hc_public [sum]
    }
}

This second zome is the "caller" that makes the call into the summer Zome.



#[no_mangle]

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: []

    init: || {
        Ok(())
    }

    validate_agent: |validation_data : EntryValidationData::<AgentId>| {
        Ok(())
    }

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

    traits: {
        hc_public [check_sum]
    }
}