-
Notifications
You must be signed in to change notification settings - Fork 8
Add useAllowance and useSend hooks; update tailwind config with safelist #550
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
Draft
Ben-Rey
wants to merge
6
commits into
main
Choose a base branch
from
use-send
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f717f89
Add useAllowance and useSend hooks; update tailwind config with safelist
Ben-Rey f04aeae
Refactor error messages and remove debug logs in useAllowance and use…
Ben-Rey f5e4281
Refactor useAllowance and useSend hooks to format amount display; rem…
Ben-Rey c1bc0ec
Refactor allowance messages in useAllowance hook to utilize formatted…
Ben-Rey de8d84c
Update toast messages in useAllowance hook for improved clarity and a…
Ben-Rey 6fe813a
Refactor asset type definition and update useAllowance and useSend ho…
Ben-Rey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,69 @@ | ||
| import { useCallback, useMemo, useState } from 'react'; | ||
| import { MRC20, Provider } from '@massalabs/massa-web3'; | ||
| import { useHandleOperation } from './useHandleOperation'; | ||
| import { Asset } from './types'; | ||
|
|
||
| export interface AllowanceParams { | ||
| spender: string; | ||
| amount: bigint; | ||
| token: Asset; | ||
| } | ||
|
|
||
| export interface UseAllowanceOptions { | ||
| provider: Provider | null; | ||
| } | ||
|
|
||
| export function useAllowance(options: UseAllowanceOptions) { | ||
| const { provider } = options; | ||
| const [isProcessing, setIsProcessing] = useState(false); | ||
| const { handleOperation } = useHandleOperation(); | ||
|
|
||
| const increaseAllowance = useCallback( | ||
| async ({ spender, amount, token }: AllowanceParams): Promise<void> => { | ||
| if (!provider) throw new Error('No provider'); | ||
| setIsProcessing(true); | ||
|
|
||
| if (!token.address) throw new Error('Token address required'); | ||
| const mrc20 = new MRC20(provider, token.address); | ||
| try { | ||
| const op = await mrc20.increaseAllowance(spender, amount); | ||
|
|
||
| await handleOperation(op, { | ||
| pending: `Increasing allowance ${amount} ${token.symbol}`, | ||
| success: `Increased allowance ${amount} ${token.symbol}`, | ||
| error: `Error increasing allowance`, | ||
| timeout: `Timeout increasing allowance`, | ||
| }); | ||
| } finally { | ||
| setIsProcessing(false); | ||
| } | ||
| }, | ||
| [provider, handleOperation], | ||
| ); | ||
|
|
||
| const decreaseAllowance = useCallback( | ||
| async ({ spender, amount, token }: AllowanceParams): Promise<void> => { | ||
| if (!provider) throw new Error('No provider'); | ||
| setIsProcessing(true); | ||
| if (!token.address) throw new Error('Token address required'); | ||
| const mrc20 = new MRC20(provider, token.address); | ||
| try { | ||
| const op = await mrc20.decreaseAllowance(spender, amount); | ||
| await handleOperation(op, { | ||
| pending: `Decreasing allowance ${amount} ${token.symbol}`, | ||
| success: `Decreased allowance ${amount} ${token.symbol}`, | ||
Ben-Rey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| error: `Error decreasing allowance`, | ||
| timeout: `Timeout decreasing allowance`, | ||
| }); | ||
| } finally { | ||
| setIsProcessing(false); | ||
| } | ||
| }, | ||
| [provider, handleOperation], | ||
| ); | ||
|
|
||
| return useMemo( | ||
| () => ({ isProcessing, increaseAllowance, decreaseAllowance }), | ||
| [isProcessing, increaseAllowance, decreaseAllowance], | ||
| ); | ||
| } | ||
This file contains hidden or 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,145 @@ | ||
| import { useCallback, useMemo, useState } from 'react'; | ||
| import { Address, MRC20, Operation, Provider } from '@massalabs/massa-web3'; | ||
| import { useHandleOperation } from './useHandleOperation'; | ||
| import toast from 'react-hot-toast'; | ||
| import { formatAmount } from '../../util'; | ||
| import { Asset } from './types'; | ||
|
|
||
| export interface SendParams { | ||
| recipient: string; | ||
| amount: bigint; | ||
| asset: Asset; | ||
| } | ||
|
|
||
| export interface UseSendOptions { | ||
| provider: Provider | null; | ||
| } | ||
|
|
||
| export function useSend(options: UseSendOptions) { | ||
| const { provider } = options; | ||
| const [isProcessing, setIsProcessing] = useState(false); | ||
| const { handleOperation } = useHandleOperation(); | ||
|
|
||
| /** | ||
| * Executes a send operation | ||
| * @param sendFn - The function to send the asset | ||
| * @param asset - The asset to send | ||
| * @param amount - The amount to send | ||
| * @param recipient - The recipient address | ||
| * @returns void | ||
| */ | ||
| const execute = useCallback( | ||
| async ( | ||
| sendFn: () => Promise<Operation>, | ||
| asset: Asset, | ||
| amount: bigint, | ||
| recipient: string, | ||
| ): Promise<void> => { | ||
| setIsProcessing(true); | ||
|
|
||
| if (!provider) throw new Error('No provider'); | ||
|
|
||
| try { | ||
| Address.fromString(recipient); | ||
| } catch { | ||
| toast.error('Invalid address'); | ||
| return; | ||
| } | ||
|
|
||
| if (amount > asset.balance) { | ||
| toast.error('Insufficient balance'); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
Ben-Rey marked this conversation as resolved.
Show resolved
Hide resolved
Ben-Rey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const op = await sendFn(); | ||
|
|
||
| await handleOperation(op, { | ||
| pending: `Sending ${ | ||
| formatAmount(amount.toString(), asset.decimals).preview | ||
| } ${asset.symbol}`, | ||
| success: `Sent ${ | ||
| formatAmount(amount.toString(), asset.decimals).preview | ||
| } ${asset.symbol}`, | ||
| error: `Error sending`, | ||
| timeout: `Timeout sending`, | ||
| }); | ||
| } finally { | ||
| setIsProcessing(false); | ||
| } | ||
| }, | ||
| [provider, handleOperation], | ||
| ); | ||
|
|
||
| /** | ||
| * Sends a native Massa coin to a recipient | ||
| * @param recipient - The recipient address | ||
| * @param amount - The amount to send | ||
| * @param asset - The asset to send | ||
| * @returns void | ||
| */ | ||
| const sendMassa = useCallback( | ||
| async ({ recipient, amount, asset }: SendParams): Promise<void> => { | ||
| if (!provider) throw new Error('No provider'); | ||
| await execute( | ||
| () => provider.transfer(recipient, amount), | ||
| asset, | ||
| amount, | ||
| recipient, | ||
| ); | ||
| }, | ||
| [provider, execute], | ||
| ); | ||
|
|
||
| /** | ||
| * Sends a mrc20 token to a recipient | ||
| * @param recipient - The recipient address | ||
| * @param amount - The amount to send | ||
| * @param asset - The token to send | ||
| * @returns void | ||
| */ | ||
| const sendToken = useCallback( | ||
| async ({ recipient, amount, asset }: SendParams): Promise<void> => { | ||
| if (!provider) throw new Error('No provider'); | ||
| if (!asset.address) throw new Error('Token address required'); | ||
| const mrc20 = new MRC20(provider, asset.address); | ||
|
|
||
| await execute( | ||
| () => mrc20.transfer(recipient, amount), | ||
| asset, | ||
| amount, | ||
| recipient, | ||
| ); | ||
| }, | ||
| [provider, execute], | ||
| ); | ||
|
|
||
| /** | ||
| * Sends an asset to a recipient | ||
| * The Asset can be a native Massa coin or a mrc20 token | ||
| * @param recipient - The recipient address | ||
| * @param amount - The amount to send | ||
| * @param asset - The asset to send | ||
| * @returns void | ||
| */ | ||
| const sendAsset = useCallback( | ||
| async ({ recipient, amount, asset }: SendParams): Promise<void> => { | ||
| if (asset.isNative) { | ||
| return sendMassa({ recipient, amount, asset }); | ||
| } | ||
|
|
||
| return sendToken({ recipient, amount, asset }); | ||
| }, | ||
| [sendMassa, sendToken], | ||
| ); | ||
|
|
||
| return useMemo( | ||
| () => ({ | ||
| isProcessing, | ||
| sendAsset, | ||
| sendMassa, | ||
| sendToken, | ||
| }), | ||
| [isProcessing, sendAsset, sendMassa, sendToken], | ||
| ); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.