Skip to content

Commit

Permalink
Merge pull request #31 from Terran-One/feat/BalancesByStep
Browse files Browse the repository at this point in the history
Fetching balance using store snapshot at every step.
  • Loading branch information
dabralyogesh authored Jan 25, 2023
2 parents 32bf0d8 + b3303f6 commit 362d81e
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/modules/bank.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Coin } from '@cosmjs/amino';
import { fromJS, List, Map } from 'immutable';
import { Err, Ok, Result } from 'ts-results';
import { Binary } from '../types';
import { Binary, Snapshot } from '../types';
import { CWSimulateApp } from '../CWSimulateApp';
import { toBinary } from '../util';
import { fromImmutable, toImmutable, TransactionalLens } from '../store/transactional';
import { Transactional, TransactionalLens } from '../store/transactional';

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

public getBalance(address: string): Coin[] {
return this.store.getObject('balances', address) ?? [];
public getBalance(address: string, storage?: Snapshot): Coin[] {
return this.lens(storage).getObject('balances', address) ?? [];
}

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

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

public deleteBalance(address:string) {
this.store.tx((_, deleter) => {
deleter('balances', address);
Expand Down Expand Up @@ -201,6 +197,9 @@ export class BankModule {
}
return Err('Unknown bank query');
}
private lens(storage?: Snapshot) {
return storage ? lensFromSnapshot(storage) : this.store;
}
}

/** Essentially a `Coin`, but the `amount` is a `bigint` for more convenient use. */
Expand All @@ -218,3 +217,7 @@ export class ParsedCoin {
return new ParsedCoin(coin.denom, BigInt(coin.amount));
}
}

export function lensFromSnapshot(snapshot: Snapshot) {
return new Transactional(snapshot).lens<BankData>('contracts');
}

0 comments on commit 362d81e

Please sign in to comment.