Skip to content

Commit

Permalink
feat: integrate advancer in StartFn
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpatrickdev committed Nov 15, 2024
1 parent cbbdd0b commit b0ec75a
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 37 deletions.
26 changes: 11 additions & 15 deletions packages/fast-usdc/src/exos/advancer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,11 @@ const { isGTE } = AmountMath;
* @import {HostInterface} from '@agoric/async-flow';
* @import {NatAmount} from '@agoric/ertp';
* @import {ChainAddress, ChainHub, Denom, DenomAmount, OrchestrationAccount} from '@agoric/orchestration';
* @import {VowTools} from '@agoric/vow';
* @import {Vow, VowTools} from '@agoric/vow';
* @import {Zone} from '@agoric/zone';
* @import {CctpTxEvidence, FeeConfig, LogFn} from '../types.js';
* @import {StatusManager} from './status-manager.js';
*/

/**
* Expected interface from LiquidityPool
*
* @typedef {{
* lookupBalance(): NatAmount;
* borrow(amount: Amount<"nat">): Promise<Payment<"nat">>;
* repay(payments: PaymentKeywordRecord): Promise<void>
* }} AssetManagerFacet
* @import {AssetManagerFacet} from './liquidity-pool.js';
*/

/**
Expand Down Expand Up @@ -96,7 +87,7 @@ export const prepareAdvancerKit = (
/**
* @param {{
* assetManagerFacet: AssetManagerFacet;
* poolAccount: ERef<HostInterface<OrchestrationAccount<{chainId: 'agoric'}>>>;
* poolAccount: Vow<HostInterface<OrchestrationAccount<{chainId: 'agoric'}>>> | HostInterface<OrchestrationAccount<{chainId: 'agoric'}>>;
* }} config
*/
config => harden(config),
Expand All @@ -115,8 +106,9 @@ export const prepareAdvancerKit = (
async handleTransactionEvent(evidence) {
await null;
try {
// TODO poolAccount might be a vow we need to unwrap
const { assetManagerFacet, poolAccount } = this.state;
const { assetManagerFacet } = this.state;
// TODO is there a better way than this?
const poolAccount = await when(this.state.poolAccount);
const { recipientAddress } = evidence.aux;
const { EUD } = addressTools.getQueryParams(
recipientAddress,
Expand Down Expand Up @@ -177,7 +169,11 @@ export const prepareAdvancerKit = (
* @param {{ destination: ChainAddress; payment: Payment<'nat'> }} ctx
*/
onFulfilled(amount, { destination }) {
const { poolAccount } = this.state;
// TODO do we need to ensure this isn't Vow for an LOA?
const poolAccount =
/** @type {HostInterface<OrchestrationAccount<{chainId: 'agoric'}>>} */ (
this.state.poolAccount
);
const transferV = E(poolAccount).transfer(
destination,
/** @type {DenomAmount} */ ({
Expand Down
51 changes: 35 additions & 16 deletions packages/fast-usdc/src/fast-usdc.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ import { prepareStatusManager } from './exos/status-manager.js';
import { prepareTransactionFeedKit } from './exos/transaction-feed.js';
import { defineInertInvitation } from './utils/zoe.js';
import { FeeConfigShape } from './typeGuards.js';
import * as flows from './fast-usdc.flows.js';

const trace = makeTracer('FastUsdc');

/**
* @import {Denom} from '@agoric/orchestration';
* @import {HostInterface} from '@agoric/async-flow';
* @import {OrchestrationAccount} from '@agoric/orchestration';
* @import {OrchestrationPowers, OrchestrationTools} from '@agoric/orchestration/src/utils/start-helper.js';
* @import {Vow} from '@agoric/vow';
* @import {Zone} from '@agoric/zone';
* @import {OperatorKit} from './exos/operator-kit.js';
* @import {CctpTxEvidence, FeeConfig} from './types.js';
Expand Down Expand Up @@ -59,7 +63,7 @@ export const contract = async (zcf, privateArgs, zone, tools) => {

const statusManager = prepareStatusManager(zone);
const makeSettler = prepareSettler(zone, { statusManager });
const { chainHub, vowTools } = tools;
const { chainHub, orchestrateAll, vowTools } = tools;
const makeAdvancer = prepareAdvancer(zone, {
chainHub,
feeConfig: privateArgs.feeConfig,
Expand All @@ -73,21 +77,6 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
});
const makeFeedKit = prepareTransactionFeedKit(zone, zcf);
assertAllDefined({ makeFeedKit, makeAdvancer, makeSettler, statusManager });
const feedKit = makeFeedKit();
const advancer = makeAdvancer(
// @ts-expect-error FIXME
{},
);
// Connect evidence stream to advancer
void observeIteration(subscribeEach(feedKit.public.getEvidenceSubscriber()), {
updateState(evidence) {
try {
void advancer.handleTransactionEvent(evidence);
} catch (err) {
trace('🚨 Error handling transaction event', err);
}
},
});
const makeLiquidityPoolKit = prepareLiquidityPoolKit(zone, {
zcf,
usdc: {
Expand All @@ -105,6 +94,7 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
const creatorFacet = zone.exo('Fast USDC Creator', undefined, {
/** @type {(operatorId: string) => Promise<Invitation<OperatorKit>>} */
async makeOperatorInvitation(operatorId) {
// eslint-disable-next-line no-use-before-define
return feedKit.creator.makeOperatorInvitation(operatorId);
},
simulateFeesFromAdvance(/* amount, payment */) {
Expand All @@ -113,6 +103,7 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
// return poolKit.feeSink.receive(amount, payment);
},
});
const { makeLocalAccount } = orchestrateAll(flows, {});

const publicFacet = zone.exo('Fast USDC Public', undefined, {
// XXX to be removed before production
Expand All @@ -126,6 +117,7 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
* @param {CctpTxEvidence} evidence
*/
makeTestPushInvitation(evidence) {
// eslint-disable-next-line no-use-before-define
void advancer.handleTransactionEvent(evidence);
return makeTestInvitation();
},
Expand Down Expand Up @@ -169,6 +161,33 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
makeLiquidityPoolKit(shareMint, privateArgs.storageNode),
);

const poolAccount =
// cast to HostInterface
/** @type { Vow<HostInterface<OrchestrationAccount<{chainId: 'agoric';}>>>} */ (
/** @type {unknown}*/ (
zone.makeOnce('Pool Local Orch Account', () => makeLocalAccount())
)
);

const advancer = zone.makeOnce('Advancer', () =>
makeAdvancer({
assetManagerFacet: poolKit.assetManager,
poolAccount,
}),
);

const feedKit = zone.makeOnce('Feed Kit', () => makeFeedKit());
// Connect evidence stream to advancer
void observeIteration(subscribeEach(feedKit.public.getEvidenceSubscriber()), {
updateState(evidence) {
try {
void advancer.handleTransactionEvent(evidence);
} catch (err) {
trace('🚨 Error handling transaction event', err);
}
},
});

return harden({ creatorFacet, publicFacet });
};
harden(contract);
Expand Down
15 changes: 15 additions & 0 deletions packages/fast-usdc/src/fast-usdc.flows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @import {HostInterface} from '@agoric/async-flow';
* @import {Orchestrator, OrchestrationFlow, OrchestrationAccount} from '@agoric/orchestration';
*/

/**
* @satisfies {OrchestrationFlow}w
* @param {Orchestrator} orch
* @returns {Promise<OrchestrationAccount<{ chainId: 'agoric'; }>>}
*/
export const makeLocalAccount = async orch => {
const agoricChain = await orch.getChain('agoric');
return agoricChain.makeAccount();
};
harden(makeLocalAccount);
49 changes: 43 additions & 6 deletions packages/fast-usdc/test/snapshots/fast-usdc.contract.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,42 @@ Generated by [AVA](https://avajs.dev).
},
LogStore_kindHandle: 'Alleged: kind',
StateUnwrapper_kindHandle: 'Alleged: kind',
asyncFuncEagerWakers: [],
asyncFuncEagerWakers: [
Object @Alleged: asyncFlow flow {},
],
asyncFuncFailures: {},
flowForOutcomeVow: {},
flowForOutcomeVow: {
'Alleged: VowInternalsKit vowV0': 'Alleged: asyncFlow flow',
},
unwrapMap: 'Alleged: weakMapStore',
},
chainHub: {
ChainHub_kindHandle: 'Alleged: kind',
ChainHub_singleton: 'Alleged: ChainHub',
bech32PrefixToChainName: {},
bech32PrefixToChainName: {
agoric: 'agoric',
},
brandDenom: {},
chainInfos: {},
chainInfos: {
agoric: {
bech32Prefix: 'agoric',
chainId: 'agoric-3',
icqEnabled: false,
stakingTokens: [
{
denom: 'ubld',
},
],
},
},
connectionInfos: {},
denom: {},
lookupChainInfo_kindHandle: 'Alleged: kind',
lookupChainsAndConnection_kindHandle: 'Alleged: kind',
lookupConnectionInfo_kindHandle: 'Alleged: kind',
},
contract: {
Advancer: 'Alleged: Fast USDC Advancer advancer',
'Fast USDC Advancer_kindHandle': 'Alleged: kind',
'Fast USDC Creator_kindHandle': 'Alleged: kind',
'Fast USDC Creator_singleton': 'Alleged: Fast USDC Creator',
Expand All @@ -49,6 +67,11 @@ Generated by [AVA](https://avajs.dev).
'Fast USDC Settler_kindHandle': 'Alleged: kind',
'Fast USDC Status Manager_kindHandle': 'Alleged: kind',
'Fast USDC Status Manager_singleton': 'Alleged: Fast USDC Status Manager',
'Feed Kit': {
creator: Object @Alleged: Fast USDC Feed creator {},
operatorPowers: Object @Alleged: Fast USDC Feed operatorPowers {},
public: Object @Alleged: Fast USDC Feed public {},
},
Kinds: {
'Transaction Feed_kindHandle': 'Alleged: kind',
},
Expand All @@ -62,12 +85,17 @@ Generated by [AVA](https://avajs.dev).
'Liquidity Pool_kindHandle': 'Alleged: kind',
'Operator Kit_kindHandle': 'Alleged: kind',
PendingTxs: {},
'Pool Local Orch Account': 'Vow',
SeenTxs: [],
mint: {
PoolShare: 'Alleged: zcfMint',
},
operators: {},
orchestration: {},
orchestration: {
makeLocalAccount: {
asyncFlow_kindHandle: 'Alleged: kind',
},
},
pending: {},
vstorage: {
'Durable Publish Kit_kindHandle': 'Alleged: kind',
Expand All @@ -80,7 +108,16 @@ Generated by [AVA](https://avajs.dev).
LocalChainFacade_kindHandle: 'Alleged: kind',
Orchestrator_kindHandle: 'Alleged: kind',
RemoteChainFacade_kindHandle: 'Alleged: kind',
chainName: {},
chainName: {
agoric: {
pending: true,
vow: Object @Vow {
payload: {
vowV0: Object @Alleged: VowInternalsKit vowV0 {},
},
},
},
},
ibcTools: {
IBCTransferSenderKit_kindHandle: 'Alleged: kind',
ibcResultWatcher_kindHandle: 'Alleged: kind',
Expand Down
Binary file modified packages/fast-usdc/test/snapshots/fast-usdc.contract.test.ts.snap
Binary file not shown.

0 comments on commit b0ec75a

Please sign in to comment.