VotingMachineService.ts
1 import { BigNumber, PopulatedTransaction, providers } from 'ethers'; 2 import { splitSignature } from 'ethers/lib/utils'; 3 4 import { VotingMachine__factory } from './typechain/factory/VotingMachine__factory'; 5 6 export interface ProviderWithSend extends providers.Provider { 7 // eslint-disable-next-line @typescript-eslint/no-explicit-any 8 send<P = any, R = any>(method: string, params: Array<P>): Promise<R>; 9 } 10 11 interface VotingBalanceProof { 12 underlyingAsset: string; 13 slot: string; 14 proof: string; 15 } 16 17 export class VotingMachineService { 18 private readonly _interface; 19 20 constructor(private readonly votingMachineContractAddress: string) { 21 this._interface = VotingMachine__factory.createInterface(); 22 } 23 24 generateSubmitVoteTxData = async ( 25 user: string, 26 proposalId: number, 27 support: boolean, 28 votingProofs: VotingBalanceProof[] 29 ) => { 30 const tx: PopulatedTransaction = {}; 31 const txData = this._interface.encodeFunctionData('submitVote', [ 32 proposalId, 33 support, 34 votingProofs, 35 ]); 36 tx.to = this.votingMachineContractAddress; 37 tx.from = user; 38 tx.data = txData; 39 tx.gasLimit = BigNumber.from(1000000); 40 return tx; 41 }; 42 43 generateSubmitVoteBySignatureTxData = async ( 44 user: string, 45 proposalId: number, 46 support: boolean, 47 votingProofs: VotingBalanceProof[], 48 signature: string 49 ) => { 50 const { v, r, s } = splitSignature(signature); 51 const tx: PopulatedTransaction = {}; 52 const txData = this._interface.encodeFunctionData('submitVoteBySignature', [ 53 proposalId, 54 user, 55 support, 56 votingProofs, 57 v, 58 r, 59 s, 60 ]); 61 tx.to = this.votingMachineContractAddress; 62 tx.from = user; 63 tx.data = txData; 64 tx.gasLimit = BigNumber.from(1000000); 65 return tx; 66 }; 67 }