@across-protocol/sdk 4.3.113 → 4.3.114-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/arch/svm/SpokeUtils.d.ts +16 -23
- package/dist/cjs/arch/svm/SpokeUtils.js.map +1 -1
- package/dist/cjs/arch/svm/utils.d.ts +3 -8
- package/dist/cjs/arch/svm/utils.js.map +1 -1
- package/dist/cjs/gasPriceOracle/adapters/solana.js.map +1 -1
- package/dist/cjs/index.d.ts +0 -1
- package/dist/cjs/index.js +1 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/relayFeeCalculator/chain-queries/svmQuery.d.ts +3 -3
- package/dist/cjs/relayFeeCalculator/chain-queries/svmQuery.js +1 -1
- package/dist/cjs/relayFeeCalculator/chain-queries/svmQuery.js.map +1 -1
- package/dist/esm/arch/svm/SpokeUtils.d.ts +16 -23
- package/dist/esm/arch/svm/SpokeUtils.js.map +1 -1
- package/dist/esm/arch/svm/utils.d.ts +3 -8
- package/dist/esm/arch/svm/utils.js.map +1 -1
- package/dist/esm/gasPriceOracle/adapters/solana.js +1 -1
- package/dist/esm/gasPriceOracle/adapters/solana.js.map +1 -1
- package/dist/esm/index.d.ts +0 -1
- package/dist/esm/index.js +0 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/relayFeeCalculator/chain-queries/svmQuery.d.ts +3 -3
- package/dist/esm/relayFeeCalculator/chain-queries/svmQuery.js +3 -3
- package/dist/esm/relayFeeCalculator/chain-queries/svmQuery.js.map +1 -1
- package/dist/types/arch/svm/SpokeUtils.d.ts +16 -23
- package/dist/types/arch/svm/SpokeUtils.d.ts.map +1 -1
- package/dist/types/arch/svm/utils.d.ts +3 -8
- package/dist/types/arch/svm/utils.d.ts.map +1 -1
- package/dist/types/gasPriceOracle/adapters/solana.d.ts.map +1 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/relayFeeCalculator/chain-queries/svmQuery.d.ts +3 -3
- package/dist/types/relayFeeCalculator/chain-queries/svmQuery.d.ts.map +1 -1
- package/package.json +17 -8
- package/src/arch/svm/SpokeUtils.ts +25 -24
- package/src/arch/svm/utils.ts +6 -3
- package/src/gasPriceOracle/adapters/solana.ts +8 -3
- package/src/index.ts +0 -1
- package/src/relayFeeCalculator/chain-queries/svmQuery.ts +5 -5
- package/dist/cjs/pool/TransactionManager.d.ts +0 -12
- package/dist/cjs/pool/TransactionManager.js +0 -121
- package/dist/cjs/pool/TransactionManager.js.map +0 -1
- package/dist/cjs/pool/index.d.ts +0 -1
- package/dist/cjs/pool/index.js +0 -5
- package/dist/cjs/pool/index.js.map +0 -1
- package/dist/cjs/pool/poolClient.d.ts +0 -228
- package/dist/cjs/pool/poolClient.js +0 -869
- package/dist/cjs/pool/poolClient.js.map +0 -1
- package/dist/cjs/pool/uma/across/constants.d.ts +0 -2
- package/dist/cjs/pool/uma/across/constants.js +0 -6
- package/dist/cjs/pool/uma/across/constants.js.map +0 -1
- package/dist/cjs/pool/uma/across/index.d.ts +0 -2
- package/dist/cjs/pool/uma/across/index.js +0 -8
- package/dist/cjs/pool/uma/across/index.js.map +0 -1
- package/dist/cjs/pool/uma/across/transactionManager.d.ts +0 -12
- package/dist/cjs/pool/uma/across/transactionManager.js +0 -121
- package/dist/cjs/pool/uma/across/transactionManager.js.map +0 -1
- package/dist/cjs/pool/uma/clients/erc20/client.d.ts +0 -26
- package/dist/cjs/pool/uma/clients/erc20/client.js +0 -38
- package/dist/cjs/pool/uma/clients/erc20/client.js.map +0 -1
- package/dist/cjs/pool/uma/clients/erc20/index.d.ts +0 -1
- package/dist/cjs/pool/uma/clients/erc20/index.js +0 -5
- package/dist/cjs/pool/uma/clients/erc20/index.js.map +0 -1
- package/dist/cjs/pool/uma/clients/index.d.ts +0 -1
- package/dist/cjs/pool/uma/clients/index.js +0 -6
- package/dist/cjs/pool/uma/clients/index.js.map +0 -1
- package/dist/cjs/pool/uma/index.d.ts +0 -13
- package/dist/cjs/pool/uma/index.js +0 -9
- package/dist/cjs/pool/uma/index.js.map +0 -1
- package/dist/cjs/pool/uma/oracle/index.d.ts +0 -1
- package/dist/cjs/pool/uma/oracle/index.js +0 -6
- package/dist/cjs/pool/uma/oracle/index.js.map +0 -1
- package/dist/cjs/pool/uma/oracle/utils.d.ts +0 -7
- package/dist/cjs/pool/uma/oracle/utils.js +0 -20
- package/dist/cjs/pool/uma/oracle/utils.js.map +0 -1
- package/dist/cjs/pool/uma/utils.d.ts +0 -17
- package/dist/cjs/pool/uma/utils.js +0 -69
- package/dist/cjs/pool/uma/utils.js.map +0 -1
- package/dist/esm/pool/TransactionManager.d.ts +0 -12
- package/dist/esm/pool/TransactionManager.js +0 -122
- package/dist/esm/pool/TransactionManager.js.map +0 -1
- package/dist/esm/pool/index.d.ts +0 -1
- package/dist/esm/pool/index.js +0 -2
- package/dist/esm/pool/index.js.map +0 -1
- package/dist/esm/pool/poolClient.d.ts +0 -238
- package/dist/esm/pool/poolClient.js +0 -899
- package/dist/esm/pool/poolClient.js.map +0 -1
- package/dist/esm/pool/uma/across/constants.d.ts +0 -2
- package/dist/esm/pool/uma/across/constants.js +0 -3
- package/dist/esm/pool/uma/across/constants.js.map +0 -1
- package/dist/esm/pool/uma/across/index.d.ts +0 -2
- package/dist/esm/pool/uma/across/index.js +0 -4
- package/dist/esm/pool/uma/across/index.js.map +0 -1
- package/dist/esm/pool/uma/across/transactionManager.d.ts +0 -12
- package/dist/esm/pool/uma/across/transactionManager.js +0 -122
- package/dist/esm/pool/uma/across/transactionManager.js.map +0 -1
- package/dist/esm/pool/uma/clients/erc20/client.d.ts +0 -26
- package/dist/esm/pool/uma/clients/erc20/client.js +0 -35
- package/dist/esm/pool/uma/clients/erc20/client.js.map +0 -1
- package/dist/esm/pool/uma/clients/erc20/index.d.ts +0 -1
- package/dist/esm/pool/uma/clients/erc20/index.js +0 -2
- package/dist/esm/pool/uma/clients/erc20/index.js.map +0 -1
- package/dist/esm/pool/uma/clients/index.d.ts +0 -1
- package/dist/esm/pool/uma/clients/index.js +0 -3
- package/dist/esm/pool/uma/clients/index.js.map +0 -1
- package/dist/esm/pool/uma/index.d.ts +0 -13
- package/dist/esm/pool/uma/index.js +0 -9
- package/dist/esm/pool/uma/index.js.map +0 -1
- package/dist/esm/pool/uma/oracle/index.d.ts +0 -1
- package/dist/esm/pool/uma/oracle/index.js +0 -3
- package/dist/esm/pool/uma/oracle/index.js.map +0 -1
- package/dist/esm/pool/uma/oracle/utils.d.ts +0 -23
- package/dist/esm/pool/uma/oracle/utils.js +0 -33
- package/dist/esm/pool/uma/oracle/utils.js.map +0 -1
- package/dist/esm/pool/uma/utils.d.ts +0 -17
- package/dist/esm/pool/uma/utils.js +0 -68
- package/dist/esm/pool/uma/utils.js.map +0 -1
- package/dist/types/pool/TransactionManager.d.ts +0 -13
- package/dist/types/pool/TransactionManager.d.ts.map +0 -1
- package/dist/types/pool/index.d.ts +0 -2
- package/dist/types/pool/index.d.ts.map +0 -1
- package/dist/types/pool/poolClient.d.ts +0 -239
- package/dist/types/pool/poolClient.d.ts.map +0 -1
- package/dist/types/pool/uma/across/constants.d.ts +0 -3
- package/dist/types/pool/uma/across/constants.d.ts.map +0 -1
- package/dist/types/pool/uma/across/index.d.ts +0 -3
- package/dist/types/pool/uma/across/index.d.ts.map +0 -1
- package/dist/types/pool/uma/across/transactionManager.d.ts +0 -13
- package/dist/types/pool/uma/across/transactionManager.d.ts.map +0 -1
- package/dist/types/pool/uma/clients/erc20/client.d.ts +0 -27
- package/dist/types/pool/uma/clients/erc20/client.d.ts.map +0 -1
- package/dist/types/pool/uma/clients/erc20/index.d.ts +0 -2
- package/dist/types/pool/uma/clients/erc20/index.d.ts.map +0 -1
- package/dist/types/pool/uma/clients/index.d.ts +0 -2
- package/dist/types/pool/uma/clients/index.d.ts.map +0 -1
- package/dist/types/pool/uma/index.d.ts +0 -14
- package/dist/types/pool/uma/index.d.ts.map +0 -1
- package/dist/types/pool/uma/oracle/index.d.ts +0 -2
- package/dist/types/pool/uma/oracle/index.d.ts.map +0 -1
- package/dist/types/pool/uma/oracle/utils.d.ts +0 -24
- package/dist/types/pool/uma/oracle/utils.d.ts.map +0 -1
- package/dist/types/pool/uma/utils.d.ts +0 -18
- package/dist/types/pool/uma/utils.d.ts.map +0 -1
- package/src/pool/TransactionManager.ts +0 -73
- package/src/pool/index.ts +0 -1
- package/src/pool/poolClient.ts +0 -849
- package/src/pool/uma/across/constants.ts +0 -2
- package/src/pool/uma/across/index.ts +0 -2
- package/src/pool/uma/across/transactionManager.ts +0 -73
- package/src/pool/uma/clients/erc20/README.md +0 -29
- package/src/pool/uma/clients/erc20/client.e2e.ts +0 -26
- package/src/pool/uma/clients/erc20/client.ts +0 -67
- package/src/pool/uma/clients/erc20/index.ts +0 -1
- package/src/pool/uma/clients/index.ts +0 -1
- package/src/pool/uma/index.ts +0 -33
- package/src/pool/uma/oracle/index.ts +0 -2
- package/src/pool/uma/oracle/utils.ts +0 -38
- package/src/pool/uma/utils.test.ts +0 -24
- package/src/pool/uma/utils.ts +0 -53
|
@@ -1,899 +0,0 @@
|
|
|
1
|
-
import { __assign, __awaiter, __generator, __spreadArray } from "tslib";
|
|
2
|
-
import assert from "assert";
|
|
3
|
-
import * as uma from "./uma";
|
|
4
|
-
import { bnZero, toBNWei, fixedPointAdjustment, calcPeriodicCompoundInterest, calcApr, BigNumber, fromWei, } from "../utils";
|
|
5
|
-
import { ethers } from "ethers";
|
|
6
|
-
import set from "lodash/set";
|
|
7
|
-
import get from "lodash/get";
|
|
8
|
-
import has from "lodash/has";
|
|
9
|
-
import { calculateInstantaneousRate } from "../lpFeeCalculator";
|
|
10
|
-
import { hubPool, acrossConfigStore } from "../contracts";
|
|
11
|
-
import { AcceleratingDistributor__factory, MerkleDistributor__factory, } from "../typechain";
|
|
12
|
-
var erc20 = uma.clients.erc20;
|
|
13
|
-
var loop = uma.utils.loop;
|
|
14
|
-
var TransactionManager = uma.across.TransactionManager;
|
|
15
|
-
var _a = uma.across.constants, SECONDS_PER_YEAR = _a.SECONDS_PER_YEAR, DEFAULT_BLOCK_DELTA = _a.DEFAULT_BLOCK_DELTA;
|
|
16
|
-
var AddressZero = ethers.constants.AddressZero;
|
|
17
|
-
var PoolState = /** @class */ (function () {
|
|
18
|
-
function PoolState(contract, address) {
|
|
19
|
-
this.contract = contract;
|
|
20
|
-
this.address = address;
|
|
21
|
-
}
|
|
22
|
-
PoolState.prototype.read = function (l1Token, latestBlock, previousBlock) {
|
|
23
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
24
|
-
var exchangeRatePrevious, exchangeRateCurrent, pooledToken, liquidityUtilizationCurrent;
|
|
25
|
-
return __generator(this, function (_a) {
|
|
26
|
-
switch (_a.label) {
|
|
27
|
-
case 0: return [4 /*yield*/, this.exchangeRateAtBlock(l1Token, previousBlock || latestBlock - 1)];
|
|
28
|
-
case 1:
|
|
29
|
-
exchangeRatePrevious = _a.sent();
|
|
30
|
-
return [4 /*yield*/, this.contract.callStatic.exchangeRateCurrent(l1Token)];
|
|
31
|
-
case 2:
|
|
32
|
-
exchangeRateCurrent = _a.sent();
|
|
33
|
-
return [4 /*yield*/, this.contract.pooledTokens(l1Token)];
|
|
34
|
-
case 3:
|
|
35
|
-
pooledToken = _a.sent();
|
|
36
|
-
return [4 /*yield*/, this.contract.callStatic.liquidityUtilizationCurrent(l1Token)];
|
|
37
|
-
case 4:
|
|
38
|
-
liquidityUtilizationCurrent = _a.sent();
|
|
39
|
-
return [2 /*return*/, __assign({ address: this.address, l1Token: l1Token, latestBlock: latestBlock, previousBlock: previousBlock, exchangeRatePrevious: exchangeRatePrevious, exchangeRateCurrent: exchangeRateCurrent, liquidityUtilizationCurrent: liquidityUtilizationCurrent }, pooledToken)];
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
|
-
PoolState.prototype.exchangeRateAtBlock = function (l1Token, blockTag) {
|
|
45
|
-
return this.contract.callStatic.exchangeRateCurrent(l1Token, { blockTag: blockTag });
|
|
46
|
-
};
|
|
47
|
-
return PoolState;
|
|
48
|
-
}());
|
|
49
|
-
var PoolEventState = /** @class */ (function () {
|
|
50
|
-
function PoolEventState(contract, startBlock) {
|
|
51
|
-
if (startBlock === void 0) { startBlock = 0; }
|
|
52
|
-
var _this = this;
|
|
53
|
-
this.contract = contract;
|
|
54
|
-
this.startBlock = startBlock;
|
|
55
|
-
this.seen = new Set();
|
|
56
|
-
// maintain ordered unique list of events so we can calculate state
|
|
57
|
-
this.events = [];
|
|
58
|
-
this.makeId = function (params) {
|
|
59
|
-
return uma.oracle.utils.eventKey(params);
|
|
60
|
-
};
|
|
61
|
-
this.filterSeen = function (params) {
|
|
62
|
-
var seen = _this.hasEvent(params);
|
|
63
|
-
if (!seen)
|
|
64
|
-
_this.addEvent(params);
|
|
65
|
-
return !seen;
|
|
66
|
-
};
|
|
67
|
-
this.processEvent = function (event) {
|
|
68
|
-
if (!_this.filterSeen(event))
|
|
69
|
-
return;
|
|
70
|
-
_this.events = uma.oracle.utils.insertOrderedAscending(_this.events, event, _this.makeId);
|
|
71
|
-
};
|
|
72
|
-
this.processEvents = function (events) {
|
|
73
|
-
events.forEach(_this.processEvent);
|
|
74
|
-
};
|
|
75
|
-
this.makeEventFromLog = function (log) {
|
|
76
|
-
var description = _this.iface.parseLog(log);
|
|
77
|
-
return __assign(__assign(__assign({}, log), description), { event: description.name, eventSignature: description.signature });
|
|
78
|
-
};
|
|
79
|
-
this.iface = new ethers.utils.Interface(hubPool.Factory.abi);
|
|
80
|
-
}
|
|
81
|
-
PoolEventState.prototype.hasEvent = function (params) {
|
|
82
|
-
return this.seen.has(this.makeId(params));
|
|
83
|
-
};
|
|
84
|
-
PoolEventState.prototype.addEvent = function (params) {
|
|
85
|
-
this.seen.add(this.makeId(params));
|
|
86
|
-
};
|
|
87
|
-
PoolEventState.prototype.read = function (endBlock, l1TokenAddress, userAddress) {
|
|
88
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
89
|
-
var events, _a, _b, _c, _d;
|
|
90
|
-
return __generator(this, function (_e) {
|
|
91
|
-
switch (_e.label) {
|
|
92
|
-
case 0:
|
|
93
|
-
_b = (_a = Promise).all;
|
|
94
|
-
_c = [[]];
|
|
95
|
-
return [4 /*yield*/, this.contract.queryFilter(this.contract.filters.LiquidityAdded(l1TokenAddress, undefined, undefined, userAddress), this.startBlock, endBlock)];
|
|
96
|
-
case 1:
|
|
97
|
-
_d = [__spreadArray.apply(void 0, _c.concat([(_e.sent()), true]))];
|
|
98
|
-
return [4 /*yield*/, this.contract.queryFilter(this.contract.filters.LiquidityRemoved(l1TokenAddress, undefined, undefined, userAddress), this.startBlock, endBlock)];
|
|
99
|
-
case 2: return [4 /*yield*/, _b.apply(_a, [__spreadArray.apply(void 0, _d.concat([(_e.sent()), true]))])];
|
|
100
|
-
case 3:
|
|
101
|
-
events = _e.sent();
|
|
102
|
-
this.processEvents(events);
|
|
103
|
-
return [2 /*return*/, hubPool.getEventState(this.events)];
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
};
|
|
108
|
-
PoolEventState.prototype.getL1TokenFromReceipt = function (receipt) {
|
|
109
|
-
var _this = this;
|
|
110
|
-
var events = receipt.logs
|
|
111
|
-
.filter(function (log) { return ethers.utils.getAddress(log.address) === ethers.utils.getAddress(_this.contract.address); })
|
|
112
|
-
.map(this.makeEventFromLog);
|
|
113
|
-
// save these events
|
|
114
|
-
this.processEvents(events);
|
|
115
|
-
// only process token receipt events, because we just want the l1 token involved with this transfer
|
|
116
|
-
var eventState = hubPool.getEventState(events);
|
|
117
|
-
// event state is keyed by l1token address
|
|
118
|
-
var l1Tokens = Object.keys(eventState);
|
|
119
|
-
assert(l1Tokens.length, "Token not found from events");
|
|
120
|
-
assert(l1Tokens.length === 1, "Multiple tokens found from events");
|
|
121
|
-
return l1Tokens[0];
|
|
122
|
-
};
|
|
123
|
-
PoolEventState.prototype.readTxReceipt = function (receipt) {
|
|
124
|
-
var _this = this;
|
|
125
|
-
var events = receipt.logs
|
|
126
|
-
.filter(function (log) { return ethers.utils.getAddress(log.address) === ethers.utils.getAddress(_this.contract.address); })
|
|
127
|
-
.map(this.makeEventFromLog);
|
|
128
|
-
this.processEvents(events);
|
|
129
|
-
return hubPool.getEventState(this.events);
|
|
130
|
-
};
|
|
131
|
-
return PoolEventState;
|
|
132
|
-
}());
|
|
133
|
-
export { PoolEventState };
|
|
134
|
-
var UserState = /** @class */ (function () {
|
|
135
|
-
function UserState(contract, userAddress, startBlock, acceleratingDistributorContractAddress) {
|
|
136
|
-
if (startBlock === void 0) { startBlock = 0; }
|
|
137
|
-
if (acceleratingDistributorContractAddress === void 0) { acceleratingDistributorContractAddress = ""; }
|
|
138
|
-
var _this = this;
|
|
139
|
-
this.contract = contract;
|
|
140
|
-
this.userAddress = userAddress;
|
|
141
|
-
this.startBlock = startBlock;
|
|
142
|
-
this.acceleratingDistributorContractAddress = acceleratingDistributorContractAddress;
|
|
143
|
-
this.seen = new Set();
|
|
144
|
-
this.events = [];
|
|
145
|
-
this.filterSeen = function (params) {
|
|
146
|
-
var seen = _this.hasEvent(params);
|
|
147
|
-
if (!seen)
|
|
148
|
-
_this.addEvent(params);
|
|
149
|
-
return !seen;
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
UserState.prototype.makeId = function (params) {
|
|
153
|
-
return uma.oracle.utils.eventKey(params);
|
|
154
|
-
};
|
|
155
|
-
UserState.prototype.hasEvent = function (params) {
|
|
156
|
-
return this.seen.has(this.makeId(params));
|
|
157
|
-
};
|
|
158
|
-
UserState.prototype.addEvent = function (params) {
|
|
159
|
-
this.seen.add(this.makeId(params));
|
|
160
|
-
};
|
|
161
|
-
/**
|
|
162
|
-
* readEvents. Fetch and cache events for the user.
|
|
163
|
-
*
|
|
164
|
-
* @param {number} endBlock
|
|
165
|
-
*/
|
|
166
|
-
UserState.prototype.readEvents = function (endBlock) {
|
|
167
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
168
|
-
var userAddress, events, _a, _b, _c, _d;
|
|
169
|
-
var _this = this;
|
|
170
|
-
return __generator(this, function (_e) {
|
|
171
|
-
switch (_e.label) {
|
|
172
|
-
case 0:
|
|
173
|
-
if (endBlock <= this.startBlock)
|
|
174
|
-
return [2 /*return*/, []];
|
|
175
|
-
userAddress = this.userAddress;
|
|
176
|
-
_b = (_a = Promise).all;
|
|
177
|
-
_c = [[]];
|
|
178
|
-
return [4 /*yield*/, this.contract.queryFilter(this.contract.filters.Transfer(userAddress, undefined), this.startBlock, endBlock)];
|
|
179
|
-
case 1:
|
|
180
|
-
_d = [__spreadArray.apply(void 0, _c.concat([(_e.sent()), true]))];
|
|
181
|
-
return [4 /*yield*/, this.contract.queryFilter(this.contract.filters.Transfer(undefined, userAddress), this.startBlock, endBlock)];
|
|
182
|
-
case 2: return [4 /*yield*/, _b.apply(_a, [__spreadArray.apply(void 0, _d.concat([(_e.sent()), true]))])];
|
|
183
|
-
case 3:
|
|
184
|
-
events = (_e.sent())
|
|
185
|
-
// filter out events we have seen
|
|
186
|
-
.filter(this.filterSeen)
|
|
187
|
-
// filter out mint/burn transfers
|
|
188
|
-
.filter(function (event) {
|
|
189
|
-
// ignore mint events
|
|
190
|
-
return event.args.from !== AddressZero &&
|
|
191
|
-
// ignore burn events
|
|
192
|
-
event.args.to !== AddressZero &&
|
|
193
|
-
// ignore AD transfer events in
|
|
194
|
-
event.args.to !== _this.acceleratingDistributorContractAddress &&
|
|
195
|
-
// ignore AD transfer events out
|
|
196
|
-
event.args.from !== _this.acceleratingDistributorContractAddress &&
|
|
197
|
-
// ignore self transfer events
|
|
198
|
-
event.args.from !== event.args.to;
|
|
199
|
-
})
|
|
200
|
-
.flat();
|
|
201
|
-
this.events = this.events.concat(events).sort(function (a, b) {
|
|
202
|
-
if (a.blockNumber !== b.blockNumber)
|
|
203
|
-
return a.blockNumber - b.blockNumber;
|
|
204
|
-
if (a.transactionIndex !== b.transactionIndex)
|
|
205
|
-
return a.transactionIndex - b.transactionIndex;
|
|
206
|
-
if (a.logIndex !== b.logIndex)
|
|
207
|
-
return a.logIndex - b.logIndex;
|
|
208
|
-
throw new Error("Duplicate events at tx hash: " + a.transactionHash);
|
|
209
|
-
});
|
|
210
|
-
// ethers queries are inclusive [start,end] unless start === end, then exclusive (start,end). we increment to make sure we dont see same event twice
|
|
211
|
-
this.startBlock = endBlock + 1;
|
|
212
|
-
return [2 /*return*/, this.events];
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
});
|
|
216
|
-
};
|
|
217
|
-
/**
|
|
218
|
-
* read. Reads the state for the user, building state from events as well as contract calls.
|
|
219
|
-
*
|
|
220
|
-
* @param {number} endBlock
|
|
221
|
-
*/
|
|
222
|
-
UserState.prototype.read = function (endBlock) {
|
|
223
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
224
|
-
var userAddress, transferEvents, state, balanceTransferred;
|
|
225
|
-
var _a;
|
|
226
|
-
var _b;
|
|
227
|
-
return __generator(this, function (_c) {
|
|
228
|
-
switch (_c.label) {
|
|
229
|
-
case 0:
|
|
230
|
-
userAddress = this.userAddress;
|
|
231
|
-
return [4 /*yield*/, this.readEvents(endBlock)];
|
|
232
|
-
case 1:
|
|
233
|
-
transferEvents = _c.sent();
|
|
234
|
-
state = uma.clients.erc20.getEventState(transferEvents);
|
|
235
|
-
balanceTransferred = ((_b = state === null || state === void 0 ? void 0 : state.balances) === null || _b === void 0 ? void 0 : _b[userAddress]) || "0";
|
|
236
|
-
_a = {
|
|
237
|
-
transferEvents: transferEvents,
|
|
238
|
-
balanceTransferred: balanceTransferred,
|
|
239
|
-
address: userAddress
|
|
240
|
-
};
|
|
241
|
-
return [4 /*yield*/, this.contract.balanceOf(userAddress)];
|
|
242
|
-
case 2: return [2 /*return*/, (_a.balanceOf = _c.sent(),
|
|
243
|
-
_a)];
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
});
|
|
247
|
-
};
|
|
248
|
-
return UserState;
|
|
249
|
-
}());
|
|
250
|
-
export function calculateRemoval(amountWei, percentWei) {
|
|
251
|
-
var receive = amountWei.mul(percentWei).div(fixedPointAdjustment);
|
|
252
|
-
var remain = amountWei.sub(receive);
|
|
253
|
-
return {
|
|
254
|
-
receive: receive.toString(),
|
|
255
|
-
remain: remain.toString(),
|
|
256
|
-
};
|
|
257
|
-
}
|
|
258
|
-
// params here mimic the user object type
|
|
259
|
-
export function previewRemoval(values, percentFloat) {
|
|
260
|
-
var percentWei = toBNWei(percentFloat);
|
|
261
|
-
return {
|
|
262
|
-
position: __assign({}, calculateRemoval(BigNumber.from(values.totalDeposited), percentWei)),
|
|
263
|
-
fees: __assign({}, calculateRemoval(BigNumber.from(values.feesEarned), percentWei)),
|
|
264
|
-
total: __assign({}, calculateRemoval(BigNumber.from(values.positionValue), percentWei)),
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
function joinUserState(poolState, tokenEventState, userState, transferValue, cumulativeStakeBalance, cumulativeStakeClaimBalance) {
|
|
268
|
-
if (transferValue === void 0) { transferValue = bnZero; }
|
|
269
|
-
if (cumulativeStakeBalance === void 0) { cumulativeStakeBalance = bnZero; }
|
|
270
|
-
if (cumulativeStakeClaimBalance === void 0) { cumulativeStakeClaimBalance = bnZero; }
|
|
271
|
-
var positionValue = BigNumber.from(poolState.exchangeRateCurrent)
|
|
272
|
-
.mul(userState.balanceOf.add(cumulativeStakeBalance))
|
|
273
|
-
.div(fixedPointAdjustment);
|
|
274
|
-
var totalDeposited = BigNumber.from((tokenEventState === null || tokenEventState === void 0 ? void 0 : tokenEventState.tokenBalances[userState.address]) || "0").add(cumulativeStakeClaimBalance);
|
|
275
|
-
var feesEarned = positionValue.sub(totalDeposited.add(transferValue));
|
|
276
|
-
return {
|
|
277
|
-
address: userState.address,
|
|
278
|
-
poolAddress: poolState.address,
|
|
279
|
-
lpTokens: userState.balanceOf.toString(),
|
|
280
|
-
positionValue: positionValue.toString(),
|
|
281
|
-
totalDeposited: totalDeposited.toString(),
|
|
282
|
-
feesEarned: feesEarned.toString(),
|
|
283
|
-
};
|
|
284
|
-
}
|
|
285
|
-
function joinPoolState(poolState, latestBlock, previousBlock, rateModel) {
|
|
286
|
-
var totalPoolSize = poolState.liquidReserves.add(poolState.utilizedReserves);
|
|
287
|
-
var secondsElapsed = latestBlock.timestamp - previousBlock.timestamp;
|
|
288
|
-
var blocksElapsed = latestBlock.number - previousBlock.number;
|
|
289
|
-
var exchangeRatePrevious = poolState.exchangeRatePrevious.toString();
|
|
290
|
-
var exchangeRateCurrent = poolState.exchangeRateCurrent.toString();
|
|
291
|
-
var liquidityUtilizationCurrent = poolState.liquidityUtilizationCurrent.toString();
|
|
292
|
-
var estimatedApy = calcPeriodicCompoundInterest(exchangeRatePrevious, exchangeRateCurrent, secondsElapsed, SECONDS_PER_YEAR);
|
|
293
|
-
var estimatedApr = calcApr(exchangeRatePrevious, exchangeRateCurrent, secondsElapsed, SECONDS_PER_YEAR);
|
|
294
|
-
var projectedApr = "";
|
|
295
|
-
if (rateModel) {
|
|
296
|
-
projectedApr = fromWei(calculateInstantaneousRate(rateModel, liquidityUtilizationCurrent)
|
|
297
|
-
.mul(liquidityUtilizationCurrent)
|
|
298
|
-
.div(fixedPointAdjustment));
|
|
299
|
-
}
|
|
300
|
-
return {
|
|
301
|
-
address: poolState.address,
|
|
302
|
-
totalPoolSize: totalPoolSize.toString(),
|
|
303
|
-
l1Token: poolState.l1Token,
|
|
304
|
-
lpToken: poolState.lpToken,
|
|
305
|
-
liquidReserves: poolState.liquidReserves.toString(),
|
|
306
|
-
exchangeRateCurrent: poolState.exchangeRateCurrent.toString(),
|
|
307
|
-
exchangeRatePrevious: poolState.exchangeRatePrevious.toString(),
|
|
308
|
-
estimatedApy: estimatedApy,
|
|
309
|
-
estimatedApr: estimatedApr,
|
|
310
|
-
blocksElapsed: blocksElapsed,
|
|
311
|
-
secondsElapsed: secondsElapsed,
|
|
312
|
-
projectedApr: projectedApr,
|
|
313
|
-
utilizedReserves: poolState.utilizedReserves.toString(),
|
|
314
|
-
liquidityUtilizationCurrent: liquidityUtilizationCurrent,
|
|
315
|
-
};
|
|
316
|
-
}
|
|
317
|
-
var ReadPoolClient = /** @class */ (function () {
|
|
318
|
-
function ReadPoolClient(address, provider) {
|
|
319
|
-
this.address = address;
|
|
320
|
-
this.provider = provider;
|
|
321
|
-
this.contract = hubPool.connect(address, this.provider);
|
|
322
|
-
this.poolState = new PoolState(this.contract, this.address);
|
|
323
|
-
}
|
|
324
|
-
ReadPoolClient.prototype.read = function (tokenAddress, latestBlock) {
|
|
325
|
-
return this.poolState.read(tokenAddress, latestBlock);
|
|
326
|
-
};
|
|
327
|
-
return ReadPoolClient;
|
|
328
|
-
}());
|
|
329
|
-
export { ReadPoolClient };
|
|
330
|
-
export function validateWithdraw(pool, user, lpTokenAmount) {
|
|
331
|
-
var l1TokensToReturn = BigNumber.from(lpTokenAmount).mul(pool.exchangeRateCurrent).div(fixedPointAdjustment);
|
|
332
|
-
assert(BigNumber.from(l1TokensToReturn).gt("0"), "Must withdraw amount greater than 0");
|
|
333
|
-
assert(BigNumber.from(lpTokenAmount).lte(user.lpTokens), "You cannot withdraw more than you have");
|
|
334
|
-
return { lpTokenAmount: lpTokenAmount, l1TokensToReturn: l1TokensToReturn.toString() };
|
|
335
|
-
}
|
|
336
|
-
var Client = /** @class */ (function () {
|
|
337
|
-
function Client(config, deps, emit) {
|
|
338
|
-
this.config = config;
|
|
339
|
-
this.deps = deps;
|
|
340
|
-
this.emit = emit;
|
|
341
|
-
this.transactionManagers = {};
|
|
342
|
-
this.state = { pools: {}, users: {}, transactions: {} };
|
|
343
|
-
this.erc20s = {};
|
|
344
|
-
this.intervalStarted = false;
|
|
345
|
-
this.exchangeRateTable = {};
|
|
346
|
-
this.userServices = {};
|
|
347
|
-
config.chainId = config.chainId || 1;
|
|
348
|
-
this.hubPool = this.createHubPoolContract(deps.provider);
|
|
349
|
-
this.acceleratingDistributor = this.createAcceleratingDistributorContract(deps.provider);
|
|
350
|
-
this.merkleDistributor = this.createMerkleDistributorContract(deps.provider);
|
|
351
|
-
this.poolEvents = new PoolEventState(this.hubPool, this.config.hubPoolStartBlock);
|
|
352
|
-
this.configStoreClient = new acrossConfigStore.Client(config.configStoreAddress, deps.provider);
|
|
353
|
-
}
|
|
354
|
-
Client.prototype.getOrCreateErc20Contract = function (address) {
|
|
355
|
-
if (this.erc20s[address])
|
|
356
|
-
return this.erc20s[address];
|
|
357
|
-
this.erc20s[address] = erc20.connect(address, this.deps.provider);
|
|
358
|
-
return this.erc20s[address];
|
|
359
|
-
};
|
|
360
|
-
Client.prototype.getOrCreatePoolContract = function () {
|
|
361
|
-
return this.hubPool;
|
|
362
|
-
};
|
|
363
|
-
Client.prototype.createHubPoolContract = function (signerOrProvider) {
|
|
364
|
-
return hubPool.connect(this.config.hubPoolAddress, signerOrProvider);
|
|
365
|
-
};
|
|
366
|
-
Client.prototype.getOrCreatePoolEvents = function () {
|
|
367
|
-
return this.poolEvents;
|
|
368
|
-
};
|
|
369
|
-
Client.prototype.createAcceleratingDistributorContract = function (signerOrProvider) {
|
|
370
|
-
return AcceleratingDistributor__factory.connect(this.config.acceleratingDistributorAddress, signerOrProvider);
|
|
371
|
-
};
|
|
372
|
-
Client.prototype.createMerkleDistributorContract = function (signerOrProvider) {
|
|
373
|
-
return MerkleDistributor__factory.connect(this.config.merkleDistributorAddress, signerOrProvider);
|
|
374
|
-
};
|
|
375
|
-
Client.prototype.getOrCreateAcceleratingDistributorContract = function () {
|
|
376
|
-
return this.acceleratingDistributor;
|
|
377
|
-
};
|
|
378
|
-
Client.prototype.getOrCreateMerkleDistributorContract = function () {
|
|
379
|
-
return this.merkleDistributor;
|
|
380
|
-
};
|
|
381
|
-
Client.prototype.getOrCreateUserService = function (userAddress, tokenAddress) {
|
|
382
|
-
if (has(this.userServices, [tokenAddress, userAddress]))
|
|
383
|
-
return get(this.userServices, [tokenAddress, userAddress]);
|
|
384
|
-
var erc20Contract = this.getOrCreateErc20Contract(tokenAddress);
|
|
385
|
-
var userService = new UserState(erc20Contract, userAddress);
|
|
386
|
-
// this service is stateful now, so needs to be cached
|
|
387
|
-
set(this.userServices, [tokenAddress, userAddress], userService);
|
|
388
|
-
return userService;
|
|
389
|
-
};
|
|
390
|
-
Client.prototype.updateExchangeRateTable = function (l1TokenAddress, exchangeRateTable) {
|
|
391
|
-
if (!this.exchangeRateTable[l1TokenAddress])
|
|
392
|
-
this.exchangeRateTable[l1TokenAddress] = {};
|
|
393
|
-
this.exchangeRateTable[l1TokenAddress] = __assign(__assign({}, this.exchangeRateTable[l1TokenAddress]), exchangeRateTable);
|
|
394
|
-
return this.exchangeRateTable[l1TokenAddress];
|
|
395
|
-
};
|
|
396
|
-
Client.prototype.resolveStakingData = function (lpToken, l1TokenAddress, userState) {
|
|
397
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
398
|
-
var acceleratingDistributorContract, merkleDistributorContract, poolContract, claimList, amountOfLPClaimed, cumulativeBalance;
|
|
399
|
-
var _this = this;
|
|
400
|
-
return __generator(this, function (_a) {
|
|
401
|
-
switch (_a.label) {
|
|
402
|
-
case 0:
|
|
403
|
-
assert(this.config.acceleratingDistributorAddress, "Must have the accelerating distributor address");
|
|
404
|
-
assert(this.config.merkleDistributorAddress, "Must have the merkle distributor address");
|
|
405
|
-
acceleratingDistributorContract = this.getOrCreateAcceleratingDistributorContract();
|
|
406
|
-
merkleDistributorContract = this.getOrCreateMerkleDistributorContract();
|
|
407
|
-
poolContract = this.getOrCreatePoolContract();
|
|
408
|
-
return [4 /*yield*/, merkleDistributorContract.queryFilter(merkleDistributorContract.filters.Claimed(undefined, undefined, userState.address, undefined, undefined, lpToken))];
|
|
409
|
-
case 1:
|
|
410
|
-
claimList = _a.sent();
|
|
411
|
-
return [4 /*yield*/, Promise.all(claimList.map(function (claim) { return __awaiter(_this, void 0, void 0, function () {
|
|
412
|
-
var _a, _b;
|
|
413
|
-
return __generator(this, function (_c) {
|
|
414
|
-
switch (_c.label) {
|
|
415
|
-
case 0:
|
|
416
|
-
_b = (_a = claim.args.amount).mul;
|
|
417
|
-
return [4 /*yield*/, poolContract.callStatic.exchangeRateCurrent(l1TokenAddress, { blockTag: claim.blockNumber })];
|
|
418
|
-
case 1: return [2 /*return*/, _b.apply(_a, [_c.sent()])];
|
|
419
|
-
}
|
|
420
|
-
});
|
|
421
|
-
}); }))];
|
|
422
|
-
case 2:
|
|
423
|
-
amountOfLPClaimed = (_a.sent()).reduce(function (prev, acc) { return acc.add(prev); }, bnZero);
|
|
424
|
-
return [4 /*yield*/, acceleratingDistributorContract.getUserStake(lpToken, userState.address)];
|
|
425
|
-
case 3:
|
|
426
|
-
cumulativeBalance = (_a.sent()).cumulativeBalance;
|
|
427
|
-
return [2 /*return*/, {
|
|
428
|
-
cumulativeBalance: cumulativeBalance,
|
|
429
|
-
amountAirdropped: amountOfLPClaimed,
|
|
430
|
-
}];
|
|
431
|
-
}
|
|
432
|
-
});
|
|
433
|
-
});
|
|
434
|
-
};
|
|
435
|
-
// calculates the value of each LP token transfer at the block it was sent. this only works if we have archive node
|
|
436
|
-
Client.prototype.calculateLpTransferValue = function (l1TokenAddress, userState) {
|
|
437
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
438
|
-
var contract, pool, blockNumbers, exchangeRateTable, _a, _b, _c, _d;
|
|
439
|
-
var _this = this;
|
|
440
|
-
return __generator(this, function (_e) {
|
|
441
|
-
switch (_e.label) {
|
|
442
|
-
case 0:
|
|
443
|
-
assert(this.config.hasArchive, "Can only calculate historical lp values with archive node");
|
|
444
|
-
contract = this.getOrCreatePoolContract();
|
|
445
|
-
pool = new PoolState(contract, this.config.hubPoolAddress);
|
|
446
|
-
blockNumbers = userState.transferEvents
|
|
447
|
-
.map(function (x) { return x.blockNumber; })
|
|
448
|
-
// we are going to lookup exchange rates for block numbers only if we dont already have it
|
|
449
|
-
// its possible these values do not exist, so to prevent crashing do optional chaining
|
|
450
|
-
.filter(function (blockNumber) { var _a, _b; return !((_b = (_a = _this.exchangeRateTable) === null || _a === void 0 ? void 0 : _a[l1TokenAddress]) === null || _b === void 0 ? void 0 : _b[blockNumber]); });
|
|
451
|
-
_a = this.updateExchangeRateTable;
|
|
452
|
-
_b = [l1TokenAddress];
|
|
453
|
-
_d = (_c = Object).fromEntries;
|
|
454
|
-
return [4 /*yield*/, Promise.all(blockNumbers.map(function (blockNumber) { return __awaiter(_this, void 0, void 0, function () {
|
|
455
|
-
var _a;
|
|
456
|
-
return __generator(this, function (_b) {
|
|
457
|
-
switch (_b.label) {
|
|
458
|
-
case 0:
|
|
459
|
-
_a = [blockNumber];
|
|
460
|
-
return [4 /*yield*/, pool.exchangeRateAtBlock(l1TokenAddress, blockNumber)];
|
|
461
|
-
case 1: return [2 /*return*/, _a.concat([_b.sent()])];
|
|
462
|
-
}
|
|
463
|
-
});
|
|
464
|
-
}); }))];
|
|
465
|
-
case 1:
|
|
466
|
-
exchangeRateTable = _a.apply(this, _b.concat([_d.apply(_c, [_e.sent()])]));
|
|
467
|
-
return [2 /*return*/, userState.transferEvents.reduce(function (result, transfer) {
|
|
468
|
-
var exchangeRate = exchangeRateTable[transfer.blockNumber];
|
|
469
|
-
if (transfer.args.to === userState.address) {
|
|
470
|
-
return result.add(transfer.args.value.mul(exchangeRate).div(fixedPointAdjustment));
|
|
471
|
-
}
|
|
472
|
-
if (transfer.args.from === userState.address) {
|
|
473
|
-
return result.sub(transfer.args.value.mul(exchangeRate).div(fixedPointAdjustment));
|
|
474
|
-
}
|
|
475
|
-
// we make sure to filter out any transfers where to/from is the same user
|
|
476
|
-
return result;
|
|
477
|
-
}, bnZero)];
|
|
478
|
-
}
|
|
479
|
-
});
|
|
480
|
-
});
|
|
481
|
-
};
|
|
482
|
-
Client.prototype.getOrCreateTransactionManager = function (signer, address) {
|
|
483
|
-
var _this = this;
|
|
484
|
-
if (this.transactionManagers[address])
|
|
485
|
-
return this.transactionManagers[address];
|
|
486
|
-
var txman = TransactionManager({ confirmations: this.config.confirmations }, signer, function (event, id, data) {
|
|
487
|
-
if (event === "submitted") {
|
|
488
|
-
_this.state.transactions[id].state = event;
|
|
489
|
-
_this.state.transactions[id].hash = data;
|
|
490
|
-
_this.emit(["transactions", id], __assign({}, _this.state.transactions[id]));
|
|
491
|
-
}
|
|
492
|
-
if (event === "mined") {
|
|
493
|
-
var txReceipt = data;
|
|
494
|
-
_this.state.transactions[id].state = event;
|
|
495
|
-
_this.state.transactions[id].receipt = txReceipt;
|
|
496
|
-
_this.emit(["transactions", id], __assign({}, _this.state.transactions[id]));
|
|
497
|
-
// trigger pool and user update for a known mined transaction
|
|
498
|
-
var tx = _this.state.transactions[id];
|
|
499
|
-
_this.updateUserWithTransaction(tx.fromAddress, txReceipt).catch(function (err) {
|
|
500
|
-
_this.emit(["error"], err);
|
|
501
|
-
});
|
|
502
|
-
}
|
|
503
|
-
if (event === "error") {
|
|
504
|
-
_this.state.transactions[id].state = event;
|
|
505
|
-
_this.state.transactions[id].error = data;
|
|
506
|
-
_this.emit(["transactions", id], __assign({}, _this.state.transactions[id]));
|
|
507
|
-
}
|
|
508
|
-
});
|
|
509
|
-
this.transactionManagers[address] = txman;
|
|
510
|
-
return txman;
|
|
511
|
-
};
|
|
512
|
-
Client.prototype.addEthLiquidity = function (signer_1, l1TokenAmount_1) {
|
|
513
|
-
return __awaiter(this, arguments, void 0, function (signer, l1TokenAmount, overrides) {
|
|
514
|
-
var _a, hubPoolAddress, l1Token, userAddress, contract, txman, request, id;
|
|
515
|
-
if (overrides === void 0) { overrides = {}; }
|
|
516
|
-
return __generator(this, function (_b) {
|
|
517
|
-
switch (_b.label) {
|
|
518
|
-
case 0:
|
|
519
|
-
_a = this.config, hubPoolAddress = _a.hubPoolAddress, l1Token = _a.wethAddress;
|
|
520
|
-
return [4 /*yield*/, signer.getAddress()];
|
|
521
|
-
case 1:
|
|
522
|
-
userAddress = _b.sent();
|
|
523
|
-
contract = this.getOrCreatePoolContract();
|
|
524
|
-
txman = this.getOrCreateTransactionManager(signer, userAddress);
|
|
525
|
-
return [4 /*yield*/, contract.populateTransaction.addLiquidity(l1Token, l1TokenAmount, __assign(__assign({}, overrides), { value: l1TokenAmount }))];
|
|
526
|
-
case 2:
|
|
527
|
-
request = _b.sent();
|
|
528
|
-
id = txman.request(request);
|
|
529
|
-
this.state.transactions[id] = {
|
|
530
|
-
id: id,
|
|
531
|
-
state: "requested",
|
|
532
|
-
toAddress: hubPoolAddress,
|
|
533
|
-
fromAddress: userAddress,
|
|
534
|
-
type: "Add Liquidity",
|
|
535
|
-
description: "Adding ETH to pool",
|
|
536
|
-
request: request,
|
|
537
|
-
};
|
|
538
|
-
this.emit(["transactions", id], __assign({}, this.state.transactions[id]));
|
|
539
|
-
return [4 /*yield*/, txman.update()];
|
|
540
|
-
case 3:
|
|
541
|
-
_b.sent();
|
|
542
|
-
return [2 /*return*/, id];
|
|
543
|
-
}
|
|
544
|
-
});
|
|
545
|
-
});
|
|
546
|
-
};
|
|
547
|
-
Client.prototype.addTokenLiquidity = function (signer_1, l1Token_1, l1TokenAmount_1) {
|
|
548
|
-
return __awaiter(this, arguments, void 0, function (signer, l1Token, l1TokenAmount, overrides) {
|
|
549
|
-
var hubPoolAddress, userAddress, contract, txman, request, id;
|
|
550
|
-
if (overrides === void 0) { overrides = {}; }
|
|
551
|
-
return __generator(this, function (_a) {
|
|
552
|
-
switch (_a.label) {
|
|
553
|
-
case 0:
|
|
554
|
-
hubPoolAddress = this.config.hubPoolAddress;
|
|
555
|
-
return [4 /*yield*/, signer.getAddress()];
|
|
556
|
-
case 1:
|
|
557
|
-
userAddress = _a.sent();
|
|
558
|
-
contract = this.getOrCreatePoolContract();
|
|
559
|
-
txman = this.getOrCreateTransactionManager(signer, userAddress);
|
|
560
|
-
return [4 /*yield*/, contract.populateTransaction.addLiquidity(l1Token, l1TokenAmount, overrides)];
|
|
561
|
-
case 2:
|
|
562
|
-
request = _a.sent();
|
|
563
|
-
return [4 /*yield*/, txman.request(request)];
|
|
564
|
-
case 3:
|
|
565
|
-
id = _a.sent();
|
|
566
|
-
this.state.transactions[id] = {
|
|
567
|
-
id: id,
|
|
568
|
-
state: "requested",
|
|
569
|
-
toAddress: hubPoolAddress,
|
|
570
|
-
fromAddress: userAddress,
|
|
571
|
-
type: "Add Liquidity",
|
|
572
|
-
description: "Adding Tokens to pool",
|
|
573
|
-
request: request,
|
|
574
|
-
};
|
|
575
|
-
this.emit(["transactions", id], __assign({}, this.state.transactions[id]));
|
|
576
|
-
return [4 /*yield*/, txman.update()];
|
|
577
|
-
case 4:
|
|
578
|
-
_a.sent();
|
|
579
|
-
return [2 /*return*/, id];
|
|
580
|
-
}
|
|
581
|
-
});
|
|
582
|
-
});
|
|
583
|
-
};
|
|
584
|
-
Client.prototype.validateWithdraw = function (l1Token, userAddress, lpAmount) {
|
|
585
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
586
|
-
var poolState, userState;
|
|
587
|
-
return __generator(this, function (_a) {
|
|
588
|
-
switch (_a.label) {
|
|
589
|
-
case 0: return [4 /*yield*/, this.updatePool(l1Token)];
|
|
590
|
-
case 1:
|
|
591
|
-
_a.sent();
|
|
592
|
-
poolState = this.getPoolState(l1Token);
|
|
593
|
-
if (!!this.hasUserState(l1Token, userAddress)) return [3 /*break*/, 3];
|
|
594
|
-
return [4 /*yield*/, this.updateUser(l1Token, userAddress)];
|
|
595
|
-
case 2:
|
|
596
|
-
_a.sent();
|
|
597
|
-
_a.label = 3;
|
|
598
|
-
case 3:
|
|
599
|
-
userState = this.getUserState(poolState.l1Token, userAddress);
|
|
600
|
-
return [2 /*return*/, validateWithdraw(poolState, userState, lpAmount)];
|
|
601
|
-
}
|
|
602
|
-
});
|
|
603
|
-
});
|
|
604
|
-
};
|
|
605
|
-
Client.prototype.removeTokenLiquidity = function (signer_1, l1Token_1, lpTokenAmount_1) {
|
|
606
|
-
return __awaiter(this, arguments, void 0, function (signer, l1Token, lpTokenAmount, overrides) {
|
|
607
|
-
var hubPoolAddress, userAddress, contract, txman, request, id;
|
|
608
|
-
if (overrides === void 0) { overrides = {}; }
|
|
609
|
-
return __generator(this, function (_a) {
|
|
610
|
-
switch (_a.label) {
|
|
611
|
-
case 0:
|
|
612
|
-
hubPoolAddress = this.config.hubPoolAddress;
|
|
613
|
-
return [4 /*yield*/, signer.getAddress()];
|
|
614
|
-
case 1:
|
|
615
|
-
userAddress = _a.sent();
|
|
616
|
-
return [4 /*yield*/, this.validateWithdraw(l1Token, userAddress, lpTokenAmount)];
|
|
617
|
-
case 2:
|
|
618
|
-
_a.sent();
|
|
619
|
-
contract = this.getOrCreatePoolContract();
|
|
620
|
-
txman = this.getOrCreateTransactionManager(signer, userAddress);
|
|
621
|
-
return [4 /*yield*/, contract.populateTransaction.removeLiquidity(l1Token, lpTokenAmount, false, overrides)];
|
|
622
|
-
case 3:
|
|
623
|
-
request = _a.sent();
|
|
624
|
-
return [4 /*yield*/, txman.request(request)];
|
|
625
|
-
case 4:
|
|
626
|
-
id = _a.sent();
|
|
627
|
-
this.state.transactions[id] = {
|
|
628
|
-
id: id,
|
|
629
|
-
state: "requested",
|
|
630
|
-
toAddress: hubPoolAddress,
|
|
631
|
-
fromAddress: userAddress,
|
|
632
|
-
type: "Remove Liquidity",
|
|
633
|
-
description: "Withdrawing Tokens from pool",
|
|
634
|
-
request: request,
|
|
635
|
-
};
|
|
636
|
-
this.emit(["transactions", id], __assign({}, this.state.transactions[id]));
|
|
637
|
-
return [4 /*yield*/, txman.update()];
|
|
638
|
-
case 5:
|
|
639
|
-
_a.sent();
|
|
640
|
-
return [2 /*return*/, id];
|
|
641
|
-
}
|
|
642
|
-
});
|
|
643
|
-
});
|
|
644
|
-
};
|
|
645
|
-
Client.prototype.removeEthliquidity = function (signer_1, lpTokenAmount_1) {
|
|
646
|
-
return __awaiter(this, arguments, void 0, function (signer, lpTokenAmount, overrides) {
|
|
647
|
-
var _a, hubPoolAddress, l1Token, userAddress, contract, txman, request, id;
|
|
648
|
-
if (overrides === void 0) { overrides = {}; }
|
|
649
|
-
return __generator(this, function (_b) {
|
|
650
|
-
switch (_b.label) {
|
|
651
|
-
case 0:
|
|
652
|
-
_a = this.config, hubPoolAddress = _a.hubPoolAddress, l1Token = _a.wethAddress;
|
|
653
|
-
return [4 /*yield*/, signer.getAddress()];
|
|
654
|
-
case 1:
|
|
655
|
-
userAddress = _b.sent();
|
|
656
|
-
return [4 /*yield*/, this.validateWithdraw(l1Token, userAddress, lpTokenAmount)];
|
|
657
|
-
case 2:
|
|
658
|
-
_b.sent();
|
|
659
|
-
contract = this.getOrCreatePoolContract();
|
|
660
|
-
txman = this.getOrCreateTransactionManager(signer, userAddress);
|
|
661
|
-
return [4 /*yield*/, contract.populateTransaction.removeLiquidity(l1Token, lpTokenAmount, true, overrides)];
|
|
662
|
-
case 3:
|
|
663
|
-
request = _b.sent();
|
|
664
|
-
return [4 /*yield*/, txman.request(request)];
|
|
665
|
-
case 4:
|
|
666
|
-
id = _b.sent();
|
|
667
|
-
this.state.transactions[id] = {
|
|
668
|
-
id: id,
|
|
669
|
-
state: "requested",
|
|
670
|
-
toAddress: hubPoolAddress,
|
|
671
|
-
fromAddress: userAddress,
|
|
672
|
-
type: "Remove Liquidity",
|
|
673
|
-
description: "Withdrawing Eth from pool",
|
|
674
|
-
request: request,
|
|
675
|
-
};
|
|
676
|
-
this.emit(["transactions", id], __assign({}, this.state.transactions[id]));
|
|
677
|
-
return [4 /*yield*/, txman.update()];
|
|
678
|
-
case 5:
|
|
679
|
-
_b.sent();
|
|
680
|
-
return [2 /*return*/, id];
|
|
681
|
-
}
|
|
682
|
-
});
|
|
683
|
-
});
|
|
684
|
-
};
|
|
685
|
-
Client.prototype.getPoolState = function (l1TokenAddress) {
|
|
686
|
-
return this.state.pools[l1TokenAddress];
|
|
687
|
-
};
|
|
688
|
-
Client.prototype.hasPoolState = function (l1TokenAddress) {
|
|
689
|
-
return Boolean(this.state.pools[l1TokenAddress]);
|
|
690
|
-
};
|
|
691
|
-
Client.prototype.setUserState = function (l1TokenAddress, userAddress, state) {
|
|
692
|
-
set(this.state, ["users", userAddress, l1TokenAddress], state);
|
|
693
|
-
return state;
|
|
694
|
-
};
|
|
695
|
-
Client.prototype.getUserState = function (l1TokenAddress, userAddress) {
|
|
696
|
-
return get(this.state, ["users", userAddress, l1TokenAddress]);
|
|
697
|
-
};
|
|
698
|
-
Client.prototype.hasUserState = function (l1TokenAddress, userAddress) {
|
|
699
|
-
return has(this.state, ["users", userAddress, l1TokenAddress]);
|
|
700
|
-
};
|
|
701
|
-
Client.prototype.hasTxState = function (id) {
|
|
702
|
-
return has(this.state, ["transactions", id]);
|
|
703
|
-
};
|
|
704
|
-
Client.prototype.getTxState = function (id) {
|
|
705
|
-
return get(this.state, ["transactions", id]);
|
|
706
|
-
};
|
|
707
|
-
Client.prototype.updateAndEmitUser = function (userState, poolState, poolEventState) {
|
|
708
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
709
|
-
var l1TokenAddress, lpToken, userAddress, transferValue, _a, stakeData, tokenEventState, newUserState;
|
|
710
|
-
return __generator(this, function (_b) {
|
|
711
|
-
switch (_b.label) {
|
|
712
|
-
case 0:
|
|
713
|
-
l1TokenAddress = poolState.l1Token, lpToken = poolState.lpToken;
|
|
714
|
-
userAddress = userState.address;
|
|
715
|
-
if (!this.config.hasArchive) return [3 /*break*/, 2];
|
|
716
|
-
return [4 /*yield*/, this.calculateLpTransferValue(l1TokenAddress, userState)];
|
|
717
|
-
case 1:
|
|
718
|
-
_a = _b.sent();
|
|
719
|
-
return [3 /*break*/, 3];
|
|
720
|
-
case 2:
|
|
721
|
-
_a = bnZero;
|
|
722
|
-
_b.label = 3;
|
|
723
|
-
case 3:
|
|
724
|
-
transferValue = _a;
|
|
725
|
-
return [4 /*yield*/, this.resolveStakingData(lpToken, l1TokenAddress, userState)];
|
|
726
|
-
case 4:
|
|
727
|
-
stakeData = _b.sent();
|
|
728
|
-
tokenEventState = poolEventState[l1TokenAddress];
|
|
729
|
-
newUserState = this.setUserState(l1TokenAddress, userAddress, joinUserState(poolState, tokenEventState, userState, transferValue, stakeData.cumulativeBalance, stakeData.amountAirdropped));
|
|
730
|
-
this.emit(["users", userAddress, l1TokenAddress], newUserState);
|
|
731
|
-
return [2 /*return*/];
|
|
732
|
-
}
|
|
733
|
-
});
|
|
734
|
-
});
|
|
735
|
-
};
|
|
736
|
-
Client.prototype.updateUserWithTransaction = function (userAddress, txReceipt) {
|
|
737
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
738
|
-
var latestBlock, getPoolEventState, l1TokenAddress, poolState, poolEventState, lpToken, getUserState, userState;
|
|
739
|
-
return __generator(this, function (_a) {
|
|
740
|
-
switch (_a.label) {
|
|
741
|
-
case 0: return [4 /*yield*/, this.deps.provider.getBlock("latest")];
|
|
742
|
-
case 1:
|
|
743
|
-
latestBlock = _a.sent();
|
|
744
|
-
getPoolEventState = this.getOrCreatePoolEvents();
|
|
745
|
-
l1TokenAddress = getPoolEventState.getL1TokenFromReceipt(txReceipt);
|
|
746
|
-
return [4 /*yield*/, this.updatePool(l1TokenAddress, latestBlock)];
|
|
747
|
-
case 2:
|
|
748
|
-
_a.sent();
|
|
749
|
-
poolState = this.getPoolState(l1TokenAddress);
|
|
750
|
-
poolEventState = getPoolEventState.readTxReceipt(txReceipt);
|
|
751
|
-
lpToken = poolState.lpToken;
|
|
752
|
-
getUserState = this.getOrCreateUserService(userAddress, lpToken);
|
|
753
|
-
return [4 /*yield*/, getUserState.read(latestBlock.number)];
|
|
754
|
-
case 3:
|
|
755
|
-
userState = _a.sent();
|
|
756
|
-
return [4 /*yield*/, this.updateAndEmitUser(userState, poolState, poolEventState)];
|
|
757
|
-
case 4:
|
|
758
|
-
_a.sent();
|
|
759
|
-
return [2 /*return*/];
|
|
760
|
-
}
|
|
761
|
-
});
|
|
762
|
-
});
|
|
763
|
-
};
|
|
764
|
-
Client.prototype.updateUser = function (userAddress, l1TokenAddress) {
|
|
765
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
766
|
-
var latestBlock, poolState, lpToken, getPoolEventState, poolEventState, getUserState, userState;
|
|
767
|
-
return __generator(this, function (_a) {
|
|
768
|
-
switch (_a.label) {
|
|
769
|
-
case 0: return [4 /*yield*/, this.deps.provider.getBlock("latest")];
|
|
770
|
-
case 1:
|
|
771
|
-
latestBlock = _a.sent();
|
|
772
|
-
return [4 /*yield*/, this.updatePool(l1TokenAddress, latestBlock)];
|
|
773
|
-
case 2:
|
|
774
|
-
_a.sent();
|
|
775
|
-
poolState = this.getPoolState(l1TokenAddress);
|
|
776
|
-
lpToken = poolState.lpToken;
|
|
777
|
-
getPoolEventState = this.getOrCreatePoolEvents();
|
|
778
|
-
return [4 /*yield*/, getPoolEventState.read(latestBlock.number, l1TokenAddress, userAddress)];
|
|
779
|
-
case 3:
|
|
780
|
-
poolEventState = _a.sent();
|
|
781
|
-
getUserState = this.getOrCreateUserService(userAddress, lpToken);
|
|
782
|
-
return [4 /*yield*/, getUserState.read(latestBlock.number)];
|
|
783
|
-
case 4:
|
|
784
|
-
userState = _a.sent();
|
|
785
|
-
return [4 /*yield*/, this.updateAndEmitUser(userState, poolState, poolEventState)];
|
|
786
|
-
case 5:
|
|
787
|
-
_a.sent();
|
|
788
|
-
return [2 /*return*/];
|
|
789
|
-
}
|
|
790
|
-
});
|
|
791
|
-
});
|
|
792
|
-
};
|
|
793
|
-
Client.prototype.updatePool = function (l1TokenAddress, overrideLatestBlock) {
|
|
794
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
795
|
-
var _a, blockDelta, contract, pool, latestBlock, _b, previousBlock, state, rateModel, err_1;
|
|
796
|
-
return __generator(this, function (_c) {
|
|
797
|
-
switch (_c.label) {
|
|
798
|
-
case 0:
|
|
799
|
-
_a = this.config.blockDelta, blockDelta = _a === void 0 ? DEFAULT_BLOCK_DELTA : _a;
|
|
800
|
-
contract = this.getOrCreatePoolContract();
|
|
801
|
-
pool = new PoolState(contract, this.config.hubPoolAddress);
|
|
802
|
-
_b = overrideLatestBlock;
|
|
803
|
-
if (_b) return [3 /*break*/, 2];
|
|
804
|
-
return [4 /*yield*/, this.deps.provider.getBlock("latest")];
|
|
805
|
-
case 1:
|
|
806
|
-
_b = (_c.sent());
|
|
807
|
-
_c.label = 2;
|
|
808
|
-
case 2:
|
|
809
|
-
latestBlock = _b;
|
|
810
|
-
return [4 /*yield*/, this.deps.provider.getBlock(latestBlock.number - blockDelta)];
|
|
811
|
-
case 3:
|
|
812
|
-
previousBlock = _c.sent();
|
|
813
|
-
return [4 /*yield*/, pool.read(l1TokenAddress, latestBlock.number, previousBlock.number)];
|
|
814
|
-
case 4:
|
|
815
|
-
state = _c.sent();
|
|
816
|
-
rateModel = undefined;
|
|
817
|
-
_c.label = 5;
|
|
818
|
-
case 5:
|
|
819
|
-
_c.trys.push([5, 7, , 8]);
|
|
820
|
-
return [4 /*yield*/, this.configStoreClient.getRateModel(l1TokenAddress)];
|
|
821
|
-
case 6:
|
|
822
|
-
// Use the default rate model (i.e. not any of the routeRateModels to project the Pool's APR). This assumes
|
|
823
|
-
// that the default rate model is the most often used, but this may change in future if many different
|
|
824
|
-
// route rate models are set.
|
|
825
|
-
rateModel = _c.sent();
|
|
826
|
-
return [3 /*break*/, 8];
|
|
827
|
-
case 7:
|
|
828
|
-
err_1 = _c.sent();
|
|
829
|
-
// we could swallow this error or just log it since getting the rate model is optional,
|
|
830
|
-
// but we will just emit it to the caller and let them decide what to do with it.
|
|
831
|
-
this.emit(["error"], err_1);
|
|
832
|
-
return [3 /*break*/, 8];
|
|
833
|
-
case 8:
|
|
834
|
-
this.state.pools[l1TokenAddress] = joinPoolState(state, latestBlock, previousBlock, rateModel);
|
|
835
|
-
this.emit(["pools", l1TokenAddress], this.state.pools[l1TokenAddress]);
|
|
836
|
-
return [2 /*return*/];
|
|
837
|
-
}
|
|
838
|
-
});
|
|
839
|
-
});
|
|
840
|
-
};
|
|
841
|
-
Client.prototype.updateTransactions = function () {
|
|
842
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
843
|
-
var _i, _a, txMan, err_2;
|
|
844
|
-
return __generator(this, function (_b) {
|
|
845
|
-
switch (_b.label) {
|
|
846
|
-
case 0:
|
|
847
|
-
_i = 0, _a = Object.values(this.transactionManagers);
|
|
848
|
-
_b.label = 1;
|
|
849
|
-
case 1:
|
|
850
|
-
if (!(_i < _a.length)) return [3 /*break*/, 6];
|
|
851
|
-
txMan = _a[_i];
|
|
852
|
-
_b.label = 2;
|
|
853
|
-
case 2:
|
|
854
|
-
_b.trys.push([2, 4, , 5]);
|
|
855
|
-
return [4 /*yield*/, txMan.update()];
|
|
856
|
-
case 3:
|
|
857
|
-
_b.sent();
|
|
858
|
-
return [3 /*break*/, 5];
|
|
859
|
-
case 4:
|
|
860
|
-
err_2 = _b.sent();
|
|
861
|
-
this.emit(["error"], err_2);
|
|
862
|
-
return [3 /*break*/, 5];
|
|
863
|
-
case 5:
|
|
864
|
-
_i++;
|
|
865
|
-
return [3 /*break*/, 1];
|
|
866
|
-
case 6: return [2 /*return*/];
|
|
867
|
-
}
|
|
868
|
-
});
|
|
869
|
-
});
|
|
870
|
-
};
|
|
871
|
-
// starts transaction checking intervals, defaults to 30 seconds
|
|
872
|
-
Client.prototype.startInterval = function (delayMs) {
|
|
873
|
-
var _this = this;
|
|
874
|
-
if (delayMs === void 0) { delayMs = 30000; }
|
|
875
|
-
assert(!this.intervalStarted, "Interval already started, try stopping first");
|
|
876
|
-
this.intervalStarted = true;
|
|
877
|
-
loop(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
878
|
-
return __generator(this, function (_a) {
|
|
879
|
-
switch (_a.label) {
|
|
880
|
-
case 0:
|
|
881
|
-
assert(this.intervalStarted, "HubPool Interval Stopped");
|
|
882
|
-
return [4 /*yield*/, this.updateTransactions()];
|
|
883
|
-
case 1:
|
|
884
|
-
_a.sent();
|
|
885
|
-
return [2 /*return*/];
|
|
886
|
-
}
|
|
887
|
-
});
|
|
888
|
-
}); }, delayMs).catch(function (err) {
|
|
889
|
-
_this.emit(["error"], err);
|
|
890
|
-
});
|
|
891
|
-
};
|
|
892
|
-
// starts transaction checking intervals
|
|
893
|
-
Client.prototype.stopInterval = function () {
|
|
894
|
-
this.intervalStarted = false;
|
|
895
|
-
};
|
|
896
|
-
return Client;
|
|
897
|
-
}());
|
|
898
|
-
export { Client };
|
|
899
|
-
//# sourceMappingURL=poolClient.js.map
|