2021-08-09 14:00:42 +00:00
|
|
|
import { expect } from 'chai'
|
2021-08-16 14:11:22 +00:00
|
|
|
import { PollInitMsg } from '../../src/models/PollInitMsg'
|
2021-08-09 14:00:42 +00:00
|
|
|
import { MockProvider } from 'ethereum-waffle'
|
|
|
|
import { PollType } from '../../src/types/PollType'
|
2021-08-12 14:37:21 +00:00
|
|
|
import { BigNumber } from 'ethers'
|
2021-08-09 14:00:42 +00:00
|
|
|
|
|
|
|
describe('PollInitMsg', () => {
|
|
|
|
const provider = new MockProvider()
|
|
|
|
const [alice] = provider.getWallets()
|
2021-09-03 07:45:09 +00:00
|
|
|
describe('create', () => {
|
|
|
|
it('success', async () => {
|
|
|
|
const poll = await PollInitMsg._createWithSignFunction(
|
2021-09-03 12:41:29 +00:00
|
|
|
async () => '0x01',
|
2021-09-03 07:45:09 +00:00
|
|
|
alice,
|
|
|
|
'test',
|
|
|
|
['one', 'two', 'three'],
|
2021-09-03 12:41:29 +00:00
|
|
|
PollType.WEIGHTED,
|
|
|
|
0
|
2021-09-03 07:45:09 +00:00
|
|
|
)
|
2021-08-09 14:00:42 +00:00
|
|
|
|
2021-09-03 07:45:09 +00:00
|
|
|
expect(poll).to.not.be.undefined
|
|
|
|
if (poll) {
|
|
|
|
expect(poll.owner).to.eq(alice.address)
|
|
|
|
expect(poll.endTime).to.eq(poll.timestamp + 100000000)
|
|
|
|
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')
|
2021-08-09 14:00:42 +00:00
|
|
|
|
2021-09-03 07:45:09 +00:00
|
|
|
expect(poll.signature).to.eq('0x01')
|
|
|
|
}
|
|
|
|
})
|
2021-08-09 14:00:42 +00:00
|
|
|
|
2021-09-03 07:45:09 +00:00
|
|
|
it('success NON_WEIGHTED', async () => {
|
|
|
|
const poll = await PollInitMsg._createWithSignFunction(
|
2021-09-03 12:41:29 +00:00
|
|
|
async () => '0x01',
|
2021-09-03 07:45:09 +00:00
|
|
|
alice,
|
|
|
|
'test',
|
|
|
|
['one', 'two', 'three'],
|
|
|
|
PollType.NON_WEIGHTED,
|
2021-09-03 12:41:29 +00:00
|
|
|
0,
|
2021-09-03 07:45:09 +00:00
|
|
|
BigNumber.from(123)
|
|
|
|
)
|
|
|
|
expect(poll).to.not.be.undefined
|
|
|
|
expect(poll?.minToken?.toNumber()).to.eq(123)
|
|
|
|
if (poll) {
|
|
|
|
expect(poll.signature).to.eq('0x01')
|
|
|
|
}
|
|
|
|
})
|
2021-08-09 14:00:42 +00:00
|
|
|
|
2021-09-03 07:45:09 +00:00
|
|
|
it('NON_WEIGHTED no minToken', async () => {
|
|
|
|
const poll = await PollInitMsg._createWithSignFunction(
|
2021-09-03 12:41:29 +00:00
|
|
|
async () => '0x01',
|
2021-09-03 07:45:09 +00:00
|
|
|
alice,
|
|
|
|
'test',
|
|
|
|
['one', 'two', 'three'],
|
2021-09-03 12:41:29 +00:00
|
|
|
PollType.NON_WEIGHTED,
|
|
|
|
0
|
2021-09-03 07:45:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(poll?.minToken?.toNumber()).to.eq(1)
|
|
|
|
expect(poll).to.not.be.undefined
|
|
|
|
if (poll) {
|
|
|
|
expect(poll.signature).to.eq('0x01')
|
|
|
|
}
|
|
|
|
})
|
2021-08-09 14:00:42 +00:00
|
|
|
|
2021-09-03 07:45:09 +00:00
|
|
|
it('specific end time', async () => {
|
|
|
|
const poll = await PollInitMsg._createWithSignFunction(
|
2021-09-03 12:41:29 +00:00
|
|
|
async () => '0x01',
|
2021-09-03 07:45:09 +00:00
|
|
|
alice,
|
|
|
|
'test',
|
|
|
|
['one', 'two', 'three'],
|
|
|
|
PollType.NON_WEIGHTED,
|
2021-09-03 12:41:29 +00:00
|
|
|
0,
|
2021-09-03 07:45:09 +00:00
|
|
|
undefined,
|
|
|
|
100
|
|
|
|
)
|
2021-08-09 14:00:42 +00:00
|
|
|
|
2021-09-03 07:45:09 +00:00
|
|
|
expect(poll?.endTime).to.eq(100)
|
|
|
|
})
|
2021-08-09 14:00:42 +00:00
|
|
|
})
|
2021-09-03 07:45:09 +00:00
|
|
|
describe('decode/encode', () => {
|
|
|
|
it('success', async () => {
|
|
|
|
const data = await PollInitMsg._createWithSignFunction(
|
2021-09-03 12:41:29 +00:00
|
|
|
async () => '0x01',
|
2021-09-03 07:45:09 +00:00
|
|
|
alice,
|
|
|
|
'whats up',
|
|
|
|
['ab', 'cd', 'ef'],
|
2021-09-03 12:41:29 +00:00
|
|
|
PollType.WEIGHTED,
|
|
|
|
0
|
2021-09-03 07:45:09 +00:00
|
|
|
)
|
|
|
|
expect(data).to.not.be.undefined
|
|
|
|
if (data) {
|
|
|
|
const payload = data.encode()
|
|
|
|
expect(payload).to.not.be.undefined
|
|
|
|
if (payload) {
|
2021-09-03 12:41:29 +00:00
|
|
|
expect(PollInitMsg.decode(payload, new Date(data.timestamp), 0, () => true)).to.deep.eq(data)
|
2021-09-03 07:45:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('random decode', async () => {
|
2021-09-03 12:41:29 +00:00
|
|
|
expect(PollInitMsg.decode(new Uint8Array([12, 12, 3, 32, 31, 212, 31, 32, 23]), new Date(10), 0)).to.be.undefined
|
2021-09-03 07:45:09 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('NON_WEIGHTED init', async () => {
|
|
|
|
const data = await PollInitMsg._createWithSignFunction(
|
2021-09-03 12:41:29 +00:00
|
|
|
async () => '0x01',
|
2021-09-03 07:45:09 +00:00
|
|
|
alice,
|
|
|
|
'whats up',
|
|
|
|
['ab', 'cd', 'ef'],
|
|
|
|
PollType.NON_WEIGHTED,
|
2021-09-03 12:41:29 +00:00
|
|
|
0,
|
2021-09-03 07:45:09 +00:00
|
|
|
BigNumber.from(10)
|
|
|
|
)
|
|
|
|
expect(data).to.not.be.undefined
|
|
|
|
if (data) {
|
|
|
|
const payload = data.encode()
|
|
|
|
expect(payload).to.not.be.undefined
|
|
|
|
if (payload) {
|
2021-09-03 12:41:29 +00:00
|
|
|
expect(PollInitMsg.decode(payload, new Date(data.timestamp), 0, () => true)).to.deep.eq(data)
|
2021-09-03 07:45:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-08-09 14:00:42 +00:00
|
|
|
|
2021-09-03 07:45:09 +00:00
|
|
|
it('NON_WEIGHTED no min token', async () => {
|
|
|
|
const data = await PollInitMsg._createWithSignFunction(
|
2021-09-03 12:41:29 +00:00
|
|
|
async () => '0x01',
|
2021-09-03 07:45:09 +00:00
|
|
|
alice,
|
|
|
|
'whats up',
|
|
|
|
['ab', 'cd', 'ef'],
|
2021-09-03 12:41:29 +00:00
|
|
|
PollType.NON_WEIGHTED,
|
|
|
|
0
|
2021-09-03 07:45:09 +00:00
|
|
|
)
|
|
|
|
expect(data).to.not.be.undefined
|
|
|
|
if (data) {
|
|
|
|
const payload = data.encode()
|
2021-08-09 14:00:42 +00:00
|
|
|
|
2021-09-03 07:45:09 +00:00
|
|
|
expect(payload).to.not.be.undefined
|
|
|
|
if (payload) {
|
2021-09-03 12:41:29 +00:00
|
|
|
expect(PollInitMsg.decode(payload, new Date(data.timestamp), 0, () => true)).to.deep.eq({
|
2021-09-03 07:45:09 +00:00
|
|
|
...data,
|
|
|
|
minToken: BigNumber.from(1),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-08-09 14:00:42 +00:00
|
|
|
})
|
|
|
|
})
|