Skip to main content

Interface instances

Interface modules can be instantiated in a Wasm wrap, agnostic to any concrete implementation.

Instantiating an interface

After an interface is imported in your Wrap Schema, you can update the generated classes with the Polywrap CLI's codegen command. You will then be able to import the interface module in your wrap.

To instantiate an interface module, you must provide a URI that resolves to a wrap that implements the interface.

import { MyInterface_Module, Args_foo } from "./wrap";

export function foo(args: Args_foo): boolean {
const instance = new MyInterface_Module("wrap://...");
...
}

Getting Interface Implementations

To instantiate an interface agnostic to the implementation, you can use getImplementations to obtain a list of interface implementations registered in the Polywrap Client.

Declaring getImplementations

In addition to importing the interface module in the Wrap Schema, you must declare that getImplementations will be used for the interface with the use { getImplementations } keywords.

#import { Module } into MyInterface from "wrap://ens/interface.eth"
#use { getImplementations } for MyInterface

Using getImplementations

Now you can import the interface namespace and call its getImplementations method. The getImplementations method returns an array of URI strings that can be used to instantiate the interface module.

import { MyInterface, MyInterface_Module, Args_foo } from "./wrap";

export function foo(args: Args_foo): boolean {
const impls = MyInterface.getImplementations();
if (impls.length < 1) {
throw new Error("...")
}
const instance = new MyInterface_Module(impls[0]);
...
}