mirror of
https://github.com/status-im/dappconnect-vote-poll-sdk.git
synced 2025-01-11 13:54:40 +00:00
Introduce PollInitMsg model (#4)
This commit is contained in:
parent
67178f0de3
commit
624f126f72
@ -22,6 +22,7 @@
|
||||
"@typescript-eslint/parser": "^4.29.0",
|
||||
"chai": "^4.3.4",
|
||||
"eslint": "^7.32.0",
|
||||
"ethereum-waffle": "^3.4.0",
|
||||
"jsdom": "^16.7.0",
|
||||
"jsdom-global": "^3.0.2",
|
||||
"mocha": "^9.0.3",
|
||||
|
75
packages/core/src/models/PollInitMsg.ts
Normal file
75
packages/core/src/models/PollInitMsg.ts
Normal file
@ -0,0 +1,75 @@
|
||||
import { PollType } from '../types/PollType'
|
||||
import { BigNumber, utils } from 'ethers'
|
||||
import { JsonRpcSigner } from '@ethersproject/providers'
|
||||
|
||||
export class PollInitMsg {
|
||||
public owner: string
|
||||
public timestamp: number
|
||||
public question: string
|
||||
public answers: string[]
|
||||
public pollType: PollType
|
||||
public minToken?: BigNumber
|
||||
public endTime: number
|
||||
public signature: string
|
||||
|
||||
private constructor(
|
||||
owner: string,
|
||||
signature: string,
|
||||
timestamp: number,
|
||||
question: string,
|
||||
answers: string[],
|
||||
pollType: PollType,
|
||||
endTime: number,
|
||||
minToken?: BigNumber
|
||||
) {
|
||||
this.owner = owner
|
||||
this.timestamp = timestamp
|
||||
this.question = question
|
||||
this.answers = answers
|
||||
this.pollType = pollType
|
||||
this.minToken = minToken
|
||||
this.endTime = endTime
|
||||
|
||||
this.signature = signature
|
||||
}
|
||||
|
||||
static async create(
|
||||
signer: JsonRpcSigner,
|
||||
question: string,
|
||||
answers: string[],
|
||||
pollType: PollType,
|
||||
minToken?: BigNumber,
|
||||
endTime?: number
|
||||
): Promise<PollInitMsg> {
|
||||
const owner = await signer.getAddress()
|
||||
const timestamp = Date.now()
|
||||
let newEndTime = timestamp + 10000000
|
||||
if (endTime) {
|
||||
newEndTime = endTime
|
||||
}
|
||||
|
||||
const msg: (string | number | BigNumber | PollType)[] = [
|
||||
owner,
|
||||
timestamp,
|
||||
question,
|
||||
answers.join(),
|
||||
pollType,
|
||||
newEndTime,
|
||||
]
|
||||
const types = ['address', 'uint256', 'string', 'string', 'uint8', 'uint256']
|
||||
if (pollType === PollType.NON_WEIGHTED) {
|
||||
if (minToken) {
|
||||
msg.push(minToken)
|
||||
} else {
|
||||
msg.push(BigNumber.from(1))
|
||||
minToken = BigNumber.from(1)
|
||||
}
|
||||
types.push('uint256')
|
||||
}
|
||||
|
||||
const packedData = utils.arrayify(utils.solidityPack(types, msg))
|
||||
const signature = await signer.signMessage(packedData)
|
||||
|
||||
return new PollInitMsg(owner, signature, timestamp, question, answers, pollType, newEndTime, minToken)
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
import { BigNumber } from 'ethers'
|
||||
|
||||
export enum PollType {
|
||||
WEIGHTED = 0,
|
||||
NON_WEIGHTED = 1,
|
||||
}
|
||||
|
||||
export type PollInitMsg = {
|
||||
owner: string
|
||||
timestamp: number
|
||||
question: string
|
||||
answers: string[]
|
||||
pollType: PollType
|
||||
minToken?: BigNumber
|
||||
endTime: number
|
||||
signature: string
|
||||
}
|
4
packages/core/src/types/PollType.ts
Normal file
4
packages/core/src/types/PollType.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export enum PollType {
|
||||
WEIGHTED = 0,
|
||||
NON_WEIGHTED = 1,
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
import protons, { PollInit } from 'protons'
|
||||
import { PollInitMsg, PollType } from '../../types/PollInit'
|
||||
import { PollType } from '../../types/PollType'
|
||||
import { utils, BigNumber } from 'ethers'
|
||||
import { PollInitMsg } from '../../models/PollInitMsg'
|
||||
|
||||
const proto = protons(`
|
||||
message PollInit {
|
||||
|
101
packages/core/test/models/PollInitMsg.test.ts
Normal file
101
packages/core/test/models/PollInitMsg.test.ts
Normal file
@ -0,0 +1,101 @@
|
||||
import { expect } from 'chai'
|
||||
import { PollInitMsg } from '../../src/models/PollInitMsg'
|
||||
import { MockProvider } from 'ethereum-waffle'
|
||||
import { PollType } from '../../src/types/PollType'
|
||||
import { JsonRpcSigner } from '@ethersproject/providers'
|
||||
import { BigNumber, utils } from 'ethers'
|
||||
|
||||
describe('PollInitMsg', () => {
|
||||
const provider = new MockProvider()
|
||||
const [alice] = provider.getWallets()
|
||||
|
||||
it('success', async () => {
|
||||
const poll = await PollInitMsg.create(
|
||||
alice as unknown as JsonRpcSigner,
|
||||
'test',
|
||||
['one', 'two', 'three'],
|
||||
PollType.WEIGHTED
|
||||
)
|
||||
|
||||
expect(poll).to.not.be.undefined
|
||||
expect(poll.owner).to.eq(alice.address)
|
||||
expect(poll.endTime).to.eq(poll.timestamp + 10000000)
|
||||
expect(poll.answers).to.deep.eq(['one', 'two', 'three'])
|
||||
expect(poll.minToken).to.be.undefined
|
||||
expect(poll.pollType).to.eq(PollType.WEIGHTED)
|
||||
expect(poll.question).to.eq('test')
|
||||
|
||||
const types = ['address', 'uint256', 'string', 'string', 'uint8', 'uint256']
|
||||
const msg = [poll.owner, poll.timestamp, poll.question, poll.answers.join(), poll.pollType, poll.endTime]
|
||||
const packedData = utils.arrayify(utils.solidityPack(types, msg))
|
||||
|
||||
const verifiedAddress = utils.verifyMessage(packedData, poll.signature)
|
||||
expect(verifiedAddress).to.eq(alice.address)
|
||||
})
|
||||
|
||||
it('success NON_WEIGHTED', async () => {
|
||||
const poll = await PollInitMsg.create(
|
||||
alice as unknown as JsonRpcSigner,
|
||||
'test',
|
||||
['one', 'two', 'three'],
|
||||
PollType.NON_WEIGHTED,
|
||||
BigNumber.from(123)
|
||||
)
|
||||
|
||||
expect(poll?.minToken?.toNumber()).to.eq(123)
|
||||
|
||||
const types = ['address', 'uint256', 'string', 'string', 'uint8', 'uint256', 'uint256']
|
||||
const msg = [
|
||||
poll.owner,
|
||||
poll.timestamp,
|
||||
poll.question,
|
||||
poll.answers.join(),
|
||||
poll.pollType,
|
||||
poll.endTime,
|
||||
poll.minToken,
|
||||
]
|
||||
const packedData = utils.arrayify(utils.solidityPack(types, msg))
|
||||
|
||||
const verifiedAddress = utils.verifyMessage(packedData, poll.signature)
|
||||
expect(verifiedAddress).to.eq(alice.address)
|
||||
})
|
||||
|
||||
it('NON_WEIGHTED no minToken', async () => {
|
||||
const poll = await PollInitMsg.create(
|
||||
alice as unknown as JsonRpcSigner,
|
||||
'test',
|
||||
['one', 'two', 'three'],
|
||||
PollType.NON_WEIGHTED
|
||||
)
|
||||
|
||||
expect(poll?.minToken?.toNumber()).to.eq(1)
|
||||
|
||||
const types = ['address', 'uint256', 'string', 'string', 'uint8', 'uint256', 'uint256']
|
||||
const msg = [
|
||||
poll.owner,
|
||||
poll.timestamp,
|
||||
poll.question,
|
||||
poll.answers.join(),
|
||||
poll.pollType,
|
||||
poll.endTime,
|
||||
poll.minToken,
|
||||
]
|
||||
const packedData = utils.arrayify(utils.solidityPack(types, msg))
|
||||
|
||||
const verifiedAddress = utils.verifyMessage(packedData, poll.signature)
|
||||
expect(verifiedAddress).to.eq(alice.address)
|
||||
})
|
||||
|
||||
it('specific end time', async () => {
|
||||
const poll = await PollInitMsg.create(
|
||||
alice as unknown as JsonRpcSigner,
|
||||
'test',
|
||||
['one', 'two', 'three'],
|
||||
PollType.NON_WEIGHTED,
|
||||
undefined,
|
||||
100
|
||||
)
|
||||
|
||||
expect(poll?.endTime).to.eq(100)
|
||||
})
|
||||
})
|
@ -1,7 +1,8 @@
|
||||
import { expect } from 'chai'
|
||||
import { PollInitMsg, PollType } from '../../../src/types/PollInit'
|
||||
import { PollType } from '../../../src/types/PollType'
|
||||
import PollInit from '../../../src/utils/proto/PollInit'
|
||||
import { BigNumber } from 'ethers'
|
||||
import { PollInitMsg } from '../../../src/models/PollInitMsg'
|
||||
|
||||
describe('PollInit', () => {
|
||||
it('success', async () => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user