Coder Social home page Coder Social logo

cosmology-tech / cw-simulate Goto Github PK

View Code? Open in Web Editor NEW
29.0 3.0 3.0 2.42 MB

Mock blockchain environment for simulating CosmWasm interactions

Home Page: https://cwsimulate.terran.one

JavaScript 2.50% TypeScript 97.50%
cosmwasm javascript wasm webassembly

cw-simulate's Introduction

cw-simulate

This package combines cosmwasm-vm-js with additional abstractions and state management to more accurately simulate the effects of CosmWasm contracts on the blockchain environments on which they are hosted.

Features

  • configure multiple host chain environments with chain-specific settings / state
  • multiple simultaneous contract instances can exist per chain
  • chain modules can be simulated through custom user code
  • extensible for further instrumentation via custom middlewares

Getting Started

Import the cw-simulate library from NPM in your package.json.

$ npm install -S @terran-one/cw-simulate

If you're using Yarn:

$ yarn add @terran-one/cw-simulate

Usage

  1. Create a CWSimulateApp object - this is a simulation environment describing a single chain.
  2. As needed, per chain:
    • Upload the WASM bytecode using App.wasm.create. This will register a new codeId to reference the uploaded contract code.
    • Create a new contract instance using App.wasm.instantiateContract, passing in the codeId generated in the previous step.
    • From the response, retrieve the contractAddress to refer to the contract instance.
  • You can now run execute and query messages against the instance, and they should work as expected.

Example

The following example creates a chain, instantiates a contract on it, and performs an execute and query.

import { CWSimulateApp } from '@terran-one/cw-simulate';
import { readFileSync } from 'fs';

const sender = 'terra1hgm0p7khfk85zpz5v0j8wnej3a90w709vhkdfu';
const funds = [];
const wasmBytecode = readFileSync('cw-template.wasm');

const app = new CWSimulateApp({
  chainId: 'phoenix-1',
  bech32Prefix: 'terra'
});

// import the wasm bytecode
const codeId = app.wasm.create(sender, wasmBytecode);

// instantiate the contract
let result = await app.wasm.instantiateContract(sender, funds, codeId, { count: 0 });
console.log('instantiateContract:', result.constructor.name, JSON.stringify(result, null, 2));

// pull out the contract address
const contractAddress = result.val.events[0].attributes[0].value;

// execute the contract
result = await app.wasm.executeContract(sender, funds, contractAddress, { increment: {} });
console.log('executeContract:', result.constructor.name, JSON.stringify(result, null, 2));

// query the contract
result = await app.wasm.query(contractAddress, { get_count: {} });
console.log('query:', result.constructor.name, JSON.stringify(result, null, 2));

Using with Vue.js and vite

Vite doesn't include shims for Node variables like Webpack 4 does, and cw-simulate currently relies on these. The following workaround exists:

  1. Add the buffer package (npm add buffer)
  2. Add the following to your index.html (inside the body tag, before your other js imports):
<script>
  window.global = window;
</script>
<script type="module">
  import {Buffer} from "buffer";
  window.Buffer = Buffer;
</script>

See this github issue for more details.

cw-simulate's People

Contributors

bunchhieng avatar dabralyogesh avatar dependabot[bot] avatar kiruse avatar lukedawilson avatar ouiliame avatar zmarouf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cw-simulate's Issues

ICS721 via ts-relayer

would be fun to have ts-relayer inbrowser in addition, especially with ICS721 for E2E tests and fun

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.