Skip to content

Commit c40b865

Browse files
committed
chore: bring back address hooks
Refs: #8863
1 parent 9d7f42b commit c40b865

File tree

3 files changed

+84
-18
lines changed

3 files changed

+84
-18
lines changed

multichain-testing/test/xcs-swap-anything/swap-anything.test.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,7 @@ test.serial(
428428
},
429429
);
430430

431-
/**
432-
* UNTIL https://github.com/Agoric/BytePitchPartnerEng/issues/51, we are skipping this
433-
* until the ticket above is done
434-
*/
435-
test.skip('address hook - BLD for OSMO, receiver on Agoric', async t => {
431+
test.serial('address hook - BLD for OSMO, receiver on Agoric', async t => {
436432
const { wallets, vstorageClient, retryUntilCondition, useChain } = t.context;
437433
const { getRestEndpoint, chain: cosmosChain } = useChain('cosmoshub');
438434

@@ -507,7 +503,7 @@ test.skip('address hook - BLD for OSMO, receiver on Agoric', async t => {
507503
const currentBalanceAmount = BigInt(balances[0]?.amount || 0);
508504
return currentBalanceAmount > balancesBeforeAmount;
509505
},
510-
'Deposit reflected in localOrchAccount balance',
506+
'Osmosis swap output reflected on Agoric receiver balance',
511507
);
512508
t.log(agoricReceiverBalances);
513509

packages/orchestration/src/examples/swap-anything.contract.js

+77-4
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ import { InvitationShape } from '@agoric/zoe/src/typeGuards.js';
22
import { makeTracer } from '@agoric/internal';
33
import { E } from '@endo/far';
44
import { M } from '@endo/patterns';
5+
import { decodeAddressHook } from '@agoric/cosmic-proto/address-hooks.js';
56
import { prepareChainHubAdmin } from '../exos/chain-hub-admin.js';
67
import { withOrchestration } from '../utils/start-helper.js';
78
import * as sharedFlows from './shared.flows.js';
8-
import { swapIt } from './swap-anything.flows.js';
9+
import { swapAnythingViaHook, swapIt } from './swap-anything.flows.js';
910
import { AnyNatAmountShape } from '../typeGuards.js';
1011
import { registerChainsAndAssets } from '../utils/chain-hub-helper.js';
1112

1213
const trace = makeTracer('SwapAnything.Contract');
13-
14+
const interfaceTODO = undefined;
1415
/**
1516
* @import {Remote, Vow} from '@agoric/vow';
1617
* @import {Zone} from '@agoric/zone';
1718
* @import {OrchestrationPowers, OrchestrationTools} from '../utils/start-helper.js';
18-
* @import {CosmosChainInfo, Denom, DenomDetail} from '@agoric/orchestration';
19+
* @import {CosmosChainInfo, Denom, DenomDetail, OrchestrationAccount} from '@agoric/orchestration';
1920
*/
2021

2122
export const SingleNatAmountRecord = M.and(
@@ -48,6 +49,11 @@ export const contract = async (
4849
) => {
4950
const creatorFacet = prepareChainHubAdmin(zone, chainHub);
5051

52+
/**
53+
* @type {OrchestrationAccount<{ chainId: 'agoric' }>} ;
54+
*/
55+
let sharedLocalAccount;
56+
5157
// UNTIL https://github.com/Agoric/agoric-sdk/issues/9066
5258
const logNode = E(privateArgs.storageNode).makeChildNode('log');
5359
/** @type {(msg: string) => Vow<void>} */
@@ -74,7 +80,74 @@ export const contract = async (
7480
swapIt,
7581
);
7682

77-
void vowTools.when(sharedLocalAccountP, async sharedLocalAccount => {
83+
const swapAnythingAddressHook = orchestrate(
84+
'swapAnythingViaHook',
85+
{
86+
chainHub,
87+
sharedLocalAccountP,
88+
log,
89+
},
90+
swapAnythingViaHook,
91+
);
92+
93+
const tap = zone.makeOnce('tapPosition', _key => {
94+
console.log('making tap');
95+
return zone.exo('tap', interfaceTODO, {
96+
/**
97+
* @param {import('@agoric/vats').VTransferIBCEvent} event
98+
*/
99+
async receiveUpcall(event) {
100+
await null;
101+
trace('receiveUpcall', event);
102+
103+
if (event.event !== 'writeAcknowledgement') return;
104+
trace('Moving on...');
105+
106+
const {
107+
amount,
108+
extra: { receiver: origReceiver },
109+
} = await vowTools.when(
110+
E(sharedLocalAccount).parseInboundTransfer(event.packet),
111+
);
112+
113+
const { baseAddress, query } = decodeAddressHook(origReceiver);
114+
115+
/**
116+
* @type {{
117+
* destAddr: string;
118+
* receiverAddr: string;
119+
* outDenom: string;
120+
* }}
121+
*/
122+
// @ts-expect-error
123+
const { destAddr, receiverAddr, outDenom } = query;
124+
125+
trace({
126+
baseAddress,
127+
destAddr,
128+
receiverAddr,
129+
outDenom,
130+
});
131+
132+
if (!receiverAddr || !destAddr || !outDenom) return;
133+
// Invoke the flow to perform swap and end up at the final destination.
134+
return swapAnythingAddressHook(amount, {
135+
destAddr,
136+
receiverAddr,
137+
outDenom, // swapOutDenom
138+
onFailedDelivery: 'do_nothing',
139+
slippage: {
140+
slippagePercentage: '20',
141+
windowSeconds: 10,
142+
},
143+
});
144+
},
145+
});
146+
});
147+
148+
void vowTools.when(sharedLocalAccountP, async lca => {
149+
sharedLocalAccount = lca;
150+
await sharedLocalAccount.monitorTransfers(tap);
78151
const encoded = await E(privateArgs.marshaller).toCapData({
79152
sharedLocalAccount: sharedLocalAccount.getAddress(),
80153
});

packages/orchestration/src/examples/swap-anything.flows.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const { entries } = Object;
2222
* @import {Vow} from '@agoric/vow';
2323
* @import {LocalOrchestrationAccountKit} from '../exos/local-orchestration-account.js';
2424
* @import {ZoeTools} from '../utils/zoe-tools.js';
25-
* @import {Orchestrator, OrchestrationFlow, ChainHub, CosmosChainInfo} from '../types.js';
25+
* @import {Orchestrator, OrchestrationFlow, ChainHub, CosmosChainInfo, DenomAmount} from '../types.js';
2626
*/
2727

2828
const denomForBrand = async (orch, brand) => {
@@ -166,21 +166,18 @@ export const swapIt = async (
166166
harden(swapIt);
167167

168168
/**
169-
* UNTIL https://github.com/Agoric/BytePitchPartnerEng/issues/51 is done, we
170-
* can't demonstrate this
171-
*
172169
* @satisfies {OrchestrationFlow}
173170
* @param {Orchestrator} _orch
174171
* @param {object} ctx
175172
* @param {GuestInterface<ChainHub>} ctx.chainHub
176173
* @param {Promise<GuestInterface<LocalOrchestrationAccountKit['holder']>>} ctx.sharedLocalAccountP
177-
* @param {{ denom: string; amount: string }} transferInfo
174+
* @param {DenomAmount} transferInfo
178175
* @param {SwapInfo} memoArgs
179176
*/
180177
export const swapAnythingViaHook = async (
181178
_orch,
182179
{ chainHub, sharedLocalAccountP },
183-
{ denom, amount },
180+
{ denom, value },
184181
memoArgs,
185182
) => {
186183
mustMatch(
@@ -198,7 +195,7 @@ export const swapAnythingViaHook = async (
198195
);
199196

200197
const { receiverAddr, destAddr } = memoArgs;
201-
trace(`sending {${amount}} from osmosis to ${receiverAddr}`);
198+
trace(`sending {${value}} from osmosis to ${receiverAddr}`);
202199

203200
/**
204201
* @type {any} XXX methods returning vows
@@ -222,7 +219,7 @@ export const swapAnythingViaHook = async (
222219
encoding: 'bech32',
223220
chainId: /** @type {CosmosChainInfo} */ (osmosisChainInfo).chainId,
224221
},
225-
{ denom, value: BigInt(amount) },
222+
{ denom, value },
226223
{ memo },
227224
);
228225

0 commit comments

Comments
 (0)