2.8.87 • Published 27 days ago

@oraichain/cw-simulate v2.8.87

Weekly downloads
-
License
MIT
Repository
github
Last release
27 days ago

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
  • load fork state from running blockhain

Getting Started

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

$ npm install "@oraichain/cw-simulate" --save-dev

If you're using Yarn:

$ yarn add "@oraichain/cw-simulate" -D

Usage

  1. Create a SimulateCosmWasmClient object - this is a simulation environment describing a single chain that extends SigningCosmWasmClient.
  2. As needed, per chain:
    • Upload the WASM bytecode using client.update. This will register a new codeId to reference the uploaded contract code.
    • Create a new contract instance using client.instantiate, 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 { SimulateCosmWasmClient } from '@oraichain/cw-simulate';
import { readFileSync } from 'fs';

const sender = 'orai12zyu8w93h0q2lcnt50g3fn0w3yqnhy4fvawaqz';
const funds = [];

const client = new SimulateCosmWasmClient({
  chainId: 'Oraichain',
  bech32Prefix: 'orai',
  metering: true,
});

// import the wasm bytecode
const bytecode = fs.readFileSync('cw-template.wasm');
// deploy
const { codeId } = await client.upload(sender, bytecode, 'auto');
const { contractAddress } = await client.instantiate(sender, codeId, { count: 0 }, 'counter', 'auto');

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

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

// use with codegen
const contractClient = new ContractClient(client, sender, contractAddress);
const res = await contractClient.executeMethod({});

// use with IBC mocks
const cosmosChain = new CWSimulateApp({
  chainId: 'cosmoshub-4',
  bech32Prefix: 'cosmos'
});

// create channel between oraichain and cosmos
client.app.ibc.relay(channel, 'wasm.' + contractAddress, 'channel-0', 'transfer', cosmosChain);

// send IBC packet
await cosmosChain.ibc.sendChannelOpen({
  open_init: {
    channel: {
      counterparty_endpoint: {
        port_id: oraiPort,
        channel_id: channel
      },
      endpoint: {
        port_id: cosmosPort,
        channel_id: channel
      },
      order: IbcOrder.Unordered,
      version: 'ics20-1',
      connection_id: 'connection-0'
    }
  }
});

await cosmosChain.ibc.sendChannelConnect({
  open_ack: {
    channel: {
      counterparty_endpoint: {
        port_id: oraiPort,
        channel_id: channel
      },
      endpoint: {
        port_id: cosmosPort,
        channel_id: channel
      },
      order: IbcOrder.Unordered,
      version: 'ics20-1',
      connection_id: 'connection-0'
    },
    counterparty_version: 'ics20-1'
  }
});

// write hook
cosmosChain.ibc.addMiddleWare((msg, app) => {
  const data = msg.data.packet as IbcPacket;
  if (Number(data.timeout.timestamp) < cosmosChain.time) {
    throw new GenericError('timeout at ' + data.timeout.timestamp);
  }
});

// load fork state from mainnet
import { DownloadState } from "@oraichain/cw-simulate";
const downloadState = new DownloadState("https://lcd.orai.io", path.resolve(__dirname, "data"));
await downloadState.loadState(client, senderAddress, contractAddress, "label");

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.

2.8.87

27 days ago

2.8.85

1 month ago

2.8.84

1 month ago

2.8.83

1 month ago

2.8.86

1 month ago

2.8.82

1 month ago

2.8.81

1 month ago

2.8.80

1 month ago

2.8.79

2 months ago

2.8.78

2 months ago

2.8.77

2 months ago

2.8.76

4 months ago

2.8.75

5 months ago

2.8.74

5 months ago

2.8.73

5 months ago

2.8.72

6 months ago

2.8.71

7 months ago

2.8.70

7 months ago

2.8.69

7 months ago

2.8.68

8 months ago

2.8.67

8 months ago

2.8.66

8 months ago

2.8.65

8 months ago

2.8.64

8 months ago

2.8.63

8 months ago

2.8.62

8 months ago

2.8.61

8 months ago

2.8.60

8 months ago

2.8.59

8 months ago

2.8.58

8 months ago

2.8.57

8 months ago

2.8.56

9 months ago

2.8.55

9 months ago

2.8.54

9 months ago

2.8.53

9 months ago

2.8.52

9 months ago

2.8.51

9 months ago

2.8.50

9 months ago

2.8.49

9 months ago

2.8.48

9 months ago

2.8.47

9 months ago

2.8.46

9 months ago

2.8.45

9 months ago

2.8.44

9 months ago

2.8.43

9 months ago

2.8.42

9 months ago

2.8.41

9 months ago

2.8.40

9 months ago

2.8.39

9 months ago

2.8.38

9 months ago

2.8.37

9 months ago

2.8.36

9 months ago

2.8.35

9 months ago

2.8.34

9 months ago

2.8.33

10 months ago

2.8.32

10 months ago

2.8.31

10 months ago

2.8.30

10 months ago

2.8.29

10 months ago

2.8.28

10 months ago

2.8.27

10 months ago

2.8.26

10 months ago

2.8.25

10 months ago

2.8.24

10 months ago

2.8.23

10 months ago

2.8.22

10 months ago

2.8.21

10 months ago

2.8.20

10 months ago

2.8.19

10 months ago

2.8.18

10 months ago

2.8.17

10 months ago

2.8.16

10 months ago

2.8.15

10 months ago

2.8.14

10 months ago

2.8.13

10 months ago

2.8.12

10 months ago

2.8.11

10 months ago

2.8.10

10 months ago

2.8.9

10 months ago

2.8.8

10 months ago

2.8.7

10 months ago

2.8.6

10 months ago

2.8.5

10 months ago