Various fixes (#294)

* small cleanup

* fix pools

* fixes

* cleanup

* cleanup
This commit is contained in:
mihaisc 2022-01-18 06:20:02 -08:00 committed by GitHub
parent 7f945f02f4
commit 22417b41ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 592 additions and 563 deletions

File diff suppressed because one or more lines are too long

View File

@ -167,12 +167,12 @@ type Pool @entity {
"block number when it was created"
block: Int
shares: [PoolShares!] @derivedFrom(field: "pool")
shares: [PoolShare!] @derivedFrom(field: "pool")
transactions: [PoolTransaction!] @derivedFrom(field: "pool")
}
# we will need to track pool share tx between users - bpool transfer tx event
type PoolShares @entity {
type PoolShare @entity {
"poolAddress + userAddress"
id: ID!
user: User!
@ -197,7 +197,7 @@ type PoolTransaction @entity {
type: PoolTransactionType!
"number of shares transfered"
sharesTransferAmount: BigDecimal
sharesTransferAmount: BigDecimal!
"pool fee value, fee goes to all liquidity providers : SWAP, JOIN , EXIT"
poolFee: BigDecimal!
@ -220,15 +220,15 @@ type PoolTransaction @entity {
gasPrice: BigDecimal!
"base tokens transfered"
baseToken: TokenValuePair
baseToken: Token
"number of tokens transfered"
"number of base tokens transfered, if value is negative it means it was removed"
baseTokenValue: BigDecimal
"datatokens transfered , if value is negative it means it was removed"
datatoken: TokenValuePair
"datatokens transfered"
datatoken: Token
"number of tokens transfered, if value is negative it means it was removed"
"number of datatokens transfered, if value is negative it means it was removed"
datatokenValue: BigDecimal
}
@ -273,7 +273,7 @@ type TokenTransaction @entity {
type User @entity {
id: ID!
sharesOwned: [PoolShares!] @derivedFrom(field: "user")
sharesOwned: [PoolShare!] @derivedFrom(field: "user")
tokenBalancesOwned: [TokenValuePair!]
tokensOwned: [Token!] @derivedFrom(field: "minter")
poolTransactions: [PoolTransaction!] @derivedFrom(field: "user")

View File

@ -1,5 +1,3 @@
// export function getGlobalStats(): Global {
// let gStats: Global | null = Global.load('1')
// if (gStats == null) {

View File

@ -1,4 +1,3 @@
import { log } from '@graphprotocol/graph-ts'
import {
DispenserActivated,
DispenserAllowedSwapperChanged,

View File

@ -1,21 +1,19 @@
import { log } from '@graphprotocol/graph-ts'
import { PoolTransaction } from '../@types/schema'
import {
LOG_BPT,
LOG_EXIT,
LOG_JOIN,
LOG_SETUP,
LOG_SWAP
} from '../@types/templates/BPool/BPool'
import { Transfer } from '../@types/templates/BPool/BToken'
import { integer, PoolTransactionType } from './utils/constants'
import { integer, PoolTransactionType, ZERO_ADDRESS } from './utils/constants'
import { weiToDecimal } from './utils/generic'
import { getGlobalStats } from './utils/globalUtils'
import {
calcSpotPrice,
getPool,
getPoolTransaction,
getPoolShares,
getPoolShare,
getPoolSnapshot
} from './utils/poolUtils'
import { getToken } from './utils/tokenUtils'
@ -183,7 +181,7 @@ export function handleSwap(event: LOG_SWAP): void {
// setup is just to set token weight(it will mostly be 50:50) and spotPrice
export function handleSetup(event: LOG_SETUP): void {
log.warning('new Pool ', [])
log.warning('new Pool from {} ', [event.transaction.from.toHexString()])
const pool = getPool(event.address.toHex())
pool.controller = event.params.caller.toHexString()
@ -211,12 +209,21 @@ export function handleSetup(event: LOG_SETUP): void {
)
pool.spotPrice = spotPrice
pool.isFinalized = true
const poolTx = PoolTransaction.load(event.transaction.hash.toHex())
if (poolTx) {
poolTx.type = PoolTransactionType.SETUP
poolTx.save()
}
// TODO: proper tx , add baseToken, datatoken
const fromUser = getUser(event.transaction.from.toHexString())
const poolTx = getPoolTransaction(
event,
fromUser.id,
PoolTransactionType.SETUP
)
poolTx.type = PoolTransactionType.SETUP
poolTx.baseToken = token.id
poolTx.datatoken = datatoken.id
const poolSnapshot = getPoolSnapshot(pool.id, event.block.timestamp.toI32())
poolSnapshot.spotPrice = spotPrice
poolTx.save()
poolSnapshot.save()
const globalStats = getGlobalStats()
globalStats.poolCount = globalStats.poolCount + 1
globalStats.save()
@ -224,42 +231,60 @@ export function handleSetup(event: LOG_SETUP): void {
datatoken.save()
}
export function handleBpt(event: LOG_BPT): void {
const pool = getPool(event.address.toHex())
const poolShares = getPoolShares(pool.id, event.transaction.from.toHex())
const poolTx = PoolTransaction.load(event.transaction.hash.toHex())
// TODO: should we return here if null? theoretically this should not be null since LOG_BPT is after the other events
if (!poolTx) return
const decimalBpt = weiToDecimal(event.params.bptAmount.toBigDecimal(), 18)
// for some reason switch is broken so reverting to good old if
if (poolTx.type === PoolTransactionType.JOIN) {
poolShares.shares = poolShares.shares.plus(decimalBpt)
pool.totalShares.plus(decimalBpt)
}
if (poolTx.type === PoolTransactionType.EXIT) {
poolShares.shares = poolShares.shares.minus(decimalBpt)
pool.totalShares.minus(decimalBpt)
}
poolShares.shares = weiToDecimal(event.params.bptAmount.toBigDecimal(), 18)
pool.save()
poolShares.save()
}
export function handlerBptTransfer(event: Transfer): void {
const fromUser = getPoolShares(
event.address.toHex(),
event.params.src.toHex()
const fromAddress = event.params.src.toHexString()
const toAddress = event.params.dst.toHexString()
const poolAddress = event.address.toHex()
const caller = getUser(event.transaction.from.toHex())
const poolTx = getPoolTransaction(event, caller.id, PoolTransactionType.SWAP)
const poolSnapshot = getPoolSnapshot(
poolAddress,
event.block.timestamp.toI32()
)
const toUser = getPoolShares(event.address.toHex(), event.params.dst.toHex())
log.warning('bpt transfer tx: {} from: {} | to {} | ammount {} ', [
event.transaction.hash.toHex(),
fromAddress,
toAddress,
event.params.amt.toString()
])
// btoken has 18 decimals
const ammount = weiToDecimal(event.params.amt.toBigDecimal(), 18)
fromUser.shares = fromUser.shares.minus(ammount)
toUser.shares = toUser.shares.plus(ammount)
if (fromAddress != ZERO_ADDRESS && toAddress != ZERO_ADDRESS) {
poolTx.sharesTransferAmount = poolTx.sharesTransferAmount.plus(ammount)
}
fromUser.save()
toUser.save()
if (fromAddress == ZERO_ADDRESS) {
// add total
const pool = getPool(poolAddress)
pool.totalShares = pool.totalShares.plus(ammount)
// check tx?
poolSnapshot.totalShares = pool.totalShares
pool.save()
} else {
if (poolAddress != fromAddress) {
const fromUser = getPoolShare(poolAddress, fromAddress)
fromUser.shares = fromUser.shares.minus(ammount)
fromUser.save()
}
}
if (toAddress == ZERO_ADDRESS) {
// remove
const pool = getPool(poolAddress)
pool.totalShares = pool.totalShares.minus(ammount)
poolSnapshot.totalShares = pool.totalShares
pool.save()
} else {
if (poolAddress != toAddress) {
const toUser = getPoolShare(poolAddress, toAddress)
toUser.shares = toUser.shares.plus(ammount)
toUser.save()
}
}
poolTx.save()
poolSnapshot.save()
}

View File

@ -1,6 +1,7 @@
import { BigDecimal, BigInt } from '@graphprotocol/graph-ts'
export const ENABLE_DEBUG = true
export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
export const DAY = 24 * 60 * 60

View File

@ -1,15 +1,16 @@
import { Address, BigDecimal, ethereum } from '@graphprotocol/graph-ts'
import {
Pool,
PoolShares,
PoolShare,
PoolSnapshot,
PoolTransaction
} from '../../@types/schema'
import { BPool } from '../../@types/templates/BPool/BPool'
import { DAY, decimal, integer } from './constants'
import { gweiToEth, weiToDecimal } from './generic'
import { getUser } from './userUtils'
export function getPoolSharesId(
export function getPoolShareId(
poolAddress: string,
userAddress: string
): string {
@ -42,15 +43,18 @@ export function getPoolTransaction(
return poolTx
}
export function getPoolShares(
export function getPoolShare(
poolAddress: string,
userAddress: string
): PoolShares {
let poolShares = PoolShares.load(getPoolSharesId(poolAddress, userAddress))
if (poolShares === null) {
poolShares = new PoolShares(getPoolSharesId(poolAddress, userAddress))
): PoolShare {
let poolShare = PoolShare.load(getPoolShareId(poolAddress, userAddress))
if (poolShare === null) {
poolShare = new PoolShare(getPoolShareId(poolAddress, userAddress))
poolShare.user = getUser(userAddress).id
poolShare.pool = poolAddress
poolShare.save()
}
return poolShares
return poolShare
}
export function getPool(poolAddress: string): Pool {

View File

@ -174,8 +174,8 @@ templates:
handler: handleExit
- event: LOG_SETUP(indexed address,indexed address,uint256,uint256,indexed address,uint256,uint256)
handler: handleSetup
- event: Transfer(indexed address,indexed address,uint256)
handler: handlerBptTransfer
- name: ERC721Template
kind: ethereum/contract
network: rinkeby

View File

@ -8,9 +8,9 @@ dataSources:
name: ERC721Factory
network: rinkeby
source:
address: '0xe4B39C90355899DB8f625D879B44Fa9C5Cdde550'
address: '0x15087E3E9eAAAb37d32d9D06Fa4000309BD7Ee6D'
abi: ERC721Factory
startBlock: 9984045
startBlock: 9989814
mapping:
kind: ethereum/events
apiVersion: 0.0.6
@ -30,9 +30,9 @@ dataSources:
name: FixedRateExchange
network: rinkeby
source:
address: '0x7084f7353bB7cfc92A65e7d23987Cb5D1A3Fb9b2'
address: '0xB5f34bd0B3E8e59447fD5a750F2dE4262BABE66C'
abi: FixedRateExchange
startBlock: 9984045
startBlock: 9989814
mapping:
kind: ethereum/events
apiVersion: 0.0.6
@ -64,9 +64,9 @@ dataSources:
name: Dispenser
network: rinkeby
source:
address: '0xa8fFDd525835795C940370FB816f82a5F7F5F860'
address: '0x17b1760c20eAc7A2656412412F6020e6c00b78BD'
abi: Dispenser
startBlock: 9984045
startBlock: 9989814
mapping:
kind: ethereum/events
apiVersion: 0.0.6
@ -95,9 +95,9 @@ dataSources:
name: FactoryRouter
network: rinkeby
source:
address: '0xAB4FD86E4aaAb2243463Cbe92CD5194C1593fb9A'
address: '0x31066E8eFe281C755dC21d828bdF30363D055baB'
abi: FactoryRouter
startBlock: 9984045
startBlock: 9989814
mapping:
kind: ethereum/events
apiVersion: 0.0.6
@ -163,6 +163,8 @@ templates:
file: ./abis/BToken.json
- name: BMath
file: ./abis/BMath.json
- name: ERC20
file: ./abis/ERC20.json
eventHandlers:
- event: LOG_SWAP(indexed address,indexed address,indexed address,uint256,uint256,uint256)
handler: handleSwap
@ -172,8 +174,8 @@ templates:
handler: handleExit
- event: LOG_SETUP(indexed address,indexed address,uint256,uint256,indexed address,uint256,uint256)
handler: handleSetup
- event: Transfer(indexed address,indexed address,uint256)
handler: handlerBptTransfer
- name: ERC721Template
kind: ethereum/contract
network: rinkeby