Skip to content

Commit

Permalink
feat: localOrchAccount.getBalances
Browse files Browse the repository at this point in the history
- use QueryAllBalancesRequest and localchain.query to return a list of a localOrchAccount's cosmos bank balances
  • Loading branch information
0xpatrickdev committed Sep 4, 2024
1 parent 400832c commit aee8ef0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/orchestration/src/exos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ classDiagram
executeTx()
getAddress()
getBalance()
getBalances()
getPublicTopics()
monitorTransfers()
send()
Expand Down
34 changes: 32 additions & 2 deletions packages/orchestration/src/exos/local-orchestration-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ export const prepareLocalOrchestrationAccountKit = (
queryBalanceWatcher: M.interface('queryBalanceWatcher', {
onFulfilled: M.call(TypedJsonShape).returns(DenomAmountShape),
}),
queryBalancesWatcher: M.interface('queryBalancesWatcher', {
onFulfilled: M.call(TypedJsonShape).returns(
M.arrayOf(DenomAmountShape),
),
}),
invitationMakers: M.interface('invitationMakers', {
Delegate: M.call(M.string(), AmountShape).returns(M.promise()),
Undelegate: M.call(M.string(), AmountShape).returns(M.promise()),
Expand Down Expand Up @@ -374,6 +379,25 @@ export const prepareLocalOrchestrationAccountKit = (
return harden(toDenomAmount(balance));
},
},
/**
* handles a QueryAllBalancesRequest from localchain.query and returns the
* balances as a DenomAmounts
*/
queryBalancesWatcher: {
/**
* @param {ResponseTo<
* TypedJson<'/cosmos.bank.v1beta1.QueryAllBalancesRequest'>
* >} result
* @returns {DenomAmount[]}
*/
onFulfilled(result) {
const { balances } = result;
if (!balances || !Array.isArray(balances)) {
throw Fail`Expected balances ${q(result)};`;
}
return harden(balances.map(toDenomAmount));
},
},
holder: {
/** @type {HostOf<OrchestrationAccountI['asContinuingOffer']>} */
asContinuingOffer() {
Expand Down Expand Up @@ -432,8 +456,14 @@ export const prepareLocalOrchestrationAccountKit = (
},
/** @type {HostOf<OrchestrationAccountI['getBalances']>} */
getBalances() {
// TODO https://github.com/Agoric/agoric-sdk/issues/9610
return asVow(() => Fail`not yet implemented`);
return watch(
E(localchain).query(
typedJson('/cosmos.bank.v1beta1.QueryAllBalancesRequest', {
address: this.state.address.value,
}),
),
this.facets.queryBalancesWatcher,
);
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';
import { AmountMath, makeIssuerKit } from '@agoric/ertp';
import { eventLoopIteration } from '@agoric/internal/src/testing-utils.js';
import { TargetApp } from '@agoric/vats/src/bridge-target.js';
import { SIMULATED_ERRORS } from '@agoric/vats/tools/fake-bridge.js';
import {
LOCALCHAIN_QUERY_ALL_BALANCES_RESPONSE,
SIMULATED_ERRORS,
} from '@agoric/vats/tools/fake-bridge.js';
import { heapVowE as VE } from '@agoric/vow/vat.js';
import { withAmountUtils } from '@agoric/zoe/tools/test-utils.js';
import { ChainAddress, type AmountArg } from '../../src/orchestration-api.js';
Expand Down Expand Up @@ -401,3 +404,25 @@ test('getBalance', async t => {
'only sent query for ibc/1234',
);
});

test('getBalances', async t => {
const common = await commonSetup(t);
const makeTestLOAKit = prepareMakeTestLOAKit(t, common);
const account = await makeTestLOAKit();
t.truthy(account, 'account is returned');

const {
utils: { inspectLocalBridge },
} = common;

t.deepEqual(
await VE(account).getBalances(),
LOCALCHAIN_QUERY_ALL_BALANCES_RESPONSE,
);

const localBridgeMessages = inspectLocalBridge();
const queryMessages = localBridgeMessages.filter(
x => x.type === 'VLOCALCHAIN_QUERY_MANY',
);
t.is(queryMessages.length, 1, 'getBalances sends query to cosmos golang');
});

0 comments on commit aee8ef0

Please sign in to comment.