Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Quote for memberships that are not for sale to a specific buyer #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,46 @@ 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[]
) {
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(
Expand Down