@@ -2,20 +2,21 @@ import { InvitationShape } from '@agoric/zoe/src/typeGuards.js';
2
2
import { makeTracer } from '@agoric/internal' ;
3
3
import { E } from '@endo/far' ;
4
4
import { M } from '@endo/patterns' ;
5
+ import { decodeAddressHook } from '@agoric/cosmic-proto/address-hooks.js' ;
5
6
import { prepareChainHubAdmin } from '../exos/chain-hub-admin.js' ;
6
7
import { withOrchestration } from '../utils/start-helper.js' ;
7
8
import * as sharedFlows from './shared.flows.js' ;
8
- import { swapIt } from './swap-anything.flows.js' ;
9
+ import { swapAnythingViaHook , swapIt } from './swap-anything.flows.js' ;
9
10
import { AnyNatAmountShape } from '../typeGuards.js' ;
10
11
import { registerChainsAndAssets } from '../utils/chain-hub-helper.js' ;
11
12
12
13
const trace = makeTracer ( 'SwapAnything.Contract' ) ;
13
-
14
+ const interfaceTODO = undefined ;
14
15
/**
15
16
* @import {Remote, Vow} from '@agoric/vow';
16
17
* @import {Zone} from '@agoric/zone';
17
18
* @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';
19
20
*/
20
21
21
22
export const SingleNatAmountRecord = M . and (
@@ -48,6 +49,11 @@ export const contract = async (
48
49
) => {
49
50
const creatorFacet = prepareChainHubAdmin ( zone , chainHub ) ;
50
51
52
+ /**
53
+ * @type {OrchestrationAccount<{ chainId: 'agoric' }> } ;
54
+ */
55
+ let sharedLocalAccount ;
56
+
51
57
// UNTIL https://github.com/Agoric/agoric-sdk/issues/9066
52
58
const logNode = E ( privateArgs . storageNode ) . makeChildNode ( 'log' ) ;
53
59
/** @type {(msg: string) => Vow<void> } */
@@ -74,7 +80,74 @@ export const contract = async (
74
80
swapIt ,
75
81
) ;
76
82
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 ) ;
78
151
const encoded = await E ( privateArgs . marshaller ) . toCapData ( {
79
152
sharedLocalAccount : sharedLocalAccount . getAddress ( ) ,
80
153
} ) ;
0 commit comments