-
Notifications
You must be signed in to change notification settings - Fork 523
/
Copy pathposition-manager.ts
185 lines (153 loc) · 6.86 KB
/
position-manager.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
/* eslint-disable prefer-const */
import {
Collect,
DecreaseLiquidity,
IncreaseLiquidity,
NonfungiblePositionManager,
Transfer
} from '../types/NonfungiblePositionManager/NonfungiblePositionManager'
import { Bundle, Position, PositionSnapshot, Token } from '../types/schema'
import { ADDRESS_ZERO, factoryContract, ZERO_BD, ZERO_BI } from '../utils/constants'
import { Address, BigInt, ethereum } from '@graphprotocol/graph-ts'
import { convertTokenToDecimal, loadTransaction } from '../utils'
function getPosition(event: ethereum.Event, tokenId: BigInt): Position | null {
let position = Position.load(tokenId.toString())
if (position === null) {
let contract = NonfungiblePositionManager.bind(event.address)
let positionCall = contract.try_positions(tokenId)
// the following call reverts in situations where the position is minted
// and deleted in the same block - from my investigation this happens
// in calls from BancorSwap
// (e.g. 0xf7867fa19aa65298fadb8d4f72d0daed5e836f3ba01f0b9b9631cdc6c36bed40)
if (!positionCall.reverted) {
let positionResult = positionCall.value
let poolAddress = factoryContract.getPool(positionResult.value2, positionResult.value3, positionResult.value4)
position = new Position(tokenId.toString())
// The owner gets correctly updated in the Transfer handler
position.owner = Address.fromString(ADDRESS_ZERO)
position.pool = poolAddress.toHexString()
position.token0 = positionResult.value2.toHexString()
position.token1 = positionResult.value3.toHexString()
position.tickLower = position.pool.concat('#').concat(positionResult.value5.toString())
position.tickUpper = position.pool.concat('#').concat(positionResult.value6.toString())
position.liquidity = ZERO_BI
position.depositedToken0 = ZERO_BD
position.depositedToken1 = ZERO_BD
position.withdrawnToken0 = ZERO_BD
position.withdrawnToken1 = ZERO_BD
position.collectedFeesToken0 = ZERO_BD
position.collectedFeesToken1 = ZERO_BD
position.transaction = loadTransaction(event).id
position.feeGrowthInside0LastX128 = positionResult.value8
position.feeGrowthInside1LastX128 = positionResult.value9
}
}
return position
}
function updateFeeVars(position: Position, event: ethereum.Event, tokenId: BigInt): Position {
let positionManagerContract = NonfungiblePositionManager.bind(event.address)
let positionResult = positionManagerContract.try_positions(tokenId)
if (!positionResult.reverted) {
position.feeGrowthInside0LastX128 = positionResult.value.value8
position.feeGrowthInside1LastX128 = positionResult.value.value9
}
return position
}
function savePositionSnapshot(position: Position, event: ethereum.Event): void {
let positionSnapshot = new PositionSnapshot(position.id.concat('#').concat(event.block.number.toString()))
positionSnapshot.owner = position.owner
positionSnapshot.pool = position.pool
positionSnapshot.position = position.id
positionSnapshot.blockNumber = event.block.number
positionSnapshot.timestamp = event.block.timestamp
positionSnapshot.liquidity = position.liquidity
positionSnapshot.depositedToken0 = position.depositedToken0
positionSnapshot.depositedToken1 = position.depositedToken1
positionSnapshot.withdrawnToken0 = position.withdrawnToken0
positionSnapshot.withdrawnToken1 = position.withdrawnToken1
positionSnapshot.collectedFeesToken0 = position.collectedFeesToken0
positionSnapshot.collectedFeesToken1 = position.collectedFeesToken1
positionSnapshot.transaction = loadTransaction(event).id
positionSnapshot.feeGrowthInside0LastX128 = position.feeGrowthInside0LastX128
positionSnapshot.feeGrowthInside1LastX128 = position.feeGrowthInside1LastX128
positionSnapshot.save()
}
export function handleIncreaseLiquidity(event: IncreaseLiquidity): void {
// temp fix
if (event.block.number.equals(BigInt.fromI32(14317993))) {
return
}
let position = getPosition(event, event.params.tokenId)
// position was not able to be fetched
if (position == null) {
return
}
// temp fix
if (Address.fromString(position.pool).equals(Address.fromHexString('0x8fe8d9bb8eeba3ed688069c3d6b556c9ca258248'))) {
return
}
let token0 = Token.load(position.token0)
let token1 = Token.load(position.token1)
let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
let amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals)
position.liquidity = position.liquidity.plus(event.params.liquidity)
position.depositedToken0 = position.depositedToken0.plus(amount0)
position.depositedToken1 = position.depositedToken1.plus(amount1)
updateFeeVars(position!, event, event.params.tokenId)
position.save()
savePositionSnapshot(position!, event)
}
export function handleDecreaseLiquidity(event: DecreaseLiquidity): void {
// temp fix
if (event.block.number == BigInt.fromI32(14317993)) {
return
}
let position = getPosition(event, event.params.tokenId)
// position was not able to be fetched
if (position == null) {
return
}
// temp fix
if (Address.fromString(position.pool).equals(Address.fromHexString('0x8fe8d9bb8eeba3ed688069c3d6b556c9ca258248'))) {
return
}
let token0 = Token.load(position.token0)
let token1 = Token.load(position.token1)
let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
let amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals)
position.liquidity = position.liquidity.minus(event.params.liquidity)
position.withdrawnToken0 = position.withdrawnToken0.plus(amount0)
position.withdrawnToken1 = position.withdrawnToken1.plus(amount1)
position = updateFeeVars(position!, event, event.params.tokenId)
position.save()
savePositionSnapshot(position!, event)
}
export function handleCollect(event: Collect): void {
let position = getPosition(event, event.params.tokenId)
// position was not able to be fetched
if (position == null) {
return
}
if (Address.fromString(position.pool).equals(Address.fromHexString('0x8fe8d9bb8eeba3ed688069c3d6b556c9ca258248'))) {
return
}
let token0 = Token.load(position.token0)
let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
let token1 = Token.load(position.token1)
let amount1 = convertTokenToDecimal(event.params.amount0, token1.decimals)
position.collectedFeesToken0 = position.collectedFeesToken0.plus(amount0)
position.collectedFeesToken1 = position.collectedFeesToken1.plus(amount1)
position = updateFeeVars(position!, event, event.params.tokenId)
position.save()
savePositionSnapshot(position!, event)
}
export function handleTransfer(event: Transfer): void {
let position = getPosition(event, event.params.tokenId)
// position was not able to be fetched
if (position == null) {
return
}
position.owner = event.params.to
position.save()
savePositionSnapshot(position!, event)
}