78 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-08-09 14:53:39 +02:00
import { expect } from 'chai'
2021-08-09 16:00:42 +02:00
import { PollType } from '../../../src/types/PollType'
2021-08-09 14:53:39 +02:00
import PollInit from '../../../src/utils/proto/PollInit'
import { BigNumber } from 'ethers'
2021-08-09 16:00:42 +02:00
import { PollInitMsg } from '../../../src/models/PollInitMsg'
2021-08-10 14:55:47 +02:00
import { MockProvider } from 'ethereum-waffle'
2021-08-09 14:53:39 +02:00
describe('PollInit', () => {
2021-08-10 14:55:47 +02:00
const provider = new MockProvider()
const [alice] = provider.getWallets()
2021-08-09 14:53:39 +02:00
it('success', async () => {
2021-08-12 16:37:21 +02:00
const data = await PollInitMsg._createWithSignFunction(
2021-08-16 09:42:22 +02:00
async (e) => new PollInitMsg('0x01', e),
2021-08-12 16:37:21 +02:00
alice,
'whats up',
['ab', 'cd', 'ef'],
PollType.WEIGHTED
)
expect(data).to.not.be.undefined
if (data) {
const payload = PollInit.encode(data)
expect(payload).to.not.be.undefined
if (payload) {
2021-08-16 09:42:22 +02:00
expect(PollInit.decode(payload, new Date(data.timestamp), () => alice.address)).to.deep.eq(data)
2021-08-12 16:37:21 +02:00
}
2021-08-09 14:53:39 +02:00
}
})
it('random decode', async () => {
2021-08-10 14:55:47 +02:00
expect(PollInit.decode(new Uint8Array([12, 12, 3, 32, 31, 212, 31, 32, 23]), new Date(10))).to.be.undefined
2021-08-09 14:53:39 +02:00
})
it('random data', async () => {
expect(PollInit.encode({ sadf: '0x0' } as unknown as PollInitMsg)).to.be.undefined
})
it('NON_WEIGHTED init', async () => {
2021-08-12 16:37:21 +02:00
const data = await PollInitMsg._createWithSignFunction(
2021-08-16 09:42:22 +02:00
async (e) => new PollInitMsg('0x01', e),
2021-08-10 14:55:47 +02:00
alice,
'whats up',
['ab', 'cd', 'ef'],
PollType.NON_WEIGHTED,
BigNumber.from(10)
)
2021-08-12 16:37:21 +02:00
expect(data).to.not.be.undefined
if (data) {
const payload = PollInit.encode(data)
expect(payload).to.not.be.undefined
if (payload) {
2021-08-16 09:42:22 +02:00
expect(PollInit.decode(payload, new Date(data.timestamp), () => alice.address)).to.deep.eq(data)
2021-08-12 16:37:21 +02:00
}
2021-08-09 14:53:39 +02:00
}
})
it('NON_WEIGHTED no min token', async () => {
2021-08-12 16:37:21 +02:00
const data = await PollInitMsg._createWithSignFunction(
2021-08-16 09:42:22 +02:00
async (e) => new PollInitMsg('0x01', e),
2021-08-12 16:37:21 +02:00
alice,
'whats up',
['ab', 'cd', 'ef'],
PollType.NON_WEIGHTED
)
expect(data).to.not.be.undefined
if (data) {
const payload = PollInit.encode(data)
2021-08-09 14:53:39 +02:00
2021-08-12 16:37:21 +02:00
expect(payload).to.not.be.undefined
if (payload) {
2021-08-16 09:42:22 +02:00
expect(PollInit.decode(payload, new Date(data.timestamp), () => alice.address)).to.deep.eq({
2021-08-12 16:37:21 +02:00
...data,
minToken: BigNumber.from(1),
})
}
2021-08-10 14:55:47 +02:00
}
2021-08-09 14:53:39 +02:00
})
})