-
Notifications
You must be signed in to change notification settings - Fork 16
/
fixedRateExchange.ts
259 lines (233 loc) · 8.67 KB
/
fixedRateExchange.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
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
import { BigInt, Address, BigDecimal } from '@graphprotocol/graph-ts'
import {
ExchangeActivated,
ExchangeAllowedSwapperChanged,
ExchangeCreated,
ExchangeDeactivated,
ExchangeMintStateChanged,
ExchangeRateChanged,
Swapped,
PublishMarketFeeChanged,
TokenCollected
} from '../@types/templates/FixedRateExchange/FixedRateExchange'
import {
FixedRateExchange,
FixedRateExchangeSwap,
FixedRateExchangeUpdate
} from '../@types/schema'
import {
getFixedRateExchange,
getUpdateOrSwapId,
getFixedRateGraphID,
updateFixedRateExchangeSupply
} from './utils/fixedRateUtils'
import { weiToDecimal } from './utils/generic'
import { addFixedRateExchange, addFixedSwap } from './utils/globalUtils'
import { getToken } from './utils/tokenUtils'
import { getUser } from './utils/userUtils'
export function handleExchangeCreated(event: ExchangeCreated): void {
const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHexString(),
event.address
)
const fixedRateExchange = new FixedRateExchange(fixedRateId)
const user = getUser(event.params.exchangeOwner.toHexString())
fixedRateExchange.owner = user.id
fixedRateExchange.contract = event.address.toHexString()
fixedRateExchange.exchangeId = event.params.exchangeId.toHexString()
fixedRateExchange.datatoken = getToken(event.params.datatoken, true).id
fixedRateExchange.baseToken = getToken(event.params.baseToken, false).id
fixedRateExchange.datatokenSupply = BigDecimal.zero()
fixedRateExchange.baseTokenSupply = BigDecimal.zero()
fixedRateExchange.datatokenBalance = BigDecimal.zero()
fixedRateExchange.baseTokenBalance = BigDecimal.zero()
fixedRateExchange.totalSwapValue = BigDecimal.zero()
fixedRateExchange.active = false
fixedRateExchange.price = weiToDecimal(
event.params.fixedRate.toBigDecimal(),
BigInt.fromI32(18).toI32()
)
fixedRateExchange.createdTimestamp = event.block.timestamp.toI32()
fixedRateExchange.tx = event.transaction.hash.toHex()
fixedRateExchange.block = event.block.number.toI32()
fixedRateExchange.save()
addFixedRateExchange()
updateFixedRateExchangeSupply(event.params.exchangeId, event.address)
}
export function handleRateChange(event: ExchangeRateChanged): void {
const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHexString(),
event.address
)
const fixedRateExchange = getFixedRateExchange(fixedRateId)
const newExchangeUpdate = new FixedRateExchangeUpdate(
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
)
newExchangeUpdate.oldPrice = fixedRateExchange.price
newExchangeUpdate.createdTimestamp = event.block.timestamp.toI32()
newExchangeUpdate.tx = event.transaction.hash.toHex()
newExchangeUpdate.block = event.block.number.toI32()
newExchangeUpdate.exchangeId = fixedRateId
fixedRateExchange.price = weiToDecimal(
event.params.newRate.toBigDecimal(),
BigInt.fromI32(18).toI32()
)
newExchangeUpdate.newPrice = fixedRateExchange.price
newExchangeUpdate.save()
fixedRateExchange.save()
}
export function handleMintStateChanged(event: ExchangeMintStateChanged): void {
const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHexString(),
event.address
)
const fixedRateExchange = getFixedRateExchange(fixedRateId)
fixedRateExchange.withMint = event.params.withMint
fixedRateExchange.save()
}
export function handleActivated(event: ExchangeActivated): void {
const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHexString(),
event.address
)
const fixedRateExchange = getFixedRateExchange(fixedRateId)
const newExchangeUpdate = new FixedRateExchangeUpdate(
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
)
newExchangeUpdate.exchangeId = fixedRateId
newExchangeUpdate.oldActive = fixedRateExchange.active
newExchangeUpdate.newActive = true
newExchangeUpdate.createdTimestamp = event.block.timestamp.toI32()
newExchangeUpdate.tx = event.transaction.hash.toHex()
newExchangeUpdate.block = event.block.number.toI32()
fixedRateExchange.active = true
newExchangeUpdate.save()
fixedRateExchange.save()
}
export function handleDeactivated(event: ExchangeDeactivated): void {
const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHexString(),
event.address
)
const fixedRateExchange = getFixedRateExchange(fixedRateId)
const newExchangeUpdate = new FixedRateExchangeUpdate(
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
)
newExchangeUpdate.oldActive = fixedRateExchange.active
newExchangeUpdate.newActive = false
newExchangeUpdate.exchangeId = fixedRateId
newExchangeUpdate.createdTimestamp = event.block.timestamp.toI32()
newExchangeUpdate.tx = event.transaction.hash.toHex()
newExchangeUpdate.block = event.block.number.toI32()
fixedRateExchange.active = false
newExchangeUpdate.save()
fixedRateExchange.save()
}
export function handleAllowedSwapperChanged(
event: ExchangeAllowedSwapperChanged
): void {
const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHexString(),
event.address
)
const fixedRateExchange = getFixedRateExchange(fixedRateId)
const newExchangeUpdate = new FixedRateExchangeUpdate(
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
)
newExchangeUpdate.createdTimestamp = event.block.timestamp.toI32()
newExchangeUpdate.tx = event.transaction.hash.toHex()
newExchangeUpdate.block = event.block.number.toI32()
newExchangeUpdate.oldAllowedSwapper = fixedRateExchange.allowedSwapper
newExchangeUpdate.exchangeId = fixedRateId
fixedRateExchange.allowedSwapper = event.params.allowedSwapper.toHex()
newExchangeUpdate.newAllowedSwapper = fixedRateExchange.allowedSwapper
newExchangeUpdate.save()
fixedRateExchange.save()
}
// TODO: implement market fee, opf fee
export function handleSwap(event: Swapped): void {
const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHexString(),
event.address
)
const fixedRateExchange = getFixedRateExchange(fixedRateId)
const swap = new FixedRateExchangeSwap(
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
)
swap.createdTimestamp = event.block.timestamp.toI32()
swap.tx = event.transaction.hash.toHex()
swap.block = event.block.number.toI32()
swap.exchangeId = fixedRateId
swap.by = getUser(event.params.by.toHex()).id
// we need to fetch the decimals of the base token
const baseToken = getToken(
Address.fromString(fixedRateExchange.baseToken),
false
)
swap.baseTokenAmount = weiToDecimal(
event.params.baseTokenSwappedAmount.toBigDecimal(),
BigInt.fromI32(baseToken.decimals).toI32()
)
swap.dataTokenAmount = weiToDecimal(
event.params.datatokenSwappedAmount.toBigDecimal(),
BigInt.fromI32(18).toI32()
)
// Track OPC swap fee
swap.oceanFeeAmount = weiToDecimal(
event.params.oceanFeeAmount.toBigDecimal(),
BigInt.fromI32(baseToken.decimals).toI32()
)
swap.save()
updateFixedRateExchangeSupply(event.params.exchangeId, event.address)
if (event.params.tokenOutAddress.toHexString() == fixedRateExchange.baseToken)
addFixedSwap(
event.params.tokenOutAddress.toHexString(),
swap.dataTokenAmount
)
else addFixedSwap(fixedRateExchange.baseToken, swap.baseTokenAmount)
// update datatoken lastPriceToken and lastPriceValue
const datatoken = getToken(
Address.fromString(fixedRateExchange.datatoken),
true
)
const priceToken = getToken(
Address.fromString(fixedRateExchange.baseToken),
false
)
datatoken.lastPriceToken = priceToken.id
datatoken.lastPriceValue = fixedRateExchange.price
datatoken.save()
}
export function handlePublishMarketFeeChanged(
event: PublishMarketFeeChanged
): void {
const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHexString(),
event.address
)
const fixedRateExchange = getFixedRateExchange(fixedRateId)
if (fixedRateExchange) {
fixedRateExchange.publishMarketFeeAddress =
event.params.newMarketCollector.toHexString()
fixedRateExchange.publishMarketSwapFee = weiToDecimal(
event.params.swapFee.toBigDecimal(),
BigInt.fromI32(18).toI32()
)
fixedRateExchange.save()
}
}
export function handleTokenCollected(event: TokenCollected): void {
const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHexString(),
event.address
)
const fixedRateExchange = getFixedRateExchange(fixedRateId)
if (event.params.token.toHexString() == fixedRateExchange.baseToken) {
const baseToken = getToken(event.params.token, false)
fixedRateExchange.baseTokenBalance =
fixedRateExchange.baseTokenBalance.minus(
weiToDecimal(event.params.amount.toBigDecimal(), baseToken.decimals)
)
fixedRateExchange.save()
}
}