Yes, it is possible to call Contract B from Contract A in Sway and have the log come from Contract B. This can be achieved by using the ABI system in Sway to make the contract call. Here is an example of how you can do this:
Contract A
contract;
use contract_b::ContractB;
abi ContractA {
fn call_contract_b();
}
const CONTRACT_B_ID: ContractId = 0x79fa8779bed2f36c3581d01c79df8da45eee09fac1fd76a5a656e16326317ef0;
impl ContractA for Contract {
fn call_contract_b() {
let contract_b = abi(ContractB, CONTRACT_B_ID);
contract_b.some_function();
}
}
Contract B
contract;
abi ContractB {
fn some_function();
}
impl ContractB for Contract {
fn some_function() {
// Emit a log here
log("Log from Contract B");
}
}
Explanation
Contract A:
Defines an ABI ContractA with a function call_contract_b.
Uses the ABI of ContractB to call some_function on Contract B.
The CONTRACT_B_ID is the address of Contract B.
Contract B:
Defines an ABI ContractB with a function some_function.
Implements the some_function which emits a log.
When call_contract_b is called on Contract A, it will call some_function on Contract B, and the log will be emitted from Contract B.
Low-Level Call Example
If you prefer to use a low-level call, you can follow the example provided in the context: