This repository has been archived by the owner on Jan 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
takerWalletWrapper.spec.ts
216 lines (174 loc) Β· 8.04 KB
/
takerWalletWrapper.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
require('module-alias/register');
import * as _ from 'lodash';
import * as chai from 'chai';
import * as setProtocolUtils from 'set-protocol-utils';
import { BigNumber } from 'bignumber.js';
import { Address, Bytes } from 'set-protocol-utils';
import ChaiSetup from '@utils/chaiSetup';
import { BigNumberSetup } from '@utils/bigNumberSetup';
import {
CoreContract,
SignatureValidatorContract,
StandardTokenMockContract,
TakerWalletWrapperContract,
TransferProxyContract,
VaultContract,
} from '@utils/contracts';
import { CoreWrapper } from '@utils/coreWrapper';
import { ExchangeData } from '@utils/orders';
import { ERC20Wrapper } from '@utils/erc20Wrapper';
import { ExchangeWrapper } from '@utils/exchangeWrapper';
import { Blockchain } from '@utils/blockchain';
import { generateTakerWalletOrders } from '@utils/orders';
import {
DEFAULT_GAS,
DEPLOYED_TOKEN_QUANTITY,
UNLIMITED_ALLOWANCE_IN_BASE_UNITS,
} from '@utils/constants';
import { expectRevertError } from '@utils/tokenAssertions';
import { getWeb3 } from '@utils/web3Helper';
BigNumberSetup.configure();
ChaiSetup.configure();
const web3 = getWeb3();
const { SetProtocolUtils: SetUtils } = setProtocolUtils;
const { expect } = chai;
const blockchain = new Blockchain(web3);
const { ZERO } = SetUtils.CONSTANTS;
contract('TakerWalletWrapper', accounts => {
const [
deployerAccount,
makerAccount,
takerAccount,
unauthorizedAddress,
issuanceOrderModuleAccount,
] = accounts;
const coreWrapper = new CoreWrapper(deployerAccount, deployerAccount);
const erc20Wrapper = new ERC20Wrapper(deployerAccount);
const exchangeWrapper = new ExchangeWrapper(deployerAccount);
let core: CoreContract;
let transferProxy: TransferProxyContract;
let vault: VaultContract;
let signatureValidator: SignatureValidatorContract;
let takerWalletWrapper: TakerWalletWrapperContract;
let components: StandardTokenMockContract[] = [];
let componentCount: number = 1;
beforeEach(async () => {
await blockchain.saveSnapshotAsync();
transferProxy = await coreWrapper.deployTransferProxyAsync();
vault = await coreWrapper.deployVaultAsync();
signatureValidator = await coreWrapper.deploySignatureValidatorAsync();
core = await coreWrapper.deployCoreMockAsync(transferProxy, vault, signatureValidator);
await coreWrapper.addModuleAsync(core, issuanceOrderModuleAccount);
takerWalletWrapper = await exchangeWrapper.deployTakerWalletExchangeWrapper(core.address, transferProxy);
await coreWrapper.addAuthorizationAsync(transferProxy, takerWalletWrapper.address);
components = await erc20Wrapper.deployTokensAsync(componentCount, takerAccount);
await erc20Wrapper.approveTransfersAsync(components, transferProxy.address, takerAccount);
});
afterEach(async () => {
await blockchain.revertAsync();
});
describe('#exchange', async () => {
let subjectCaller: Address;
let subjectMakerAccount: Address;
let subjectTakerAccount: Address;
let subjectMakerTokenAddress: Address;
let subjectMakerTokenAmount: BigNumber;
let subjectOrderCount: BigNumber;
let subjectFillQuantity: BigNumber;
let subjectAttemptedFillQuantity: BigNumber;
let subjectTakerOrdersData: Bytes;
let subjectExchangeData: ExchangeData;
let componentToken: StandardTokenMockContract;
const transferAmount: BigNumber = DEPLOYED_TOKEN_QUANTITY.div(2);
beforeEach(async () => {
componentToken = _.first(components);
const componentAddresses = _.map(components, token => token.address);
const transferAmounts = _.map(components, token => transferAmount);
subjectCaller = issuanceOrderModuleAccount;
subjectMakerAccount = makerAccount;
subjectTakerAccount = takerAccount;
subjectMakerTokenAddress = componentToken.address;
subjectMakerTokenAmount = ZERO;
subjectOrderCount = new BigNumber(componentAddresses.length);
subjectFillQuantity = new BigNumber(1);
subjectAttemptedFillQuantity = new BigNumber(1);
subjectTakerOrdersData = generateTakerWalletOrders(componentAddresses, transferAmounts);
subjectExchangeData = {
maker: subjectMakerAccount,
taker: subjectTakerAccount,
makerToken: subjectMakerTokenAddress,
makerAssetAmount: web3.utils.toBN(subjectMakerTokenAmount),
orderCount: web3.utils.toBN(subjectOrderCount),
fillQuantity: web3.utils.toBN(subjectFillQuantity),
attemptedFillQuantity: web3.utils.toBN(subjectAttemptedFillQuantity),
};
});
async function subject(): Promise<string> {
return takerWalletWrapper.exchange.sendTransactionAsync(
subjectExchangeData,
subjectTakerOrdersData,
{ from: subjectCaller, gas: DEFAULT_GAS },
);
}
it('transfers the token from the taker', async () => {
const existingBalance = await componentToken.balanceOf.callAsync(takerAccount);
await subject();
const expectedNewBalance = existingBalance.sub(transferAmount);
const newBalance = await componentToken.balanceOf.callAsync(takerAccount);
expect(newBalance).to.be.bignumber.equal(expectedNewBalance);
});
it('transfers the token to the wrapper contract', async () => {
const existingBalance = await componentToken.balanceOf.callAsync(takerWalletWrapper.address);
await subject();
const expectedNewBalance = existingBalance.add(transferAmount);
const newBalance = await componentToken.balanceOf.callAsync(takerWalletWrapper.address);
expect(newBalance).to.be.bignumber.equal(expectedNewBalance);
});
it('approves the tokens for transfer to the transferProxy with unlimited allowance', async () => {
const existingAllowance =
await componentToken.allowance.callAsync(takerWalletWrapper.address, transferProxy.address);
await subject();
const expectedNewAllowance = existingAllowance.add(UNLIMITED_ALLOWANCE_IN_BASE_UNITS);
const newBalance =
await componentToken.allowance.callAsync(takerWalletWrapper.address, transferProxy.address);
expect(newBalance).to.be.bignumber.equal(expectedNewAllowance);
});
describe('when the caller is not the deployed core address', async () => {
beforeEach(async () => {
subjectCaller = unauthorizedAddress;
});
it('should revert', async () => {
await expectRevertError(subject());
});
});
describe('when the ordersData contains multiple tokens to transfer', async() => {
before(async () => {
componentCount = 3;
});
it('transfers the tokens from the taker', async () => {
const existingTokenBalances = await erc20Wrapper.getTokenBalances(components, takerAccount);
await subject();
const expectedNewBalances = _.map(existingTokenBalances, balance => balance.sub(transferAmount));
const newTokenBalances = await erc20Wrapper.getTokenBalances(components, takerAccount);
expect(newTokenBalances).to.eql(expectedNewBalances);
});
it('transfers the token to the wrapper contract', async () => {
const existingTokenBalances = await erc20Wrapper.getTokenBalances(components, takerWalletWrapper.address);
await subject();
const expectedNewBalances = _.map(existingTokenBalances, balance => balance.add(transferAmount));
const newTokenBalances = await erc20Wrapper.getTokenBalances(components, takerWalletWrapper.address);
expect(newTokenBalances).to.eql(expectedNewBalances);
});
it('approves the tokens for transfer to the transferProxy with unlimited allowance', async () => {
const existingAllowances =
await erc20Wrapper.getTokenAllowances(components, takerWalletWrapper.address, transferProxy.address);
await subject();
const expectedNewAllowances =
_.map(existingAllowances, allowance => allowance.add(UNLIMITED_ALLOWANCE_IN_BASE_UNITS));
const newAllowances =
await erc20Wrapper.getTokenAllowances(components, takerWalletWrapper.address, transferProxy.address);
expect(newAllowances).to.eql(expectedNewAllowances);
});
});
});
});