Skip to content

Commit 48ba47b

Browse files
authored
Fix poolSnapshot and fixed rate balance (#357)
* fix * fix lint * fix import
1 parent 5918c8b commit 48ba47b

7 files changed

+335
-27
lines changed

package-lock.json

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/mappings/erc721Factory.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { log } from '@graphprotocol/graph-ts'
21
import { NFTCreated, TokenCreated } from '../@types/ERC721Factory/ERC721Factory'
32
import { decimal } from './utils/constants'
43
import { weiToDecimal } from './utils/generic'
@@ -8,7 +7,6 @@ import { getToken, getNftToken } from './utils/tokenUtils'
87
import { addDatatoken } from './utils/globalUtils'
98

109
export function handleNftCreated(event: NFTCreated): void {
11-
log.warning('nft handleNftCreated {}', [event.params.tokenURI.toString()])
1210
// const nft = new Nft(event.params.newTokenAddress.toHexString())
1311
const nft = getNftToken(event.params.newTokenAddress)
1412
const user = getUser(event.params.admin.toHexString())

src/mappings/fixedRateExchange.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
ExchangeMintStateChanged,
88
ExchangeRateChanged,
99
Swapped,
10-
PublishMarketFeeChanged
10+
PublishMarketFeeChanged,
11+
TokenCollected
1112
} from '../@types/templates/FixedRateExchange/FixedRateExchange'
1213
import {
1314
FixedRateExchange,
@@ -218,3 +219,21 @@ export function handlePublishMarketFeeChanged(
218219
fixedRateExchange.save()
219220
}
220221
}
222+
223+
export function handleTokenCollected(event: TokenCollected): void {
224+
const fixedRateId = getFixedRateGraphID(
225+
event.params.exchangeId.toHexString(),
226+
event.address
227+
)
228+
const fixedRateExchange = getFixedRateExchange(fixedRateId)
229+
230+
if (event.params.token.toHexString() == fixedRateExchange.baseToken) {
231+
const baseToken = getToken(event.params.token, false)
232+
fixedRateExchange.baseTokenBalance =
233+
fixedRateExchange.baseTokenBalance.minus(
234+
weiToDecimal(event.params.amount.toBigDecimal(), baseToken.decimals)
235+
)
236+
237+
fixedRateExchange.save()
238+
}
239+
}

src/mappings/pool.ts

+43-17
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function handleJoin(event: LOG_JOIN): void {
3636
pool.joinCount = pool.joinCount.plus(integer.ONE)
3737

3838
// get token, update pool transaction, poolSnapshot
39-
const poolSnapshot = getPoolSnapshot(pool.id, event.block.timestamp.toI32())
39+
4040
const token = getToken(event.params.tokenIn, false)
4141
const ammount = weiToDecimal(
4242
event.params.tokenAmountIn.toBigDecimal(),
@@ -45,24 +45,36 @@ export function handleJoin(event: LOG_JOIN): void {
4545
if (token.isDatatoken) {
4646
poolTx.datatoken = token.id
4747
poolTx.datatokenValue = ammount
48-
49-
poolSnapshot.datatokenLiquidity =
50-
poolSnapshot.datatokenLiquidity.plus(ammount)
48+
if (pool.isFinalized) {
49+
const poolSnapshot = getPoolSnapshot(
50+
pool.id,
51+
event.block.timestamp.toI32()
52+
)
53+
poolSnapshot.datatokenLiquidity =
54+
poolSnapshot.datatokenLiquidity.plus(ammount)
55+
56+
poolSnapshot.save()
57+
}
5158

5259
pool.datatokenLiquidity = pool.datatokenLiquidity.plus(ammount)
5360
} else {
5461
poolTx.baseToken = token.id
5562
poolTx.baseTokenValue = ammount
5663

57-
poolSnapshot.baseTokenLiquidity =
58-
poolSnapshot.baseTokenLiquidity.plus(ammount)
59-
64+
if (pool.isFinalized) {
65+
const poolSnapshot = getPoolSnapshot(
66+
pool.id,
67+
event.block.timestamp.toI32()
68+
)
69+
poolSnapshot.baseTokenLiquidity =
70+
poolSnapshot.baseTokenLiquidity.plus(ammount)
71+
poolSnapshot.save()
72+
}
6073
pool.baseTokenLiquidity = pool.baseTokenLiquidity.plus(ammount)
6174

6275
addLiquidity(token.id, ammount)
6376
}
6477

65-
poolSnapshot.save()
6678
poolTx.save()
6779
pool.save()
6880
}
@@ -225,15 +237,19 @@ export function handleSetup(event: LOG_SETUP): void {
225237
poolTx.type = PoolTransactionType.SETUP
226238
poolTx.baseToken = token.id
227239
poolTx.datatoken = datatoken.id
240+
pool.save()
241+
poolTx.save()
242+
228243
const poolSnapshot = getPoolSnapshot(pool.id, event.block.timestamp.toI32())
229244
poolSnapshot.spotPrice = spotPrice
245+
poolSnapshot.baseTokenLiquidity = pool.baseTokenLiquidity
246+
poolSnapshot.datatokenLiquidity = pool.datatokenLiquidity
247+
poolSnapshot.totalShares = pool.totalShares
230248

231-
poolTx.save()
232249
poolSnapshot.save()
233250
const globalStats = getGlobalStats()
234251
globalStats.poolCount = globalStats.poolCount + 1
235252
globalStats.save()
236-
pool.save()
237253
datatoken.save()
238254
}
239255

@@ -243,10 +259,6 @@ export function handlerBptTransfer(event: Transfer): void {
243259
const poolAddress = event.address.toHex()
244260
const caller = getUser(event.transaction.from.toHex())
245261
const poolTx = getPoolTransaction(event, caller.id, PoolTransactionType.SWAP)
246-
const poolSnapshot = getPoolSnapshot(
247-
poolAddress,
248-
event.block.timestamp.toI32()
249-
)
250262

251263
// btoken has 18 decimals
252264
const ammount = weiToDecimal(event.params.amt.toBigDecimal(), 18)
@@ -261,7 +273,15 @@ export function handlerBptTransfer(event: Transfer): void {
261273
pool.totalShares = pool.totalShares.plus(ammount)
262274

263275
// check tx?
264-
poolSnapshot.totalShares = pool.totalShares
276+
if (pool.isFinalized) {
277+
const poolSnapshot = getPoolSnapshot(
278+
poolAddress,
279+
event.block.timestamp.toI32()
280+
)
281+
poolSnapshot.totalShares = pool.totalShares
282+
poolSnapshot.save()
283+
}
284+
265285
pool.save()
266286
} else {
267287
if (poolAddress != fromAddress) {
@@ -275,7 +295,14 @@ export function handlerBptTransfer(event: Transfer): void {
275295
// remove
276296
const pool = getPool(poolAddress)
277297
pool.totalShares = pool.totalShares.minus(ammount)
278-
poolSnapshot.totalShares = pool.totalShares
298+
if (pool.isFinalized) {
299+
const poolSnapshot = getPoolSnapshot(
300+
poolAddress,
301+
event.block.timestamp.toI32()
302+
)
303+
poolSnapshot.totalShares = pool.totalShares
304+
poolSnapshot.save()
305+
}
279306
pool.save()
280307
} else {
281308
if (poolAddress != toAddress) {
@@ -286,7 +313,6 @@ export function handlerBptTransfer(event: Transfer): void {
286313
}
287314

288315
poolTx.save()
289-
poolSnapshot.save()
290316
}
291317

292318
export function handlePublishMarketFeeChanged(

src/mappings/utils/poolUtils.ts

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ export function createPoolSnapshot(
9999
const snapshotId = getPoolSnapshotId(poolAddress, timestamp)
100100

101101
const pool = getPool(poolAddress)
102-
103102
const snapshot = new PoolSnapshot(snapshotId)
104103

105104
snapshot.pool = poolAddress

subgraph.template.yaml

+6-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ dataSources:
2828
handler: handleNftCreated
2929
- event: TokenCreated(indexed address,indexed address,string,string,uint256,address)
3030
handler: handleNewToken
31-
31+
3232
- kind: ethereum/contract
3333
name: FactoryRouter
3434
network: __NETWORK__
@@ -71,7 +71,6 @@ dataSources:
7171
handler: handleDispenserContractRemoved
7272

7373
templates:
74-
7574
- name: ERC20Template
7675
kind: ethereum/contract
7776
network: __NETWORK__
@@ -262,7 +261,9 @@ templates:
262261
handler: handleSwap
263262
- event: PublishMarketFeeChanged(indexed bytes32,address,address,uint256)
264263
handler: handlePublishMarketFeeChanged
265-
264+
- event: TokenCollected(indexed bytes32,indexed address,indexed address,uint256)
265+
handler: handleTokenCollected
266+
266267
- name: SSContract
267268
kind: ethereum/contract
268269
network: __NETWORK__
@@ -277,9 +278,9 @@ templates:
277278
- SSContract
278279
abis:
279280
- name: SSContract
280-
file: ./node_modules/@oceanprotocol/contracts/artifacts/contracts/pools/ssContracts/SideStaking.sol/SideStaking.json
281+
file: ./node_modules/@oceanprotocol/contracts/artifacts/contracts/pools/ssContracts/SideStaking.sol/SideStaking.json
281282
eventHandlers:
282283
- event: VestingCreated(indexed address,indexed address,uint256,uint256)
283284
handler: handleVestingCreated
284285
- event: Vesting(indexed address,indexed address,indexed address,uint256)
285-
handler: handleVesting
286+
handler: handleVesting

0 commit comments

Comments
 (0)