Skip to content

Commit

Permalink
test(fast-usdc): types for agoric-mock
Browse files Browse the repository at this point in the history
  • Loading branch information
dckc committed Oct 24, 2024
1 parent c6be42f commit f0551b9
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions packages/fast-usdc/tools/agoric-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ import { ibcTransfer } from './cosmoverse-mock.js';
/**
* @import {Remote} from '@agoric/vow';
* @import {OrchestrationAccount} from '@agoric/orchestration';
* @import {LocalAccountMethods} from '@agoric/orchestration';
* @import {TargetRegistration} from '@agoric/vats/src/bridge-target.js';
* @import {CosmosChain} from './cosmoverse-mock.js';
* @import {ExecutionContext as AvaT} from 'ava';
*/

/**
* @param {any} t
* @param {Record<string, import('./cosmoverse-mock.js').CosmosChain>} chains
* @param {AvaT<any>} t
* @param {{
* agoric: ReturnType<typeof withVTransfer>
* } & Record<string, CosmosChain>
* } chains
* @param {{ brand: Brand; denom: string }[]} assetInfo
*/
export const makeOrchestration = (t, chains, assetInfo) => {
Expand All @@ -18,8 +25,11 @@ export const makeOrchestration = (t, chains, assetInfo) => {
/** @returns {Promise<OrchestrationAccount<any>>} */
const makeAccount = async () => {
const addr = await chains.agoric.makeAccount();
// @ts-expect-error only some methods are mocked
return harden({
/** @type {OrchestrationAccount<any>['getAddress']} */
getAddress: () => ({ value: addr, chainId: 'agoric3', encoding }),
/** @type {OrchestrationAccount<any>['getPublicTopics']} */
getPublicTopics: async () => ({
account: {
subscriber: {
Expand All @@ -34,16 +44,19 @@ export const makeOrchestration = (t, chains, assetInfo) => {
storagePath: 'XXX',
},
}),
/** @type {OrchestrationAccount<any>['transfer']} */
transfer: async (dest, { value: amount }) => {
t.log(next(), 'orch acct', addr, 'txfr', amount, 'to', dest.value);
await ibcTransfer(chains, { amount, dest: dest.value, from: addr, t });
},
/** @type {OrchestrationAccount<any>['send']} */
send: async (dest, { value: amount }) => {
t.log(next(), 'orch acct', addr, 'send', amount, 'to', dest.value);
await chains.agoric.send({ amount, dest: dest.value, from: addr });
},
monitorTransfers: async handler => {
await chains.agoric.register({ addr, handler });
/** @type {LocalAccountMethods['monitorTransfers']} */
monitorTransfers: async tap => {
return chains.agoric.register({ addr, tap });
},
});
};
Expand Down Expand Up @@ -75,14 +88,26 @@ export const makeVStorage = () => {

const mkEvent = data => ({ packet: { data: btoa(JSON.stringify(data)) } });

/**
* @param {CosmosChain} chain
* @param {AvaT} t
*/
export const withVTransfer = (chain, t) => {
const addrToTap = new Map();
return harden({
...chain,
register: async ({ addr, handler }) => {
t.log('vtransfer register', { addr, handler });
/**
* @param {{addr: string, tap: {receiveUpcall: (obj: any) => Promise<any>}}} param0
* @returns {Promise<TargetRegistration>}
*/
register: async ({ addr, tap }) => {
t.log('vtransfer register', { addr, tap });
!addrToTap.has(addr) || assert.fail('already registered');
addrToTap.set(addr, handler);
addrToTap.set(addr, tap);
return harden({
revoke: async () => {},
updateTargetApp: async () => {},
});
},
send: async ({ amount, from, dest }) => {
const [agAddr, extra] = AgoricCalc.isVirtualAddress(dest)
Expand Down

0 comments on commit f0551b9

Please sign in to comment.