Skip to content

Commit

Permalink
actually transfer balances + some minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiruse committed Dec 9, 2022
1 parent eaaa608 commit ea39149
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 34 deletions.
48 changes: 24 additions & 24 deletions src/modules/bank.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type WrappedBankMessage = {
bank: BankMessage;
};

describe('BankModule', () => {
describe.only('BankModule', () => {
let chain: CWSimulateApp;

beforeEach(function() {
Expand All @@ -21,18 +21,18 @@ describe('BankModule', () => {
it('handle send', () => {
// Arrange
const bank = chain.bank;
bank.setBalance('alice', [{denom: 'foo', amount: '1000'}]);
bank.setBalance('alice', [coin('foo', 1000)]);

// Act
bank.send('alice', 'bob', [{denom: 'foo', amount: '100'}]).unwrap();
bank.send('alice', 'bob', [coin('foo', 100)]).unwrap();

// Assert
expect(bank.getBalance('alice')).toEqual([coin('foo', 900)]);
expect(bank.getBalance('bob')).toEqual([coin('foo', 100)]);
expect(bank.getBalances()).toEqual(fromJS({
expect(bank.getBalances()).toEqual({
alice: [coin('foo', 900)],
bob: [coin('foo', 100)],
}));
});
});

it('handle send failure', () => {
Expand All @@ -45,9 +45,9 @@ describe('BankModule', () => {

// Assert
expect(res.err).toBeDefined();
expect(bank.getBalances()).toEqual(fromJS({
expect(bank.getBalances()).toEqual({
alice: [coin('foo', 100)],
}));
});
expect(bank.getBalance('alice')).toEqual([coin('foo', 100)]);
});

Expand All @@ -61,25 +61,25 @@ describe('BankModule', () => {

// Assert
expect(bank.getBalance('alice')).toEqual([coin('foo', 900)]);
expect(bank.getBalances()).toEqual(fromJS({
expect(bank.getBalances()).toEqual({
alice: [coin('foo', 900)],
}));
});
});

it('handle burn failure', () => {
// Arrange
const bank = chain.bank;
bank.setBalance('alice', [{denom: 'foo', amount: '100'}]);
bank.setBalance('alice', [coin('foo', 100)]);

// Act
const res = bank.burn('alice', [{denom: 'foo', amount: '1000'}]);
const res = bank.burn('alice', [coin('foo', 1000)]);

// Assert
expect(res.err).toBeDefined()
expect(bank.getBalance('alice')).toEqual([coin('foo', 100)]);
expect(bank.getBalances()).toEqual(fromJS({
expect(bank.getBalances()).toEqual({
alice: [coin('foo', 100)],
}));
});
});

it('handle msg', () => {
Expand All @@ -92,24 +92,24 @@ describe('BankModule', () => {
bank: {
send: {
to_address: 'bob',
amount: [{denom: 'foo', amount: '100'}],
amount: [coin('foo', 100)],
}
}
};
chain.handleMsg('alice', msg);

// Assert
expect(bank.getBalances()).toEqual(fromJS({
expect(bank.getBalances()).toEqual({
alice: [coin('foo', 900)],
bob: [coin('foo', 100)],
}));
});
});

it('contract integration', async () => {
// Arrange
const bank = chain.bank;
const contract = await new TestContract(chain).instantiate();
bank.setBalance(contract.address, [{denom: 'foo', amount: '1000'}]);
bank.setBalance(contract.address, [coin('foo', 1000)]);

// Act
const msg = exec.run(
Expand All @@ -135,11 +135,11 @@ describe('BankModule', () => {

// Assert
expect(res.ok).toBeTruthy();
expect(bank.getBalances()).toEqual(fromJS({
expect(bank.getBalances()).toMatchObject({
[contract.address]: [coin('foo', 700)],
alice: [coin('foo', 100)],
bob: [coin('foo', 100)],
}));
});
});

it('querier integration', () => {
Expand Down Expand Up @@ -184,17 +184,17 @@ describe('BankModule', () => {
it('handle delete', () => {
// Arrange
const bank = chain.bank;
bank.setBalance('alice', [{denom: 'foo', amount: '1000'}]);
bank.setBalance('bob', [{denom: 'fizz', amount: '900'}]);
bank.setBalance('alice', [coin('foo', 1000)]);
bank.setBalance('bob', [coin('fizz', 900)]);

// Act
bank.deleteBalance('bob');

// Assert
expect(bank.getBalance('alice')).toBeDefined();
expect(bank.getBalances()).toEqual(fromJS({
alice: [{denom: 'foo', amount: '1000'}],
}));
expect(bank.getBalances()).toEqual({
alice: [coin('foo', 1000)],
});
});
});

Expand Down
14 changes: 7 additions & 7 deletions src/modules/bank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Err, Ok, Result } from 'ts-results';
import { Binary } from '../types';
import { CWSimulateApp } from '../CWSimulateApp';
import { toBinary } from '../util';
import { TransactionalLens } from '../store/transactional';
import { fromImmutable, toImmutable, TransactionalLens } from '../store/transactional';

export interface AppResponse {
events: any[];
Expand Down Expand Up @@ -123,15 +123,15 @@ export class BankModule {
});
}

public getBalance(address: string) {
const immu = this.getBalances().get(address)?.toArray();
return immu?.map<Coin>(immuCoin => ({
denom: immuCoin.get('denom')!,
amount: immuCoin.get('amount')!,
})) ?? [];
public getBalance(address: string): Coin[] {
return this.store.getObject('balances', address) ?? [];
}

public getBalances() {
return this.store.getObject('balances');
}

public getImmutableBalances() {
return this.store.get('balances');
}

Expand Down
4 changes: 2 additions & 2 deletions src/modules/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export class WasmModule {
instantiateMsg: any,
trace: TraceLog[] = []
): Promise<Result<AppResponse, string>> {
return this.chain.pushBlock(async () => {
return await this.chain.pushBlock(async () => {
const snapshot = this.store.db.data;

// first register the contract instance
Expand Down Expand Up @@ -411,7 +411,7 @@ export class WasmModule {
executeMsg: any,
trace: TraceLog[] = []
): Promise<Result<AppResponse, string>> {
return this.chain.pushBlock(async () => {
return await this.chain.pushBlock(async () => {
let snapshot = this.store.db.data;
let logs: DebugLog[] = [];

Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { fromBase64, fromUtf8, toBase64, toUtf8 } from "@cosmjs/encoding";
import { Err, Ok, Result } from "ts-results";
import { Binary, RustResult } from "./types";

export const isArrayLike = (value: any): value is any[] => typeof value === 'object' && typeof value.length === 'number' && value[0] && value[value.length-1];
export const isArrayLike = (value: any): value is any[] => typeof value === 'object' && typeof value.length === 'number';

export const toBinary = (value: any): Binary => toBase64(toUtf8(JSON.stringify(value)));
export const fromBinary = (str: string): unknown => JSON.parse(fromUtf8(fromBase64(str)));
Expand Down

0 comments on commit ea39149

Please sign in to comment.