Skip to content

Commit

Permalink
chore: only use try/catch when commit phase is not atomic
Browse files Browse the repository at this point in the history
- see e33f9e8#r1878631017
- note: try/catch/finally remains for `borrow()` and `deposit()` so we can exit seats
  • Loading branch information
0xpatrickdev committed Dec 12, 2024
1 parent 9a26cc3 commit 85ce625
Showing 1 changed file with 19 additions and 35 deletions.
54 changes: 19 additions & 35 deletions packages/fast-usdc/src/exos/liquidity-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,7 @@ export const prepareLiquidityPoolKit = (zone, zcf, USDC, tools) => {
);

// COMMIT POINT
try {
zcf.atomicRearrange(harden([[poolSeat, toSeat, amountKWR]]));
} catch (cause) {
const reason = Error('🚨 cannot commit borrow', { cause });
console.error(reason.message, cause);
zcf.shutdownWithFailure(reason);
}
zcf.atomicRearrange(harden([[poolSeat, toSeat, amountKWR]]));

Object.assign(this.state, post);
this.facets.external.publishPoolMetrics();
Expand Down Expand Up @@ -233,23 +227,17 @@ export const prepareLiquidityPoolKit = (zone, zcf, USDC, tools) => {
const { ContractFee, ...rest } = amounts;

// COMMIT POINT
try {
zcf.atomicRearrange(
harden([
[
fromSeat,
poolSeat,
rest,
{ USDC: add(amounts.PoolFee, amounts.Principal) },
],
[fromSeat, feeSeat, { ContractFee }, { USDC: ContractFee }],
]),
);
} catch (cause) {
const reason = Error('🚨 cannot commit repay', { cause });
console.error(reason.message, cause);
zcf.shutdownWithFailure(reason);
}
zcf.atomicRearrange(
harden([
[
fromSeat,
poolSeat,
rest,
{ USDC: add(amounts.PoolFee, amounts.Principal) },
],
[fromSeat, feeSeat, { ContractFee }, { USDC: ContractFee }],
]),
);

Object.assign(this.state, post);
this.facets.external.publishPoolMetrics();
Expand Down Expand Up @@ -284,9 +272,8 @@ export const prepareLiquidityPoolKit = (zone, zcf, USDC, tools) => {
const post = depositCalc(shareWorth, proposal);

// COMMIT POINT

const mint = shareMint.mintGains(post.payouts);
try {
const mint = shareMint.mintGains(post.payouts);
this.state.shareWorth = post.shareWorth;
zcf.atomicRearrange(
harden([
Expand All @@ -296,12 +283,11 @@ export const prepareLiquidityPoolKit = (zone, zcf, USDC, tools) => {
[mint, lp, post.payouts],
]),
);
} catch (cause) {
throw new Error('🚨 cannot commit deposit', { cause });
} finally {
lp.exit();
mint.exit();
} catch (cause) {
const reason = Error('🚨 cannot commit deposit', { cause });
console.error(reason.message, cause);
zcf.shutdownWithFailure(reason);
}
external.publishPoolMetrics();
},
Expand All @@ -321,7 +307,6 @@ export const prepareLiquidityPoolKit = (zone, zcf, USDC, tools) => {
const post = withdrawCalc(shareWorth, proposal);

// COMMIT POINT

try {
this.state.shareWorth = post.shareWorth;
zcf.atomicRearrange(
Expand All @@ -333,12 +318,11 @@ export const prepareLiquidityPoolKit = (zone, zcf, USDC, tools) => {
]),
);
shareMint.burnLosses(proposal.give, burn);
} catch (cause) {
throw new Error('🚨 cannot commit withdraw', { cause });
} finally {
lp.exit();
burn.exit();
} catch (cause) {
const reason = Error('🚨 cannot commit withdraw', { cause });
console.error(reason.message, cause);
zcf.shutdownWithFailure(reason);
}
external.publishPoolMetrics();
},
Expand Down

0 comments on commit 85ce625

Please sign in to comment.