This repository has been archived by the owner on Jan 18, 2023. It is now read-only.
/
coreFactory.spec.ts
145 lines (118 loc) Β· 4.47 KB
/
coreFactory.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
require('module-alias/register');
import * as ABIDecoder from 'abi-decoder';
import * as chai from 'chai';
import * as setProtocolUtils from 'set-protocol-utils';
import { BigNumber } from 'bignumber.js';
import { Address, Log, Bytes } from 'set-protocol-utils';
import ChaiSetup from '@utils/chaiSetup';
import { BigNumberSetup } from '@utils/bigNumberSetup';
import { CoreContract, SetTokenFactoryContract, StandardTokenMockContract } from '@utils/contracts';
import { expectRevertError } from '@utils/tokenAssertions';
import { Blockchain } from '@utils/blockchain';
import { extractNewSetTokenAddressFromLogs, SetTokenCreated } from '@utils/contract_logs/core';
import { ONE } from '@utils/constants';
import { CoreWrapper } from '@utils/coreWrapper';
import { ERC20Wrapper } from '@utils/erc20Wrapper';
import { getWeb3 } from '@utils/web3Helper';
BigNumberSetup.configure();
ChaiSetup.configure();
const web3 = getWeb3();
const Core = artifacts.require('Core');
const { SetProtocolTestUtils: SetTestUtils, SetProtocolUtils: SetUtils } = setProtocolUtils;
const setTestUtils = new SetTestUtils(web3);
const { expect } = chai;
const blockchain = new Blockchain(web3);
const { NULL_ADDRESS } = SetUtils.CONSTANTS;
contract('CoreFactory', accounts => {
const [
ownerAccount,
] = accounts;
let core: CoreContract;
let setTokenFactory: SetTokenFactoryContract;
const coreWrapper = new CoreWrapper(ownerAccount, ownerAccount);
const erc20Wrapper = new ERC20Wrapper(ownerAccount);
before(async () => {
ABIDecoder.addABI(Core.abi);
});
after(async () => {
ABIDecoder.removeABI(Core.abi);
});
beforeEach(async () => {
await blockchain.saveSnapshotAsync();
core = await coreWrapper.deployCoreAndDependenciesAsync();
setTokenFactory = await coreWrapper.deploySetTokenFactoryAsync(core.address);
await coreWrapper.enableFactoryAsync(core, setTokenFactory);
});
afterEach(async () => {
await blockchain.revertAsync();
});
describe('#create', async () => {
let factoryAddress: Address;
let components: Address[];
let mockToken: StandardTokenMockContract;
const units: BigNumber[] = [ONE];
const naturalUnit: BigNumber = ONE;
const asciiSubjectName: string = 'Set Token';
const asciiSubjectSymbol: string = 'SET';
const subjectName: Bytes = SetUtils.stringToBytes(asciiSubjectName);
const subjectSymbol: Bytes = SetUtils.stringToBytes(asciiSubjectSymbol);
const subjectCallData: Bytes = '0x0';
beforeEach(async () => {
mockToken = await erc20Wrapper.deployTokenAsync(ownerAccount);
factoryAddress = setTokenFactory.address;
components = [mockToken.address];
});
async function subject(): Promise<string> {
return core.create.sendTransactionAsync(
factoryAddress,
components,
units,
naturalUnit,
subjectName,
subjectSymbol,
subjectCallData,
{ from: ownerAccount },
);
}
it('creates a new SetToken and tracks it in mapping', async () => {
const txHash = await subject();
const logs = await setTestUtils.getLogsFromTxHash(txHash);
const newSetTokenAddress = extractNewSetTokenAddressFromLogs(logs);
const isSetTokenValid = await core.validSets.callAsync(newSetTokenAddress);
expect(isSetTokenValid).to.be.true;
});
it('creates a new SetToken and adds to setTokens array', async () => {
const txHash = await subject();
const logs = await setTestUtils.getLogsFromTxHash(txHash);
const newSetTokenAddress = extractNewSetTokenAddressFromLogs(logs);
const setTokens = await core.setTokens.callAsync();
expect(setTokens).to.include(newSetTokenAddress);
});
it('emits a SetTokenCreated event', async () => {
const txHash = await subject();
const logs = await setTestUtils.getLogsFromTxHash(txHash);
const newSetTokenAddress = extractNewSetTokenAddressFromLogs(logs);
const expectedLogs: Log[] = [
SetTokenCreated(
core.address,
newSetTokenAddress,
factoryAddress,
components,
units,
naturalUnit,
subjectName,
subjectSymbol,
),
];
await SetTestUtils.assertLogEquivalence(logs, expectedLogs);
});
describe('when the factory is not valid', async () => {
beforeEach(async () => {
factoryAddress = NULL_ADDRESS;
});
it('should revert', async () => {
await expectRevertError(subject());
});
});
});
});