ocean-subgraph/src/mappings/utils/poolUtils.ts

127 lines
3.4 KiB
TypeScript
Raw Normal View History

2021-11-19 15:42:17 +01:00
import { Address, BigDecimal, ethereum } from '@graphprotocol/graph-ts'
import {
Pool,
PoolShares,
2021-11-23 13:59:34 +01:00
PoolSnapshot,
2021-11-19 15:42:17 +01:00
PoolTransaction
} from '../../@types/schema'
import { BPool } from '../../@types/templates/BPool/BPool'
2021-12-07 10:47:58 +01:00
import { DAY, decimal } from './constants'
2021-11-19 15:42:17 +01:00
import { gweiToEth, weiToDecimal } from './generic'
export function getPoolSharesId(
poolAddress: string,
userAddress: string
): string {
return `${poolAddress}-${userAddress}`
}
export function getPoolTransaction(
event: ethereum.Event,
userAddress: string,
2021-12-02 13:10:23 +01:00
type: string
2021-11-19 15:42:17 +01:00
): PoolTransaction {
let poolTx = PoolTransaction.load(event.transaction.hash.toHex())
// create pool transaction and fill basic fields
if (poolTx === null) {
poolTx = new PoolTransaction(event.transaction.hash.toHex())
poolTx.user = userAddress
poolTx.pool = event.address.toHex()
poolTx.type = type
poolTx.timestamp = event.block.timestamp.toI32()
2021-12-02 13:10:23 +01:00
poolTx.tx = event.transaction.hash.toHex()
2021-11-19 15:42:17 +01:00
poolTx.block = event.block.number.toI32()
poolTx.gasPrice = gweiToEth(event.transaction.gasPrice.toBigDecimal())
poolTx.gasLimit = event.transaction.gasLimit.toBigDecimal()
}
return poolTx
}
export function getPoolShares(
poolAddress: string,
userAddress: string
): PoolShares {
let poolShares = PoolShares.load(getPoolSharesId(poolAddress, userAddress))
if (poolShares === null) {
poolShares = new PoolShares(getPoolSharesId(poolAddress, userAddress))
}
return poolShares
}
export function getPool(poolAddress: string): Pool {
const pool = Pool.load(poolAddress)
if (pool === null) {
// what now?
throw new Error(`Didn't find pool with address ${poolAddress} `)
}
return pool
}
2021-11-12 14:22:35 +01:00
2021-11-19 15:42:17 +01:00
export function calcSpotPrice(
poolAddress: string,
baseTokenAddress: string,
datatokenAddress: string,
baseTokenDecimals: i32
): BigDecimal {
const poolContract = BPool.bind(Address.fromString(poolAddress))
// tokenIn is always the baseToken and tokenOut is the datatoken because we want the spot price to be in baseToken eg: 1 DT = 0.5 OCEAN
const weiPrice = poolContract.try_getSpotPrice(
Address.fromString(baseTokenAddress),
Address.fromString(datatokenAddress)
2021-12-02 13:10:23 +01:00
).value
const price = weiToDecimal(weiPrice.toBigDecimal(), baseTokenDecimals)
2021-11-19 15:42:17 +01:00
return price
}
2021-11-23 10:54:40 +01:00
2021-11-23 13:59:34 +01:00
export function getDateFromTimestamp(timestamp: i32): i32 {
// date without time
return timestamp - (timestamp % DAY)
}
2021-11-23 10:54:40 +01:00
export function getPoolSnapshotId(poolAddress: string, timestamp: i32): string {
2021-11-23 13:59:34 +01:00
return `${poolAddress}-${getDateFromTimestamp(timestamp)}`
2021-11-23 10:54:40 +01:00
}
2021-11-23 13:59:34 +01:00
export function createPoolSnapshot(
poolAddress: string,
timestamp: i32
): PoolSnapshot {
const snapshotId = getPoolSnapshotId(poolAddress, timestamp)
2021-11-23 10:54:40 +01:00
2021-12-02 13:10:23 +01:00
const pool = getPool(poolAddress)
2021-11-23 10:54:40 +01:00
const snapshot = new PoolSnapshot(snapshotId)
2021-11-23 13:59:34 +01:00
snapshot.pool = poolAddress
2021-11-23 10:54:40 +01:00
snapshot.totalShares = pool.totalShares
2021-11-23 13:59:34 +01:00
snapshot.swapVolume = decimal.ZERO
snapshot.swapFees = decimal.ZERO
snapshot.baseToken = pool.baseToken
snapshot.datatoken = pool.datatoken
snapshot.datatokenLiquidity = decimal.ZERO
2021-11-23 10:54:40 +01:00
2021-11-23 13:59:34 +01:00
snapshot.date = getDateFromTimestamp(timestamp)
snapshot.spotPrice = pool.spotPrice
2021-11-23 10:54:40 +01:00
snapshot.save()
2021-11-23 13:59:34 +01:00
return snapshot
2021-11-23 10:54:40 +01:00
}
2021-11-23 13:59:34 +01:00
export function getPoolSnapshot(
2021-11-23 10:54:40 +01:00
poolAddress: string,
2021-11-23 13:59:34 +01:00
timestamp: i32
): PoolSnapshot {
let snapshot = PoolSnapshot.load(getPoolSnapshotId(poolAddress, timestamp))
if (snapshot === null) {
snapshot = createPoolSnapshot(poolAddress, timestamp)
2021-11-23 10:54:40 +01:00
}
2021-11-23 13:59:34 +01:00
return snapshot
2021-11-23 10:54:40 +01:00
}