Skip to content

Commit

Permalink
chore(fast-usdc): limited operation before connecting to noble
Browse files Browse the repository at this point in the history
Don't require connecting to noble until we want to advance
to chains other than agoric.
  • Loading branch information
dckc committed Dec 7, 2024
1 parent a319e01 commit ac8cf06
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 37 deletions.
40 changes: 31 additions & 9 deletions packages/fast-usdc/src/exos/advancer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { makeFeeTools } from '../utils/fees.js';
* @import {HostInterface} from '@agoric/async-flow';
* @import {TypedPattern} from '@agoric/internal'
* @import {NatAmount} from '@agoric/ertp';
* @import {ChainAddress, ChainHub, Denom, OrchestrationAccount} from '@agoric/orchestration';
* @import {ChainAddress, ChainHub, Denom, OrchestrationAccount, IBCMsgTransferOptions} from '@agoric/orchestration';
* @import {ZoeTools} from '@agoric/orchestration/src/utils/zoe-tools.js';
* @import {VowTools} from '@agoric/vow';
* @import {Zone} from '@agoric/zone';
Expand Down Expand Up @@ -54,6 +54,9 @@ const AdvancerVowCtxShape = M.splitRecord(

/** type guards internal to the AdvancerKit */
const AdvancerKitI = harden({
admin: M.interface('AdminI', {
setIntermediateRecipient: M.call(ChainAddressShape).returns(),
}),
advancer: M.interface('AdvancerI', {
handleTransactionEvent: M.callWhen(CctpTxEvidenceShape).returns(),
}),
Expand All @@ -70,6 +73,19 @@ const AdvancerKitI = harden({
}),
});

/**
* @param {ChainAddress|undefined} intermediateRecipient
* @returns {IBCMsgTransferOptions | undefined}
*/
const ibcOpts = intermediateRecipient =>
intermediateRecipient
? {
forwardOpts: {
intermediateRecipient,
},
}
: undefined;

/**
* @typedef {{
* fullAmount: NatAmount;
Expand Down Expand Up @@ -116,11 +132,21 @@ export const prepareAdvancerKit = (
* notifyFacet: import('./settler.js').SettlerKit['notify'];
* borrowerFacet: LiquidityPoolKit['borrower'];
* poolAccount: HostInterface<OrchestrationAccount<{chainId: 'agoric'}>>;
* intermediateRecipient: ChainAddress;
* intermediateRecipient?: ChainAddress;
* }} config
*/
config => harden(config),
config =>
harden({
...config,
intermediateRecipient: config.intermediateRecipient,
}),
{
admin: {
/** @param {ChainAddress} intermediateRecipient */
setIntermediateRecipient(intermediateRecipient) {
this.state.intermediateRecipient = intermediateRecipient;
},
},
advancer: {
/**
* Must perform a status update for every observed transaction.
Expand Down Expand Up @@ -196,11 +222,7 @@ export const prepareAdvancerKit = (
denom: usdc.denom,
value: advanceAmount.value,
},
{
forwardOpts: {
intermediateRecipient,
},
},
ibcOpts(intermediateRecipient),
);
return watch(transferV, this.facets.transferHandler, {
destination,
Expand Down Expand Up @@ -259,7 +281,7 @@ export const prepareAdvancerKit = (
notifyFacet: M.remotable(),
borrowerFacet: M.remotable(),
poolAccount: M.remotable(),
intermediateRecipient: ChainAddressShape,
intermediateRecipient: M.opt(ChainAddressShape),
}),
},
);
Expand Down
31 changes: 23 additions & 8 deletions packages/fast-usdc/src/exos/settler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { EvmHashShape } from '../type-guards.js';

/**
* @import {FungibleTokenPacketData} from '@agoric/cosmic-proto/ibc/applications/transfer/v2/packet.js';
* @import {Denom, OrchestrationAccount, ChainHub, ChainAddress} from '@agoric/orchestration';
* @import {Denom, OrchestrationAccount, ChainHub, ChainAddress, IBCMsgTransferOptions} from '@agoric/orchestration';
* @import {WithdrawToSeat} from '@agoric/orchestration/src/utils/zoe-tools'
* @import {IBCChannelID, VTransferIBCEvent} from '@agoric/vats';
* @import {Zone} from '@agoric/zone';
Expand All @@ -31,6 +31,19 @@ import { EvmHashShape } from '../type-guards.js';
const makeMintedEarlyKey = (addr, amount) =>
`pendingTx:${JSON.stringify([addr, String(amount)])}`;

/**
* @param {ChainAddress|undefined} intermediateRecipient
* @returns {IBCMsgTransferOptions | undefined}
*/
const ibcOpts = intermediateRecipient =>
intermediateRecipient
? {
forwardOpts: {
intermediateRecipient,
},
}
: undefined;

/**
* @param {Zone} zone
* @param {object} caps
Expand Down Expand Up @@ -62,6 +75,7 @@ export const prepareSettler = (
{
creator: M.interface('SettlerCreatorI', {
monitorMintingDeposits: M.callWhen().returns(M.any()),
setIntermediateRecipient: M.call(ChainAddressShape).returns(),
}),
tap: M.interface('SettlerTapI', {
receiveUpcall: M.call(M.record()).returns(M.promise()),
Expand Down Expand Up @@ -94,13 +108,14 @@ export const prepareSettler = (
* remoteDenom: Denom;
* repayer: LiquidityPoolKit['repayer'];
* settlementAccount: HostInterface<OrchestrationAccount<{ chainId: 'agoric' }>>
* intermediateRecipient: ChainAddress;
* intermediateRecipient?: ChainAddress;
* }} config
*/
config => {
log('config', config);
return {
...config,
intermediateRecipient: config.intermediateRecipient,
/** @type {HostInterface<TargetRegistration>|undefined} */
registration: undefined,
/** @type {SetStore<ReturnType<typeof makeMintedEarlyKey>>} */
Expand All @@ -117,6 +132,10 @@ export const prepareSettler = (
assert.typeof(registration, 'object');
this.state.registration = registration;
},
/** @param {ChainAddress} intermediateRecipient */
setIntermediateRecipient(intermediateRecipient) {
this.state.intermediateRecipient = intermediateRecipient;
},
},
tap: {
/** @param {VTransferIBCEvent} event */
Expand Down Expand Up @@ -265,11 +284,7 @@ export const prepareSettler = (
const txfrV = E(settlementAccount).transfer(
dest,
AmountMath.make(USDC, fullValue),
{
forwardOpts: {
intermediateRecipient,
},
},
ibcOpts(intermediateRecipient),
);
void vowTools.watch(txfrV, this.facets.transferHandler, {
txHash,
Expand Down Expand Up @@ -312,7 +327,7 @@ export const prepareSettler = (
sourceChannel: M.string(),
remoteDenom: M.string(),
mintedEarly: M.remotable('mintedEarly'),
intermediateRecipient: ChainAddressShape,
intermediateRecipient: M.opt(ChainAddressShape),
}),
},
);
Expand Down
41 changes: 24 additions & 17 deletions packages/fast-usdc/src/fast-usdc.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { provideSingleton } from '@agoric/zoe/src/contractSupport/durability.js'
import { prepareRecorderKitMakers } from '@agoric/zoe/src/contractSupport/recorder.js';
import { E } from '@endo/far';
import { M } from '@endo/patterns';
import { prepareAdvancer } from './exos/advancer.js';
import { prepareAdvancer, prepareAdvancerKit } from './exos/advancer.js';

Check failure on line 17 in packages/fast-usdc/src/fast-usdc.contract.js

View workflow job for this annotation

GitHub Actions / lint-primary

'prepareAdvancer' is defined but never used. Allowed unused vars must match /^_/u
import { prepareLiquidityPoolKit } from './exos/liquidity-pool.js';
import { prepareSettler } from './exos/settler.js';
import { prepareStatusManager } from './exos/status-manager.js';
Expand Down Expand Up @@ -112,7 +112,7 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
});

const { localTransfer } = makeZoeTools(zcf, vowTools);
const makeAdvancer = prepareAdvancer(zone, {
const makeAdvancerKit = prepareAdvancerKit(zone, {
chainHub,
feeConfig,
localTransfer,
Expand All @@ -126,7 +126,7 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
});

const makeFeedKit = prepareTransactionFeedKit(zone, zcf);
assertAllDefined({ makeFeedKit, makeAdvancer, makeSettler, statusManager });
assertAllDefined({ makeFeedKit, makeSettler, statusManager });

const makeLiquidityPoolKit = prepareLiquidityPoolKit(
zone,
Expand All @@ -142,11 +142,27 @@ export const contract = async (zcf, privateArgs, zone, tools) => {

const { makeLocalAccount, makeNobleAccount } = orchestrateAll(flows, {});

const nobleAccountV = zone.makeOnce('NobleAccount', makeNobleAccount);

const creatorFacet = zone.exo('Fast USDC Creator', undefined, {
/** @type {(operatorId: string) => Promise<Invitation<OperatorKit>>} */
async makeOperatorInvitation(operatorId) {
return feedKit.creator.makeOperatorInvitation(operatorId);
},
async connectToNoble() {
return vowTools.when(nobleAccountV, nobleAccount => {
trace('nobleAccount', nobleAccount);
return vowTools.when(
E(nobleAccount).getAddress(),
intermediateRecipient => {
trace('intermediateRecipient', intermediateRecipient);
advancerAdmin.setIntermediateRecipient(intermediateRecipient);
settlerKit.creator.setIntermediateRecipient(intermediateRecipient);
return intermediateRecipient;
},
);
});
},
});

const publicFacet = zone.exo('Fast USDC Public', undefined, {
Expand Down Expand Up @@ -214,26 +230,19 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
privateArgs.assetInfo,
);
}
const nobleAccountV = zone.makeOnce('NobleAccount', () => makeNobleAccount());
const feedKit = zone.makeOnce('Feed Kit', () => makeFeedKit());

const poolAccountV = zone.makeOnce('PoolAccount', () => makeLocalAccount());
const settleAccountV = zone.makeOnce('SettleAccount', () =>
makeLocalAccount(),
);
// when() is OK here since this clearly resolves promptly.
/** @type {[HostInterface<OrchestrationAccount<{chainId: 'noble-1';}>>, HostInterface<OrchestrationAccount<{chainId: 'agoric-3';}>>, HostInterface<OrchestrationAccount<{chainId: 'agoric-3';}>>]} */
const [nobleAccount, poolAccount, settlementAccount] = await vowTools.when(
vowTools.all([nobleAccountV, poolAccountV, settleAccountV]),
/** @type {[HostInterface<OrchestrationAccount<{chainId: 'agoric-3';}>>, HostInterface<OrchestrationAccount<{chainId: 'agoric-3';}>>]} */
const [poolAccount, settlementAccount] = await vowTools.when(
vowTools.all([poolAccountV, settleAccountV]),
);
trace('settlementAccount', settlementAccount);
trace('poolAccount', poolAccount);
trace('nobleAccount', nobleAccount);

const intermediateRecipient = await vowTools.when(
E(nobleAccount).getAddress(),
);
trace('intermediateRecipient', intermediateRecipient);

const [_agoric, _noble, agToNoble] = await vowTools.when(
chainHub.getChainsAndConnection('agoric', 'noble'),
Expand All @@ -243,15 +252,13 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
sourceChannel: agToNoble.transferChannel.counterPartyChannelId,
remoteDenom: 'uusdc',
settlementAccount,
intermediateRecipient,
});

const advancer = zone.makeOnce('Advancer', () =>
makeAdvancer({
const { advancer, admin: advancerAdmin } = zone.makeOnce('AdvancerKit', () =>
makeAdvancerKit({
borrowerFacet: poolKit.borrower,
notifyFacet: settlerKit.notify,
poolAccount,
intermediateRecipient,
}),
);
// Connect evidence stream to advancer
Expand Down
5 changes: 5 additions & 0 deletions packages/fast-usdc/src/fast-usdc.start.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ export const startFastUSDC = async (

produceInstance.reset();
produceInstance.resolve(instance);

if ('uusdc' in assetInfo) {
const addr = await E(kit.creatorFacet).connectToNoble();
trace('noble intermediate recipient', addr);
}
trace('startFastUSDC done', instance);
};
harden(startFastUSDC);
Expand Down
1 change: 1 addition & 0 deletions packages/fast-usdc/test/fast-usdc.contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const startContract = async (
E(startKit.creatorFacet).makeOperatorInvitation(`operator-${opIx}`),
),
);
await E(startKit.creatorFacet).connectToNoble();

return {
...startKit,
Expand Down
15 changes: 12 additions & 3 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,9 +22,13 @@ 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: {
Expand Down Expand Up @@ -561,7 +565,12 @@ Generated by [AVA](https://avajs.dev).
lookupConnectionInfo_kindHandle: 'Alleged: kind',
},
contract: {
Advancer: 'Alleged: Fast USDC Advancer advancer',
AdvancerKit: {
admin: Object @Alleged: Fast USDC Advancer admin {},
advancer: Object @Alleged: Fast USDC Advancer advancer {},
depositHandler: Object @Alleged: Fast USDC Advancer depositHandler {},
transferHandler: Object @Alleged: Fast USDC Advancer transferHandler {},
},
'Fast USDC Advancer_kindHandle': 'Alleged: kind',
'Fast USDC Creator_kindHandle': 'Alleged: kind',
'Fast USDC Creator_singleton': 'Alleged: Fast USDC Creator',
Expand Down
Binary file modified packages/fast-usdc/test/snapshots/fast-usdc.contract.test.ts.snap
Binary file not shown.

0 comments on commit ac8cf06

Please sign in to comment.