This repository was archived by the owner on Jan 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSetTokenAPI.ts
More file actions
226 lines (192 loc) · 8.22 KB
/
Copy pathSetTokenAPI.ts
File metadata and controls
226 lines (192 loc) · 8.22 KB
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
217
218
219
220
221
222
223
224
225
226
/*
Copyright 2018 Set Labs Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict';
import * as _ from 'lodash';
import Web3 from 'web3';
import { ZERO } from '../constants';
import { coreAPIErrors, setTokenAssertionsErrors } from '../errors';
import { Assertions } from '../assertions';
import { ERC20Wrapper, SetTokenWrapper } from '../wrappers';
import { BigNumber, calculatePartialAmount } from '../util';
import { Address, Component, SetDetails } from '../types/common';
/**
* @title SetTokenAPI
* @author Set Protocol
*
* A library for interacting with SetToken contracts
*/
export class SetTokenAPI {
private web3: Web3;
private assert: Assertions;
private setToken: SetTokenWrapper;
private erc20: ERC20Wrapper;
/**
* Instantiates a new SetTokenAPI instance that contains methods for interacting with SetToken contracts
*
* @param web3 Web3.js Provider instance you would like the SetProtocol.js library to use for interacting
* with the Ethereum network
* @param assertions An instance of the Assertion library
*/
constructor(web3: Web3, assertions: Assertions) {
this.web3 = web3;
this.assert = assertions;
this.erc20 = new ERC20Wrapper(this.web3);
this.setToken = new SetTokenWrapper(this.web3);
}
/**
* Calculates the required amount of a component token required for issuance or redemption for a quantity of the Set
*
* @param setAddress Address of the Set
* @param componentAddress Address of the component
* @param quantity Quantity of Set to issue or redeem
* @return Amount of `componentAddress` required for issuance or redemption
*/
public async calculateComponentAmountForIssuanceAsync(
setAddress: Address,
componentAddress: Address,
quantity: BigNumber,
): Promise<BigNumber> {
await this.assertCalculateUnitTransferred(setAddress, componentAddress, quantity);
const components = await this.setToken.getComponents(setAddress);
const componentIndex = _.indexOf(components, componentAddress);
const amountsForIssuance = await this.calculateComponentAmountsForIssuanceAsync(setAddress, quantity);
return amountsForIssuance[componentIndex].unit;
}
/**
* Calculates the amounts of each component required for issuance or redemption for a quantity of the Set
*
* @param setAddress Address of the Set
* @param quantity Quantity of Set to issue or redeem
* @return List of objects conforming to `Component` interface with addresses and amounts required for
* issuance or redemption
*/
public async calculateComponentAmountsForIssuanceAsync(
setAddress: Address,
quantity: BigNumber,
): Promise<Component[]> {
this.assertcalculateComponentAmountsForIssuance(setAddress, quantity);
const [naturalUnit, componentUnits, components] = await Promise.all([
this.setToken.naturalUnit(setAddress),
this.setToken.getUnits(setAddress),
this.setToken.getComponents(setAddress),
]);
return _.map(componentUnits, (componentUnit, index) => {
return {
address: components[index],
unit: calculatePartialAmount(componentUnit, quantity, naturalUnit),
} as Component;
});
}
/**
* Fetches the address of the factory that created the Set
*
* @param setAddress Address of the Set
* @return Address of the factory that ceated the Set
*/
public async getFactoryAsync(setAddress: Address): Promise<Address> {
this.assert.schema.isValidAddress('setAddress', setAddress);
return await this.setToken.factory(setAddress);
}
/**
* Fetches the addresses of the component tokens that make up the Set
*
* @param setAddress Address of the Set
* @return An array of token addresses
*/
public async getComponentsAsync(setAddress: Address): Promise<Address[]> {
this.assert.schema.isValidAddress('setAddress', setAddress);
return await this.setToken.getComponents(setAddress);
}
/**
* Fetches the natural unit of the Set
*
* @param setAddress Address of the Set
* @return Natural unit of the Set
*/
public async getNaturalUnitAsync(setAddress: Address): Promise<BigNumber> {
this.assert.schema.isValidAddress('setAddress', setAddress);
return await this.setToken.naturalUnit(setAddress);
}
/**
* Fetches units of each component token that make up the Set
*
* @param setAddress Address of the Set
* @return An array of units that make up the Set composition which correspond to the component tokens
* in the Set
*/
public async getUnitsAsync(setAddress: Address): Promise<BigNumber[]> {
this.assert.schema.isValidAddress('setAddress', setAddress);
return await this.setToken.getUnits(setAddress);
}
/**
* Fetches details of a Set comprised of factory address, name, symbol, natural unit, component addresses,
* and component units
*
* @param setAddress Address of the Set
* @return Object conforming to `SetDetails` interface
*/
public async getDetailsAsync(setAddress: Address): Promise<SetDetails> {
this.assert.schema.isValidAddress('setAddress', setAddress);
const [factoryAddress, naturalUnit, componentAddresses, componentUnits, name, symbol] = await Promise.all([
this.setToken.factory(setAddress),
this.setToken.naturalUnit(setAddress),
this.setToken.getComponents(setAddress),
this.setToken.getUnits(setAddress),
this.erc20.name(setAddress),
this.erc20.symbol(setAddress),
]);
const components: Component[] = componentAddresses.map(function(address, idx) {
return { address, unit: componentUnits[idx] } as Component;
});
return {
address: setAddress,
factoryAddress,
name,
symbol,
naturalUnit,
components,
} as SetDetails;
}
/**
* Validates whether the quantity of a Set to issue or redeem in is a multiple of the Set's natural unit
*
* @param setAddress Address of the Set
* @param quantity Quantity to be checked
* @return boolean Boolean representing whether the Set is a multiple of the natural unit
*
*/
public async isMultipleOfNaturalUnitAsync(setAddress: Address, quantity: BigNumber): Promise<boolean> {
this.assertIsMultipleOfNaturalUnitAsync(setAddress, quantity);
const naturalUnit = await this.setToken.naturalUnit(setAddress);
return quantity.mod(naturalUnit).eq(ZERO);
}
/* ============ Private Assertions ============ */
private assertIsMultipleOfNaturalUnitAsync(setAddress: Address, quantity: BigNumber) {
this.assert.schema.isValidAddress('setAddress', setAddress);
this.assert.common.greaterThanZero(quantity, coreAPIErrors.QUANTITY_NEEDS_TO_BE_POSITIVE(quantity));
}
private assertcalculateComponentAmountsForIssuance(setAddress: Address, quantity: BigNumber) {
this.assert.schema.isValidAddress('setAddress', setAddress);
this.assert.common.greaterThanZero(quantity, coreAPIErrors.QUANTITY_NEEDS_TO_BE_POSITIVE(quantity));
}
private async assertCalculateUnitTransferred(setAddress: Address, componentAddress: Address, quantity: BigNumber) {
this.assert.schema.isValidAddress('setAddress', setAddress);
this.assert.schema.isValidAddress('componentAddress', componentAddress);
this.assert.common.greaterThanZero(quantity, coreAPIErrors.QUANTITY_NEEDS_TO_BE_POSITIVE(quantity));
const componentAddresses = await this.setToken.getComponents(setAddress);
const componentIndex = _.indexOf(componentAddresses, componentAddress);
if (componentIndex < 0) {
throw new Error(setTokenAssertionsErrors.IS_NOT_COMPONENT(setAddress, componentAddress));
}
}
}