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

Overview

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!

Comments
  • Can't upload .wasm

    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

    opened by triccs 2
  • Review Instance trait requirement

    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");
        }
    }
    
    opened by CyberHoward 1
  • Move network configs into const Rust structs

    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.

    opened by CyberHoward 0
  • Consolidate cosmwasm traits into a single trait.

    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 CosmWasmActiontrait.

    opened by CyberHoward 0
  • cw-multi-test integration

    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.

    enhancement 
    opened by CyberHoward 0
Owner
null
CosmWasm-Examples is a collection of example contracts and applications built using the CosmWasm framework

CosmWasm-Examples is a collection of example contracts and applications built using the CosmWasm framework. CosmWasm is a secure and efficient smart contract platform designed specifically for the Cosmos ecosystem.

Vitalii Tsyhulov 20 Jun 9, 2023
My code for the terra.academy course on CosmWasm smart contracts

CosmWasm Starter Pack This is a template to build smart contracts in Rust to run inside a Cosmos SDK module on all chains that enable it. To understan

Alex Incerti 0 Nov 7, 2021
A simple example demonstrating cross-contract calls in CosmWasm smart contracts

Cross-contract calls This tutorial demonstrates cross-contract calls with CosmWasm v1. Overview An end user calls the reservation contract to register

csli Tools 3 Sep 12, 2022
A blazingly fast compiling & optimization tool for CosmWasm smart contracts.

cw-optimizoor A blazingly fast alternative to CosmWasm/rust-optimizer for compiling & optimizing CW smart contracts. It's primarily meant to speed up

Sebastian Mandrean 37 Apr 15, 2023
Minimal Substrate node configured for smart contracts via pallet-contracts.

substrate-contracts-node This repository contains Substrate's node-template configured to include Substrate's pallet-contracts ‒ a smart contract modu

Parity Technologies 73 Dec 30, 2022
CosmWasm/Sylvia counting contract w/ IBC enabled (Cosmos, Rust, CosmWasm, Sylvia)

CosmWasm/Sylvia counting contract w/ IBC enabled (Cosmos, Rust, CosmWasm, Sylvia) This repository contains counting contract created during the study

Alex Cryp 3 Nov 13, 2023
Examples and helpers to build NFT contracts on CosmWasm

CosmWasm NFTS This repo is the official repository to work on all NFT standard and examples in the CosmWasm ecosystem. cw721 and cw721-base were moved

CosmWasm 147 Jan 4, 2023
An example CosmWasm contract for connecting contracts over IBC.

CosmWasm IBC Example This is a simple IBC enabled CosmWasm smart contract. It expects to be deployed on two chains and, when prompted, will send messa

ekez 64 Jun 21, 2023
Validity is a next-generation, deduction-based language for formally verified, context-aware, autonomous & reactive smart contracts.

Validity Language Validity is a next-generation, deduction-based language for formally verified, context-aware, autonomous & reactive smart contracts.

Tempest Labs 4 Nov 11, 2022
Rust library for build smart contracts on Internet Computer, by the Spinner.Cash team.

Spinner Rust library for building smart contracts on the Internet Computer. More specifically it is used by Spinner.Cash, a decentralized layer-2 prot

Spinner 6 May 31, 2022
RGB smart contracts: client-facing library & command-line for desktop and mobile

RGB smart contracts RGB is confidential & scalable client-validated smart contracts for Bitcoin & Lightning. It embraces the concepts of private & mut

RGB: scalable & private smart contracts for bitcoin & lightning 4 Mar 15, 2023
Unofficial Rust SDK library for Uniswap smart contracts.

uniswap-rs Unofficial Rust SDK library for Uniswap smart contracts. Quickstart Add this to your Cargo.toml: [dependencies] uniswap-rs = { git = "https

HopefulHeart 3 Sep 22, 2023
zink! is a library for developing ink! smart contracts with useful Rust macros that extend functionality and reduce boilerplate code.

zink! Smart Contract Macros This is a helper library for developing ink! smart contracts. It contains useful Rust macros that extend functionality and

Scio Labs 3 Nov 3, 2023
Unofficial Rust SDK library for Uniswap smart contracts.

uniswap-rs Unofficial Rust SDK library for Uniswap smart contracts. Quickstart Add this to your Cargo.toml: [dependencies] uniswap-rs = { git = "https

SlickCharmer 3 Mar 29, 2024
evm2near compiles Solidity contracts into NEAR WebAssembly contracts.

EVM → NEAR evm2near is a project for compiling EVM bytecode into wasm bytecode, with the particular goal of having that wasm artifact be executable on

Aurora 125 Dec 3, 2022
Ticketed Discreet Log Contracts (DLCs) to enable instant buy-in for wager-like contracts on Bitcoin.

dlctix Ticketed Discreet Log Contracts (DLCs) to enable instant buy-in for wager-like contracts on Bitcoin. This project is part of the Backdrop Build

null 7 Feb 29, 2024
Smart contracts for Ref Finance

Ref Finance Contracts This mono repo contains the source code for the smart contracts of Ref Finance on NEAR. Contracts Contract Reference Description

Ref Finance 92 Dec 7, 2022
Skyward Finance smart-contracts

Build and Init ./build.sh near dev-deploy res/skyward.was export CONTRACT_ID=skyward.testnet near call $CONTRACT_ID new --account_id=$CONTRACT_ID Regi

Skyward Finance 777 Jan 4, 2023
Rust client to Opensea's APIs and Ethereum smart contracts

opensea.rs Rust bindings & CLI to the Opensea API and Contracts CLI Usage Run cargo r -- --help to get the top level help menu: opensea-cli 0.1.0 Choo

Georgios Konstantopoulos 226 Dec 27, 2022