|
| 1 | +import { Box, Button, Divider, Heading } from '@chakra-ui/react'; |
| 2 | +import PendingText from '@/components/shared/PendingText.tsx'; |
| 3 | +import { ContractId } from 'contracts/deployments'; |
| 4 | +import { Psp22ContractApi } from 'contracts/types/psp22'; |
| 5 | +import { formatBalance, txToaster, useContract, useContractQuery, useContractTx, useTypink } from 'typink'; |
| 6 | +import { useStatus } from '@luno-kit/react'; |
| 7 | +import { ConnectButton } from '@luno-kit/ui'; |
| 8 | + |
| 9 | +export default function Psp22Board() { |
| 10 | + const { contract } = useContract<Psp22ContractApi>(ContractId.PSP22); |
| 11 | + const { defaultCaller, connectedAccount, network } = useTypink(); |
| 12 | + const mintTx = useContractTx(contract, 'psp22MintableMint'); |
| 13 | + const connectionStatus = useStatus() |
| 14 | + |
| 15 | + const { data: tokenName, isLoading: loadingTokenName } = useContractQuery({ |
| 16 | + contract, |
| 17 | + fn: 'psp22MetadataTokenName', |
| 18 | + }); |
| 19 | + |
| 20 | + const { data: tokenSymbol, isLoading: loadingTokenSymbol } = useContractQuery({ |
| 21 | + contract, |
| 22 | + fn: 'psp22MetadataTokenSymbol', |
| 23 | + }); |
| 24 | + |
| 25 | + const { data: tokenDecimal, isLoading: loadingTokenDecimal } = useContractQuery({ |
| 26 | + contract, |
| 27 | + fn: 'psp22MetadataTokenDecimals', |
| 28 | + }); |
| 29 | + |
| 30 | + const { |
| 31 | + data: totalSupply, |
| 32 | + isLoading: loadingTotalSupply, |
| 33 | + refresh: refreshTotalSupply, |
| 34 | + } = useContractQuery({ |
| 35 | + contract, |
| 36 | + fn: 'psp22TotalSupply', |
| 37 | + }); |
| 38 | + |
| 39 | + const { |
| 40 | + data: myBalance, |
| 41 | + isLoading: loadingBalance, |
| 42 | + refresh: refreshMyBalance, |
| 43 | + } = useContractQuery({ |
| 44 | + contract, |
| 45 | + fn: 'psp22BalanceOf', |
| 46 | + args: [connectedAccount?.address || defaultCaller], |
| 47 | + }); |
| 48 | + |
| 49 | + const mintNewToken = async () => { |
| 50 | + if (!tokenDecimal) return; |
| 51 | + |
| 52 | + const toaster = txToaster('Signing transaction...'); |
| 53 | + try { |
| 54 | + await mintTx.signAndSend({ |
| 55 | + args: [BigInt(100 * Math.pow(10, tokenDecimal))], |
| 56 | + callback: (progress) => { |
| 57 | + const { status } = progress; |
| 58 | + console.log(status); |
| 59 | + |
| 60 | + if (status.type === 'BestChainBlockIncluded') { |
| 61 | + refreshMyBalance(); |
| 62 | + refreshTotalSupply(); |
| 63 | + } |
| 64 | + |
| 65 | + toaster.onTxProgress(progress); |
| 66 | + }, |
| 67 | + }); |
| 68 | + } catch (e: any) { |
| 69 | + console.error(e); |
| 70 | + toaster.onTxError(e); |
| 71 | + } finally { |
| 72 | + refreshMyBalance(); |
| 73 | + refreshTotalSupply(); |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + return ( |
| 78 | + <Box> |
| 79 | + <Heading size='md'>PSP22 Contract</Heading> |
| 80 | + <Box mt={4}> |
| 81 | + <Box mb={2}> |
| 82 | + Token Name:{' '} |
| 83 | + <PendingText fontWeight='600' isLoading={loadingTokenName}> |
| 84 | + {tokenName} |
| 85 | + </PendingText> |
| 86 | + </Box> |
| 87 | + <Box mb={2}> |
| 88 | + Token Symbol:{' '} |
| 89 | + <PendingText fontWeight='600' isLoading={loadingTokenSymbol}> |
| 90 | + {tokenSymbol} |
| 91 | + </PendingText> |
| 92 | + </Box> |
| 93 | + <Box mb={2}> |
| 94 | + Token Decimal:{' '} |
| 95 | + <PendingText fontWeight='600' isLoading={loadingTokenDecimal}> |
| 96 | + {tokenDecimal} |
| 97 | + </PendingText> |
| 98 | + </Box> |
| 99 | + <Box mb={2}> |
| 100 | + Total Supply:{' '} |
| 101 | + <PendingText fontWeight='600' isLoading={loadingTotalSupply}> |
| 102 | + {formatBalance(totalSupply, network)} |
| 103 | + </PendingText> |
| 104 | + </Box> |
| 105 | + <Divider my={4} /> |
| 106 | + <Box> |
| 107 | + My Balance:{' '} |
| 108 | + {connectionStatus === 'connected' ? ( |
| 109 | + <PendingText fontWeight='600' isLoading={loadingBalance}> |
| 110 | + {formatBalance(myBalance, network)} |
| 111 | + </PendingText> |
| 112 | + ) : ( |
| 113 | + <ConnectButton /> |
| 114 | + )} |
| 115 | + </Box> |
| 116 | + {connectedAccount && ( |
| 117 | + <Box mt={4}> |
| 118 | + <Button size='sm' onClick={mintNewToken} isLoading={mintTx.inBestBlockProgress}> |
| 119 | + Mint 100 {tokenSymbol} |
| 120 | + </Button> |
| 121 | + </Box> |
| 122 | + )} |
| 123 | + </Box> |
| 124 | + </Box> |
| 125 | + ); |
| 126 | +} |
0 commit comments