Skip to content

Commit c2ad998

Browse files
committed
feat: integrate advancer in StartFn
1 parent 7915eaa commit c2ad998

6 files changed

+116
-34
lines changed

packages/fast-usdc/src/exos/advancer.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { addressTools } from '../utils/address.js';
1313
* @import {HostInterface} from '@agoric/async-flow';
1414
* @import {NatAmount} from '@agoric/ertp';
1515
* @import {ChainAddress, ChainHub, Denom, DenomAmount, OrchestrationAccount} from '@agoric/orchestration';
16-
* @import {VowTools} from '@agoric/vow';
16+
* @import {Vow, VowTools} from '@agoric/vow';
1717
* @import {Zone} from '@agoric/zone';
1818
* @import {CctpTxEvidence, LogFn} from '../types.js';
1919
* @import {StatusManager} from './status-manager.js';
@@ -85,7 +85,7 @@ export const prepareAdvancerKit = (
8585
* @param {{
8686
* assetManagerFacet: AssetManagerFacet;
8787
* localDenom: Denom;
88-
* poolAccount: ERef<HostInterface<OrchestrationAccount<{chainId: 'agoric';}>>>
88+
* poolAccount: Vow<HostInterface<OrchestrationAccount<{chainId: 'agoric';}>>> | HostInterface<OrchestrationAccount<{chainId: 'agoric';}>>;
8989
* usdcBrand: Brand<'nat'>;
9090
* }} config
9191
*/
@@ -107,8 +107,9 @@ export const prepareAdvancerKit = (
107107
async handleTransactionEvent(evidence) {
108108
await null;
109109
try {
110-
// TODO poolAccount might be a vow we need to unwrap
111-
const { assetManagerFacet, poolAccount, usdcBrand } = this.state;
110+
const { assetManagerFacet, usdcBrand } = this.state;
111+
// is there a better way than this?
112+
const poolAccount = await when(this.state.poolAccount);
112113
const { recipientAddress } = evidence.aux;
113114
const { EUD } =
114115
addressTools.getQueryParams(recipientAddress).params;
@@ -173,6 +174,7 @@ export const prepareAdvancerKit = (
173174
*/
174175
onFulfilled(amount, destination) {
175176
const { localDenom, poolAccount } = this.state;
177+
// @ts-expect-error poolAccountV already settled
176178
const transferV = E(poolAccount).transfer(
177179
destination,
178180
/** @type {DenomAmount} */ ({

packages/fast-usdc/src/fast-usdc.contract.js

+39-12
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ import { prepareLiquidityPoolKit } from './exos/liquidity-pool.js';
1111
import { prepareSettler } from './exos/settler.js';
1212
import { prepareStatusManager } from './exos/status-manager.js';
1313
import { prepareTransactionFeedKit } from './exos/transaction-feed.js';
14+
import * as flows from './fast-usdc.flows.js';
1415

1516
const trace = makeTracer('FastUsdc');
1617

1718
/**
19+
* @import {HostInterface} from '@agoric/async-flow';
20+
* @import {OrchestrationAccount} from '@agoric/orchestration';
1821
* @import {OrchestrationPowers, OrchestrationTools} from '@agoric/orchestration/src/utils/start-helper.js';
22+
* @import {Vow} from '@agoric/vow';
1923
* @import {Zone} from '@agoric/zone';
2024
* @import {CctpTxEvidence} from './types.js';
2125
*/
@@ -24,13 +28,15 @@ const trace = makeTracer('FastUsdc');
2428
* @typedef {{
2529
* poolFee: Amount<'nat'>;
2630
* contractFee: Amount<'nat'>;
31+
* usdcDenom: string;
2732
* }} FastUsdcTerms
2833
*/
2934
const NatAmountShape = { brand: BrandShape, value: M.nat() };
3035
export const meta = {
3136
customTermsShape: {
3237
contractFee: NatAmountShape,
3338
poolFee: NatAmountShape,
39+
usdcDenom: M.string(),
3440
},
3541
};
3642
harden(meta);
@@ -55,7 +61,7 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
5561

5662
const statusManager = prepareStatusManager(zone);
5763
const makeSettler = prepareSettler(zone, { statusManager });
58-
const { chainHub, vowTools } = tools;
64+
const { chainHub, orchestrateAll, vowTools } = tools;
5965
const makeAdvancer = prepareAdvancer(zone, {
6066
chainHub,
6167
log: trace,
@@ -64,17 +70,6 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
6470
});
6571
const makeFeedKit = prepareTransactionFeedKit(zone);
6672
assertAllDefined({ makeFeedKit, makeAdvancer, makeSettler, statusManager });
67-
const feedKit = makeFeedKit();
68-
const advancer = makeAdvancer(
69-
// @ts-expect-error FIXME
70-
{},
71-
);
72-
// Connect evidence stream to advancer
73-
void observeIteration(subscribeEach(feedKit.public.getEvidenceStream()), {
74-
updateState(evidence) {
75-
void advancer.handleTransactionEvent(evidence);
76-
},
77-
});
7873
const makeLiquidityPoolKit = prepareLiquidityPoolKit(zone, {
7974
zcf,
8075
USDC: {
@@ -91,6 +86,7 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
9186
// return poolKit.feeSink.receive(amount, payment);
9287
},
9388
});
89+
const { makeLocalAccount } = orchestrateAll(flows, {});
9490

9591
const publicFacet = zone.exo('Fast USDC Public', undefined, {
9692
// XXX to be removed before production
@@ -106,6 +102,7 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
106102
makeTestPushInvitation(evidence) {
107103
// TODO(bootstrap integration): force this to throw and confirm that it
108104
// shows up in the the smart-wallet UpdateRecord `error` property
105+
// eslint-disable-next-line no-use-before-define
109106
feedKit.admin.submitEvidence(evidence);
110107
return zcf.makeInvitation(async cSeat => {
111108
trace('Offer made on noop invitation');
@@ -153,6 +150,36 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
153150
makeLiquidityPoolKit(shareMint, privateArgs.storageNode),
154151
);
155152

153+
const poolAccount =
154+
// cast to HostInterface
155+
/** @type { Vow<HostInterface<OrchestrationAccount<{chainId: 'agoric';}>>>} */ (
156+
/** @type {unknown}*/ (
157+
zone.makeOnce('Pool Local Orch Account', () => makeLocalAccount())
158+
)
159+
);
160+
161+
const advancer = zone.makeOnce('Advancer', () => {
162+
// TODO, ensure ChainHub is populated
163+
const localDenom = terms.usdcDenom;
164+
// from ChainHub is better?
165+
// chainHub.getDenom(terms.brands.USDC) ||
166+
// Fail`No brand or AssetDetail for USDC `;
167+
return makeAdvancer({
168+
localDenom,
169+
poolAccount,
170+
usdcBrand: terms.brands.USDC,
171+
assetManagerFacet: poolKit.assetManager,
172+
});
173+
});
174+
175+
const feedKit = zone.makeOnce('Feed Kit', () => makeFeedKit());
176+
// Connect evidence stream to advancer
177+
void observeIteration(subscribeEach(feedKit.public.getEvidenceStream()), {
178+
updateState(evidence) {
179+
void advancer.handleTransactionEvent(evidence);
180+
},
181+
});
182+
156183
return harden({ creatorFacet, publicFacet });
157184
};
158185
harden(contract);
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @import {HostInterface} from '@agoric/async-flow';
3+
* @import {Orchestrator, OrchestrationFlow, OrchestrationAccount} from '@agoric/orchestration';
4+
*/
5+
6+
/**
7+
* @satisfies {OrchestrationFlow}w
8+
* @param {Orchestrator} orch
9+
* @returns {Promise<OrchestrationAccount<{ chainId: 'agoric'; }>>}
10+
*/
11+
export const makeLocalAccount = async orch => {
12+
const agoricChain = await orch.getChain('agoric');
13+
return agoricChain.makeAccount();
14+
};
15+
harden(makeLocalAccount);

packages/fast-usdc/test/fast-usdc.contract.test.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { setUpZoeForTest } from '@agoric/zoe/tools/setup-zoe.js';
1111
import { E } from '@endo/far';
1212
import path from 'path';
1313
import fetchedChainInfo from '@agoric/orchestration/src/fetched-chain-info.js';
14+
import { denomHash } from '@agoric/orchestration';
15+
import type { NatAmount } from '@agoric/ertp';
1416
import { MockCctpTxEvidences } from './fixtures.js';
1517
import { commonSetup } from './supports.js';
1618

@@ -20,6 +22,15 @@ const contractName = 'fast-usdc';
2022
const contractFile = `${dirname}/../src/fast-usdc.contract.js`;
2123
type StartFn = typeof import('../src/fast-usdc.contract.js').start;
2224

25+
const makeDefaultTerms = (usdc: { make: (v: NatValue) => NatAmount }) => ({
26+
poolFee: usdc.make(1n),
27+
contractFee: usdc.make(1n),
28+
usdcDenom: denomHash({
29+
denom: 'uusdc',
30+
channelId: 'channel-62',
31+
}),
32+
});
33+
2334
test('start', async t => {
2435
const {
2536
bootstrap,
@@ -39,10 +50,7 @@ test('start', async t => {
3950
const { creatorFacet, publicFacet } = await E(zoe).startInstance(
4051
installation,
4152
{ USDC: usdc.issuer },
42-
{
43-
poolFee: usdc.make(1n),
44-
contractFee: usdc.make(1n),
45-
},
53+
makeDefaultTerms(usdc),
4654
commonPrivateArgs,
4755
);
4856
t.truthy(creatorFacet);
@@ -88,10 +96,7 @@ test('LP deposits, earns fees, withdraws', async t => {
8896
const { creatorFacet, publicFacet, instance } = await E(zoe).startInstance(
8997
installation,
9098
{ USDC: usdc.issuer },
91-
{
92-
poolFee: usdc.make(1n),
93-
contractFee: usdc.make(1n),
94-
},
99+
makeDefaultTerms(usdc),
95100
commonPrivateArgs,
96101
);
97102
t.truthy(creatorFacet);
@@ -203,10 +208,7 @@ test('baggage', async t => {
203208
await E(zoe).startInstance(
204209
installation,
205210
{ USDC: usdc.issuer },
206-
{
207-
poolFee: usdc.make(1n),
208-
contractFee: usdc.make(1n),
209-
},
211+
makeDefaultTerms(usdc),
210212
commonPrivateArgs,
211213
);
212214

packages/fast-usdc/test/snapshots/fast-usdc.contract.test.ts.md

+42-6
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,42 @@ Generated by [AVA](https://avajs.dev).
2222
},
2323
LogStore_kindHandle: 'Alleged: kind',
2424
StateUnwrapper_kindHandle: 'Alleged: kind',
25-
asyncFuncEagerWakers: [],
25+
asyncFuncEagerWakers: [
26+
Object @Alleged: asyncFlow flow {},
27+
],
2628
asyncFuncFailures: {},
27-
flowForOutcomeVow: {},
29+
flowForOutcomeVow: {
30+
'Alleged: VowInternalsKit vowV0': 'Alleged: asyncFlow flow',
31+
},
2832
unwrapMap: 'Alleged: weakMapStore',
2933
},
3034
chainHub: {
3135
ChainHub_kindHandle: 'Alleged: kind',
3236
ChainHub_singleton: 'Alleged: ChainHub',
33-
bech32PrefixToChainName: {},
37+
bech32PrefixToChainName: {
38+
agoric: 'agoric',
39+
},
3440
brandDenom: {},
35-
chainInfos: {},
41+
chainInfos: {
42+
agoric: {
43+
bech32Prefix: 'agoric',
44+
chainId: 'agoric-3',
45+
icqEnabled: false,
46+
stakingTokens: [
47+
{
48+
denom: 'ubld',
49+
},
50+
],
51+
},
52+
},
3653
connectionInfos: {},
3754
denom: {},
3855
lookupChainInfo_kindHandle: 'Alleged: kind',
3956
lookupChainsAndConnection_kindHandle: 'Alleged: kind',
4057
lookupConnectionInfo_kindHandle: 'Alleged: kind',
4158
},
4259
contract: {
60+
Advancer: 'Alleged: Fast USDC Advancer advancer',
4361
'Fast USDC Advancer_kindHandle': 'Alleged: kind',
4462
'Fast USDC Creator_kindHandle': 'Alleged: kind',
4563
'Fast USDC Creator_singleton': 'Alleged: Fast USDC Creator',
@@ -49,6 +67,10 @@ Generated by [AVA](https://avajs.dev).
4967
'Fast USDC Settler_kindHandle': 'Alleged: kind',
5068
'Fast USDC Status Manager_kindHandle': 'Alleged: kind',
5169
'Fast USDC Status Manager_singleton': 'Alleged: Fast USDC Status Manager',
70+
'Feed Kit': {
71+
admin: Object @Alleged: Fast USDC Feed admin {},
72+
public: Object @Alleged: Fast USDC Feed public {},
73+
},
5274
Kinds: {
5375
'Transaction Feed_kindHandle': 'Alleged: kind',
5476
},
@@ -61,11 +83,16 @@ Generated by [AVA](https://avajs.dev).
6183
},
6284
'Liquidity Pool_kindHandle': 'Alleged: kind',
6385
PendingTxs: {},
86+
'Pool Local Orch Account': 'Vow',
6487
SeenTxs: [],
6588
mint: {
6689
PoolShare: 'Alleged: zcfMint',
6790
},
68-
orchestration: {},
91+
orchestration: {
92+
makeLocalAccount: {
93+
asyncFlow_kindHandle: 'Alleged: kind',
94+
},
95+
},
6996
vstorage: {
7097
'Durable Publish Kit_kindHandle': 'Alleged: kind',
7198
Recorder_kindHandle: 'Alleged: kind',
@@ -77,7 +104,16 @@ Generated by [AVA](https://avajs.dev).
77104
LocalChainFacade_kindHandle: 'Alleged: kind',
78105
Orchestrator_kindHandle: 'Alleged: kind',
79106
RemoteChainFacade_kindHandle: 'Alleged: kind',
80-
chainName: {},
107+
chainName: {
108+
agoric: {
109+
pending: true,
110+
vow: Object @Vow {
111+
payload: {
112+
vowV0: Object @Alleged: VowInternalsKit vowV0 {},
113+
},
114+
},
115+
},
116+
},
81117
ibcTools: {
82118
IBCTransferSenderKit_kindHandle: 'Alleged: kind',
83119
ibcResultWatcher_kindHandle: 'Alleged: kind',
Binary file not shown.

0 commit comments

Comments
 (0)