Skip to content

Commit

Permalink
feat(liquidationVisibility): handle errors that might arise from othe…
Browse files Browse the repository at this point in the history
…r vats
  • Loading branch information
anilhelvaci committed Jan 31, 2024
1 parent 683f56d commit 732e1d7
Showing 1 changed file with 69 additions and 23 deletions.
92 changes: 69 additions & 23 deletions packages/inter-protocol/src/vaultFactory/vaultManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {
TopicsRecordShape,
} from '@agoric/zoe/src/contractSupport/index.js';
import { PriceQuoteShape, SeatShape } from '@agoric/zoe/src/typeGuards.js';
import { E } from '@endo/eventual-send';
import { E, Far } from '@endo/far';
import { TimestampShape } from '@agoric/time';
import { AuctionPFShape } from '../auction/auctioneer.js';
import {
Expand Down Expand Up @@ -746,20 +746,46 @@ export const prepareVaultManagerKit = (
collateralSold: plan.collateralSold,
collateralRemaining: plan.collatRemaining,
// @ts-expect-error
endTime: auctionSchedule.liveAuctionSchedule.endTime, // write if nor rejected
// eslint-disable-next-line @endo/no-optional-chaining
endTime: auctionSchedule?.liveAuctionSchedule.endTime,
};
return E(
liquidationRecorderKits.auctionResultRecorderKit.recorder,
).writeFinal(auctionResultState);
};

return harden({
return Far('Liquidation Visibility Writers', {
writePreAuction,
writePostAuction,
writeAuctionResults,
});
},

/**
* This method checks if liquidationVisibilityWriters is undefined or
* not in case of a rejected promise when creating the writers. If
* liquidationVisibilityWriters is undefined it silently notifies the
* console. Otherwise, it goes on with the writing.
*
* @param {LiquidationVisibilityWriters} liquidationVisibilityWriters
* @param {[string, object][]} writes
*/
async writeLiqVisibility(liquidationVisibilityWriters, writes) {
console.log('WRITES', writes);
if (!liquidationVisibilityWriters) {
trace(
'writeLiqVisibility',
`Error: liquidationVisibilityWriters is ${liquidationVisibilityWriters}`,
);
return;
}

for (const [methodName, params] of writes) {
trace('DEBUG', methodName, params);
void liquidationVisibilityWriters[methodName](params);
}
},

/**
* @param {TimestampRecord} timestamp
* @returns {Promise<LiquidationRecorderKits>}
Expand Down Expand Up @@ -1354,22 +1380,31 @@ export const prepareVaultManagerKit = (
),
);

const [{ userSeatPromise, deposited }, liquidationVisibilityWriters] =
await Promise.all([
// TODO: Explain what happens in here
await null;
const [
{ userSeatPromise, deposited },
liquidationVisibilityWriters,
auctionSchedule,
] = (
await Promise.allSettled([
makeDeposit,
helper.makeLiquidationVisibilityWriters(timestamp),
]);

void liquidationVisibilityWriters.writePreAuction(vaultData);
schedulesP,
])
)
.filter(result => result.status === 'fulfilled')
// @ts-expect-error
.map(result => result.value);

void helper.writeLiqVisibility(liquidationVisibilityWriters, [
['writePreAuction', vaultData],
]);

// This is expected to wait for the duration of the auction, which
// is controlled by the auction parameters startFrequency, clockStep,
// and the difference between startingRate and lowestRate.
const [auctionSchedule, proceeds] = await Promise.all([
schedulesP,
deposited,
userSeatPromise,
]);
const [proceeds] = await Promise.all([deposited, userSeatPromise]);

const { storedCollateralQuote } = collateralEphemera(
this.state.collateralBrand,
Expand Down Expand Up @@ -1397,16 +1432,27 @@ export const prepareVaultManagerKit = (
vaultsInPlan,
});

void liquidationVisibilityWriters.writeAuctionResults({
plan,
totalCollateral,
totalDebt,
auctionSchedule,
});
void liquidationVisibilityWriters.writePostAuction({
plan,
vaultsInPlan,
});
void helper.writeLiqVisibility(
liquidationVisibilityWriters,
harden([
[
'writeAuctionResults',
{
plan,
totalCollateral,
totalDebt,
auctionSchedule,
},
],
[
'writePostAuction',
{
plan,
vaultsInPlan,
},
],
]),
);
} catch (err) {
console.error('🚨 Error distributing proceeds:', err);
}
Expand Down

0 comments on commit 732e1d7

Please sign in to comment.