From 2251e106f1a3b829a636ec4a9413f40de63cbded Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Mon, 10 Jan 2022 17:09:08 +1100 Subject: [PATCH] Rename `roomIdAndType` to `ProposalidAndType` everywhere Impacts the EIP712 format and hash. --- packages/contracts/README.md | 6 +++--- packages/contracts/contracts/VotingContract.sol | 3 ++- packages/contracts/test/1.votingContract.test.ts | 16 ++++++++-------- packages/core/src/models/VoteMsg.ts | 8 ++++---- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/packages/contracts/README.md b/packages/contracts/README.md index 56b4a2d..a6919e8 100644 --- a/packages/contracts/README.md +++ b/packages/contracts/README.md @@ -104,7 +104,7 @@ The structure of typed data for a vote message is as follows: { name: 'verifyingContract', type: 'address' }, ], Vote: [ - { name: 'roomIdAndType', type: 'uint256' }, + { name: 'proposalIdAndType', type: 'uint256' }, { name: 'tokenAmount', type: 'uint256' }, { name: 'voter', type: 'address' }, ], @@ -119,7 +119,7 @@ The structure of typed data for a vote message is as follows: message: { voter: voterAddress, tokenAmount: tokenAmount, - roomIdAndType: roomIdAndType + proposalIdAndType: proposalIdAndType } } ``` @@ -174,7 +174,7 @@ For more information about EIP-712 go to [docs](https://eips.ethereum.org/EIPS/e Cast a single vote. Updates `totalVotes` amount of proposal with index corresponding to `proposalId`. - If voting first bit of `vote.roomIdAndType` is 1 that means that vote is for and `vote.tokenAmount` is added to `votingRooms[roomId].totalVotesFor`, otherwise if `vote.roomIdAndType` is 0 `vote.tokenAmount` is added to `votingRooms[roomId].totalVotesAgainst`. + If voting first bit of `vote.proposalIdAndType` is 1 that means that vote is for and `vote.tokenAmount` is added to `votingRooms[roomId].totalVotesFor`, otherwise if `vote.proposalIdAndType` is 0 `vote.tokenAmount` is added to `votingRooms[roomId].totalVotesAgainst`. After that add new address to room `voters` and updates mapping `voted` accordingly. diff --git a/packages/contracts/contracts/VotingContract.sol b/packages/contracts/contracts/VotingContract.sol index 1c48632..f942775 100644 --- a/packages/contracts/contracts/VotingContract.sol +++ b/packages/contracts/contracts/VotingContract.sol @@ -14,7 +14,8 @@ contract VotingContract { bytes32 private constant EIP712DOMAIN_TYPEHASH = keccak256('EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)'); - bytes32 private constant VOTE_TYPEHASH = keccak256('Vote(uint256 roomIdAndType,uint256 tokenAmount,address voter)'); + bytes32 private constant VOTE_TYPEHASH = + keccak256('Vote(uint256 proposalIdAndType,uint256 tokenAmount,address voter)'); bytes32 private DOMAIN_SEPARATOR; struct Proposal { diff --git a/packages/contracts/test/1.votingContract.test.ts b/packages/contracts/test/1.votingContract.test.ts index a1c0229..f19b197 100644 --- a/packages/contracts/test/1.votingContract.test.ts +++ b/packages/contracts/test/1.votingContract.test.ts @@ -26,7 +26,7 @@ const typedData = { { name: 'verifyingContract', type: 'address' }, ], Vote: [ - { name: 'roomIdAndType', type: 'uint256' }, + { name: 'proposalIdAndType', type: 'uint256' }, { name: 'tokenAmount', type: 'uint256' }, { name: 'voter', type: 'address' }, ], @@ -69,7 +69,7 @@ const getSignedMessages = async ( const signedMessages = messages.map((msg, idx) => { const t: TypedMessage = { ...typedData, - message: { roomIdAndType: msg[1].toHexString(), tokenAmount: msg[2].toHexString(), voter: msg[0] }, + message: { proposalIdAndType: msg[1].toHexString(), tokenAmount: msg[2].toHexString(), voter: msg[0] }, } const sig = utils.splitSignature( signTypedMessage(Buffer.from(utils.arrayify(votes[idx].voter.privateKey)), { data: t }, 'V3') @@ -120,9 +120,9 @@ describe('Contract', () => { it('no tokens address', async () => { const { contract, noTokensAddress } = await loadFixture(fixture) const noTokensContract = contract.connect(noTokensAddress) - await expect( - noTokensContract.initializeProposal('test', 'short desc', BigNumber.from(10)) - ).to.be.revertedWith('sender does not have the amount of token they voted for') + await expect(noTokensContract.initializeProposal('test', 'short desc', BigNumber.from(10))).to.be.revertedWith( + 'sender does not have the amount of token they voted for' + ) }) it("can't start voting with 0 tokens", async () => { @@ -356,7 +356,7 @@ describe('Contract', () => { const msg = [firstAddress.address, BigNumber.from(0).mul(2).add(1), BigNumber.from(100000000000)] const t: TypedMessage = { ...typedData, - message: { roomIdAndType: msg[1].toHexString(), tokenAmount: msg[2].toHexString(), voter: msg[0] }, + message: { proposalIdAndType: msg[1].toHexString(), tokenAmount: msg[2].toHexString(), voter: msg[0] }, } const sig = utils.splitSignature( signTypedMessage(Buffer.from(utils.arrayify(firstAddress.privateKey)), { data: t }, 'V3') @@ -429,7 +429,7 @@ describe('Contract', () => { messages.map(async (msg) => { const t: TypedMessage = { ...typedData, - message: { roomIdAndType: msg[1].toHexString(), tokenAmount: msg[2].toHexString(), voter: msg[0] }, + message: { proposalIdAndType: msg[1].toHexString(), tokenAmount: msg[2].toHexString(), voter: msg[0] }, } const sig = utils.splitSignature( signTypedMessage(Buffer.from(utils.arrayify(firstAddress.privateKey)), { data: t }, 'V3') @@ -448,7 +448,7 @@ describe('Contract', () => { const correctMessageData: TypedMessage = { ...domainSeparator, message: { - roomIdAndType: correctMessageParams[1].toHexString(), + proposalIdAndType: correctMessageParams[1].toHexString(), tokenAmount: correctMessageParams[2].toHexString(), voter: correctMessageParams[0], }, diff --git a/packages/core/src/models/VoteMsg.ts b/packages/core/src/models/VoteMsg.ts index 55e42f9..4506e92 100644 --- a/packages/core/src/models/VoteMsg.ts +++ b/packages/core/src/models/VoteMsg.ts @@ -18,7 +18,7 @@ message Vote { `) type Message = { - roomIdAndType: string + proposalIdAndType: string tokenAmount: string voter: string } @@ -43,7 +43,7 @@ export function createSignMsgParams(message: Message, chainId: number, verifying { name: 'verifyingContract', type: 'address' }, ], Vote: [ - { name: 'roomIdAndType', type: 'uint256' }, + { name: 'proposalIdAndType', type: 'uint256' }, { name: 'tokenAmount', type: 'uint256' }, { name: 'voter', type: 'address' }, ], @@ -92,7 +92,7 @@ export class VoteMsg { const signFunction = createSignFunction(signer) const voter = await signer.getAddress() const msg = { - roomIdAndType: BigNumber.from(roomId).mul(2).add(answer).toHexString(), + proposalIdAndType: BigNumber.from(roomId).mul(2).add(answer).toHexString(), tokenAmount: tokenAmount.toHexString(), voter, } @@ -136,7 +136,7 @@ export class VoteMsg { const signature = utils.hexlify(payload.signature) const msg = { - roomIdAndType: BigNumber.from(payload.roomId).mul(2).add(payload.answer).toHexString(), + proposalIdAndType: BigNumber.from(payload.roomId).mul(2).add(payload.answer).toHexString(), tokenAmount: utils.hexlify(payload.tokenAmount), voter: utils.getAddress(utils.hexlify(payload.voter)), }