Skip to content

Commit

Permalink
Merge pull request #20 from Terran-One/feat/wasm-query-and-instantiate
Browse files Browse the repository at this point in the history
WASM module query & instantiate
  • Loading branch information
Kiruse authored Nov 13, 2022
2 parents bb8d4c4 + a99a061 commit 0368e45
Show file tree
Hide file tree
Showing 12 changed files with 514 additions and 346 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@terran-one/cw-simulate",
"version": "2.5.0",
"version": "2.6.0",
"description": "Mock blockchain environment for simulating CosmWasm interactions",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
16 changes: 9 additions & 7 deletions src/CWSimulateApp.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { QuerierBase } from '@terran-one/cosmwasm-vm-js';
import { Map } from 'immutable';
import { Err, Result } from 'ts-results';
import { WasmModule } from './modules/wasm';
import { WasmModule, WasmQuery } from './modules/wasm';
import { BankModule, BankQuery } from './modules/bank';
import { AppResponse } from './types';
import { AppResponse, Binary } from './types';

export interface CWSimulateAppOptions {
chainId: string;
Expand Down Expand Up @@ -40,7 +40,7 @@ export class CWSimulateApp {
trace: any = []
): Promise<Result<AppResponse, string>> {
if ('wasm' in msg) {
return await this.wasm.handleMsg(sender, msg, trace);
return await this.wasm.handleMsg(sender, msg.wasm, trace);
} else if ('bank' in msg) {
return await this.bank.handleMsg(sender, msg.bank);
} else {
Expand All @@ -49,18 +49,20 @@ export class CWSimulateApp {
}
}

type QueryMessage = {
bank: BankQuery;
};
export type QueryMessage =
| { bank: BankQuery }
| { wasm: WasmQuery };

export class Querier extends QuerierBase {
constructor(public readonly app: CWSimulateApp) {
super();
}

handleQuery(query: QueryMessage) {
handleQuery(query: QueryMessage): Result<Binary, string> {
if ('bank' in query) {
return this.app.bank.handleQuery(query.bank);
} else if ('wasm' in query) {
return this.app.wasm.handleQuery(query.wasm);
} else {
return Err('Unknown query message');
}
Expand Down
10 changes: 10 additions & 0 deletions src/instrumentation/CWSimulateVMInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,14 @@ export class CWSimulateVMInstance extends VMInstance {
});
return result;
}

/** Reset debug information such as debug messages & call history.
*
* These should be valid only for individual contract executions.
*/
resetDebugInfo() {
this.debugMsgs = [];
this.logs = [];
return this;
}
}
25 changes: 14 additions & 11 deletions src/modules/bank.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Map } from 'immutable';
import { cmd, run, TestContract } from '../../testing/wasm-util';
import { cmd, exec, TestContract } from '../../testing/wasm-util';
import { CWSimulateApp } from '../CWSimulateApp';
import { fromBinary } from '../util';
import { BankMessage, BankQuery, ParsedCoin } from './bank';

type WrappedBankMessage = {
Expand Down Expand Up @@ -111,7 +112,7 @@ describe('BankModule', () => {
bank.setBalance(contract.address, [{denom: 'foo', amount: '1000'}]);

// Act
const msg = run(
const msg = exec.run(
cmd.bank({
send: {
to_address: 'alice',
Expand Down Expand Up @@ -166,15 +167,17 @@ describe('BankModule', () => {
{ denom: 'bar', amount: '200' },
]);

let response = chain.querier.handleQuery({ bank: queryBalance });
expect(response.ok).toBeTruthy();
expect(response.unwrap().amount).toEqual({ denom: 'foo', amount: '100' });
let res = chain.querier.handleQuery({ bank: queryBalance });
expect(res.ok).toBeTruthy();
expect(fromBinary(res.val)).toEqual({ amount: { denom: 'foo', amount: '100' }});

response = chain.querier.handleQuery({ bank: queryAllBalances });
expect(response.ok).toBeTruthy();
expect(response.unwrap().amount).toEqual([
{ denom: 'foo', amount: '200' },
{ denom: 'bar', amount: '200' },
]);
res = chain.querier.handleQuery({ bank: queryAllBalances });
expect(res.ok).toBeTruthy();
expect(fromBinary(res.val)).toEqual({
amount: [
{ denom: 'foo', amount: '200' },
{ denom: 'bar', amount: '200' },
],
});
});
});
12 changes: 7 additions & 5 deletions src/modules/bank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Coin } from '@cosmjs/amino';
import { toAscii } from '@cosmjs/encoding';
import { Map } from 'immutable';
import { Err, Ok, Result } from 'ts-results';
import { Binary } from '../types';
import { CWSimulateApp } from '../CWSimulateApp';
import { toBinary } from '../util';

export interface AppResponse {
events: any[];
Expand Down Expand Up @@ -159,22 +161,22 @@ export class BankModule {
}
}

public handleQuery(query: BankQuery) {
public handleQuery(query: BankQuery): Result<Binary, string> {
let bankQuery = query;
if ('balance' in bankQuery) {
let { address, denom } = bankQuery.balance;
const hasCoin = this
.getBalance(address)
.find(c => c.denom === denom);
return Ok<BalanceResponse>({
return Ok(toBinary({
amount: hasCoin?.toCoin() ?? { denom, amount: '0' },
});
}));
}
else if ('all_balances' in bankQuery) {
let { address } = bankQuery.all_balances;
return Ok<AllBalancesResponse>({
return Ok(toBinary({
amount: this.getBalance(address).map(c => c.toCoin()),
});
}));
}
return Err('Unknown bank query');
}
Expand Down
Loading

0 comments on commit 0368e45

Please sign in to comment.