#Calling contract method on dynamic contract address

7 messages · Page 1 of 1 (latest)

south kernel
#

Summary: I have a trait which can be implemented by any contract. I have a contract which has initialize method which takes contract address as parameter. This contract address should have implemented trait. How can I call method on that contract address ?

Description:
I have a trait called ITrait which has verify method

    fn verify(&self, _metadata: Bytes, _message: Bytes) -> bool;
}```

This trait can be implemented by any contract that wants to implement verify method.
For e.g. 
```impl ITrait for Contract1 {
    fn verify(&self, _metadata: Bytes, _message: Bytes) -> bool {
        ...some logic here...        
    }
}```

Now main contract's initialize method looks like this
```impl MainContract {
    pub fn initialize(env: Env, address: Address) {
        env.storage().instance().set(&TRAIT_CONTRACT, &address);
    }

    pub fn someMethod(env: Env) {
        const address = env.storage().instance().get(&TRAIT_CONTRACT);
        // I want to call method here dynamically so that it doesn't throw any type error
        // address.verify(metadata, message)
    }
}```

In `someMethod` I want to call that contract's `verify` method.
I also want to make sure that address passed in `initialize` is `contract address` and has `verify` method

How can I achieve that in Soroban ? I am new to soroban, so not sure whether this is possible or not. Let me know if there is any another approach that can handle this.
Thank you
south kernel
#

@distant panther / @wind sierra may be you can help me here ?

wind sierra
#

You can make this trait a client. This will let you instanciate a new client for the address. You could then use the function try_verify to check that the function exists and works as expected

#
mod stub {
    use soroban_sdk::{contract, contractimpl, Address, Env, Bytes};

    #[contract]
    pub struct Dummy;

    #[contractimpl]
    #[allow(dead_code)]
    impl ITrait for Dummy {
        fn verify(env: Env, _metadata: Bytes, _message: Bytes) -> bool {
            // Nothing 
        }
    }
}

// Then in  your initialize
let client = stub::DummyClient::new(&env, &address);
let result = client.try_verify(...);
// Check result...
south kernel
#

@wind sierra Thanks , Let me try it out

south kernel
#

@wind sierra its giving me error self argument not supported any idea ?

wind sierra
#

right, just copied quicky your trait example. It cannot have a self argument, contract function uses the name(env: Env, args...) signature, you cannot use self in soroban contract