#cosmwasm #module #reuse #contracts #composability #glue #withing

burnt-glue

A library that enables composability and reuse withing CosmWasm contracts

6 releases

0.2.2 Aug 22, 2022
0.2.1 Aug 22, 2022
0.1.4 Aug 15, 2022

#5 in #composability

MIT license

22KB
357 lines

glue

For documentation, run cargo doc --open.


lib.rs:

A library that enables composability and reuse withing CosmWasm contracts.

Creating Module

To create a reusable module, one must create a struct that implements the Module trait. Simply define the associated types and provide implementations for instantiate, execute, and query and you will have a module ready to use with the manager.

By convention, it's acceptable for modules to take references to other modules from their constructors. This allows modules to compose easily.

Using Modules

The Manager is a struct used to dynamically dispatch messages to their corresponding modules. Create a new Manager with Manager::new and then register modules for dynamic dispatch with register.

When implementing the entrypoints for contracts built with glue, you can simply call the corresponding functions: execute, query, and instantiate.

Entities interacting with your contract may follow a simple convention for addressing messages to a specific module withing your contract. For execute and query calls, the Manager expects an object structured as follows:

{ "module_name": { /* payload object to be sent to the module */ } }

NOTE: The root object must contain a single key. If you attempt to address more than one module in an execute call, it will fail.

The Manager will automatically strip away the root object and forward the payload object to the relevant module. The response object returned by the module will be returned directly.

The instantiate method is only marginally different. Whereas the calls to execute and query require that caller to send a root object with only a single key, the object sent to the instantiate entrypoint may contain a key for each module registered with the manager, e.g.:

{
  "module_one": { /* payload for module one instantiation */ },
  "module_two": { /* payload for module two instantiation */ },
  /* and so on */
}

Dependencies

~4.5–6.5MB
~135K SLoC