Skip to content

Commit

Permalink
feat(orchestration): ChainAccountKit returns unwrapped vows
Browse files Browse the repository at this point in the history
- refs: #9449
  • Loading branch information
0xpatrickdev committed Jun 17, 2024
1 parent 865aeff commit 9049871
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
42 changes: 30 additions & 12 deletions packages/orchestration/src/exos/chainAccountKit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { NonNullish } from '@agoric/assert';
import { PurseShape } from '@agoric/ertp';
import { makeTracer } from '@agoric/internal';
import { V as E } from '@agoric/vow/vat.js';
import { E } from '@endo/far';
import { M } from '@endo/patterns';
import {
ChainAddressShape,
Expand All @@ -15,7 +15,7 @@ import { makeTxPacket, parseTxPacket } from '../utils/packet.js';
/**
* @import {Zone} from '@agoric/base-zone';
* @import {Connection, Port} from '@agoric/network';
* @import {Remote} from '@agoric/vow';
* @import {Remote, VowTools} from '@agoric/vow';
* @import {AnyJson} from '@agoric/cosmic-proto';
* @import {TxBody} from '@agoric/cosmic-proto/cosmos/tx/v1beta1/tx.js';
* @import {LocalIbcAddress, RemoteIbcAddress} from '@agoric/vats/tools/ibc-utils.js';
Expand Down Expand Up @@ -55,11 +55,22 @@ export const ChainAccountI = M.interface('ChainAccount', {
* }} State
*/

/** @param {Zone} zone */
export const prepareChainAccountKit = zone =>
/**
* @param {Zone} zone
* @param {VowTools} vowTools
*/
export const prepareChainAccountKit = (zone, { watch, when }) =>
zone.exoClassKit(
'ChainAccountKit',
{ account: ChainAccountI, connectionHandler: ConnectionHandlerI },
{
account: ChainAccountI,
connectionHandler: ConnectionHandlerI,
handleExecuteEncodedTxWatcher: M.interface('HandleQueryWatcher', {
onFulfilled: M.call(M.string())
.optional(M.arrayOf(M.undefined())) // does not need watcherContext
.returns(M.string()),
}),
},
/**
* @param {string} chainId
* @param {Port} port
Expand All @@ -76,6 +87,12 @@ export const prepareChainAccountKit = zone =>
localAddress: undefined,
}),
{
handleExecuteEncodedTxWatcher: {
/** @param {string} ack */
onFulfilled(ack) {
return parseTxPacket(ack);
},
},
account: {
/** @returns {ChainAddress} */
getAddress() {
Expand Down Expand Up @@ -123,20 +140,21 @@ export const prepareChainAccountKit = zone =>
executeEncodedTx(msgs, opts) {
const { connection } = this.state;
if (!connection) throw Fail`connection not available`;
return E.when(
E(connection).send(makeTxPacket(msgs, opts)),
// if parseTxPacket cannot find a `result` key, it throws
ack => parseTxPacket(ack),
return when(
watch(
E(connection).send(makeTxPacket(msgs, opts)),
this.facets.handleExecuteEncodedTxWatcher,
),
);
},
/** Close the remote account */
async close() {
/// XXX what should the behavior be here? and `onClose`?
close() {
/// TODO #9192 what should the behavior be here? and `onClose`?
// - retrieve assets?
// - revoke the port?
const { connection } = this.state;
if (!connection) throw Fail`connection not available`;
await E(connection).close();
return when(watch(E(connection).close()));
},
/**
* get Purse for a brand to .withdraw() a Payment from the account
Expand Down
2 changes: 1 addition & 1 deletion packages/orchestration/src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ const prepareOrchestrationKit = (
* @param {VowTools} vowTools
*/
export const prepareOrchestrationTools = (zone, vowTools) => {
const makeChainAccountKit = prepareChainAccountKit(zone);
const makeChainAccountKit = prepareChainAccountKit(zone, vowTools);
const makeICQConnectionKit = prepareICQConnectionKit(zone, vowTools);
const makeOrchestrationKit = prepareOrchestrationKit(
zone,
Expand Down
35 changes: 26 additions & 9 deletions packages/orchestration/test/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { toRequestQueryJson } from '@agoric/cosmic-proto';
import { QueryBalanceRequest } from '@agoric/cosmic-proto/cosmos/bank/v1beta1/query.js';
import { MsgDelegate } from '@agoric/cosmic-proto/cosmos/staking/v1beta1/tx.js';
import { Any } from '@agoric/cosmic-proto/google/protobuf/any.js';
import { matches } from '@endo/patterns';
import { commonSetup } from './supports.js';
import { ChainAddressShape } from '../src/typeGuards.js';

test('makeICQConnection returns an ICQConnection', async t => {
const {
Expand Down Expand Up @@ -89,17 +91,32 @@ test('makeAccount returns a ChainAccount', async t => {
'remote address contains version and encoding in version string',
);

t.true(matches(chainAddr, ChainAddressShape));
t.regex(
chainAddr.address,
/UNPARSABLE_CHAIN_ADDRESS/,
'TODO use mocked ibc connection instead of echo connection',
);

const delegateMsg = Any.toJSON(
MsgDelegate.toProtoMsg({
delegatorAddress: 'cosmos1test',
validatorAddress: 'cosmosvaloper1test',
amount: { denom: 'uatom', amount: '10' },
}),
);
await t.throwsAsync(
E(account).executeEncodedTx([
Any.toJSON(
MsgDelegate.toProtoMsg({
delegatorAddress: 'cosmos1test',
validatorAddress: 'cosmosvaloper1test',
amount: { denom: 'uatom', amount: '10' },
}),
),
]),
E(account).executeEncodedTx([delegateMsg]),
{ message: /"type":1(.*)"data":"(.*)"memo":""/ },
'TODO do not use echo connection',
);

await E(account).close();
await t.throwsAsync(
E(account).executeEncodedTx([delegateMsg]),
{
message: 'Connection closed',
},
'cannot execute transaction if connection is closed',
);
});

0 comments on commit 9049871

Please sign in to comment.