/ packages / DApp / src / hooks / useTypedVote.ts
useTypedVote.ts
 1  import { useEthers } from '@usedapp/core'
 2  import { useCallback } from 'react'
 3  import { useContracts } from './useContracts'
 4  import { BigNumber } from 'ethers'
 5  import { TypedVote } from '../models/TypedData'
 6  
 7  export function useTypedVote() {
 8    const { chainId } = useEthers()
 9    const { votingContract } = useContracts()
10  
11    const getTypedVote = useCallback(
12      (data: [string, BigNumber, BigNumber, BigNumber]) => {
13        return {
14          types: {
15            EIP712Domain: [
16              { name: 'name', type: 'string' },
17              { name: 'version', type: 'string' },
18              { name: 'chainId', type: 'uint256' },
19              { name: 'verifyingContract', type: 'address' },
20            ],
21            Vote: [
22              { name: 'roomIdAndType', type: 'uint256' },
23              { name: 'sntAmount', type: 'uint256' },
24              { name: 'voter', type: 'address' },
25              { name: 'timestamp', type: 'uint256' },
26            ],
27          },
28          primaryType: 'Vote',
29          domain: {
30            name: 'Voting Contract',
31            version: '1',
32            chainId: chainId,
33            verifyingContract: votingContract.address,
34          },
35          message: {
36            roomIdAndType: data[1].toHexString(),
37            sntAmount: data[2].toHexString(),
38            voter: data[0],
39            timestamp: data[3].toHexString(),
40          },
41        } as TypedVote
42      },
43      [chainId, votingContract.address],
44    )
45    return { getTypedVote }
46  }