To make a cross-contract call in Sway, one must use the following:
...
let handle = abi(MyContract, contract_addr.into());
handle.call_some_method();
...
Here, the type of handle
is ContractCaller<MyContract>
.
Easy enough, but let’s say you have multiple functions within this contract and you’ll be calling a lot of methods from the MyContract
contract, instead of doing this:
#[storage(read)]
fn fn1() {
let handle = abi(MyContract, storage.contr_add.into());
handle.call_some_method_1();
}
#[storage(read)]
fn fn2() {
let handle = abi(MyContract, storage.contr_add.into());
handle.call_some_method_2();
}
#[storage(read)]
fn fn3() {
let handle = abi(MyContract, storage.contr_add.into());
handle.call_some_method_3();
}
we can try to do:
#[storage(read)]
fn fn1() {
let handle = _get_handle();
handle.call_some_method_1();
}
#[storage(read)]
fn fn2() {
let handle = _get_handle();
handle.call_some_method_2();
}
#[storage(read)]
fn fn3() {
let handle = _get_handle();
handle.call_some_method_3();
}
#[storage(read)]
fn _get_handle() -> ContractCaller<MyContract> {
let handle = abi(MyContract, storage.contr_add.into());
handle
}
But this doesn’t work. The compiler throws the errors:
No method named "call_some_method_1" found for type "ContractCaller<MyContract>".
...
No method named "call_some_method_2" found for type "ContractCaller<MyContract>".
...
No method named "call_some_method_3" found for type "ContractCaller<MyContract>".
even though all 3 of these methods are in the MyContract
abi. The moment I add the internal helper function, the compiler throws the errrors above.
any thoughts? @calldelegation @nedsalk @IGI-111