-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace old faucet approach with use-faucet (#1484)
- Loading branch information
1 parent
5336dc7
commit 12b9271
Showing
6 changed files
with
90 additions
and
92 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
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
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
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,79 @@ | ||
import { FaucetClient } from '@interlay/interbtc-api'; | ||
import { useMemo, useRef } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useMutation } from 'react-query'; | ||
|
||
import { ACCOUNT_ID_TYPE_NAME } from '@/config/general'; | ||
import { GOVERNANCE_TOKEN } from '@/config/relay-chains'; | ||
import { BITCOIN_NETWORK, FAUCET_URL } from '@/constants'; | ||
import { useGetBalances } from '@/hooks/api/tokens/use-get-balances'; | ||
import { useWallet } from '@/hooks/use-wallet'; | ||
import { BitcoinNetwork } from '@/types/bitcoin'; | ||
|
||
import { NotificationToastType, useNotifications } from '../context/Notifications'; | ||
|
||
type UseFaucetResult = { | ||
isAvailable: boolean; | ||
buttonProps: { | ||
pending: boolean; | ||
onClick: () => void; | ||
}; | ||
}; | ||
|
||
const useFaucet = (): UseFaucetResult => { | ||
const { t } = useTranslation(); | ||
|
||
const wallet = useWallet(); | ||
const notifications = useNotifications(); | ||
const { getAvailableBalance } = useGetBalances(); | ||
|
||
const faucetRef = useRef<FaucetClient>(); | ||
|
||
const { mutate, isLoading } = useMutation<void, Error, string, unknown>({ | ||
mutationFn: async (account: string) => { | ||
const faucet = faucetRef.current || new FaucetClient(window.bridge.api, FAUCET_URL); | ||
|
||
faucetRef.current = faucet; | ||
|
||
const receiverId = window.bridge.api.createType(ACCOUNT_ID_TYPE_NAME, account); | ||
await faucet.fundAccount(receiverId, GOVERNANCE_TOKEN); | ||
}, | ||
onSuccess: () => | ||
notifications.show('faucet', { | ||
type: NotificationToastType.STANDARD, | ||
props: { variant: 'success', title: t('notifications.funding_account_successful') } | ||
}), | ||
onError: () => | ||
notifications.show('faucet', { | ||
type: NotificationToastType.STANDARD, | ||
props: { variant: 'error', title: t('notifications.funding_account_failed') } | ||
}) | ||
}); | ||
|
||
const isAvailable = useMemo(() => { | ||
if (isLoading) return true; | ||
|
||
const isTestnet = BITCOIN_NETWORK === BitcoinNetwork.Testnet; | ||
|
||
const hasGovernanceBalance = getAvailableBalance(GOVERNANCE_TOKEN.ticker)?.isZero(); | ||
|
||
return !!wallet.account && isTestnet && !!FAUCET_URL && !!hasGovernanceBalance; | ||
}, [getAvailableBalance, isLoading, wallet.account]); | ||
|
||
const handleFundAccount = () => { | ||
if (!wallet.account || !isAvailable) return; | ||
|
||
mutate(wallet.account.toString()); | ||
}; | ||
|
||
return { | ||
isAvailable, | ||
buttonProps: { | ||
pending: isLoading, | ||
onClick: handleFundAccount | ||
} | ||
}; | ||
}; | ||
|
||
export { useFaucet }; | ||
export type { UseFaucetResult }; |
12b9271
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
interbtc-ui-interlay-testnet – ./
interbtc-ui-interlay-testnet-git-master-interlay.vercel.app
testnet.interlay.io
interbtc-ui-interlay-testnet.vercel.app
interbtc-ui-interlay-testnet-interlay.vercel.app
12b9271
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
interbtc-ui-kintsugi-testnet – ./
interbtc-ui.vercel.app
interbtc-ui-kintsugi-testnet-interlay.vercel.app
interbtc-ui-kintsugi-testnet-git-master-interlay.vercel.app
kintnet.interlay.io