Skip to content

Commit

Permalink
feat: combine-invitation-makers exo
Browse files Browse the repository at this point in the history
- Takes two or more InvitationMaker exos and combines them into a new one.
- Useful for combining exos that are already instantiated
  • Loading branch information
0xpatrickdev authored and turadg committed Sep 3, 2024
1 parent 3c66f4b commit bc558d0
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 19 deletions.
71 changes: 71 additions & 0 deletions packages/orchestration/src/exos/combine-invitation-makers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { M } from '@endo/patterns';
import {
prepareGuardedAttenuator,
makeSyncMethodCallback,
} from '@agoric/internal/src/callback.js';
import { getMethodNames } from '@agoric/internal';
import { orchestrationAccountInvitationMakers } from '../utils/orchestrationAccount.js';

/**
* @import {MakeAttenuator} from '@agoric/internal/src/callback.js';
* @import {InvitationMakers} from '@agoric/smart-wallet/src/types.js';
* @import {Zone} from '@agoric/zone';
* @import {MethodGuard} from '@endo/patterns';
* @import {CosmosOrchestrationAccountKit} from './cosmos-orchestration-account.js';
*/

/**
* XXX parameterize this in `prepareGuardedAttenuator`
*
* @typedef {{
* Restake: (
* validator: import('../cosmos-api.js').CosmosValidatorAddress,
* opts: import('../examples/restake.kit.js').RepeaterOpts,
* ) => Promise<Invitation>;
* CancelRestake: () => Promise<Invitation>;
* }} NewMethods
*/

/**
* Takes two or more InvitationMaker exos and combines them into a new one.
*
* @param {Zone} zone
* @param {Record<string, MethodGuard>} methodGuards
*/
export const prepareCombineInvitationMakers = (zone, methodGuards) => {
const CombinedInterfaceGuard = M.interface('ResolvedContinuingOfferResult', {
...orchestrationAccountInvitationMakers,
...methodGuards,
});

/**
* @typedef {CosmosOrchestrationAccountKit['invitationMakers']} CosmosOrchAccountInvMakers
*/

// @ts-expect-error Index signature for type 'string' is missing in type
/** @type {MakeAttenuator<CosmosOrchAccountInvMakers & NewMethods>} */
const mixin = prepareGuardedAttenuator(zone, CombinedInterfaceGuard, {
tag: 'CombinedInvitationMakers',
});

/**
* @param {...InvitationMakers} invitationMakers
*/
const combineInvitationMakers = (...invitationMakers) => {
const overrides = {};
for (const invMakers of invitationMakers) {
// remove '__getInterfaceGuard__', '__getMethodNames__'
const names = getMethodNames(invMakers).filter(n => !n.startsWith('__'));
for (const key of names) {
overrides[key] = makeSyncMethodCallback(invMakers, key);
}
}
return mixin({
overrides,
});
};

return combineInvitationMakers;
};

/** @typedef {ReturnType<typeof prepareCombineInvitationMakers>} MakeCombineInvitationMakers */
27 changes: 8 additions & 19 deletions packages/orchestration/src/exos/cosmos-orchestration-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ import {
} from '../typeGuards.js';
import { coerceCoin, coerceDenom } from '../utils/amounts.js';
import { maxClockSkew, tryDecodeResponse } from '../utils/cosmos.js';
import { orchestrationAccountMethods } from '../utils/orchestrationAccount.js';
import {
orchestrationAccountInvitationMakers,
orchestrationAccountMethods,
} from '../utils/orchestrationAccount.js';
import { makeTimestampHelper } from '../utils/time.js';

/**
Expand Down Expand Up @@ -169,24 +172,10 @@ export const prepareCosmosOrchestrationAccountKit = (
.returns(Vow$(M.record())),
}),
holder: IcaAccountHolderI,
invitationMakers: M.interface('invitationMakers', {
Delegate: M.call(ChainAddressShape, AmountArgShape).returns(
M.promise(),
),
Redelegate: M.call(
ChainAddressShape,
ChainAddressShape,
AmountArgShape,
).returns(M.promise()),
WithdrawReward: M.call(ChainAddressShape).returns(M.promise()),
Undelegate: M.call(M.arrayOf(DelegationShape)).returns(M.promise()),
DeactivateAccount: M.call().returns(M.promise()),
ReactivateAccount: M.call().returns(M.promise()),
TransferAccount: M.call().returns(M.promise()),
Send: M.call().returns(M.promise()),
SendAll: M.call().returns(M.promise()),
Transfer: M.call().returns(M.promise()),
}),
invitationMakers: M.interface(
'invitationMakers',
orchestrationAccountInvitationMakers,
),
},
/**
* @param {object} info
Expand Down
18 changes: 18 additions & 0 deletions packages/orchestration/src/utils/orchestrationAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { M } from '@endo/patterns';
import {
AmountArgShape,
ChainAddressShape,
DelegationShape,
DenomAmountShape,
IBCTransferOptionsShape,
} from '../typeGuards.js';
Expand Down Expand Up @@ -38,3 +39,20 @@ export const orchestrationAccountMethods = {
),
getPublicTopics: M.call().returns(Vow$(TopicsRecordShape)),
};

export const orchestrationAccountInvitationMakers = {
Delegate: M.call(ChainAddressShape, AmountArgShape).returns(M.promise()),
Redelegate: M.call(
ChainAddressShape,
ChainAddressShape,
AmountArgShape,
).returns(M.promise()),
WithdrawReward: M.call(ChainAddressShape).returns(M.promise()),
Undelegate: M.call(M.arrayOf(DelegationShape)).returns(M.promise()),
DeactivateAccount: M.call().returns(M.promise()),
ReactivateAccount: M.call().returns(M.promise()),
TransferAccount: M.call().returns(M.promise()),
Send: M.call().returns(M.promise()),
SendAll: M.call().returns(M.promise()),
Transfer: M.call().returns(M.promise()),
};

0 comments on commit bc558d0

Please sign in to comment.