Skip to content

Commit

Permalink
refactor(fast-usdc): sender type, simulate.advance
Browse files Browse the repository at this point in the history
  • Loading branch information
dckc committed Nov 22, 2024
1 parent 1a4aca3 commit fb8c53b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
13 changes: 6 additions & 7 deletions packages/fast-usdc/src/exos/settler.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,15 @@ export const prepareSettler = (
// only interested in packets from the issuing chain
return;
}

// TODO: why is it safe to cast this without a runtime check?
const tx = /** @type {FungibleTokenPacketData} */ (
JSON.parse(atob(event.packet.data))
);

// given the sourceChannel check, we can be certain of this cast
const sender = /** @type {NobleAddress} */ (tx.sender);

if (tx.denom !== this.state.remoteDenom) {
// only interested in uusdc
return;
Expand All @@ -94,13 +99,7 @@ export const prepareSettler = (

const amountInt = BigInt(tx.amount); // TODO: what if this throws?

// TODO discern between SETTLED and OBSERVED; each has different fees/destinations
const hasPendingSettlement = statusManager.hasPendingSettlement(
// given the sourceChannel check, we can be certain of this cast
/** @type {NobleAddress} */ (tx.sender),
amountInt,
);
if (!hasPendingSettlement) {
if (!statusManager.hasPendingSettlement(sender, amountInt)) {
// TODO FAILURE PATH -> put money in recovery account or .transfer to receiver
// TODO should we have an ORPHANED TxStatus for this?
throw makeError(
Expand Down
42 changes: 28 additions & 14 deletions packages/fast-usdc/test/exos/settler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,31 @@ const makeTestContext = async t => {
remoteDenom: 'uusdc',
});

const simulateAdvance = (evidence?: CctpTxEvidence) => {
const cctpTxEvidence: CctpTxEvidence = {
...MockCctpTxEvidences.AGORIC_PLUS_OSMO(),
...evidence,
};
t.log('Mock CCTP Evidence:', cctpTxEvidence);
t.log('Pretend we initiated advance, mark as `ADVANCED`');
statusManager.advance(cctpTxEvidence);

return cctpTxEvidence;
};
const simulate = harden({
advance: (evidence?: CctpTxEvidence) => {
const cctpTxEvidence: CctpTxEvidence = {
...MockCctpTxEvidences.AGORIC_PLUS_OSMO(),
...evidence,
};
t.log('Mock CCTP Evidence:', cctpTxEvidence);
t.log('Pretend we initiated advance, mark as `ADVANCED`');
statusManager.advance(cctpTxEvidence);

return cctpTxEvidence;
},

observe: (evidence?: CctpTxEvidence) => {
const cctpTxEvidence: CctpTxEvidence = {
...MockCctpTxEvidences.AGORIC_PLUS_OSMO(),
...evidence,
};
t.log('Mock CCTP Evidence:', cctpTxEvidence);
t.log('Pretend we `OBSERVED`');
statusManager.observe(cctpTxEvidence);

return cctpTxEvidence;
},
});

const repayer = zone.exo('Repayer Mock', undefined, {
repay(fromSeat: ZCFSeat, amounts: AmountKeywordRecord) {
Expand All @@ -103,7 +117,7 @@ const makeTestContext = async t => {
makeSettler,
statusManager,
defaultSettlerParams,
simulateAdvance,
simulate,
repayer,
peekCalls: () => harden([...callLog]),
accounts: mockAccounts,
Expand All @@ -121,7 +135,7 @@ test('happy path: disburse to LPs; StatusManager removes tx', async t => {
statusManager,
defaultSettlerParams,
repayer,
simulateAdvance,
simulate,
accounts,
peekCalls,
} = t.context;
Expand All @@ -134,7 +148,7 @@ test('happy path: disburse to LPs; StatusManager removes tx', async t => {
...defaultSettlerParams,
});

const cctpTxEvidence = simulateAdvance();
const cctpTxEvidence = simulate.advance();
t.deepEqual(
statusManager.lookupPending(
cctpTxEvidence.tx.forwardingAddress,
Expand Down

0 comments on commit fb8c53b

Please sign in to comment.