Skip to main content
Full working code: The complete source code for this example is available in test-contracts/proxy/
This example demonstrates proxying (a common pattern used in contract upgrades), utilizing the 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 the fallback hook:
Key points:
  • Storage accessed via ctx.model()
  • fallback handles missing function calls by forwarding to the target contract
  • Uses if let Some(...) to safely handle the Option<ContractAddress>
  • FallContext can be converted to ViewContext for read-only access
  • foreign::call is the low-level API used by interface! and import! macros

Testing

The test demonstrates configuring the proxy and forwarding calls to a shared-account contract:
Key points:
  • Uses #[testlib::test] with auto-injected runtime
  • 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 fallback function automatically forwards calls