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

View File

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

View File

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

View File

@ -1,21 +1,19 @@
import { log } from '@graphprotocol/graph-ts' import { log } from '@graphprotocol/graph-ts'
import { PoolTransaction } from '../@types/schema'
import { import {
LOG_BPT,
LOG_EXIT, LOG_EXIT,
LOG_JOIN, LOG_JOIN,
LOG_SETUP, LOG_SETUP,
LOG_SWAP LOG_SWAP
} from '../@types/templates/BPool/BPool' } from '../@types/templates/BPool/BPool'
import { Transfer } from '../@types/templates/BPool/BToken' 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 { weiToDecimal } from './utils/generic'
import { getGlobalStats } from './utils/globalUtils' import { getGlobalStats } from './utils/globalUtils'
import { import {
calcSpotPrice, calcSpotPrice,
getPool, getPool,
getPoolTransaction, getPoolTransaction,
getPoolShares, getPoolShare,
getPoolSnapshot getPoolSnapshot
} from './utils/poolUtils' } from './utils/poolUtils'
import { getToken } from './utils/tokenUtils' 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 // setup is just to set token weight(it will mostly be 50:50) and spotPrice
export function handleSetup(event: LOG_SETUP): void { 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()) const pool = getPool(event.address.toHex())
pool.controller = event.params.caller.toHexString() pool.controller = event.params.caller.toHexString()
@ -211,12 +209,21 @@ export function handleSetup(event: LOG_SETUP): void {
) )
pool.spotPrice = spotPrice pool.spotPrice = spotPrice
pool.isFinalized = true pool.isFinalized = true
const poolTx = PoolTransaction.load(event.transaction.hash.toHex()) // TODO: proper tx , add baseToken, datatoken
if (poolTx) { const fromUser = getUser(event.transaction.from.toHexString())
const poolTx = getPoolTransaction(
event,
fromUser.id,
PoolTransactionType.SETUP
)
poolTx.type = PoolTransactionType.SETUP poolTx.type = PoolTransactionType.SETUP
poolTx.save() 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() const globalStats = getGlobalStats()
globalStats.poolCount = globalStats.poolCount + 1 globalStats.poolCount = globalStats.poolCount + 1
globalStats.save() globalStats.save()
@ -224,42 +231,60 @@ export function handleSetup(event: LOG_SETUP): void {
datatoken.save() 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 { export function handlerBptTransfer(event: Transfer): void {
const fromUser = getPoolShares( const fromAddress = event.params.src.toHexString()
event.address.toHex(), const toAddress = event.params.dst.toHexString()
event.params.src.toHex() 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) const ammount = weiToDecimal(event.params.amt.toBigDecimal(), 18)
fromUser.shares = fromUser.shares.minus(ammount) if (fromAddress != ZERO_ADDRESS && toAddress != ZERO_ADDRESS) {
toUser.shares = toUser.shares.plus(ammount) poolTx.sharesTransferAmount = poolTx.sharesTransferAmount.plus(ammount)
}
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() 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() toUser.save()
} }
}
poolTx.save()
poolSnapshot.save()
}

View File

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

View File

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

View File

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

View File

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