From c9370f219ecdc196a274cfb8bab8de64b099345a Mon Sep 17 00:00:00 2001 From: Jorge-Lopes Date: Wed, 13 Nov 2024 12:46:39 +0000 Subject: [PATCH 1/3] feat(sync-tools): add method to wait until offer exited rel: https://github.com/Agoric/BytePitchPartnerEng/issues/38 --- packages/client-utils/src/sync-tools.js | 44 +++++++++++++++++++ packages/client-utils/test/sync-tools.test.js | 19 ++++++++ 2 files changed, 63 insertions(+) diff --git a/packages/client-utils/src/sync-tools.js b/packages/client-utils/src/sync-tools.js index bf67c2e4abf..16471d05cd2 100644 --- a/packages/client-utils/src/sync-tools.js +++ b/packages/client-utils/src/sync-tools.js @@ -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 * */ @@ -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 }, + ); +}; diff --git a/packages/client-utils/test/sync-tools.test.js b/packages/client-utils/test/sync-tools.test.js index 0c6bfe3f5f0..0d880324937 100644 --- a/packages/client-utils/test/sync-tools.test.js +++ b/packages/client-utils/test/sync-tools.test.js @@ -6,6 +6,7 @@ import { waitUntilAccountFunded, waitUntilContractDeployed, waitUntilInvitationReceived, + waitUntilOfferExited, waitUntilOfferResult, } from '../src/sync-tools.js'; @@ -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); +}); From b09ae6f7cd5f277caa855b30d2c2c8670255f215 Mon Sep 17 00:00:00 2001 From: Jorge-Lopes Date: Wed, 13 Nov 2024 15:54:36 +0000 Subject: [PATCH 2/3] test(sync-tools): update snapshot rel: https://github.com/Agoric/BytePitchPartnerEng/issues/38 --- .../test/snapshots/exports.test.js.md | 1 + .../test/snapshots/exports.test.js.snap | Bin 459 -> 472 bytes 2 files changed, 1 insertion(+) diff --git a/packages/client-utils/test/snapshots/exports.test.js.md b/packages/client-utils/test/snapshots/exports.test.js.md index 8a1c268100a..7c39db1562f 100644 --- a/packages/client-utils/test/snapshots/exports.test.js.md +++ b/packages/client-utils/test/snapshots/exports.test.js.md @@ -24,5 +24,6 @@ Generated by [AVA](https://avajs.dev). 'waitUntilAccountFunded', 'waitUntilContractDeployed', 'waitUntilInvitationReceived', + 'waitUntilOfferExited', 'waitUntilOfferResult', ] diff --git a/packages/client-utils/test/snapshots/exports.test.js.snap b/packages/client-utils/test/snapshots/exports.test.js.snap index 831d79980ba086c6675f7c861674e044790fd5c1..6d65d812692c36b4fdfedccf522d6ba2aafec158 100644 GIT binary patch literal 472 zcmV;}0Vn=JRzVS8oC1pudfe?3i0C<6rwgMqQN@x*BcXyJG z;@B(iq-{>97aoFJ5j+J?fOrE=JPDwM+N&J!$@2esJf5rRiS@<2zWl^0YMI5Ad?B?n zRekEUzPu_+qvl=qy@O3Oi?(Rj0tf&;0{9Bx2Y_Dy9ulxm02AJCFNE26l|^BlAd>VtY1P=-C~fVzFUcj8SbC)TTqo4qulJ~h^I zxOPMl<_k0D;mijqUYP~g5o8hUG~h7!#nYx@j5{&jZQ_ZVFf+;{E9^9y8`0ct%o}zk zhlMzO{AAsAqw6*9c%nX-8OL;Et<&TBU;0AQ_02W#w)v(Wso3^28cd=4Tv@+r>Fr1#B>LQ)iJGR{F&8XnEzTp}OZfG4%HiNbv{iThqsCk> Ot^NVs@fE4{0ssKW58UDa literal 459 zcmV;+0W|(WRzVUva)Ke9kzp|I>H&V@?ySnkRdHs4y2Z1%L zPMwM!c~^^ycM0t$)Qy(8Nyj(xkgnQ#!M529xt9mD{soXOTZhX6000TE B((wQQ From 7f4de7c1ce9e56de5f12250e10fe524e35f9bc05 Mon Sep 17 00:00:00 2001 From: Jorge-Lopes Date: Wed, 13 Nov 2024 19:26:21 +0000 Subject: [PATCH 3/3] chore(sync-tools): improve makeQueryWalletCurrent and JSDoc annotations --- packages/client-utils/src/sync-tools.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/client-utils/src/sync-tools.js b/packages/client-utils/src/sync-tools.js index 16471d05cd2..35701da3263 100644 --- a/packages/client-utils/src/sync-tools.js +++ b/packages/client-utils/src/sync-tools.js @@ -291,14 +291,10 @@ export const waitUntilInvitationReceived = (addr, io, options) => { /// ////////// 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; -}; +const makeQueryWalletCurrent = follow => (/** @type {string} */ addr) => + follow('-lF', `:published.wallet.${addr}.current`); /** - * * @param {object} update * @param {string} offerId * @returns {boolean} @@ -312,12 +308,10 @@ const checkLiveOffers = (update, 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;