-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathlp-tokens.js
328 lines (286 loc) · 11.3 KB
/
lp-tokens.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
/** @typedef { import("../lib/context").Context } Context */
/** @typedef { import("../lib/ethereum-helper").Address } Address */
import BN from "bn.js"
import { ITruthSource } from "./truth-source.js"
import { getPastEvents, getChainID } from "../lib/ethereum-helper.js"
import { dumpDataToFile } from "../lib/file-helper.js"
import { logger } from "../lib/winston.js"
import { getDeploymentBlockNumber } from "../lib/contract-helper.js"
import { EthereumHelpers } from "@keep-network/tbtc.js"
const { callWithRetry } = EthereumHelpers
// https://etherscan.io/address/0xe6f19dab7d43317344282f803f8e8d240708174a#code
import keepEthTokenJson from "../../artifacts/KEEP-ETH-UNI-V2-Token.json"
// https://etherscan.io/address/0x38c8ffee49f286f25d25bad919ff7552e5daf081#code
import keepTbtcTokenJson from "../../artifacts/KEEP-TBTC-UNI-V2-Token.json"
import LPRewardsKEEPETHJson from "@keep-network/keep-ecdsa/artifacts/LPRewardsKEEPETH.json"
import LPRewardsKEEPTBTCJson from "@keep-network/keep-ecdsa/artifacts/LPRewardsKEEPTBTC.json"
// https://info.uniswap.org/pair/0xe6f19dab7d43317344282f803f8e8d240708174a
const KEEPETH_PAIR_ADDRESS = "0xe6f19dab7d43317344282f803f8e8d240708174a"
// https://info.uniswap.org/pair/0x38c8ffee49f286f25d25bad919ff7552e5daf081
const KEEPTBTC_PAIR_ADDRESS = "0x38c8ffee49f286f25d25bad919ff7552e5daf081"
const KEEP_IN_LP_KEEPETH_BALANCES_PATH =
"./tmp/keep-in-lp-keepeth-token-balances.json"
const KEEP_IN_LP_KEEPTBTC_BALANCES_PATH =
"./tmp/keep-in-lp-keeptbtc-token-balances.json"
export class LPTokenTruthSource extends ITruthSource {
/**
* @param {Context} context
* @param {Number} targetBlock
*/
constructor(context, targetBlock) {
super(context, targetBlock)
}
async initialize() {
const chainID = await getChainID(this.context.web3)
const keepEthTokenContract = EthereumHelpers.buildContract(
this.context.web3,
JSON.parse(keepEthTokenJson.result),
KEEPETH_PAIR_ADDRESS
)
const lpRewardKeepEthContract = EthereumHelpers.buildContract(
this.context.web3,
LPRewardsKEEPETHJson.abi,
LPRewardsKEEPETHJson.networks[chainID].address
)
const lpRewardKeepEthDeploymentBlock = await getDeploymentBlockNumber(
LPRewardsKEEPETHJson,
this.context.web3
)
const keepTbtcTokenContract = EthereumHelpers.buildContract(
this.context.web3,
JSON.parse(keepTbtcTokenJson.result),
KEEPTBTC_PAIR_ADDRESS
)
const lpRewardKeepTbtcContract = EthereumHelpers.buildContract(
this.context.web3,
LPRewardsKEEPTBTCJson.abi,
LPRewardsKEEPTBTCJson.networks[chainID].address
)
const lpRewardKeepTbtcDeploymentBlock = await getDeploymentBlockNumber(
LPRewardsKEEPTBTCJson,
this.context.web3
)
this.liquidityStaking = {
KEEPETH: {
lpTokenContract: keepEthTokenContract,
lpRewardsContract: lpRewardKeepEthContract,
lpRewardsContractName: "LPRewardsKEEPETH",
lpRewardsContractDeploymentBlock: lpRewardKeepEthDeploymentBlock,
keepInLpTokenFilePath: KEEP_IN_LP_KEEPETH_BALANCES_PATH,
lpPairAddress: KEEPETH_PAIR_ADDRESS,
},
KEEPTBTC: {
lpTokenContract: keepTbtcTokenContract,
lpRewardsContract: lpRewardKeepTbtcContract,
lpRewardsContractName: "LPRewardsKEEPTBTC",
lpRewardsContractDeploymentBlock: lpRewardKeepTbtcDeploymentBlock,
keepInLpTokenFilePath: KEEP_IN_LP_KEEPTBTC_BALANCES_PATH,
lpPairAddress: KEEPTBTC_PAIR_ADDRESS,
},
}
}
/**
* Finds all historic stakers in LP Rewards contracts based on "Staked" events
*
* @param {String} pairName LP pair name
* @param {Object} pairObj LP pair object
*
* @return {Array<Address>} All historic LP token stakers
* */
async findStakers(pairName, pairObj) {
const lpRewardsContractAddress = pairObj.lpRewardsContract.options.address
logger.info(
`looking for "Staked" events emitted from ${pairObj.lpRewardsContractName} ` +
`contract at ${lpRewardsContractAddress} when staking ${pairName} pair ` +
`${pairObj.lpTokenContract.options.address} between blocks ` +
`${pairObj.lpRewardsContractDeploymentBlock} and ${this.targetBlock}`
)
const events = await getPastEvents(
this.context.web3,
pairObj.lpRewardsContract,
"Staked",
pairObj.lpRewardsContractDeploymentBlock,
this.targetBlock
)
logger.info(`found ${events.length} LP ${pairName} token staked events`)
const lpTokenStakersSet = new Set()
events.forEach((event) => {
lpTokenStakersSet.add(event.returnValues.user)
})
logger.info(`found ${lpTokenStakersSet.size} unique historic stakers`)
return Array.from(lpTokenStakersSet)
}
/**
* Retrieves balances of LP tokens locked in LPRewards contracts for stakers.
*
* @param {Array<Address>} lpStakers LP stakers addresses.
* @param {Object} pairObj LP pair object.
*
* @return {Map<Address,BN>} Staked LP balances by lp stakers.
*/
async getLpTokenStakersBalances(lpStakers, pairObj) {
const lpBalanceByStaker = new Map()
let expectedTotalSupply = new BN(0)
logger.info(`looking for active stakers at block ${this.targetBlock}`)
for (const staker of lpStakers) {
const lpBalance = new BN(
await callWithRetry(
pairObj.lpRewardsContract.methods.balanceOf(staker),
undefined,
undefined,
this.targetBlock
)
)
if (!lpBalance.isZero()) {
lpBalanceByStaker.set(staker, lpBalance)
logger.info(
`found an active staker ${staker} with the balance of ${lpBalance}`
)
expectedTotalSupply = expectedTotalSupply.add(lpBalance)
}
}
const actualTotalSupply = new BN(
await callWithRetry(
pairObj.lpRewardsContract.methods.totalSupply(),
undefined,
undefined,
this.targetBlock
)
)
if (!expectedTotalSupply.eq(actualTotalSupply)) {
logger.error(
`sum of LP staker balances ${expectedTotalSupply} does not match
the total supply ${actualTotalSupply} of locked LP tokens
in ${pairObj.lpRewardsContractName} contract`
)
}
logger.info(
`total supply of LP Tokens locked in ${
pairObj.lpRewardsContractName
} is: ${expectedTotalSupply.toString()}`
)
return lpBalanceByStaker
}
/**
* Calculates KEEP amount for all LP stakers.
*
* @param {Map<Address, BN>} stakersBalances LP Token amounts by stakers
* @param {String} pairName LP pair name
* @param {Object} pairObj LP pair object
*
* @return {Map<Address,BN>} KEEP amounts in LP Token at the target block
*/
async calcKeepInStakersBalances(stakersBalances, pairName, pairObj) {
const totalSupply = await callWithRetry(
pairObj.lpTokenContract.methods.totalSupply(),
undefined,
undefined,
this.targetBlock
)
logger.info(
`total supply of LP ${pairName} token at block ${this.targetBlock} is: ${totalSupply}`
)
const lpReserves = await callWithRetry(
pairObj.lpTokenContract.methods.getReserves(),
undefined,
undefined,
this.targetBlock
)
logger.info(
`KEEP reserve in the KEEP liquidity pool at block ${this.targetBlock} is: ${lpReserves._reserve0}`
)
// Token reserve0 must be KEEP token 0x85eee30c52b0b379b046fb0f85f4f3dc3009afec
// which makes a LP pair token.
const lpPairData = {
keepLiquidityPool: lpReserves._reserve0,
lpTotalSupply: totalSupply,
}
let totalLpStaked = new BN(0)
let totalKeepInLpStaked = new BN(0)
const keepInLpByStakers = new Map()
for (const [stakerAddress, lpStakerBalance] of stakersBalances.entries()) {
const keepInLPToken = await this.calcKeepTokenfromLPToken(
lpStakerBalance,
lpPairData
)
keepInLpByStakers.set(stakerAddress, keepInLPToken)
totalLpStaked = totalLpStaked.add(lpStakerBalance)
totalKeepInLpStaked = totalKeepInLpStaked.add(keepInLPToken)
logger.info(
`staker: ${stakerAddress} - LP ${pairName} Balance: ${lpStakerBalance} - KEEP in LP: ${keepInLPToken}`
)
}
logger.info(
`found ${keepInLpByStakers.size} active stakers with the total of ` +
`${totalLpStaked} LP ${pairName} tokens staked. The total of KEEP asset is ${totalKeepInLpStaked}`
)
if (totalKeepInLpStaked.lte(lpPairData.keepLiquidityPool)) {
logger.error(
`total of KEEP asset ${totalKeepInLpStaked} in LP tokens must be less or equal ` +
`the total amount in the KEEP liquidity reserve ${lpPairData.keepLiquidityPool}`
)
}
dumpDataToFile(keepInLpByStakers, pairObj.keepInLpTokenFilePath)
return keepInLpByStakers
}
/**
* Calculates amount of KEEP token which makes a KEEP-ETH / KEEP-TBTC Uniswap pair.
* A Uniswap LP pair is a bookeeping tool to keep track of how much the liquidity
* stakers are owed. They store two assets of equivalent value of each, ex. KEEP-ETH.
* This means that the value of KEEP owned is dependent on the ratio of staked
* LP tokens and the total LP supply. Ratio between LP tokens and KEEP tokens
* should be equal:
* LP_staker_balance / LP_total_supply_pool == KEEP_staker_owed / KEEP_total_liquidity_pool
* Now, the number of KEEP tokens which makes a KEEP-ETH pair can be calculated
* using the following equation:
* KEEP_staker_owed = (LP_staker_balance * KEEP_total_liquidity_pool) / LP_total_supply_pool
* where:
* LP_staker_balance is retrieved from LPRewardsContract
* KEEP_total_liquidity_pool is fetched from Uniswap LP Token - lpToken.getReserves()._reserve0
* LP_total_supply_pool is fetched from Uniswap LP Token - lpToken.totalSupply()
*
* Another way to look at asset calculation in LP tokens is referring to a mint()
* function in UniswapV2Pair contract, which produces the same equation as above.
*
* References:
* Returns in Uniswap: https://app.uniswap.org/
* LP minting: https://github.com/Uniswap/uniswap-v2-core/blob/4dd59067c76dea4a0e8e4bfdda41877a6b16dedc/contracts/UniswapV2Pair.sol#L123
*
* @param {BN} lpStakerBalance LP amount staked by a staker in a LPRewardsContract
* @param {PairData} lpPairData Pair data fetched from LP Token Contract
*
* @return {BN} KEEP token amounts in LP token balance
*/
async calcKeepTokenfromLPToken(lpStakerBalance, lpPairData) {
return lpStakerBalance
.mul(new BN(lpPairData.keepLiquidityPool))
.div(new BN(lpPairData.lpTotalSupply))
}
/**
* @return {Map<Address,BN>} KEEP token amounts staked by stakers at the target block
*/
async getTokenHoldingsAtTargetBlock() {
await this.initialize()
const keepInLPsByStakers = new Map()
for (const [pairName, pairObj] of Object.entries(this.liquidityStaking)) {
const lpStakers = await this.findStakers(pairName, pairObj)
const stakersBalances = await this.getLpTokenStakersBalances(
lpStakers,
pairObj
)
const keepInLpByStakers = await this.calcKeepInStakersBalances(
stakersBalances,
pairName,
pairObj
)
keepInLpByStakers.forEach((balance, staker) => {
if (keepInLPsByStakers.has(staker)) {
keepInLPsByStakers.get(staker).iadd(balance)
} else {
keepInLPsByStakers.set(staker, new BN(balance))
}
})
}
return keepInLPsByStakers
}
}