Skip to content

Commit

Permalink
feat(sync-tools): add method to wait until offer exited
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge-Lopes committed Nov 14, 2024
1 parent 511b789 commit c9370f2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/client-utils/src/sync-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* - operation: query dest account's balance
* - condition: dest account has a balance >= sent token
* - Making sure an offer resulted successfully
* - Making sure an offer was exited successfully
*
*/

Expand Down Expand Up @@ -287,3 +288,46 @@ export const waitUntilInvitationReceived = (addr, io, options) => {
{ reusePromise: true, setTimeout, ...resolvedOptions },
);
};

/// ////////// Making sure an offer was exited successfully /////////////

const makeQueryWalletCurrent = follow => async (/** @type {string} */ addr) => {
const update = await follow('-lF', `:published.wallet.${addr}.current`);

return update;
};

/**
*
* @param {object} update
* @param {string} offerId
* @returns {boolean}
*/
const checkLiveOffers = (update, offerId) => {
const liveOffers = update.liveOffers;
if (!liveOffers) {
return false;
}
return !liveOffers.some(element => element.includes(offerId));
};

/**
*
* @param {string} addr
* @param {string} offerId
* @param {{ follow: () => object, log: typeof console.log, setTimeout: typeof global.setTimeout}} io
* @param {WaitUntilOptions} options
* @returns
*/
export const waitUntilOfferExited = async (addr, offerId, io, options) => {
const { follow, setTimeout } = io;
const queryWalletCurrent = makeQueryWalletCurrent(follow);
const { errorMessage, ...resolvedOptions } = overrideDefaultOptions(options);

return retryUntilCondition(
async () => queryWalletCurrent(addr),
update => checkLiveOffers(update, offerId),
errorMessage,
{ setTimeout, ...resolvedOptions },
);
};
19 changes: 19 additions & 0 deletions packages/client-utils/test/sync-tools.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
waitUntilAccountFunded,
waitUntilContractDeployed,
waitUntilInvitationReceived,
waitUntilOfferExited,
waitUntilOfferResult,
} from '../src/sync-tools.js';

Expand Down Expand Up @@ -403,3 +404,21 @@ test.serial('wait until invitation recevied', async t => {

await t.notThrowsAsync(waitP);
});

test.serial('wait until offer exited', async t => {
const { setValue, follow } = makeFakeFollow();
setValue({});

const waitP = waitUntilOfferExited(
'agoric12345',
'my-offer',
{ follow, log: t.log, setTimeout },
{
maxRetries: 5,
retryIntervalMs,
errorMessage: 'Offer not exited',
},
);

await t.throwsAsync(waitP);
});

0 comments on commit c9370f2

Please sign in to comment.