npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@terran-one/cw-simulate

v2.8.4

Published

Mock blockchain environment for simulating CosmWasm interactions

Downloads

45

Readme

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.