Skip to content

Commit

Permalink
SpaceDapp: getJoinPriceDetails, add prepaidSupply and remainingFreeSu… (
Browse files Browse the repository at this point in the history
#1069)

…pply to getMembershipInfo
  • Loading branch information
salzbrenner authored Sep 17, 2024
1 parent 5ae1ad9 commit c823b99
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 18 deletions.
5 changes: 4 additions & 1 deletion packages/web3/src/ContractTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ export type MembershipInfo = Pick<
MembershipInfoStruct,
'maxSupply' | 'currency' | 'feeRecipient' | 'price' | 'duration' | 'pricingModule'
> &
TotalSupplyInfo
TotalSupplyInfo & {
prepaidSupply: number
remainingFreeSupply: number
}

export type TotalSupplyInfo = Pick<TotalSupplyOutputStruct, 'totalSupply'>

Expand Down
7 changes: 6 additions & 1 deletion packages/web3/src/ISpaceDapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,11 @@ export interface ISpaceDapp {
) => Promise<TransactionType>
getSpace(spaceId: string): Space | undefined
getSpaceMembershipTokenAddress: (spaceId: string) => Promise<string>
getJoinSpacePrice: (spaceId: string) => Promise<BigNumber>
getJoinSpacePriceDetails: (spaceId: string) => Promise<{
price: ethers.BigNumber
prepaidSupply: ethers.BigNumber
remainingFreeSupply: ethers.BigNumber
}>
joinSpace: (
spaceId: string,
recipient: string,
Expand Down Expand Up @@ -312,4 +316,5 @@ export interface ISpaceDapp {
receiver: string,
abortController?: AbortController,
) => Promise<{ issued: true; tokenId: string } | { issued: false; tokenId: undefined }>
getMembershipFreeAllocation: (spaceId: string) => Promise<BigNumber>
}
4 changes: 4 additions & 0 deletions packages/web3/src/v3/PlatformRequirements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export class PlatformRequirements extends BaseContractShim<LocalhostContract, Lo
super(address, provider, LocalhostAbi)
}

public getMembershipMintLimit() {
return this.read.getMembershipMintLimit()
}

public getMembershipFee() {
return this.read.getMembershipFee()
}
Expand Down
65 changes: 49 additions & 16 deletions packages/web3/src/v3/SpaceDapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ChannelMetadata,
EntitlementModuleType,
isPermission,
MembershipInfo,
Permission,
PricingModuleStruct,
RoleDetails,
Expand Down Expand Up @@ -1277,17 +1278,39 @@ export class SpaceDapp implements ISpaceDapp {
return space.Membership.address
}

public async getJoinSpacePrice(spaceId: string): Promise<ethers.BigNumber> {
public async getJoinSpacePriceDetails(spaceId: string): Promise<{
price: ethers.BigNumber
prepaidSupply: ethers.BigNumber
remainingFreeSupply: ethers.BigNumber
}> {
const space = this.getSpace(spaceId)
if (!space) {
throw new Error(`Space with spaceId "${spaceId}" is not found.`)
}
const prepaidSupply = await space.Prepay.read.prepaidMembershipSupply()
const membershipPrice = await space.Membership.read.getMembershipPrice()
const freeAllocation = await this.getMembershipFreeAllocation(spaceId)
const totalSupply = await space.ERC721A.read.totalSupply()
// totalSupply = number of memberships minted
// freeAllocation = number of memberships that are free to mint, set during space creation
// prepaidSupply = number of additional prepaid memberships
const remainingFreeSupply = totalSupply.lt(freeAllocation)
? freeAllocation.add(prepaidSupply).sub(totalSupply)
: prepaidSupply

return {
price: prepaidSupply.gt(0) ? ethers.BigNumber.from(0) : membershipPrice,
prepaidSupply,
remainingFreeSupply,
}
}

if (prepaidSupply.gt(0)) {
return ethers.BigNumber.from(0)
public async getMembershipFreeAllocation(spaceId: string) {
const space = this.getSpace(spaceId)
if (!space) {
throw new Error(`Space with spaceId "${spaceId}" is not found.`)
}
return space.Membership.read.getMembershipPrice()
return space.Membership.read.getMembershipFreeAllocation()
}

public async joinSpace(
Expand Down Expand Up @@ -1365,26 +1388,36 @@ export class SpaceDapp implements ISpaceDapp {
if (!space) {
throw new Error(`Space with spaceId "${spaceId}" is not found.`)
}
const [price, limit, currency, feeRecipient, duration, totalSupply, pricingModule] =
await Promise.all([
this.getJoinSpacePrice(spaceId),
space.Membership.read.getMembershipLimit(),
space.Membership.read.getMembershipCurrency(),
space.Ownable.read.owner(),
space.Membership.read.getMembershipDuration(),
space.ERC721A.read.totalSupply(),
space.Membership.read.getMembershipPricingModule(),
])
const [
joinSpacePriceDetails,
limit,
currency,
feeRecipient,
duration,
totalSupply,
pricingModule,
] = await Promise.all([
this.getJoinSpacePriceDetails(spaceId),
space.Membership.read.getMembershipLimit(),
space.Membership.read.getMembershipCurrency(),
space.Ownable.read.owner(),
space.Membership.read.getMembershipDuration(),
space.ERC721A.read.totalSupply(),
space.Membership.read.getMembershipPricingModule(),
])
const { price, prepaidSupply, remainingFreeSupply } = joinSpacePriceDetails

return {
price: price, // keep as BigNumber (wei)
price, // keep as BigNumber (wei)
maxSupply: limit.toNumber(),
currency: currency,
feeRecipient: feeRecipient,
duration: duration.toNumber(),
totalSupply: totalSupply.toNumber(),
pricingModule: pricingModule,
}
prepaidSupply: prepaidSupply.toNumber(),
remainingFreeSupply: remainingFreeSupply.toNumber(),
} satisfies MembershipInfo
}

public getWalletLink(): WalletLink {
Expand Down

0 comments on commit c823b99

Please sign in to comment.