Skip to content

Commit

Permalink
feat: integrate Advancer with contract
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpatrickdev committed Nov 19, 2024
1 parent 3900f4e commit b4a6de7
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 28 deletions.
7 changes: 3 additions & 4 deletions packages/fast-usdc/src/exos/advancer.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const prepareAdvancerKit = (
/**
* @param {{
* borrowerFacet: BorrowerFacet;
* poolAccount: ERef<HostInterface<OrchestrationAccount<{chainId: 'agoric'}>>>;
* poolAccount: HostInterface<OrchestrationAccount<{chainId: 'agoric'}>>;
* }} config
*/
config => harden(config),
Expand Down Expand Up @@ -133,7 +133,7 @@ export const prepareAdvancerKit = (
const advanceAmount = feeTools.calculateAdvance(requestedAmount);

// TODO: consider skipping and using `borrow()`s internal balance check
const poolBalance = borrowerFacet.lookupBalance();
const poolBalance = borrowerFacet.getBalance();
if (!isGTE(poolBalance, requestedAmount)) {
log(
`Insufficient pool funds`,
Expand Down Expand Up @@ -191,7 +191,6 @@ export const prepareAdvancerKit = (
* @param {{ amount: Amount<'nat'>; destination: ChainAddress; tmpSeat: ZCFSeat }} ctx
*/
onFulfilled(result, { amount, destination }) {
// TODO do we need to ensure this isn't Vow for an LOA?
const { poolAccount } = this.state;
const transferV = E(poolAccount).transfer(destination, {
denom: usdc.denom,
Expand Down Expand Up @@ -237,7 +236,7 @@ export const prepareAdvancerKit = (
{
stateShape: harden({
borrowerFacet: M.remotable(),
poolAccount: M.or(VowShape, M.remotable()),
poolAccount: M.remotable(),
}),
},
);
Expand Down
56 changes: 38 additions & 18 deletions packages/fast-usdc/src/fast-usdc.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,27 @@ import {
} from '@agoric/orchestration';
import { provideSingleton } from '@agoric/zoe/src/contractSupport/durability.js';
import { prepareRecorderKitMakers } from '@agoric/zoe/src/contractSupport/recorder.js';
import { makeZoeTools } from '@agoric/orchestration/src/utils/zoe-tools.js';
import { depositToSeat } from '@agoric/zoe/src/contractSupport/zoeHelpers.js';
import { E } from '@endo/far';
import { M, objectMap } from '@endo/patterns';
import { depositToSeat } from '@agoric/zoe/src/contractSupport/zoeHelpers.js';
import { makeZoeTools } from '@agoric/orchestration/src/utils/zoe-tools.js';
import { prepareAdvancer } from './exos/advancer.js';
import { prepareLiquidityPoolKit } from './exos/liquidity-pool.js';
import { prepareSettler } from './exos/settler.js';
import { prepareStatusManager } from './exos/status-manager.js';
import { prepareTransactionFeedKit } from './exos/transaction-feed.js';
import { defineInertInvitation } from './utils/zoe.js';
import { FastUSDCTermsShape, FeeConfigShape } from './type-guards.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 @@ -74,7 +78,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 zoeTools = makeZoeTools(zcf, vowTools);
const makeAdvancer = prepareAdvancer(zone, {
chainHub,
Expand All @@ -91,21 +95,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,
Expand All @@ -121,6 +110,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);
},
/**
Expand Down Expand Up @@ -155,6 +145,7 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
return tmpAssetManagerSeat.getCurrentAllocation();
},
});
const { makeLocalAccount } = orchestrateAll(flows, {});

const publicFacet = zone.exo('Fast USDC Public', undefined, {
// XXX to be removed before production
Expand All @@ -168,6 +159,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 @@ -211,6 +203,34 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
makeLiquidityPoolKit(shareMint, privateArgs.storageNode),
);

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

const advancer = zone.makeOnce('Advancer', () =>
makeAdvancer({
borrowerFacet: poolKit.borrower,
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}
* @param {Orchestrator} orch
* @returns {Promise<OrchestrationAccount<{ chainId: 'agoric' }>>}
*/
export const makeLocalAccount = async orch => {
const agoricChain = await orch.getChain('agoric');
return agoricChain.makeAccount();
};
harden(makeLocalAccount);
4 changes: 2 additions & 2 deletions packages/fast-usdc/test/exos/advancer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const createTestExtensions = (t, common: CommonSetup) => {

const advancer = makeAdvancer({
borrowerFacet: Far('LiquidityPool Borrow Facet', {
lookupBalance: () => mockPoolBalance,
getBalance: () => mockPoolBalance,
borrow: (seat: ZCFSeat, amounts: { USDC: NatAmount }) => {
t.log('borrowUnderlying called with', amounts);
return borrowUnderlyingPK.promise;
Expand Down Expand Up @@ -239,7 +239,7 @@ test('updates status to OBSERVED if balance query fails', async t => {
const advancer = makeAdvancer({
// @ts-expect-error mock
borrowerFacet: Far('LiquidityPool Borrow Facet', {
lookupBalance: () => {
getBalance: () => {
throw new Error('lookupBalance failed');
},
}),
Expand Down
37 changes: 33 additions & 4 deletions packages/fast-usdc/test/snapshots/fast-usdc.contract.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,30 @@ Generated by [AVA](https://avajs.dev).
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 +63,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 @@ -63,12 +82,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 @@ -81,7 +105,12 @@ Generated by [AVA](https://avajs.dev).
LocalChainFacade_kindHandle: 'Alleged: kind',
Orchestrator_kindHandle: 'Alleged: kind',
RemoteChainFacade_kindHandle: 'Alleged: kind',
chainName: {},
chainName: {
agoric: {
pending: false,
value: Object @Alleged: LocalChainFacade public {},
},
},
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 b4a6de7

Please sign in to comment.