Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/CRISP/client/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
VITE_ENCLAVE_API=http://127.0.0.1:4000
VITE_TWITTER_SERVERLESS_API=
VITE_WALLETCONNECT_PROJECT_ID=
# A token with a public mint function. Also used as the fee token for Enclave.
# This is its default address in Hardhat node
VITE_CRISP_TOKEN=0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0
Comment thread
ctrlc03 marked this conversation as resolved.
11 changes: 11 additions & 0 deletions examples/CRISP/client/src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Logo from '@/assets/icons/logo.svg'
import { Link } from 'react-router-dom'
import NavMenu from '@/components/NavMenu'
import { ConnectKitButton } from 'connectkit'
import useToken from '@/hooks/generic/useMintToken'

const PAGES = [
{
Expand All @@ -22,6 +23,8 @@ const PAGES = [
]

const Navbar: React.FC = () => {
const { mintTokens, isMinting } = useToken()

return (
<nav className='absolute left-0 top-0 z-10 w-screen px-6 lg:px-9'>
<div className='mx-auto max-w-screen-xl'>
Expand All @@ -34,6 +37,14 @@ const Navbar: React.FC = () => {
</Link>

<div className='flex items-center gap-2 md:gap-8'>
<button
disabled={isMinting}
onClick={mintTokens}
className='hover:text-twilight-blue-600 cursor-pointer font-bold text-slate-600 duration-300 ease-in-out hover:opacity-70 max-md:hidden'
>
Mint tokens
</button>

{PAGES.map(({ label, path }) => (
<Link
key={label}
Expand Down
79 changes: 79 additions & 0 deletions examples/CRISP/client/src/hooks/generic/useMintToken.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// SPDX-License-Identifier: LGPL-3.0-only
//
// This file is provided WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.

import { iERC20Abi } from '@/utils/abi'
import { useCallback, useState } from 'react'
import { usePublicClient, useWalletClient } from 'wagmi'
import { getChain } from '@/utils/methods'
import { useNotificationAlertContext } from '@/context/NotificationAlert'
import { ROUND_TOKEN } from '@/utils/constants'

/**
* Hooks to interact with the token contract used to gatekeep access to voting rounds
*/
const useToken = () => {
const { showToast } = useNotificationAlertContext()
const [isMinting, setIsMinting] = useState<boolean>(false)

const { data: walletClient } = useWalletClient()
const publicClient = usePublicClient()

const mintTokens = useCallback(async () => {
if (!walletClient || !publicClient) {
showToast({
type: 'danger',
message: 'Wallet not connected',
})
return
}

const balance = await publicClient.readContract({
abi: iERC20Abi,
address: ROUND_TOKEN,
functionName: 'balanceOf',
args: [walletClient.account.address],
})

if (balance && balance > BigInt(0)) {
showToast({
type: 'info',
message: 'You already have tokens, no need to mint more',
})
return
}
Comment thread
ctrlc03 marked this conversation as resolved.

setIsMinting(true)

try {
await walletClient.writeContract({
abi: iERC20Abi,
address: ROUND_TOKEN,
functionName: 'mint',
args: [walletClient.account.address, BigInt(1 * 1e18)],
chain: getChain(),
})
showToast({
type: 'success',
message: 'Tokens minted successfully',
})
} catch (error) {
console.log(error)
showToast({
type: 'danger',
message: 'Error minting tokens',
})
} finally {
setIsMinting(false)
}
}, [walletClient, showToast, publicClient])

return {
isMinting,
mintTokens,
}
}

export default useToken
12 changes: 12 additions & 0 deletions examples/CRISP/client/src/utils/abi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: LGPL-3.0-only
//
// This file is provided WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.

import { parseAbi } from 'viem'

export const iERC20Abi = parseAbi([
'function balanceOf(address owner) view returns (uint256)',
'function mint(address to, uint256 amount) external',
])
7 changes: 7 additions & 0 deletions examples/CRISP/client/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: LGPL-3.0-only
//
// This file is provided WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.

export const ROUND_TOKEN = import.meta.env.VITE_CRISP_TOKEN
2 changes: 1 addition & 1 deletion examples/CRISP/server/src/cli/approve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub async fn approve_token(
amount: U256,
) -> Result<()> {
use alloy::network::EthereumWallet;
use alloy::providers::{Provider, ProviderBuilder};
use alloy::providers::{ProviderBuilder};
use alloy::signers::local::PrivateKeySigner;

let token_address: Address = token_address.parse()?;
Expand Down
Loading