Skip to content

Commit

Permalink
chore: advancer validates settlementAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpatrickdev committed Dec 18, 2024
1 parent 401aa98 commit d52f76d
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 50 deletions.
9 changes: 8 additions & 1 deletion packages/boot/test/fast-usdc/fast-usdc.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { test as anyTest } from '@agoric/zoe/tools/prepare-test-env-ava.js';

import type { TestFn } from 'ava';
import { encodeAddressHook } from '@agoric/cosmic-proto/address-hooks.js';
import { configurations } from '@agoric/fast-usdc/src/utils/deploy-config.js';
import { MockCctpTxEvidences } from '@agoric/fast-usdc/test/fixtures.js';
import { documentStorageSchema } from '@agoric/governance/tools/storageDoc.js';
Expand Down Expand Up @@ -233,7 +234,13 @@ test.serial('makes usdc advance', async t => {
});
await eventLoopIteration();

const evidence = MockCctpTxEvidences.AGORIC_PLUS_OSMO();
const accountsData = storage.data.get('published.fastUsdc');
const { settlementAccount } = JSON.parse(JSON.parse(accountsData!).values[0]);
const evidence = MockCctpTxEvidences.AGORIC_PLUS_OSMO(
encodeAddressHook(settlementAccount, {
EUD: 'osmo183dejcnmkka5dzcu9xw6mywq0p2m5peks28men',
}),
);

harness?.useRunPolicy(true);
await Promise.all(
Expand Down
22 changes: 19 additions & 3 deletions packages/fast-usdc/src/exos/advancer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { pickFacet } from '@agoric/vat-data';
import { VowShape } from '@agoric/vow';
import { E } from '@endo/far';
import { M, mustMatch } from '@endo/patterns';
import { Fail, q } from '@endo/errors';
import {
CctpTxEvidenceShape,
AddressHookShape,
Expand Down Expand Up @@ -56,6 +57,7 @@ const AdvancerKitI = harden({
advancer: M.interface('AdvancerI', {
handleTransactionEvent: M.callWhen(CctpTxEvidenceShape).returns(),
setIntermediateRecipient: M.call(ChainAddressShape).returns(),
setSettlementAddress: M.call(ChainAddressShape).returns(),
}),
depositHandler: M.interface('DepositHandlerI', {
onFulfilled: M.call(M.undefined(), AdvancerVowCtxShape).returns(VowShape),
Expand Down Expand Up @@ -117,13 +119,15 @@ export const prepareAdvancerKit = (
* borrowerFacet: LiquidityPoolKit['borrower'];
* poolAccount: HostInterface<OrchestrationAccount<{chainId: 'agoric'}>>;
* intermediateRecipient?: ChainAddress;
* settlementAddress?: ChainAddress;
* }} config
*/
config =>
harden({
...config,
// make sure the state record has this property, perhaps with an undefined value
// make sure the state record has these properties, perhaps with an undefined value
intermediateRecipient: config.intermediateRecipient,
settlementAddress: config.settlementAddress,
}),
{
advancer: {
Expand All @@ -145,10 +149,17 @@ export const prepareAdvancerKit = (
return;
}

const { borrowerFacet, poolAccount } = this.state;
const { borrowerFacet, poolAccount, settlementAddress } =
this.state;
const { recipientAddress } = evidence.aux;
const decoded = decodeAddressHook(recipientAddress);
mustMatch(decoded, AddressHookShape);
if (!settlementAddress?.value) {
throw Fail`⚠️ No 'settlementAddress'. must call 'publishAddresses' first.`;
}
if (decoded.baseAddress !== settlementAddress.value) {
throw Fail`⚠️ baseAddress of address hook ${q(decoded.baseAddress)} does not match the expected address ${q(settlementAddress.value)}`;
}
const { EUD } = /** @type {AddressHook['query']} */ (decoded.query);
log(`decoded EUD: ${EUD}`);
// throws if the bech32 prefix is not found
Expand All @@ -172,10 +183,10 @@ export const prepareAdvancerKit = (
harden({ USDC: advanceAmount }),
);
void watch(depositV, this.facets.depositHandler, {
fullAmount,
advanceAmount,
destination,
forwardingAddress: evidence.tx.forwardingAddress,
fullAmount,
tmpSeat,
txHash: evidence.txHash,
});
Expand All @@ -188,6 +199,10 @@ export const prepareAdvancerKit = (
setIntermediateRecipient(intermediateRecipient) {
this.state.intermediateRecipient = intermediateRecipient;
},
/** @param {ChainAddress} settlementAddress */
setSettlementAddress(settlementAddress) {
this.state.settlementAddress = settlementAddress;
},
},
depositHandler: {
/**
Expand Down Expand Up @@ -271,6 +286,7 @@ export const prepareAdvancerKit = (
borrowerFacet: M.remotable(),
poolAccount: M.remotable(),
intermediateRecipient: M.opt(ChainAddressShape),
settlementAddress: M.opt(ChainAddressShape),
}),
},
);
Expand Down
1 change: 1 addition & 0 deletions packages/fast-usdc/src/fast-usdc.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export const contract = async (zcf, privateArgs, zone, tools) => {
E(settlementAccount).getAddress(),
]),
);
advancer.setSettlementAddress(settlementAccountAddress);
const addresses = harden({
poolAccount: poolAccountAddress.value,
settlementAccount: settlementAccountAddress.value,
Expand Down
7 changes: 3 additions & 4 deletions packages/fast-usdc/test/cli/transfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
makeFetchMock,
makeMockSigner,
} from '../../testing/mocks.js';
import { settlementAddress } from '../fixtures.js';

test('Errors if config missing', async t => {
const path = 'config/dir/.fast-usdc/config.json';
Expand Down Expand Up @@ -73,8 +74,7 @@ test('Transfer registers the noble forwarding account if it does not exist', asy
};
const out = mockOut();
const file = mockFile(path, JSON.stringify(config));
const agoricSettlementAccount =
'agoric16kv2g7snfc4q24vg3pjdlnnqgngtjpwtetd2h689nz09lcklvh5s8u37ek';
const agoricSettlementAccount = settlementAddress.value;
const settlementAccountVstoragePath = 'published.fastUsdc.settlementAccount';
const vstorageMock = makeVstorageMock({
[settlementAccountVstoragePath]: agoricSettlementAccount,
Expand Down Expand Up @@ -150,8 +150,7 @@ test('Transfer signs and broadcasts the depositForBurn message on Ethereum', asy
};
const out = mockOut();
const file = mockFile(path, JSON.stringify(config));
const agoricSettlementAccount =
'agoric16kv2g7snfc4q24vg3pjdlnnqgngtjpwtetd2h689nz09lcklvh5s8u37ek';
const agoricSettlementAccount = settlementAddress.value;
const settlementAccountVstoragePath = 'published.fastUsdc.settlementAccount';
const vstorageMock = makeVstorageMock({
[settlementAccountVstoragePath]: agoricSettlementAccount,
Expand Down
75 changes: 70 additions & 5 deletions packages/fast-usdc/test/exos/advancer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import type { SettlerKit } from '../../src/exos/settler.js';
import { prepareStatusManager } from '../../src/exos/status-manager.js';
import type { LiquidityPoolKit } from '../../src/types.js';
import { makeFeeTools } from '../../src/utils/fees.js';
import { MockCctpTxEvidences, intermediateRecipient } from '../fixtures.js';
import {
MockCctpTxEvidences,
settlementAddress,
intermediateRecipient,
} from '../fixtures.js';
import {
makeTestFeeConfig,
makeTestLogger,
Expand Down Expand Up @@ -127,6 +131,7 @@ const createTestExtensions = (t, common: CommonSetup) => {
notifyFacet: mockNotifyF,
poolAccount: mockAccounts.mockPoolAccount.account,
intermediateRecipient,
settlementAddress,
});

return {
Expand All @@ -141,6 +146,7 @@ const createTestExtensions = (t, common: CommonSetup) => {
},
mocks: {
...mockAccounts,
mockBorrowerF,
mockNotifyF,
resolveLocalTransferV,
rejectLocalTransfeferV,
Expand Down Expand Up @@ -260,6 +266,7 @@ test('updates status to OBSERVED on insufficient pool funds', async t => {
notifyFacet: mockNotifyF,
poolAccount: mockPoolAccount.account,
intermediateRecipient,
settlementAddress,
});

const evidence = MockCctpTxEvidences.AGORIC_PLUS_DYDX();
Expand Down Expand Up @@ -393,10 +400,10 @@ test('updates status to OBSERVED if pre-condition checks fail', async t => {

await advancer.handleTransactionEvent({
...MockCctpTxEvidences.AGORIC_NO_PARAMS(
encodeAddressHook(
'agoric16kv2g7snfc4q24vg3pjdlnnqgngtjpwtetd2h689nz09lcklvh5s8u37ek',
{ EUD: 'osmo1234', extra: 'value' },
),
encodeAddressHook(settlementAddress.value, {
EUD: 'osmo1234',
extra: 'value',
}),
),
txHash:
'0xc81bc6105b60a234c7c50ac17816ebcd5561d366df8bf3be59ff387552761799',
Expand Down Expand Up @@ -550,6 +557,7 @@ test('alerts if `returnToPool` fallback fails', async t => {
notifyFacet: mockNotifyF,
poolAccount: mockPoolAccount.account,
intermediateRecipient,
settlementAddress,
});

const mockEvidence = MockCctpTxEvidences.AGORIC_PLUS_OSMO();
Expand Down Expand Up @@ -588,3 +596,60 @@ test('alerts if `returnToPool` fallback fails', async t => {
'Advancing tx is recorded as AdvanceFailed',
);
});

test('rejects advances to unknown settlementAccount', async t => {
const {
extensions: {
services: { advancer },
helpers: { inspectLogs },
},
} = t.context;

const invalidSettlementAcct =
'agoric1ax7hmw49tmqrdld7emc5xw3wf43a49rtkacr9d5nfpqa0y7k6n0sl8v94h';
t.not(settlementAddress.value, invalidSettlementAcct);
const mockEvidence = MockCctpTxEvidences.AGORIC_PLUS_OSMO(
encodeAddressHook(invalidSettlementAcct, {
EUD: 'osmo183dejcnmkka5dzcu9xw6mywq0p2m5peks28men',
}),
);

void advancer.handleTransactionEvent(mockEvidence);
await eventLoopIteration();
t.deepEqual(inspectLogs(), [
[
'Advancer error:',
Error(
'⚠️ baseAddress of address hook "agoric1ax7hmw49tmqrdld7emc5xw3wf43a49rtkacr9d5nfpqa0y7k6n0sl8v94h" does not match the expected address "agoric16kv2g7snfc4q24vg3pjdlnnqgngtjpwtetd2h689nz09lcklvh5s8u37ek"',
),
],
]);
});

test('does not advance without settlementAddress set', async t => {
const {
extensions: {
services: { makeAdvancer },
helpers: { inspectLogs },
mocks: { mockPoolAccount, mockNotifyF, mockBorrowerF },
},
} = t.context;

// make a new advancer without setting `settlementAddress`
const advancer = makeAdvancer({
borrowerFacet: mockBorrowerF,
notifyFacet: mockNotifyF,
poolAccount: mockPoolAccount.account,
intermediateRecipient,
});
const mockEvidence = MockCctpTxEvidences.AGORIC_PLUS_OSMO();

void advancer.handleTransactionEvent(mockEvidence);
await eventLoopIteration();
t.deepEqual(inspectLogs(), [
[
'Advancer error:',
Error("⚠️ No 'settlementAddress'. must call 'publishAddresses' first."),
],
]);
});
Loading

0 comments on commit d52f76d

Please sign in to comment.