Full working code: The complete source code for this example is available in
example-contracts/proxy/fallback hook and introducing the foreign::call built-in and aliased imports.
WIT Interface
The proxy contract exports functions for delegation and configuration:Rust Implementation
The proxy contract stores a target contract address and forwards all calls to it using thefallback hook:
- Storage accessed via
ctx.model() fallbackhandles missing function calls by forwarding to the target contract- Uses
if let Some(...)to safely handle theOption<ContractAddress> FallContextcan be converted to ViewContext for read-only accessforeign::callis the low-level API used byinterface!andimport!macros
Testing
The test demonstrates configuring the proxy and forwarding calls to a shared-account contract:- Uses
#[testlib::test]with auto-injectedruntime - Publishes all three contracts (proxy, token, shared-account)
- Configures proxy to point to shared-account
- Calls shared-account functions through the proxy address
- The proxy’s
fallbackfunction automatically forwards calls