useSendWakuVote.ts
1 import { useCallback } from 'react' 2 import { useWaku } from '../providers/waku/provider' 3 import { useEthers, useSigner } from '@usedapp/core' 4 import { config } from '../config' 5 import { createWakuVote } from '../helpers/wakuVote' 6 import { useTypedVote } from './useTypedVote' 7 import { createEncoder } from '@waku/core' 8 9 const { clusterId, shards } = config.wakuConfig 10 11 export function useSendWakuVote() { 12 const { waku } = useWaku() 13 const signer = useSigner() 14 const { account } = useEthers() 15 const { getTypedVote } = useTypedVote() 16 17 const sendWakuVote = useCallback( 18 async (voteAmount: number, room: number, type: number) => { 19 const timestamp = Math.floor(Date.now() / 1000) 20 const msg = await createWakuVote(account, signer, room, voteAmount, type, timestamp, getTypedVote) 21 if (msg) { 22 if (waku) { 23 await Promise.allSettled( 24 shards.map((shardId) => 25 waku.lightPush.send( 26 createEncoder({ 27 contentTopic: config.wakuConfig.wakuTopic + room.toString(), 28 routingInfo: { pubsubTopic: `/waku/2/rs/${clusterId}/${shardId}`, clusterId, shardId }, 29 }), 30 { payload: msg }, 31 ), 32 ), 33 ) 34 } else { 35 alert('error sending vote please try again') 36 } 37 } 38 }, 39 [waku, signer, account, getTypedVote], 40 ) 41 42 return sendWakuVote 43 }