Coder Social home page Coder Social logo

cyberhoward / cosm-rust-script Goto Github PK

View Code? Open in Web Editor NEW
11.0 0.0 1.0 227 KB

A gRPC-based scripting library for interacting with CosmWasm smart-contracts.

License: Apache License 2.0

Shell 0.40% Rust 99.60%
cosmos-sdk cosmwasm rust scripting tooling

cosm-rust-script's Introduction

This library is no longer in active developement. Use BOOT instead!

Cosmos Rust Script

Smart contract scripting library to ease CosmWasm smart contract development and deployment.

cosm-script is inspired by terra-rust-api and uses cosmos-rust for protocol buffer parsing.

cw-plus-script uses cosm-script to provide the standard type-safe interfaces to interact with cosmwasm-plus contracts.

The use of this software makes it easier to quickly deploy and iterate on your contracts. You should use this function responsibly when working on mainnet or testnet as ALL the code you upload to those networks takes up valuable space. Therefore I strongly suggest using a locally-hosted chain like localterra, local junod, etc. .

How it works

Usually your contracts workspace will have a package that contains the structs that get filled by a provided JSON through the FFI on execution by the CosmWasm VM. We can easily access these endpoint structs (InstantiateMsg, ExecuteMsg, QueryMsg, ...) by adding that package as a dependency to the scripting workspace.

In order to perform actions on the contract we need to specify these structs so the compiler can type-check our actions. This prevents us from executing a faulty message on a contract and it also handles converting the structs to their json format. The implementation for a CW20 token is shown below. The full file resides here

// Wrapper stuct around the contract instance.
pub struct CW20<'a>(ContractInstance<'a>);

// Interface and instance traits allow for an auto-implementation of our Cosm traits.
impl Interface for CW20<'_> {
    type Exec = ExecuteMsg;
    type Init = InstantiateMsg;
    type Query = QueryMsg;
    type Migrate = NotImplemented;
}

impl Instance for CW20<'_> {
    fn instance(&self) -> &ContractInstance {
        &self.0
    }
}

impl CW20<'_> {
    /// Create a new CW20 ContractInstance. Uses "cw20" as code-id key.
    pub fn new<'a>(
        name: &'a str,
        sender: Wallet<'a>,
        deployment: &'a Deployment,
    ) -> anyhow::Result<CW20<'a>> {
        let mut instance = ContractInstance::new(name, sender, deployment)?;
        // We want all our CW20 tokens to use the same contract (code-id)!
        instance.overwrite_code_id_key("cw20");
        Ok(CW20(instance))
    }
    ...
}

After implementing these traits you have the ability to use all the functions provided by the WasmInstantiate, WasmExecute, WasmQuery and WasmMigrate traits. This way our script calls are very ergonomic:

    let token_info = cw20_token.query(Cw20QueryMsg::TokenInfo {}).await?;

Contributing

Feel free to open issues or PRs!

cosm-rust-script's People

Contributors

cyberhoward avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

m00ntek

cosm-rust-script's Issues

cw-multi-test integration

Add cw-multi-test integration to the Wasm traits. This would allow users to write verbose document tests while also re-using the functionality defined in their contract's interfaces.

So depending on the context the code get compiled to an integration test or a binary that executes the actions on-chain.

Move network configs into const Rust structs

Network and chain configurations are currently stored in the default_store.json file along with contract address/code-id storage.
A better approach would be to create const stucts containing the chain/network information in a seperate .rs file.

Review Instance trait requirement

The Instance trait enables the blanket cosmwasm action implementation while also allowing for custom function implementations on that contract.

A different approach could be to add a PhantomData fields in the ContractInstance which would hold the contract's endpoint messages (ExecuteMsg, InstantiateMsg, ...). Custom functionality could then be added by implementing the fully defined contract.

use std::marker::PhantomData;
use serde::Serialize;

struct ContractInstance<E: Serialize, I: Serialize>
{
    // Name of the contract, used to retrieve addr/code-id
    pub name: String,
    // execute msg
    exec: PhantomData<E>,
    // init msg
    init: PhantomData<I>,
}
// Generic implementation
impl<E: Serialize, I: Serialize> Contract<E, I> {
    fn new(name: String) -> Self { Self { name, exec: PhantomData, init: PhantomData } }
}

#[derive(Debug, Serialize)]
struct Exec {}
#[derive(Debug, Serialize)]
struct Init {}

// custom implementation.
impl Contract<Exec,Init> {
    fn test(&self) {
        println!("test");
    }
}

Can't upload .wasm

I keep getting " No such file or directory (os error 2)" when trying to run a cw-plus-script esqe script.

My WASM_DIR is currently pointing to the artifacts folder and I am passing in a ".wasm" file to the upload function.

I've tried a couple of different variations but same error. I understand if you don't have the time to assist.

This is the debug output:
image

Consolidate cosmwasm traits into a single trait.

The available smart-contract calls are implemented in different traits (WasmExecute, WasmInstantiate, ...). This means a user needs to import these traits individually every time.

The goal of this task is to consolidate the traits into one CosmWasmAction trait.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.