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/hooks/useCreateNFT.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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 @@ -82,7 +81,6 @@ useCreateNFT(
preview?: File
isAnimation: boolean
isPrivate: boolean
isLazyMint: boolean
amount?: number
royalties?: number
traits?: { type: string; value: string }[]
Expand All @@ -109,7 +107,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
2 changes: 2 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#### Breaking Changes

- `TokenFormCreate` does not require the `activateLazyMint` props anymore [](https://github.com/liteflow-labs/libraries/pull/)
antho1404 marked this conversation as resolved.
Show resolved Hide resolved

#### Added

#### Changed
Expand Down
17 changes: 11 additions & 6 deletions packages/components/src/Token/Form/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import {
useToast,
} from '@chakra-ui/react'
import { Signer, TypedDataSigner } from '@ethersproject/abstract-signer'
import { CreateNftStep, formatError, useCreateNFT } from '@nft/hooks'
import { CreateNftStep, formatError, useConfig, useCreateNFT } from '@nft/hooks'
import useTranslation from 'next-translate/useTranslation'
import React, { FC, useEffect } from 'react'
import React, { FC, useEffect, useState } from 'react'
import { useForm, useWatch } from 'react-hook-form'
import Dropzone from '../../Dropzone/Dropzone'
import { Standard } from '../../graphql'
Expand Down Expand Up @@ -60,7 +60,6 @@ type Props = {
maxRoyalties: number
onCreated: (id: string) => void
onInputChange: (data: Partial<FormData>) => void
activateLazyMint: boolean
}

const TokenFormCreate: FC<Props> = ({
Expand All @@ -74,9 +73,10 @@ const TokenFormCreate: FC<Props> = ({
maxRoyalties,
onCreated,
onInputChange,
activateLazyMint,
}) => {
const { t } = useTranslation('components')
const [isLazyMint, setIsLazyMint] = useState(false)
const config = useConfig()
const toast = useToast()
const {
isOpen: loginIsOpen,
Expand Down Expand Up @@ -104,6 +104,12 @@ const TokenFormCreate: FC<Props> = ({
const res = useWatch({ control })
useEffect(() => onInputChange(res), [res, onInputChange])

useEffect(() => {
config
.then(({ hasLazyMint }) => setIsLazyMint(hasLazyMint))
.catch(console.error)
}, [config])

// const [transform] = useFileTransformer()
const [createNFT, { activeStep, transactionHash }] = useCreateNFT(signer, {
uploadUrl,
Expand Down Expand Up @@ -132,7 +138,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 Expand Up @@ -354,7 +359,7 @@ const TokenFormCreate: FC<Props> = ({
step={activeStep}
blockExplorer={blockExplorer}
transactionHash={transactionHash}
isLazyMint={activateLazyMint}
isLazyMint={isLazyMint}
/>
</Stack>
)
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 [](https://github.com/liteflow-labs/libraries/pull/)
antho1404 marked this conversation as resolved.
Show resolved Hide resolved
- `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

- Use of ts-invariant to assert conditions in the codebase + remove useless checks [#](https://github.com/liteflow-labs/libraries/pull/)
Expand All @@ -16,6 +23,8 @@

#### Fixed

- Add proper errors when calling a hook relying on a feature not activated [](https://github.com/liteflow-labs/libraries/pull/)
antho1404 marked this conversation as resolved.
Show resolved Hide resolved

#### Security

## [v1.0.0-beta.6](https://github.com/liteflow-labs/libraries/releases/tag/v1.0.0-beta.6) - 2022-10-28
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 @@ -4,6 +4,7 @@ import { useCallback, useContext, useState } from 'react'
import invariant from 'ts-invariant'
import { LiteflowContext } from './context'
import { ErrorCodes } from './error'
import useConfig from './useConfig'

gql`
mutation CreateWyrePayment($address: Address!) {
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, ErrorCodes.FEATURE_DISABLED_TOP_UP)
invariant(signer, ErrorCodes.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 { ErrorCodes } from './error'
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),
ErrorCodes.FEATURE_DISABLED_UNLOCKABLE_CONTENT,
)
invariant(signer, ErrorCodes.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 @@ -4,6 +4,7 @@ import { useCallback, useContext, useState } from 'react'
import invariant from 'ts-invariant'
import { LiteflowContext } from './context'
import { ErrorCodes } from './error'
import useConfig from './useConfig'

gql`
mutation CreateInvitation {
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,
ErrorCodes.FEATURE_DISABLED_REFERRAL,
)
invariant(signer, ErrorCodes.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,
ErrorCodes.FEATURE_DISABLED_REFERRAL,
)
try {
setAccepting(true)
const { acceptInvitation } = await sdk.AcceptInvitation({
Expand All @@ -76,7 +86,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` props anymore [](https://github.com/liteflow-labs/libraries/pull/)

#### Added

#### Changed
Expand Down
3 changes: 0 additions & 3 deletions packages/templates/src/asset/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export const Template: NextPage<
maxRoyalties?: number
restrictMintToVerifiedAccount?: boolean
reportEmail?: string
activateLazyMint?: boolean
}
> = ({
multiple,
Expand All @@ -94,7 +93,6 @@ export const Template: NextPage<
maxRoyalties = 30,
restrictMintToVerifiedAccount = false,
reportEmail,
activateLazyMint = false,
}) => {
const { t } = useTranslation('templates')
const { back, push } = useRouter()
Expand Down Expand Up @@ -241,7 +239,6 @@ export const Template: NextPage<
login={login}
activateUnlockableContent={activateUnlockableContent}
maxRoyalties={maxRoyalties}
activateLazyMint={activateLazyMint}
/>
</Flex>
</>
Expand Down