Run CosmWasm smart contracts in Node.js and web browsers

Overview

Run CosmWasm in JavaScript

THIS IS STILL UNDER DEVELOPMENT! Contributions are welcome.

NOTE: This was built for Terra-specific applications and thus is only supported for CosmWasm v0.16. However, this implementation probably can be easily adapted to support CosmWasm v1.0+.

This package implements a pure JavaScript (no Rust bindings / WASM needed) VM capable of executing compiled CosmWasm .wasm binaries in environments such as Node.js and compatible web browsers.

Setup

Add the cosmwasm-vm package as a dependency in your package.json.

npm install -S cosmwasm-vm

or

yarn add cosmwasm-vm

Usage

Please refer to the test in this repository for an example. I include a test contract based on cosmwasm/cw-template that has been augmented with additional ExecuteMsg variants for testing the various WASM imports.

yarn
yarn test
import { CosmWasmVM } from '../src';
import { readFileSync } from 'fs';

const wasm_byte_code = readFileSync('./cosmwasm_vm_test.wasm');
const vm = new CosmWasmVM(wasm_byte_code);

const mock_env = {
  block: {
    height: 1,
    time: '2000000000',
    chain_id: 'columbus-5',
  },
  contract: {
    address: 'contract',
  },
};

const mock_info = {
  sender: 'sender',
  funds: [],
};

describe('CosmWasmVM', () => {
  it('instantiates', () => {
    let res = vm.instantiate(mock_env, mock_info, { count: 20 });
    console.log(res.json);
    console.log(vm.store);
  });

  it('execute', () => {
    let res = vm.instantiate(mock_env, mock_info, { count: 20 });
    res = vm.execute(mock_env, mock_info, { increment: {} });
    console.log(res.json);
    console.log(vm.store);
  });
});
Comments
  • What are incompatibilities between CW 0.16 and 1.0+?

    What are incompatibilities between CW 0.16 and 1.0+?

    Hello! I want to use this project to debug CW contracts in Web browsers, but in my case I have CW 1.0 for contracts, is there any breaking changes between 0.16 and 1.0+?

    enhancement help wanted 
    opened by playXE 2
  • Fix `do_db_read`: wrong value when key not found

    Fix `do_db_read`: wrong value when key not found

    Before, do_db_read was returning a pointer to a newly allocated array of uint8

    According to cosmwasm-vm, we should be returning a pointer to 0 (null ptr).

    bug 
    opened by ouiliame 0
  • Replace yarn with npm

    Replace yarn with npm

    It's a library, we should use minimal dependencies and not force users to use yarn. If we use npm ourselves, it ensures the project will always work with npm (e.g. peer deps issues).

    opened by lukedawilson 0
  • Abstract QuerierBase

    Abstract QuerierBase

    Use of TypeScript's abstract keyword to make absolutely clear to third party devs that the method must be manually implemented.

    Also bump to v0.2.15 for new release.

    opened by Kiruse 0
  • Add support for saving debugging messages, generic queriers

    Add support for saving debugging messages, generic queriers

    We add a new property debugMsgs: string[] to VMInstance, to which contract calls to deps.api.debug(...) will append. Previously, we simply printed the string out to console (which matches the behavior of cosmwasm-vm-js); however this is harder to work with for consuming applications like CWSimulate.

    The old implementation for BasicQuerier was temporary and designed to only handle 1 case (in the tests), this adds generic support for query_chain. You must now subclass BasicQuerier and supply your own handleQuery() function, which will get passed the decoded query request. BasicQuerier.query_raw() will delegate and handle the proper encoding of BasicQuerier.handleQuery(), which is toUint8Array({ ok: { ok: toBase64(response) } }), where response is a JSON object containing the query response.

    In the future, we will need to provide support for Rust errors, because the Ok(Ok(response)) corresponds to SystemResult<ContractResult<Binary>> (Binary is a CosmWasm Rust type that encodes a struct to Base64 strings in JSON).

    opened by ouiliame 0
  • Fix ethereum_signature_verify_works test

    Fix ethereum_signature_verify_works test

    Issue

    WL-506

    Description

    Fixed ethereum_signature_verify_works test. The bug was never in the logic, but in the data passed to the contract: message shouldn't have been base64 encoded.

    Test Steps

    run yarn test test/integration/crypto-verify and assert ethereum_signature_verify_works test succeeds (there are still other failing tests as of now)

    opened by Kiruse 0
  • Queue tests and fix db_next

    Queue tests and fix db_next

    This PR fixes a bunch of stuff in storage:

    • next now takes a Uint8Array, as the parameter passed to the db_next hook from CosmWasm is by reference (i.e. a pointer)
    • scan returns a Uint8Array, to make it consistent with the other methods

    The test still isn't passing due to a separate bug, but I'd like to get these fixes in ASAP.

    opened by lukedawilson 0
  • cyberpunk integration test

    cyberpunk integration test

    Integration test for cyberpunk example smart contract

    VM does not support gas calculation, thus unit test execute_argon2 is not satisfied

    Also adds some useful helpers to test/common/test-vm.ts

    opened by Kiruse 0
  • Bump decode-uri-component from 0.2.0 to 0.2.2

    Bump decode-uri-component from 0.2.0 to 0.2.2

    Bumps decode-uri-component from 0.2.0 to 0.2.2.

    Release notes

    Sourced from decode-uri-component's releases.

    v0.2.2

    • Prevent overwriting previously decoded tokens 980e0bf

    https://github.com/SamVerschueren/decode-uri-component/compare/v0.2.1...v0.2.2

    v0.2.1

    • Switch to GitHub workflows 76abc93
    • Fix issue where decode throws - fixes #6 746ca5d
    • Update license (#1) 486d7e2
    • Tidelift tasks a650457
    • Meta tweaks 66e1c28

    https://github.com/SamVerschueren/decode-uri-component/compare/v0.2.0...v0.2.1

    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

    dependencies 
    opened by dependabot[bot] 0
Releases(v0.2.16)
  • v0.2.16(Nov 16, 2022)

    What's Changed

    • Replace yarn with npm by @lukedawilson in https://github.com/Terran-One/cosmwasm-vm-js/pull/60
    • Target ES6 by @Kiruse in https://github.com/Terran-One/cosmwasm-vm-js/pull/62
    • CI notifications by @lukedawilson in https://github.com/Terran-One/cosmwasm-vm-js/pull/63
    • Fix do_db_read: wrong value when key not found by @ouiliame in https://github.com/Terran-One/cosmwasm-vm-js/pull/64

    Full Changelog: https://github.com/Terran-One/cosmwasm-vm-js/compare/v0.2.15...v0.2.16

    Source code(tar.gz)
    Source code(zip)
  • v0.2.15(Nov 1, 2022)

    What's Changed

    • Add support for saving debugging messages, generic queriers by @ouiliame in https://github.com/Terran-One/cosmwasm-vm-js/pull/55
    • Add contributing and code of conduct docs by @lukedawilson in https://github.com/Terran-One/cosmwasm-vm-js/pull/56
    • Fix scan ordering bug and tendermint tests by @lukedawilson in https://github.com/Terran-One/cosmwasm-vm-js/pull/57
    • Verify message, signature and pubkey lengths for ed25519-verify by @lukedawilson in https://github.com/Terran-One/cosmwasm-vm-js/pull/58
    • Abstract QuerierBase by @Kiruse in https://github.com/Terran-One/cosmwasm-vm-js/pull/59

    Full Changelog: https://github.com/Terran-One/cosmwasm-vm-js/compare/v0.2.14...v0.2.15

    Source code(tar.gz)
    Source code(zip)
  • v0.2.14(Oct 28, 2022)

  • 0.2.1(Oct 18, 2022)

  • 0.1.17(Sep 29, 2022)

    What's Changed

    • IIterStorage extends IStorage by @Kiruse in https://github.com/Terran-One/cosmwasm-vm-js/pull/36

    Full Changelog: https://github.com/Terran-One/cosmwasm-vm-js/compare/0.1.15...0.1.17

    Source code(tar.gz)
    Source code(zip)
  • 0.1.15(Sep 29, 2022)

  • 0.1.12(Sep 10, 2022)

  • 0.1.11(Sep 2, 2022)

  • 0.1.10(Aug 26, 2022)

  • 0.1.7(Aug 18, 2022)

Owner
Terran One
Build and help build.
Terran One
On-chain query batcher for CosmWasm-enabled chains

multiquery On-chain query batcher for CosmWasm. Similar to SCB 10X's multicall contract, but supports any serializable query request, not limited to W

null 7 Dec 6, 2022
Javascript client for Sanity. Works in node.js and modern browsers (older browsers needs a Promise polyfill).

@sanity/client Javascript client for Sanity. Works in node.js and modern browsers (older browsers needs a Promise polyfill). Requirements Sanity Clien

Sanity 23 Nov 29, 2022
πŸš€ A web extension starter built with React, Typescript, and Tailwind CSS. Build once, and run on multiple browsers: Google Chrome, Mozilla Firefox, Microsoft Edge, Brave, and Opera..

Web Extension Starter A web extension starter, built with React, Typescript, and Tailwind CSS. Build once, and run on multiple browsers: Google Chrome

BCIT Design & Development Club (BCIT-DDC) 28 Dec 28, 2022
Ethereum Smart Contracts for locking your Ether and ERC20 tokens based on time and price conditions

SmartHold - a simple way to lock and hold your ETH or ERC20 in a smart contract This is a BETA software that has not been audited for security. USE AT

PaweΕ‚ Urbanek 22 May 5, 2022
Yet another library for generating NFT artwork, uploading NFT assets and metadata to IPFS, deploying NFT smart contracts, and minting NFT collections

eznft Yet another library for generating NFT artwork, uploading NFT assets and metadata to IPFS, deploying NFT smart contracts, and minting NFT collec

null 3 Sep 21, 2022
♦ Crowd funding project using Smart Contracts on the Ethereum. Created with Next.js and Tailwind CSS.

Crowdcoin ♦ Crowd funding project using Smart Contracts on the Ethereum. Created with Next.js and Tailwind CSS. Project from "Ethereum and Solidity: T

Luiz Fernando VerΓ­ssimo 2 Dec 14, 2022
a quick start boilerplate for developing web3 apps and deploying smart contracts.

create-web3 A boilerplate for starting a web3 project. This boilerplate quickly creates a mono repo with 2 environments, a Next JS environment for fro

Eric Roupe 83 Dec 16, 2022
Toolkit for development, test and deploy smart-contracts on Waves Enterprise ecosystem.

JS Contract SDK Toolkit for development, test and deploy smart-contracts on Waves Enterprise ecosystem. Quickstart The fastest way to get started with

Waves Enterprise 20 Dec 15, 2022
Cardinal generator encompasses serverless functions and smart contracts for rendering generative NFTs

Cardinal Generator An open protocol for generative NFTs. Background Cardinal generator encompasses serverless functions and smart contracts for render

Cardinal 31 Dec 6, 2022
Elrond blockchain CLI helper tools - interaction with APIs, smart contracts and protocol

Buildo Begins ?? Meet Buildo. He is here to help you start creating in the Elrond blockchain ecosystem. Here is where everything begins. I'm going on

Elrond's Dev Guild 22 Dec 30, 2022
This is a CI/CD and version controlling tool for smart contracts which is an award winning project built for ETHPrague Hackaton.

Inspiration Alfred is built-in version control and CI/CD system for your smart contracts. It uses proxy contract and a DAO to upgrade or downgrade you

Mete Karasakal 10 Aug 30, 2022
A sequence of smart contracts to practice gas optimization. These are used as practice assignments for RareSkills.io and the Udemy Gas Optimization Course

RareSkills Gas Puzzles Puzzles that are ready for you Distribute (hard) Array Sum (easy) Escrow EscrowV2 Mint Presale Require (easy) Staking Contribut

RareSkills 240 Dec 31, 2022
JellyChain1 is a blockchain network platform that supports smart contracts and can act as a payment system/cryptocurrency

JellyChain1 is an experimental blockchain network, that will serve as a starter blockchain test network and scale out database software and other applications. JellyChain1 is created to cut energy costs when mining transactions on the blockchain.

JellyChain 10 Dec 1, 2022
This package generates a unique ID/String for different browsers. Like chrome, Firefox and any other browsers which supports canvas and audio Fingerprinting.

Broprint.js The world's easiest, smallest and powerful visitor identifier for browsers. This package generates a unique ID/String for different browse

Rajesh Royal 68 Dec 25, 2022
A collection of smart contracts for the Stackup platform πŸ€– πŸ“‘

Contracts A collection of smart contracts for the Stackup platform. Dev Blog Deployed Contracts See releases for deployed contracts of previous versio

Stackup 16 Nov 29, 2021
Cooperative databases using smart contracts.

Vitra β–ˆβ–ˆβ•— β–ˆβ–ˆβ•—β–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β•šβ•β•β–ˆβ–ˆβ•”β•β•β•β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘ β•šβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆ

Paul Frazee 177 Dec 30, 2022
A 'to do list' powered by Ethereum smart contracts

A 'to do list' powered by Ethereum smart contracts. built with JavaScript, Ganache, Truffle, Node.js, Metamask, jQuery, and Bootstrap

Christotle Agholor 3 Feb 17, 2022
Context-aware smart contracts for blockchain IoT systems

Context-aware smart contracts for blockchain IoT systems It contains 2 directories: contracts: contains the ContextAwareSmartContract.sol contract wit

ibelab 6 Jun 17, 2022
This is the main repository for NFT collection dao smart contracts.

Basic Sample Hardhat Project This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, a sample s

null 3 Apr 11, 2022