-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@lens-protocol/react": minor | ||
"@lens-protocol/react-native": minor | ||
"@lens-protocol/react-web": minor | ||
--- | ||
|
||
**feat:** added `useProfilePrice` hook to fetch onchain profile minting price |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/react/src/misc/adapters/useProfilePriceController.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { formatUnits } from '@ethersproject/units'; | ||
import { UnspecifiedError } from '@lens-protocol/api-bindings'; | ||
import { permissionlessCreator } from '@lens-protocol/blockchain-bindings'; | ||
import { | ||
Amount, | ||
ChainType, | ||
Matic, | ||
PromiseResult, | ||
assertError, | ||
failure, | ||
matic, | ||
success, | ||
} from '@lens-protocol/shared-kernel'; | ||
|
||
import { useSharedDependencies } from '../../shared'; | ||
|
||
export function useProfilePriceController() { | ||
const { config, providerFactory } = useSharedDependencies(); | ||
|
||
return async (): PromiseResult<Amount<Matic>, UnspecifiedError> => { | ||
try { | ||
const provider = await providerFactory.createProvider({ chainType: ChainType.POLYGON }); | ||
const contract = permissionlessCreator( | ||
config.environment.contracts.permissionlessCreator, | ||
provider, | ||
); | ||
|
||
const priceBN = await contract.getProfileWithHandleCreationPrice(); | ||
const result = formatUnits(priceBN, matic().decimals); | ||
|
||
return success(Amount.matic(result)); | ||
} catch (error) { | ||
assertError(error); | ||
return failure(new UnspecifiedError(error)); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { QueryResult } from '@apollo/client'; | ||
import { UnspecifiedError } from '@lens-protocol/api-bindings'; | ||
import { Amount, Matic } from '@lens-protocol/shared-kernel'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { ReadResult, useReadResult } from '../helpers/reads'; | ||
import { useProfilePriceController } from './adapters/useProfilePriceController'; | ||
|
||
/** | ||
* Fetch the onchain cost in Matic associated with minting a new Lens Profile. | ||
* | ||
* @example | ||
* ```tsx | ||
* const { data, error, loading } = useProfilePrice(); | ||
* ``` | ||
* | ||
* @category Misc | ||
* @group Hooks | ||
*/ | ||
export function useProfilePrice(): ReadResult<Amount<Matic>> { | ||
const [price, setPrice] = useState<Amount<Matic>>(); | ||
const [error, setError] = useState<UnspecifiedError>(); | ||
const fetchPrice = useProfilePriceController(); | ||
|
||
useEffect(() => { | ||
const fetchData = async () => { | ||
const result = await fetchPrice(); | ||
|
||
if (result.isFailure()) { | ||
setError(result.error); | ||
return; | ||
} | ||
|
||
setPrice(result.value); | ||
}; | ||
|
||
void fetchData(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
return useReadResult({ error, data: { result: price } } as QueryResult); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.