Manage Contracts using design patterns
Last updated
Was this helpful?
Last updated
Was this helpful?
To follow this pattern, you need to install:
NPM - for managing a TypeScript mono repo
Rust - for building CosmWasm smart contracts
Even though you can upload, deploy and interact with CosmWasm contracts any way you want, it is recommended to organize your code in a pattern that is scalable and portable to different dApps.
In Oraichain Labs, we use a Nx
mono repo that consists of at least three packages below:
contracts-build:
Contains .wasm files that are related to the service you're trying to develop. They are also useful when you want to write simulate tests in Node.js
resembling the actual production code.
contracts-sdk:
Contains TypeScript code generated from the contract schemas. You can also generate types for multiple contracts and put them in the contracts-sdk
. The types usually come from the contracts used in contracts-build
for consistency. However, there are some common already been published on NPM that you can reuse. For example, the CW20 types are in the @oraichain/oraidex-contracts-sdk
.
contracts-demo:
The name speaks for itself. This package is for writing example codes to interact with the contracts you are developing.
Since intergrating the contract logic into an dApp can be confusing, having examples of how to use the contract end-to-end can be useful for other developers and for your future-self.
You can write anything in this package, and since it is just a demo package, you don't need to publish it onto NPM.
You can have a tests/
directory for contract simulation testing in this package.
Below is the demo of using TypeScript code gen @oraichain/tonbridge-contracts-sdk
to interact with TON Light Client contract:
You may ask, what's the benefits of following this coding pattern?. Say no more!
You can publish the contracts-build
and contracts-sdk
packages onto NPM and use them in various dApps.
The contracts-build
are for end-to-end dapp testing simulation, while the contracts-sdk
are for easy contracts interaction.
Whenever there are contract schema or code changes, dApps only need to pump the dependency versions and modify their code accordingly to the new types & .wasm.
By using a Nx
mono repo, the devs don't need to publish any packages before making sure everything is correct.
For example, after testing, they see that the contract schemas need to be updated to meet the user requirements. With a mono repo, they only need to update the contract code, re-generate the schemas and rebuild the mono repo if needed.
The service is also loosely-coupled. For example, if there's a new feature changing the contract logic, but not the schemas, then we only need to update the contracts-build
package and the dApp logic.
Assuming your contracts are located at the same level as your root project directory like below:
then you can run the following commands to build & generate types:
You can also build multiple contracts at different directories at once. The same goes for gents
:
For example, Our contains 3 .wasm
files: a bridge, a light client, and a cw20 token. These are up-to-date smart contract builds that are used for and deployment to the Oraichain network. Meanwhile, the src/index.ts
has two main functions: read the .wasm
files and deploy those onto the Oraichain network.
For example, contains types of the TON CW contracts.
These files are generated using . You will learn to build contracts and generate types in the .