From 9b83eac0da8d809317c095ed71da6255beac2b09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Andr=C3=A9s=20Dorado=20Su=C3=A1rez?= Date: Thu, 31 Oct 2024 15:47:49 -0500 Subject: [PATCH] fix(aqcuire-memberships-invite-members): quote for memberships that are not for sale to a specific buyer --- .../acquire-memberships-invite-members.ts | 47 ++++++++++++------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/src/top-up-then-create-community/acquire-memberships-invite-members.ts b/src/top-up-then-create-community/acquire-memberships-invite-members.ts index 45119ad..ef62abc 100644 --- a/src/top-up-then-create-community/acquire-memberships-invite-members.ts +++ b/src/top-up-then-create-community/acquire-memberships-invite-members.ts @@ -7,6 +7,13 @@ import { ApiPromise } from "@polkadot/api"; import { AccountId } from "../types"; import { u128 } from "@polkadot/types"; +type MembershipItemQuote = { + item: number; + price: number; + buyer?: string; + accountToAssign: string; +} + async function prepareMemberships( api: ApiPromise, membershipAccounts: AccountId[] @@ -14,26 +21,32 @@ async function prepareMemberships( const itemsForSale = await api.query.communityMemberships.itemPriceOf.entriesPaged({ args: [0], - pageSize: membershipAccounts.length, + // Page 50 items per each 50 memberships to buy. That way, we'll have enough space to ignore + // items not on public sale. + pageSize: 50 * Math.ceil(membershipAccounts.length / 50), }); - return itemsForSale.map(([key, value], i) => { - const itemKey = (key.toHuman() as unknown as string[])[1]; - const preItem = (key.toHuman() as unknown as string[])[1].replaceAll( - /\D/g, - "" - ); - const item = Number(preItem); - console.log(itemKey, preItem, item); + return itemsForSale + .map(([key, value], i) => { + const itemKey = (key.toHuman() as unknown as string[])[1]; + const preItem = (key.toHuman() as unknown as string[])[1].replaceAll( + /\D/g, + "" + ); + const item = Number(preItem); + const [price, buyer] = (value as unknown as { unwrap: () => [u128, string | undefined] }) + .unwrap(); + console.log(itemKey, preItem, item); - return { - item, - price: (value as unknown as { unwrap: () => [u128, string] }) - .unwrap()[0] - .toNumber(), - accountToAssign: membershipAccounts[i], - }; - }); + return { + item, + price: price.toNumber(), + buyer, + accountToAssign: membershipAccounts[i], + } as MembershipItemQuote; + }) + .filter(({ buyer }) => buyer !== undefined) + .slice(0, membershipAccounts.length); } export async function acquireMembershipsAndAddMembers(