Skip to content

Commit 22417b4

Browse files
authored
Various fixes (#294)
* small cleanup * fix pools * fixes * cleanup * cleanup
1 parent 7f945f0 commit 22417b4

9 files changed

+590
-561
lines changed

abis/BToken.json

+488-488
Large diffs are not rendered by default.

schema.graphql

+9-9
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,12 @@ type Pool @entity {
167167
"block number when it was created"
168168
block: Int
169169

170-
shares: [PoolShares!] @derivedFrom(field: "pool")
170+
shares: [PoolShare!] @derivedFrom(field: "pool")
171171
transactions: [PoolTransaction!] @derivedFrom(field: "pool")
172172
}
173173

174174
# we will need to track pool share tx between users - bpool transfer tx event
175-
type PoolShares @entity {
175+
type PoolShare @entity {
176176
"poolAddress + userAddress"
177177
id: ID!
178178
user: User!
@@ -197,7 +197,7 @@ type PoolTransaction @entity {
197197
type: PoolTransactionType!
198198

199199
"number of shares transfered"
200-
sharesTransferAmount: BigDecimal
200+
sharesTransferAmount: BigDecimal!
201201

202202
"pool fee value, fee goes to all liquidity providers : SWAP, JOIN , EXIT"
203203
poolFee: BigDecimal!
@@ -220,15 +220,15 @@ type PoolTransaction @entity {
220220
gasPrice: BigDecimal!
221221

222222
"base tokens transfered"
223-
baseToken: TokenValuePair
223+
baseToken: Token
224224

225-
"number of tokens transfered"
225+
"number of base tokens transfered, if value is negative it means it was removed"
226226
baseTokenValue: BigDecimal
227227

228-
"datatokens transfered , if value is negative it means it was removed"
229-
datatoken: TokenValuePair
228+
"datatokens transfered"
229+
datatoken: Token
230230

231-
"number of tokens transfered, if value is negative it means it was removed"
231+
"number of datatokens transfered, if value is negative it means it was removed"
232232
datatokenValue: BigDecimal
233233
}
234234

@@ -273,7 +273,7 @@ type TokenTransaction @entity {
273273

274274
type User @entity {
275275
id: ID!
276-
sharesOwned: [PoolShares!] @derivedFrom(field: "user")
276+
sharesOwned: [PoolShare!] @derivedFrom(field: "user")
277277
tokenBalancesOwned: [TokenValuePair!]
278278
tokensOwned: [Token!] @derivedFrom(field: "minter")
279279
poolTransactions: [PoolTransaction!] @derivedFrom(field: "user")

src/helpers.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
// export function getGlobalStats(): Global {
42
// let gStats: Global | null = Global.load('1')
53
// if (gStats == null) {

src/mappings/dispenser.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { log } from '@graphprotocol/graph-ts'
21
import {
32
DispenserActivated,
43
DispenserAllowedSwapperChanged,

src/mappings/pool.ts

+66-41
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
import { log } from '@graphprotocol/graph-ts'
2-
import { PoolTransaction } from '../@types/schema'
32
import {
4-
LOG_BPT,
53
LOG_EXIT,
64
LOG_JOIN,
75
LOG_SETUP,
86
LOG_SWAP
97
} from '../@types/templates/BPool/BPool'
108
import { Transfer } from '../@types/templates/BPool/BToken'
11-
import { integer, PoolTransactionType } from './utils/constants'
9+
import { integer, PoolTransactionType, ZERO_ADDRESS } from './utils/constants'
1210
import { weiToDecimal } from './utils/generic'
1311
import { getGlobalStats } from './utils/globalUtils'
1412
import {
1513
calcSpotPrice,
1614
getPool,
1715
getPoolTransaction,
18-
getPoolShares,
16+
getPoolShare,
1917
getPoolSnapshot
2018
} from './utils/poolUtils'
2119
import { getToken } from './utils/tokenUtils'
@@ -183,7 +181,7 @@ export function handleSwap(event: LOG_SWAP): void {
183181

184182
// setup is just to set token weight(it will mostly be 50:50) and spotPrice
185183
export function handleSetup(event: LOG_SETUP): void {
186-
log.warning('new Pool ', [])
184+
log.warning('new Pool from {} ', [event.transaction.from.toHexString()])
187185
const pool = getPool(event.address.toHex())
188186

189187
pool.controller = event.params.caller.toHexString()
@@ -211,55 +209,82 @@ export function handleSetup(event: LOG_SETUP): void {
211209
)
212210
pool.spotPrice = spotPrice
213211
pool.isFinalized = true
214-
const poolTx = PoolTransaction.load(event.transaction.hash.toHex())
215-
if (poolTx) {
216-
poolTx.type = PoolTransactionType.SETUP
217-
poolTx.save()
218-
}
212+
// TODO: proper tx , add baseToken, datatoken
213+
const fromUser = getUser(event.transaction.from.toHexString())
214+
const poolTx = getPoolTransaction(
215+
event,
216+
fromUser.id,
217+
PoolTransactionType.SETUP
218+
)
219+
poolTx.type = PoolTransactionType.SETUP
220+
poolTx.baseToken = token.id
221+
poolTx.datatoken = datatoken.id
222+
const poolSnapshot = getPoolSnapshot(pool.id, event.block.timestamp.toI32())
223+
poolSnapshot.spotPrice = spotPrice
219224

225+
poolTx.save()
226+
poolSnapshot.save()
220227
const globalStats = getGlobalStats()
221228
globalStats.poolCount = globalStats.poolCount + 1
222229
globalStats.save()
223230
pool.save()
224231
datatoken.save()
225232
}
226233

227-
export function handleBpt(event: LOG_BPT): void {
228-
const pool = getPool(event.address.toHex())
229-
const poolShares = getPoolShares(pool.id, event.transaction.from.toHex())
230-
const poolTx = PoolTransaction.load(event.transaction.hash.toHex())
231-
// TODO: should we return here if null? theoretically this should not be null since LOG_BPT is after the other events
232-
if (!poolTx) return
234+
export function handlerBptTransfer(event: Transfer): void {
235+
const fromAddress = event.params.src.toHexString()
236+
const toAddress = event.params.dst.toHexString()
237+
const poolAddress = event.address.toHex()
238+
const caller = getUser(event.transaction.from.toHex())
239+
const poolTx = getPoolTransaction(event, caller.id, PoolTransactionType.SWAP)
240+
const poolSnapshot = getPoolSnapshot(
241+
poolAddress,
242+
event.block.timestamp.toI32()
243+
)
244+
log.warning('bpt transfer tx: {} from: {} | to {} | ammount {} ', [
245+
event.transaction.hash.toHex(),
246+
fromAddress,
247+
toAddress,
248+
event.params.amt.toString()
249+
])
233250

234-
const decimalBpt = weiToDecimal(event.params.bptAmount.toBigDecimal(), 18)
251+
// btoken has 18 decimals
252+
const ammount = weiToDecimal(event.params.amt.toBigDecimal(), 18)
235253

236-
// for some reason switch is broken so reverting to good old if
237-
if (poolTx.type === PoolTransactionType.JOIN) {
238-
poolShares.shares = poolShares.shares.plus(decimalBpt)
239-
pool.totalShares.plus(decimalBpt)
240-
}
241-
if (poolTx.type === PoolTransactionType.EXIT) {
242-
poolShares.shares = poolShares.shares.minus(decimalBpt)
243-
pool.totalShares.minus(decimalBpt)
254+
if (fromAddress != ZERO_ADDRESS && toAddress != ZERO_ADDRESS) {
255+
poolTx.sharesTransferAmount = poolTx.sharesTransferAmount.plus(ammount)
244256
}
245257

246-
poolShares.shares = weiToDecimal(event.params.bptAmount.toBigDecimal(), 18)
258+
if (fromAddress == ZERO_ADDRESS) {
259+
// add total
260+
const pool = getPool(poolAddress)
261+
pool.totalShares = pool.totalShares.plus(ammount)
247262

248-
pool.save()
249-
poolShares.save()
250-
}
251-
252-
export function handlerBptTransfer(event: Transfer): void {
253-
const fromUser = getPoolShares(
254-
event.address.toHex(),
255-
event.params.src.toHex()
256-
)
257-
const toUser = getPoolShares(event.address.toHex(), event.params.dst.toHex())
258-
const ammount = weiToDecimal(event.params.amt.toBigDecimal(), 18)
263+
// check tx?
264+
poolSnapshot.totalShares = pool.totalShares
265+
pool.save()
266+
} else {
267+
if (poolAddress != fromAddress) {
268+
const fromUser = getPoolShare(poolAddress, fromAddress)
269+
fromUser.shares = fromUser.shares.minus(ammount)
270+
fromUser.save()
271+
}
272+
}
259273

260-
fromUser.shares = fromUser.shares.minus(ammount)
261-
toUser.shares = toUser.shares.plus(ammount)
274+
if (toAddress == ZERO_ADDRESS) {
275+
// remove
276+
const pool = getPool(poolAddress)
277+
pool.totalShares = pool.totalShares.minus(ammount)
278+
poolSnapshot.totalShares = pool.totalShares
279+
pool.save()
280+
} else {
281+
if (poolAddress != toAddress) {
282+
const toUser = getPoolShare(poolAddress, toAddress)
283+
toUser.shares = toUser.shares.plus(ammount)
284+
toUser.save()
285+
}
286+
}
262287

263-
fromUser.save()
264-
toUser.save()
288+
poolTx.save()
289+
poolSnapshot.save()
265290
}

src/mappings/utils/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BigDecimal, BigInt } from '@graphprotocol/graph-ts'
22

33
export const ENABLE_DEBUG = true
4+
export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
45

56
export const DAY = 24 * 60 * 60
67

src/mappings/utils/poolUtils.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { Address, BigDecimal, ethereum } from '@graphprotocol/graph-ts'
22
import {
33
Pool,
4-
PoolShares,
4+
PoolShare,
55
PoolSnapshot,
66
PoolTransaction
77
} from '../../@types/schema'
88
import { BPool } from '../../@types/templates/BPool/BPool'
99
import { DAY, decimal, integer } from './constants'
1010
import { gweiToEth, weiToDecimal } from './generic'
11+
import { getUser } from './userUtils'
1112

12-
export function getPoolSharesId(
13+
export function getPoolShareId(
1314
poolAddress: string,
1415
userAddress: string
1516
): string {
@@ -42,15 +43,18 @@ export function getPoolTransaction(
4243
return poolTx
4344
}
4445

45-
export function getPoolShares(
46+
export function getPoolShare(
4647
poolAddress: string,
4748
userAddress: string
48-
): PoolShares {
49-
let poolShares = PoolShares.load(getPoolSharesId(poolAddress, userAddress))
50-
if (poolShares === null) {
51-
poolShares = new PoolShares(getPoolSharesId(poolAddress, userAddress))
49+
): PoolShare {
50+
let poolShare = PoolShare.load(getPoolShareId(poolAddress, userAddress))
51+
if (poolShare === null) {
52+
poolShare = new PoolShare(getPoolShareId(poolAddress, userAddress))
53+
poolShare.user = getUser(userAddress).id
54+
poolShare.pool = poolAddress
55+
poolShare.save()
5256
}
53-
return poolShares
57+
return poolShare
5458
}
5559

5660
export function getPool(poolAddress: string): Pool {

subgraph.rinkeby.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ templates:
174174
handler: handleExit
175175
- event: LOG_SETUP(indexed address,indexed address,uint256,uint256,indexed address,uint256,uint256)
176176
handler: handleSetup
177-
178-
177+
- event: Transfer(indexed address,indexed address,uint256)
178+
handler: handlerBptTransfer
179179
- name: ERC721Template
180180
kind: ethereum/contract
181181
network: rinkeby

subgraph.yaml

+12-10
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ dataSources:
88
name: ERC721Factory
99
network: rinkeby
1010
source:
11-
address: '0xe4B39C90355899DB8f625D879B44Fa9C5Cdde550'
11+
address: '0x15087E3E9eAAAb37d32d9D06Fa4000309BD7Ee6D'
1212
abi: ERC721Factory
13-
startBlock: 9984045
13+
startBlock: 9989814
1414
mapping:
1515
kind: ethereum/events
1616
apiVersion: 0.0.6
@@ -30,9 +30,9 @@ dataSources:
3030
name: FixedRateExchange
3131
network: rinkeby
3232
source:
33-
address: '0x7084f7353bB7cfc92A65e7d23987Cb5D1A3Fb9b2'
33+
address: '0xB5f34bd0B3E8e59447fD5a750F2dE4262BABE66C'
3434
abi: FixedRateExchange
35-
startBlock: 9984045
35+
startBlock: 9989814
3636
mapping:
3737
kind: ethereum/events
3838
apiVersion: 0.0.6
@@ -64,9 +64,9 @@ dataSources:
6464
name: Dispenser
6565
network: rinkeby
6666
source:
67-
address: '0xa8fFDd525835795C940370FB816f82a5F7F5F860'
67+
address: '0x17b1760c20eAc7A2656412412F6020e6c00b78BD'
6868
abi: Dispenser
69-
startBlock: 9984045
69+
startBlock: 9989814
7070
mapping:
7171
kind: ethereum/events
7272
apiVersion: 0.0.6
@@ -95,9 +95,9 @@ dataSources:
9595
name: FactoryRouter
9696
network: rinkeby
9797
source:
98-
address: '0xAB4FD86E4aaAb2243463Cbe92CD5194C1593fb9A'
98+
address: '0x31066E8eFe281C755dC21d828bdF30363D055baB'
9999
abi: FactoryRouter
100-
startBlock: 9984045
100+
startBlock: 9989814
101101
mapping:
102102
kind: ethereum/events
103103
apiVersion: 0.0.6
@@ -163,6 +163,8 @@ templates:
163163
file: ./abis/BToken.json
164164
- name: BMath
165165
file: ./abis/BMath.json
166+
- name: ERC20
167+
file: ./abis/ERC20.json
166168
eventHandlers:
167169
- event: LOG_SWAP(indexed address,indexed address,indexed address,uint256,uint256,uint256)
168170
handler: handleSwap
@@ -172,8 +174,8 @@ templates:
172174
handler: handleExit
173175
- event: LOG_SETUP(indexed address,indexed address,uint256,uint256,indexed address,uint256,uint256)
174176
handler: handleSetup
175-
176-
177+
- event: Transfer(indexed address,indexed address,uint256)
178+
handler: handlerBptTransfer
177179
- name: ERC721Template
178180
kind: ethereum/contract
179181
network: rinkeby

0 commit comments

Comments
 (0)