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

Limit sdk based on api config #65

Merged
merged 12 commits into from
Nov 30, 2022
3 changes: 0 additions & 3 deletions docs/pages/hooks/useCreateNFT.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export default function Component() {
isAnimation: false, // set to true if content file is a video. Require to set preview
isPrivate: false, // set to true if content is private. Require to set preview.
// optional
isLazyMint: true, // mints the NFT using lazy minting
amount: 1, // number of NFT to create. Require Standard.ERC1155
royalties: 10, // royalty amount in percentage
preview: azukiImagePreview, // preview in the case of private or animation content uploaded by user
Expand Down Expand Up @@ -86,7 +85,6 @@ useCreateNFT(
preview?: File
isAnimation: boolean
isPrivate: boolean
isLazyMint: boolean
amount?: number
royalties?: number
traits?: { type: string; value: string }[]
Expand All @@ -113,7 +111,6 @@ Arguments:
preview?: File, // preview in the case of private or animation content uploaded by user
isAnimation: boolean, // set to true if content file is a video. Require to set preview
isPrivate: boolean, // set to true if content is private. Require to set preview.
isLazyMint: boolean, // set to true if you want the NFT to be lazy minted
amount?: number, // number of NFT to create. Require Standard.ERC1155
royalties?: number, // royalty amount in percentage
traits?: { type: string, value: string }[] // Array of traits associated to the NFT
Expand Down
1 change: 0 additions & 1 deletion packages/components/src/Token/Form/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ const TokenFormCreate: FC<Props> = ({
amount: multiple ? parseInt(data.amount) : 1,
royalties: parseFloat(data.royalties),
traits: [{ type: 'Category', value: data.category }],
isLazyMint: activateLazyMint,
})

onCreated(assetId)
Expand Down
9 changes: 9 additions & 0 deletions packages/hooks/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

#### Added

- Add `useConfig` hook to return the configuration of the platform [#65](https://github.com/liteflow-labs/libraries/pull/65)
- `hasLazyMint` is true when lazymint is activated
- `hasReferralSystem` is true when the referral and invitation system is activated
- `hasSocialFeatures` is true when all the social features (likes, comments...) are activated
- `hasTopUp` is true when fiat on ramp is activated
- `hasUnlockableContent` is true when unlockable content is activated

#### Changed

#### Deprecated
Expand All @@ -14,6 +21,8 @@

#### Fixed

- Add proper errors when calling a hook relying on a feature not activated [#65](https://github.com/liteflow-labs/libraries/pull/65)

#### Security

## [v1.0.0-beta.8](https://github.com/liteflow-labs/libraries/releases/tag/v1.0.0-beta.8) - 2022-11-15
Expand Down
23 changes: 21 additions & 2 deletions packages/hooks/src/context.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphQLClient } from 'graphql-request'
import { gql, GraphQLClient } from 'graphql-request'
import React, {
createContext,
PropsWithChildren,
Expand All @@ -7,13 +7,26 @@ import React, {
useMemo,
useState,
} from 'react'
import type { Sdk } from './graphql'
import type { Config, Sdk } from './graphql'
import { getSdk } from './graphql'

gql`
query GetConfig {
config {
hasLazyMint
hasReferralSystem
hasSocialFeatures
hasTopUp
hasUnlockableContent
}
}
`

export type LiteflowContext = {
setAuthenticationToken: (token: string) => void
resetAuthenticationToken: () => void
sdk: Sdk
config: Promise<Config>
}

export type LiteflowProviderProps = {
Expand All @@ -28,6 +41,7 @@ export const LiteflowContext = createContext<LiteflowContext>({
throw new Error('not implemented')
},
sdk: {} as Sdk,
config: {} as Promise<Config>,
})

export function LiteflowProvider({
Expand All @@ -37,6 +51,10 @@ export function LiteflowProvider({
const [authenticationToken, setAuthenticationToken] = useState<string>()
const client = useMemo(() => new GraphQLClient(endpoint), [endpoint])
const sdk = useMemo(() => getSdk(client), [client])
const config = useMemo(
() => sdk.GetConfig().then(({ config }) => config),
[sdk],
)

const resetAuthenticationToken = useCallback(
() => setAuthenticationToken(undefined),
Expand All @@ -58,6 +76,7 @@ export function LiteflowProvider({
resetAuthenticationToken,
setAuthenticationToken,
sdk,
config,
}}
>
{children}
Expand Down
2 changes: 2 additions & 0 deletions packages/hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import useAuctionStatus from './useAuctionStatus'
import useAuthenticate from './useAuthenticate'
import useBalance from './useBalance'
import useCancelOffer, { CancelOfferStep } from './useCancelOffer'
import useConfig from './useConfig'
import useCreateAuction from './useCreateAuction'
import useCreateNFT, { CreateNftStep } from './useCreateNFT'
import useCreateOffer, { CreateOfferStep } from './useCreateOffer'
Expand Down Expand Up @@ -38,6 +39,7 @@ export {
* Stable
* These hooks are ready to use and are unlikely to have breaking change
*/
useConfig,
useAcceptAuction,
useAcceptOffer,
useAuthenticate,
Expand Down
6 changes: 5 additions & 1 deletion packages/hooks/src/useAddFund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { gql } from 'graphql-request'
import { useCallback, useContext, useState } from 'react'
import invariant from 'ts-invariant'
import { LiteflowContext } from './context'
import useConfig from './useConfig'
import { ErrorMessages } from './errorMessages'

gql`
Expand All @@ -19,7 +20,10 @@ export default function useAddFund(
): [() => Promise<void>, { loading: boolean }] {
const { sdk } = useContext(LiteflowContext)
const [loading, setLoading] = useState(false)
const config = useConfig()

const addFunds = useCallback(async () => {
invariant((await config).hasTopUp, ErrorMessages.FEATURE_DISABLED_TOP_UP)
invariant(signer, ErrorMessages.SIGNER_FALSY)
try {
setLoading(true)
Expand All @@ -32,6 +36,6 @@ export default function useAddFund(
} finally {
setLoading(false)
}
}, [sdk, signer])
}, [sdk, signer, config])
return [addFunds, { loading }]
}
8 changes: 8 additions & 0 deletions packages/hooks/src/useConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { useContext } from 'react'
import { LiteflowContext } from './context'
import { Config } from './graphql'

export default function useConfig(): Promise<Config> {
const { config } = useContext(LiteflowContext)
return config
}
12 changes: 8 additions & 4 deletions packages/hooks/src/useCreateNFT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { LiteflowContext } from './context'
import { ErrorMessages } from './errorMessages'
import { Standard } from './graphql'
import useCheckOwnership from './useCheckOwnership'
import useConfig from './useConfig'
import useIPFSUploader from './useIPFSUploader'
import { convertTx } from './utils/transaction'

Expand Down Expand Up @@ -85,7 +86,6 @@ type createNftFn = (data: {
amount?: number
royalties?: number
traits?: { type: string; value: string }[]
isLazyMint?: boolean
}) => Promise<string>

export default function useCreateNFT(
Expand All @@ -99,6 +99,7 @@ export default function useCreateNFT(
},
] {
const { sdk } = useContext(LiteflowContext)
const config = useConfig()
const [transactionHash, setTransactionHash] = useState<string>()
const [activeStep, setActiveProcess] = useState<CreateNftStep>(
CreateNftStep.INITIAL,
Expand Down Expand Up @@ -167,8 +168,11 @@ export default function useCreateNFT(
amount,
royalties,
traits,
isLazyMint,
}) => {
invariant(
!isPrivate || (isPrivate && (await config).hasUnlockableContent),
ErrorMessages.FEATURE_DISABLED_UNLOCKABLE_CONTENT,
)
invariant(signer, ErrorMessages.SIGNER_FALSY)
const account = await signer.getAddress()

Expand All @@ -182,7 +186,7 @@ export default function useCreateNFT(
})

// lazy minting
if (isLazyMint) {
if ((await config).hasLazyMint) {
const assetToCreate = {
standard,
creatorAddress: account.toLowerCase(),
Expand Down Expand Up @@ -274,7 +278,7 @@ export default function useCreateNFT(
setTransactionHash(undefined)
}
},
[sdk, signer, pollOwnership, uploadMedia],
[sdk, signer, pollOwnership, uploadMedia, config],
)

return [createNft, { activeStep, transactionHash }]
Expand Down
14 changes: 12 additions & 2 deletions packages/hooks/src/useInvitation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { gql } from 'graphql-request'
import { useCallback, useContext, useState } from 'react'
import invariant from 'ts-invariant'
import { LiteflowContext } from './context'
import useConfig from './useConfig'
import { ErrorMessages } from './errorMessages'

gql`
Expand Down Expand Up @@ -40,9 +41,14 @@ export default function useInvitation(signer: Signer | undefined): {
creating: boolean
} {
const { sdk } = useContext(LiteflowContext)
const config = useConfig()
const [accepting, setAccepting] = useState(false)
const [creating, setCreating] = useState(false)
const create = useCallback(async () => {
invariant(
(await config).hasReferralSystem,
ErrorMessages.FEATURE_DISABLED_REFERRAL,
)
invariant(signer, ErrorMessages.SIGNER_FALSY)
try {
setCreating(true)
Expand All @@ -61,10 +67,14 @@ export default function useInvitation(signer: Signer | undefined): {
} finally {
setCreating(false)
}
}, [sdk, signer])
}, [sdk, signer, config])

const accept = useCallback(
async (invitationId: string) => {
invariant(
(await config).hasReferralSystem,
ErrorMessages.FEATURE_DISABLED_REFERRAL,
)
try {
setAccepting(true)
const { acceptInvitation } = await sdk.AcceptInvitation({
Expand All @@ -79,7 +89,7 @@ export default function useInvitation(signer: Signer | undefined): {
setAccepting(false)
}
},
[sdk],
[sdk, config],
)

return { create, accept, accepting, creating }
Expand Down
2 changes: 2 additions & 0 deletions packages/templates/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#### Breaking Changes

- `AssetForm` template does not require the `activateLazyMint` and `activateUnlockableContent` props anymore [#65](https://github.com/liteflow-labs/libraries/pull/65)

#### Added

#### Changed
Expand Down
19 changes: 12 additions & 7 deletions packages/templates/src/asset/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ import {
} from '@nft/components'
import type { Props as NFTCardProps } from '@nft/components/dist/Token/Card'
import type { FormData } from '@nft/components/dist/Token/Form/Create'
import { useSession } from '@nft/hooks'
import { useConfig, useSession } from '@nft/hooks'
import { HiBadgeCheck } from '@react-icons/all-files/hi/HiBadgeCheck'
import { useEffect } from '@storybook/addons'
import { GetServerSideProps, NextPage } from 'next'
import Trans from 'next-translate/Trans'
import useTranslation from 'next-translate/useTranslation'
import { useRouter } from 'next/router'
import React, { useCallback, useMemo, useState } from 'react'
import {
Config,
FetchAccountDocument,
FetchAccountQuery,
useFetchAccountQuery,
Expand Down Expand Up @@ -75,28 +77,26 @@ export const Template: NextPage<
walletConnect: boolean
networkName: string
}
activateUnlockableContent: boolean
maxRoyalties?: number
restrictMintToVerifiedAccount?: boolean
reportEmail?: string
activateLazyMint?: boolean
}
> = ({
multiple,
explorer,
uploadUrl,
login,
activateUnlockableContent,
traits,
currentAccount,
maxRoyalties = 30,
restrictMintToVerifiedAccount = false,
reportEmail,
activateLazyMint = false,
}) => {
const { t } = useTranslation('templates')
const { back, push } = useRouter()
const { account, ready, signer } = useSession()
const configPromise = useConfig()
const [config, setConfig] = useState<Config>()
const toast = useToast()
const { data } = useFetchAccountQuery({
variables: {
Expand Down Expand Up @@ -158,6 +158,11 @@ export const Template: NextPage<
[push, t, toast],
)

useEffect(() => {
void configPromise.then(setConfig)
return () => setConfig(undefined)
}, [configPromise])

if (restrictMintToVerifiedAccount && !creator.verified) {
return (
<>
Expand Down Expand Up @@ -235,9 +240,9 @@ export const Template: NextPage<
onCreated={onCreated}
onInputChange={setFormData}
login={login}
activateUnlockableContent={activateUnlockableContent}
activateUnlockableContent={config?.hasUnlockableContent || false}
maxRoyalties={maxRoyalties}
activateLazyMint={activateLazyMint}
activateLazyMint={config?.hasLazyMint || false}
/>
</Flex>
</>
Expand Down