Rename `roomIdAndType` to `ProposalidAndType` everywhere

Impacts the EIP712 format and hash.
This commit is contained in:
Franck Royer 2022-01-10 17:09:08 +11:00
parent 727276e721
commit 2251e106f1
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
4 changed files with 17 additions and 16 deletions

View File

@ -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.

View File

@ -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 {

View File

@ -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<MessageTypes> = {
...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<MessageTypes> = {
...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<MessageTypes> = {
...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<MessageTypes> = {
...domainSeparator,
message: {
roomIdAndType: correctMessageParams[1].toHexString(),
proposalIdAndType: correctMessageParams[1].toHexString(),
tokenAmount: correctMessageParams[2].toHexString(),
voter: correctMessageParams[0],
},

View File

@ -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)),
}