|
| 1 | +import { assertAllDefined } from '@agoric/internal'; |
| 2 | +import { ChainAddressShape } from '@agoric/orchestration'; |
| 3 | +import { VowShape } from '@agoric/vow'; |
| 4 | +import { makeError, q } from '@endo/errors'; |
| 5 | +import { E } from '@endo/far'; |
| 6 | +import { M } from '@endo/patterns'; |
| 7 | +import { CctpTxEvidenceShape } from '../typeGuards.js'; |
| 8 | +import { addressTools } from '../utils/address.js'; |
| 9 | + |
1 | 10 | /**
|
| 11 | + * @import {HostInterface} from '@agoric/async-flow'; |
| 12 | + * @import {ChainAddress, ChainHub, Denom, DenomAmount, OrchestrationAccount} from '@agoric/orchestration'; |
| 13 | + * @import {VowTools} from '@agoric/vow'; |
2 | 14 | * @import {Zone} from '@agoric/zone';
|
3 |
| - * @import {TransactionFeed} from './transaction-feed.js'; |
| 15 | + * @import {CctpTxEvidence, LogFn} from '../types.js'; |
4 | 16 | * @import {StatusManager} from './status-manager.js';
|
| 17 | + * @import {TransactionFeed} from './transaction-feed.js'; |
5 | 18 | */
|
6 | 19 |
|
7 |
| -import { assertAllDefined } from '@agoric/internal'; |
8 |
| - |
9 | 20 | /**
|
10 | 21 | * @param {Zone} zone
|
11 | 22 | * @param {object} caps
|
| 23 | + * @param {ChainHub} caps.chainHub |
12 | 24 | * @param {TransactionFeed} caps.feed
|
| 25 | + * @param {LogFn} caps.log |
13 | 26 | * @param {StatusManager} caps.statusManager
|
| 27 | + * @param {VowTools} caps.vowTools |
14 | 28 | */
|
15 |
| -export const prepareAdvancer = (zone, { feed, statusManager }) => { |
16 |
| - assertAllDefined({ feed, statusManager }); |
17 |
| - return zone.exo('Fast USDC Advancer', undefined, {}); |
| 29 | +export const prepareAdvancer = ( |
| 30 | + zone, |
| 31 | + { chainHub, feed, log, statusManager, vowTools: { watch } }, |
| 32 | +) => { |
| 33 | + assertAllDefined({ feed, statusManager, watch }); |
| 34 | + |
| 35 | + const transferHandler = zone.exo( |
| 36 | + 'Fast USDC Advance Transfer Handler', |
| 37 | + M.interface('TransferHandlerI', { |
| 38 | + // TODO confirm undefined, and not bigint (sequence) |
| 39 | + onFulfilled: M.call(M.undefined(), { |
| 40 | + amount: M.bigint(), |
| 41 | + destination: ChainAddressShape, |
| 42 | + }).returns(M.undefined()), |
| 43 | + onRejected: M.call(M.error(), { |
| 44 | + amount: M.bigint(), |
| 45 | + destination: ChainAddressShape, |
| 46 | + }).returns(M.undefined()), |
| 47 | + }), |
| 48 | + { |
| 49 | + /** |
| 50 | + * @param {undefined} result TODO confirm this is not a bigint (sequence) |
| 51 | + * @param {{ destination: ChainAddress; amount: bigint; }} ctx |
| 52 | + */ |
| 53 | + onFulfilled(result, { destination, amount }) { |
| 54 | + log( |
| 55 | + 'Advance transfer fulfilled', |
| 56 | + q({ amount, destination, result }).toString(), |
| 57 | + ); |
| 58 | + }, |
| 59 | + onRejected(error) { |
| 60 | + // XXX retry logic? |
| 61 | + // What do we do if we fail, should we keep a Status? |
| 62 | + log('Advance transfer rejected', q(error).toString()); |
| 63 | + }, |
| 64 | + }, |
| 65 | + ); |
| 66 | + |
| 67 | + return zone.exoClass( |
| 68 | + 'Fast USDC Advancer', |
| 69 | + M.interface('AdvancerI', { |
| 70 | + handleTransactionEvent: M.call(CctpTxEvidenceShape).returns(VowShape), |
| 71 | + }), |
| 72 | + /** |
| 73 | + * @param {{ |
| 74 | + * localDenom: Denom; |
| 75 | + * poolAccount: HostInterface<OrchestrationAccount<{ chainId: 'agoric' }>>; |
| 76 | + * }} config |
| 77 | + */ |
| 78 | + config => harden(config), |
| 79 | + { |
| 80 | + /** @param {CctpTxEvidence} evidence */ |
| 81 | + handleTransactionEvent(evidence) { |
| 82 | + // TODO EventFeed will perform input validation checks. |
| 83 | + const { recipientAddress } = evidence.aux; |
| 84 | + const { EUD } = addressTools.getQueryParams(recipientAddress).params; |
| 85 | + if (!EUD) { |
| 86 | + statusManager.observe(evidence); |
| 87 | + throw makeError( |
| 88 | + `recipientAddress does not contain EUD param: ${q(recipientAddress)}`, |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + // TODO #10391 this can throw, and should make a status update in the catch |
| 93 | + const destination = chainHub.makeChainAddress(EUD); |
| 94 | + |
| 95 | + /** @type {DenomAmount} */ |
| 96 | + const requestedAmount = harden({ |
| 97 | + denom: this.state.localDenom, |
| 98 | + value: BigInt(evidence.tx.amount), |
| 99 | + }); |
| 100 | + |
| 101 | + // TODO #10391 ensure there's enough funds in poolAccount |
| 102 | + |
| 103 | + const transferV = E(this.state.poolAccount).transfer( |
| 104 | + destination, |
| 105 | + requestedAmount, |
| 106 | + ); |
| 107 | + |
| 108 | + // mark as Advanced since `transferV` initiates the advance |
| 109 | + statusManager.advance(evidence); |
| 110 | + |
| 111 | + return watch(transferV, transferHandler, { |
| 112 | + destination, |
| 113 | + amount: requestedAmount.value, |
| 114 | + }); |
| 115 | + }, |
| 116 | + }, |
| 117 | + { |
| 118 | + stateShape: harden({ |
| 119 | + localDenom: M.string(), |
| 120 | + poolAccount: M.remotable(), |
| 121 | + }), |
| 122 | + }, |
| 123 | + ); |
18 | 124 | };
|
19 | 125 | harden(prepareAdvancer);
|
0 commit comments