-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathcustom-hooks.js
450 lines (368 loc) · 15 KB
/
custom-hooks.js
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import { useState, useEffect, useMemo } from "react";
import { drizzleReactHooks } from "@umaprotocol/react-plugin";
import publicNetworks from "common/PublicNetworks";
import identifiers from "identifiers.json";
import { MAX_UINT_VAL } from "common/Constants";
import { sendGaEvent } from "lib/google-analytics";
import { ContractStateEnum } from "common/TokenizedDerivativeUtils";
// This is a hack to handle reverts for view/pure functions that don't actually revert on public networks.
// See https://forum.openzeppelin.com/t/require-in-view-pure-functions-dont-revert-on-public-networks/1211 for more
// info.
export function revertWrapper(result) {
if (!result) {
return null;
}
let revertValue = "3963877391197344453575983046348115674221700746820753546331534351508065746944";
if (result.toString() === revertValue) {
return null;
}
const isObject = obj => {
return obj === Object(obj);
};
if (isObject(result)) {
// Iterate over the properties of the object and see if any match the revert value.
for (let prop in result) {
if (result[prop].toString() === revertValue) {
return null;
}
}
}
return result;
}
export function useNumRegisteredContracts() {
const { useCacheCall } = drizzleReactHooks.useDrizzle();
const account = drizzleReactHooks.useDrizzleState(drizzleState => {
return drizzleState.accounts[0];
});
const registeredContracts = useCacheCall("Registry", "getRegisteredDerivatives", account);
if (account && registeredContracts) {
return registeredContracts.length;
}
return undefined;
}
export function useEtherscanUrl() {
const networkId = drizzleReactHooks.useDrizzleState(drizzleState => {
return drizzleState.web3.networkId;
});
const networkConfig = publicNetworks[networkId];
if (networkConfig && networkConfig.etherscan) {
return networkConfig.etherscan;
}
// Default to mainnet, even though it won't work for ganache runs.
return "https://etherscan.io/";
}
export function useEthFaucetUrl() {
const networkId = drizzleReactHooks.useDrizzleState(drizzleState => {
return drizzleState.web3.networkId;
});
const networkConfig = publicNetworks[networkId];
// The only networks that are both public and have an eth faucet should be testnets.
if (networkConfig && networkConfig.ethFaucet) {
return networkConfig.ethFaucet;
}
// Mainnet and private networks will default to this case.
return null;
}
export function useDaiFaucetRequest() {
const { useCacheSend, drizzle } = drizzleReactHooks.useDrizzle();
const { account, networkId } = drizzleReactHooks.useDrizzleState(drizzleState => ({
account: drizzleState.accounts[0],
networkId: drizzleState.web3.networkId
}));
const { send } = useCacheSend("TestnetERC20", "allocateTo");
// There is no DAI faucet for mainnet.
if (publicNetworks[networkId] && publicNetworks[networkId].name === "mainnet") {
return null;
}
return event => {
if (event) {
event.preventDefault();
}
send(account, drizzle.web3.utils.toWei("100000"));
sendGaEvent("TestnetERC20", "allocateTo");
};
}
export function useTextInput() {
const [amount, setAmount] = useState("");
const handleChangeAmount = event => {
// Regular expression that matches a decimal, e.g., `2.5`.
if (event.target.value === "" || /^(\d+\.?\d*)$/.test(event.target.value)) {
setAmount(event.target.value);
}
};
return { amount, handleChangeAmount };
}
export function useSendTransactionOnLink(cacheSend, amounts, history) {
const { drizzle } = drizzleReactHooks.useDrizzle();
const { toWei } = drizzle.web3.utils;
const { account } = drizzleReactHooks.useDrizzleState(drizzleState => ({
account: drizzleState.accounts[0]
}));
const { send, status } = cacheSend;
const [linkedPage, setLinkedPage] = useState();
const handleSubmit = event => {
event.preventDefault();
const linkedPage = event.currentTarget.getAttribute("href");
setLinkedPage(linkedPage);
send(...amounts.map(val => toWei(val)), { from: account });
sendGaEvent("TokenizedDerivative", "Interaction");
};
// If we've successfully withdrawn, reroute to the linkedPage whose `Link` the user clicked on (currently, this can only
// ever be the `ManagePositions` linkedPage).
useEffect(() => {
if (status === "success" && linkedPage) {
history.replace(linkedPage);
}
}, [status, linkedPage, history]);
return handleSubmit;
}
export function useCollateralizationInformation(tokenAddress, changeInShortBalance) {
const { drizzle, useCacheCall } = drizzleReactHooks.useDrizzle();
const { web3 } = drizzle;
const { toBN, toWei } = web3.utils;
const data = {};
data.derivativeStorage = useCacheCall(tokenAddress, "derivativeStorage");
data.nav = revertWrapper(useCacheCall(tokenAddress, "calcNAV"));
data.shortMarginBalance = revertWrapper(useCacheCall(tokenAddress, "calcShortMarginBalance"));
if (!Object.values(data).every(Boolean)) {
return { ready: false };
}
data.collateralizationRequirement = toBN(data.derivativeStorage.fixedParameters.supportedMove)
.add(toBN(toWei("1")))
.muln(100)
.toString();
data.currentCollateralization = "-- %";
data.newCollateralizationAmount = "-- %";
const navBn = toBN(data.nav);
if (!navBn.isZero()) {
data.totalHoldings = navBn.add(toBN(data.shortMarginBalance));
data.currentCollateralization = data.totalHoldings.muln(100).div(navBn) + "%";
if (changeInShortBalance !== "") {
data.newCollateralizationAmount =
data.totalHoldings
.add(toBN(toWei(changeInShortBalance)))
.muln(100)
.div(navBn) + "%";
}
}
data.ready = true;
return data;
}
export function useMaxTokensThatCanBeCreated(tokenAddress, marginAmount) {
const { drizzle, useCacheCall } = drizzleReactHooks.useDrizzle();
const { toWei, toBN } = drizzle.web3.utils;
const derivativeStorage = useCacheCall(tokenAddress, "derivativeStorage");
const newExcessMargin = revertWrapper(useCacheCall(tokenAddress, "calcExcessMargin"));
const tokenValue = revertWrapper(useCacheCall(tokenAddress, "calcTokenValue"));
const dataFetched = derivativeStorage && newExcessMargin && tokenValue;
if (!dataFetched) {
return { ready: false };
}
if (marginAmount === "") {
return { ready: true, maxTokens: toBN("0") };
}
const fpScalingFactor = toBN(toWei("1"));
const sentAmount = toBN(toWei(marginAmount));
const supportedMove = toBN(derivativeStorage.fixedParameters.supportedMove);
const tokenValueBn = toBN(tokenValue);
const mul = (a, b) => a.mul(b).divRound(fpScalingFactor);
const div = (a, b) => a.mul(fpScalingFactor).divRound(b);
// `supportedTokenMarketCap` represents the extra token market cap that there is sufficient collateral for. Tokens can be purchased at
// `tokenValue` up to this amount.
const supportedTokenMarketCap = div(toBN(newExcessMargin), supportedMove);
if (sentAmount.lte(supportedTokenMarketCap)) {
// The amount of money being sent in is the limiting factor.
return { ready: true, maxTokens: div(sentAmount, tokenValueBn) };
} else {
// Tokens purchased beyond the value of `supportedTokenMarketCap` cost `(1 + supportedMove) * tokenValue`, because some of
// the money has to be diverted to support the margin requirement.
const costOfExtra = mul(tokenValueBn, fpScalingFactor.add(supportedMove));
const extra = sentAmount.sub(supportedTokenMarketCap);
return { ready: true, maxTokens: div(supportedTokenMarketCap, tokenValueBn).add(div(extra, costOfExtra)) };
}
}
export function computeLiquidationPrice(web3, navString, excessMarginString, underlyingPriceTime) {
const { toBN, toWei } = web3.utils;
// Return undefined (as drizzle would) if the blockchain values have not been received yet.
if (!navString || !excessMarginString || !underlyingPriceTime) {
return undefined;
}
// Convert string outputs to BN.
const nav = toBN(navString);
const excessMargin = toBN(excessMarginString);
const underlyingPrice = toBN(underlyingPriceTime.underlyingPrice);
// Return null if there is no valid output that can be computed.
if (nav.isZero()) {
return null;
}
const fpScalingFactor = toBN(toWei("1"));
const mul = (a, b) => a.mul(b).divRound(fpScalingFactor);
const div = (a, b) => a.mul(fpScalingFactor).divRound(b);
const maxNav = nav.add(excessMargin);
const percentChange = div(maxNav, nav);
const liquidationPrice = mul(percentChange, underlyingPrice);
return liquidationPrice;
}
export function useIsContractSponsor(contractAddress) {
const { drizzle, useCacheCall } = drizzleReactHooks.useDrizzle();
const { toChecksumAddress } = drizzle.web3.utils;
const account = drizzleReactHooks.useDrizzleState(drizzleState => drizzleState.accounts[0]);
const derivativeStorage = useCacheCall(contractAddress, "derivativeStorage");
return (
derivativeStorage && toChecksumAddress(account) === toChecksumAddress(derivativeStorage.externalAddresses.sponsor)
);
}
export function useSettle(contractAddress) {
const { useCacheCall, useCacheSend } = drizzleReactHooks.useDrizzle();
const account = drizzleReactHooks.useDrizzleState(drizzleState => drizzleState.accounts[0]);
const canBeSettled = useCacheCall(contractAddress, "canBeSettled");
const derivativeStorage = useCacheCall(contractAddress, "derivativeStorage");
const canCallRemargin = useIsContractSponsor(contractAddress);
const { send: remargin, status: remarginStatus } = useCacheSend(contractAddress, "remargin");
const { send: settle, status: settleStatus } = useCacheSend(contractAddress, "settle");
let send;
const settleHandler = e => {
e.preventDefault();
send({ from: account });
sendGaEvent("TokenizedDerivative", "Settle");
};
if (canBeSettled === undefined || !derivativeStorage) {
return { ready: false };
}
if (derivativeStorage.state.toString() === ContractStateEnum.LIVE) {
// Handle edge case where user can go directly from live to settled via a remargin.
send = remargin;
return {
ready: true,
canCallSettle: canBeSettled && canCallRemargin,
settleHandler,
isLoadingSettle: remarginStatus === "pending"
};
} else {
send = settle;
return {
ready: true,
canCallSettle: canBeSettled,
settleHandler,
isLoadingSettle: settleStatus === "pending"
};
}
}
export function useExpire(contractAddress, identifier) {
const { drizzle, useCacheCall, useCacheSend } = drizzleReactHooks.useDrizzle();
const { toBN, utf8ToHex } = drizzle.web3.utils;
const account = drizzleReactHooks.useDrizzleState(drizzleState => drizzleState.accounts[0]);
const canCallRemargin = useIsContractSponsor(contractAddress);
const derivativeStorage = useCacheCall(contractAddress, "derivativeStorage");
const underlyingPriceTime = useCacheCall("ManualPriceFeed", "latestPrice", utf8ToHex(identifier));
const { send: remargin, status: remarginStatus } = useCacheSend(contractAddress, "remargin");
if (canCallRemargin === undefined || !underlyingPriceTime || !derivativeStorage) {
return { ready: false };
}
const canCallExpire =
canCallRemargin &&
derivativeStorage.state === ContractStateEnum.LIVE &&
toBN(underlyingPriceTime.publishTime).gte(toBN(derivativeStorage.endTime));
const expireHandler = e => {
e.preventDefault();
remargin({ from: account });
sendGaEvent("TokenizedDerivative", "Expire");
};
return {
ready: true,
canCallExpire,
expireHandler,
isLoadingExpire: remarginStatus === "pending"
};
}
export function useTokenPreapproval(tokenContractName, addressToApprove) {
const { drizzle, useCacheCall, useCacheSend } = drizzleReactHooks.useDrizzle();
const { toBN } = drizzle.web3.utils;
const { account } = drizzleReactHooks.useDrizzleState(drizzleState => ({
account: drizzleState.accounts[0]
}));
const allowance = useCacheCall(tokenContractName, "allowance", account, addressToApprove);
const allowanceAmount = toBN(MAX_UINT_VAL);
const minAllowanceAmount = allowanceAmount.divRound(toBN("2"));
const { send: approve, status: approvalStatus } = useCacheSend(tokenContractName, "approve");
const approveTokensHandler = e => {
e.preventDefault();
approve(addressToApprove, allowanceAmount.toString(), { from: account });
sendGaEvent("TokenizedDerivative", "Approve");
};
if (!allowance) {
return { ready: false };
}
return {
ready: true,
approveTokensHandler,
isApproved: toBN(allowance).gte(minAllowanceAmount),
isLoadingApproval: approvalStatus === "pending"
};
}
export function useLiquidationPrice(tokenAddress) {
const { drizzle, useCacheCall } = drizzleReactHooks.useDrizzle();
const nav = revertWrapper(useCacheCall(tokenAddress, "calcNAV"));
const excessMargin = revertWrapper(useCacheCall(tokenAddress, "calcExcessMargin"));
const underlyingPriceTime = revertWrapper(useCacheCall(tokenAddress, "getUpdatedUnderlyingPrice"));
return computeLiquidationPrice(drizzle.web3, nav, excessMargin, underlyingPriceTime);
}
export function useIdentifierConfig() {
return useMemo(() => {
const identifierConfig = {};
// Extract the dappConfig from the global config.
for (const [identifier, { dappConfig }] of Object.entries(identifiers)) {
identifierConfig[identifier] = dappConfig;
}
return identifierConfig;
}, []);
}
export function useDaiAddress() {
const { drizzle } = drizzleReactHooks.useDrizzle();
const networkId = drizzleReactHooks.useDrizzleState(drizzleState => {
return drizzleState.web3.networkId;
});
if (networkId.toString() === "1") {
// Real DAI address.
return "0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359";
}
// Otherwise, we'll use the same address that our faucet depends on.
return drizzle.contracts.TestnetERC20.address;
}
export function useEnabledIdentifierConfig() {
const {
useCacheCallPromise,
drizzle: { web3 }
} = drizzleReactHooks.useDrizzle();
const { useRerenderOnResolution } = drizzleReactHooks;
const identifierConfig = useIdentifierConfig();
// Note: using the promisified useCacheCall to prevent unrelated changes from triggering rerenders.
const narrowedConfig = useCacheCallPromise(
"NotApplicable",
(callContract, resolvePromise, config) => {
let finished = true;
const call = (contractName, methodName, ...args) => {
const result = callContract(contractName, methodName, ...args);
if (result === undefined) {
finished = false;
}
return result;
};
const narrowedConfig = {};
for (const identifier in config) {
if (
call("IdentifierWhitelist", "isIdentifierSupported", web3.utils.utf8ToHex(identifier)) &&
call("ManualPriceFeed", "isIdentifierSupported", web3.utils.utf8ToHex(identifier))
) {
narrowedConfig[identifier] = config[identifier];
}
}
if (finished) {
resolvePromise(narrowedConfig);
}
},
identifierConfig
);
useRerenderOnResolution(narrowedConfig);
return narrowedConfig.isResolved ? narrowedConfig.resolvedValue : undefined;
}