Skip to content

Commit

Permalink
fixup! feat: zoeTools.withdrawToSeat
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpatrickdev committed Aug 16, 2024
1 parent c537e65 commit 7c43470
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
30 changes: 18 additions & 12 deletions packages/orchestration/src/utils/zoe-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@agoric/zoe/src/contractSupport/index.js';
import { E } from '@endo/far';

const { keys } = Object;
const { assign, keys, values } = Object;

/**
* @import {InvitationMakers} from '@agoric/smart-wallet/src/types.js';
Expand Down Expand Up @@ -89,7 +89,7 @@ export const makeZoeTools = (
console.debug(
'One or more deposits to LCA failed. Returning payment(s)...',
);
const amounts = Object.values(give);
const amounts = values(give);
const errors = [];
// withdraw the successfully deposited payments
const paymentOrWithdrawVs = settleDeposits.map((x, i) => {
Expand All @@ -103,13 +103,18 @@ export const makeZoeTools = (
// return all payments to the srcSeat
const paymentsToReturn = await when(allVows(paymentOrWithdrawVs));
const paymentsKwr = harden(
Object.keys(give).reduce(
(acc, kw, i) => Object.assign(acc, { [kw]: paymentsToReturn[i] }),
keys(give).reduce(
(acc, kw, i) => assign(acc, { [kw]: paymentsToReturn[i] }),
{},
),
);
const dRes = await depositToSeat(zcf, srcSeat, give, paymentsKwr);
console.debug(dRes);
const depositResponse = await depositToSeat(
zcf,
srcSeat,
give,
paymentsKwr,
);
console.debug(depositResponse);
throw Fail`One or more deposits to LCA failed. ${q(errors)}`;
}
// TODO remove userSeat from baggage
Expand All @@ -124,25 +129,26 @@ export const makeZoeTools = (
await null;
!recipientSeat.hasExited() || Fail`The seat cannot have exited.`;

const paymentVs = Object.values(amounts).map(amt =>
const paymentVs = values(amounts).map(amt =>
srcLocalAccount.withdraw(amt),
);
const settledPmts = await when(allVowsSettled(paymentVs));
const settledWtihdrawals = await when(allVowsSettled(paymentVs));

// if any of the withdrawals were rejected, undo the successful ones
if (settledPmts.find(x => x.status === 'rejected')) {
if (settledWtihdrawals.find(x => x.status === 'rejected')) {
const returnVs = [];
for (const pmtP of settledPmts) {
for (const pmtP of settledWtihdrawals) {
if (pmtP.status === 'fulfilled') {
returnVs.push(srcLocalAccount.deposit(pmtP.value));
}
}
await when(allVows(returnVs));
throw Fail`One or more withdrawals failed. Returned all payments to the source local account.`;
}
// successfully withdraw all payments for srcLocalAccount, deposit to recipientSeat
const paymentsKwr = harden(
Object.keys(amounts).reduce(
(acc, kw, i) => Object.assign(acc, { [kw]: settledPmts[i].value }),
keys(amounts).reduce(
(acc, kw, i) => assign(acc, { [kw]: settledWtihdrawals[i].value }),
{},
),
);
Expand Down
2 changes: 2 additions & 0 deletions packages/orchestration/test/utils/zoe-tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,5 @@ test('zoeTool.localTransfer and zoeTools.withdrawToSeat', async t => {
});
}
});

test.todo('withdrawToSeat, unknown brand');

0 comments on commit 7c43470

Please sign in to comment.