ocean-subgraph/src/mappings/pool.ts

345 lines
10 KiB
TypeScript
Raw Normal View History

2022-06-28 16:47:06 +02:00
import { BigInt, Address, log } from '@graphprotocol/graph-ts'
2021-11-19 15:42:17 +01:00
import {
LOG_EXIT,
LOG_JOIN,
LOG_SETUP,
2022-02-17 11:26:05 +01:00
LOG_SWAP,
PublishMarketFeeChanged,
SwapFeeChanged
2021-11-19 15:42:17 +01:00
} from '../@types/templates/BPool/BPool'
import { Transfer } from '../@types/templates/BPool/BToken'
2022-04-05 18:20:56 +02:00
import {
decimal,
integer,
PoolTransactionType,
ZERO_ADDRESS
} from './utils/constants'
2021-11-19 15:42:17 +01:00
import { weiToDecimal } from './utils/generic'
import { addLiquidity, addPoolSwap, removeLiquidity } from './utils/globalUtils'
2021-11-19 15:42:17 +01:00
import {
calcSpotPrice,
getPool,
getPoolTransaction,
getPoolShare,
getPoolSnapshot,
getPoolLpSwapFee,
getPoolPublisherMarketFee
2021-11-19 15:42:17 +01:00
} from './utils/poolUtils'
import { getToken } from './utils/tokenUtils'
2021-11-12 14:22:35 +01:00
import { getUser } from './utils/userUtils'
2021-11-19 15:42:17 +01:00
// kinda redundant code in join/swap/exit
2021-11-12 14:22:35 +01:00
export function handleJoin(event: LOG_JOIN): void {
2021-11-19 15:42:17 +01:00
const pool = getPool(event.address.toHex())
const user = getUser(event.params.caller.toHex())
const poolTx = getPoolTransaction(event, user.id, PoolTransactionType.JOIN)
pool.transactionCount = pool.transactionCount.plus(integer.ONE)
pool.joinCount = pool.joinCount.plus(integer.ONE)
2021-11-23 13:59:34 +01:00
// get token, update pool transaction, poolSnapshot
2022-02-18 12:09:18 +01:00
const token = getToken(event.params.tokenIn, false)
2021-11-19 15:42:17 +01:00
const ammount = weiToDecimal(
event.params.tokenAmountIn.toBigDecimal(),
token.decimals
)
if (token.isDatatoken) {
poolTx.datatoken = token.id
poolTx.datatokenValue = ammount
2021-11-23 13:59:34 +01:00
pool.datatokenLiquidity = pool.datatokenLiquidity.plus(ammount)
2021-11-19 15:42:17 +01:00
} else {
poolTx.baseToken = token.id
poolTx.baseTokenValue = ammount
pool.baseTokenLiquidity = pool.baseTokenLiquidity.plus(ammount)
addLiquidity(token.id, ammount)
2021-11-23 13:59:34 +01:00
}
2021-11-19 15:42:17 +01:00
poolTx.save()
pool.save()
2022-02-25 10:18:49 +01:00
if (pool.isFinalized) {
const poolSnapshot = getPoolSnapshot(pool.id, event.block.timestamp.toI32())
poolSnapshot.baseTokenLiquidity = pool.baseTokenLiquidity
poolSnapshot.datatokenLiquidity = pool.datatokenLiquidity
poolSnapshot.totalShares = pool.totalShares
poolSnapshot.save()
}
2021-11-19 15:42:17 +01:00
}
2021-11-12 14:22:35 +01:00
2021-11-19 15:42:17 +01:00
export function handleExit(event: LOG_EXIT): void {
const pool = getPool(event.address.toHex())
const user = getUser(event.params.caller.toHex())
const poolTx = getPoolTransaction(event, user.id, PoolTransactionType.EXIT)
2021-11-12 14:22:35 +01:00
pool.transactionCount = pool.transactionCount.plus(integer.ONE)
pool.joinCount = pool.joinCount.plus(integer.ONE)
2021-11-19 15:42:17 +01:00
// get token and update pool transaction, value is negative because this is an exit event.
2022-02-18 12:09:18 +01:00
const token = getToken(event.params.tokenOut, false)
2021-11-23 13:59:34 +01:00
const poolSnapshot = getPoolSnapshot(pool.id, event.block.timestamp.toI32())
2021-11-19 15:42:17 +01:00
const ammount = weiToDecimal(
event.params.tokenAmountOut.toBigDecimal(),
token.decimals
)
if (token.isDatatoken) {
poolTx.datatoken = token.id
poolTx.datatokenValue = ammount
2021-11-23 13:59:34 +01:00
2022-02-25 14:12:17 +01:00
pool.datatokenLiquidity = pool.datatokenLiquidity.minus(ammount)
2021-11-19 15:42:17 +01:00
} else {
poolTx.baseToken = token.id
poolTx.baseTokenValue = ammount
2021-11-19 15:42:17 +01:00
2022-02-25 14:12:17 +01:00
pool.baseTokenLiquidity = pool.baseTokenLiquidity.minus(ammount)
removeLiquidity(token.id, ammount)
2021-11-23 13:59:34 +01:00
}
2021-11-19 15:42:17 +01:00
2022-02-25 10:18:49 +01:00
poolSnapshot.baseTokenLiquidity = pool.baseTokenLiquidity
poolSnapshot.datatokenLiquidity = pool.datatokenLiquidity
poolSnapshot.totalShares = pool.totalShares
2021-11-23 13:59:34 +01:00
poolSnapshot.save()
2021-11-19 15:42:17 +01:00
poolTx.save()
pool.save()
}
export function handleSwap(event: LOG_SWAP): void {
const pool = getPool(event.address.toHex())
2021-11-12 14:22:35 +01:00
const user = getUser(event.params.caller.toHex())
2021-11-19 15:42:17 +01:00
const poolTx = getPoolTransaction(event, user.id, PoolTransactionType.SWAP)
pool.transactionCount = pool.transactionCount.plus(integer.ONE)
pool.joinCount = pool.joinCount.plus(integer.ONE)
2021-11-12 14:22:35 +01:00
2021-11-23 13:59:34 +01:00
const poolSnapshot = getPoolSnapshot(pool.id, event.block.timestamp.toI32())
2022-02-18 12:09:18 +01:00
const tokenOut = getToken(event.params.tokenOut, false)
const tokenIn = getToken(event.params.tokenIn, false)
2022-04-05 18:20:56 +02:00
let spotPrice = decimal.ZERO
2021-11-19 15:42:17 +01:00
const ammountOut = weiToDecimal(
event.params.tokenAmountOut.toBigDecimal(),
tokenOut.decimals
)
const tokenOutNewBalance = weiToDecimal(
event.params.outBalance.toBigDecimal(),
tokenOut.decimals
)
const tokenInNewBalance = weiToDecimal(
event.params.inBalance.toBigDecimal(),
tokenIn.decimals
)
2021-11-19 15:42:17 +01:00
if (tokenOut.isDatatoken) {
poolTx.datatoken = tokenOut.id
poolTx.datatokenValue = ammountOut.neg()
2021-11-23 13:59:34 +01:00
pool.datatokenLiquidity = tokenOutNewBalance
2021-11-19 15:42:17 +01:00
} else {
poolTx.baseToken = tokenOut.id
poolTx.baseTokenValue = ammountOut.neg()
2022-04-05 18:20:56 +02:00
2022-04-05 18:55:35 +02:00
spotPrice = decimal.ONE.div(
weiToDecimal(event.params.newSpotPrice.toBigDecimal(), tokenOut.decimals)
2022-04-05 18:20:56 +02:00
)
pool.baseTokenLiquidity = tokenOutNewBalance
2022-02-25 10:18:49 +01:00
poolSnapshot.swapVolume = poolSnapshot.swapVolume.plus(ammountOut)
addPoolSwap(tokenOut.id, ammountOut)
removeLiquidity(tokenOut.id, ammountOut)
2021-11-19 15:42:17 +01:00
}
2021-11-12 14:22:35 +01:00
2021-11-19 15:42:17 +01:00
// update pool token in
const ammountIn = weiToDecimal(
event.params.tokenAmountIn.toBigDecimal(),
tokenIn.decimals
)
if (tokenIn.isDatatoken) {
poolTx.datatoken = tokenIn.id
poolTx.datatokenValue = ammountIn
2021-11-23 13:59:34 +01:00
pool.datatokenLiquidity = tokenInNewBalance
2021-11-19 15:42:17 +01:00
} else {
poolTx.baseToken = tokenIn.id
poolTx.baseTokenValue = ammountIn
2022-04-05 18:20:56 +02:00
2022-04-05 18:55:35 +02:00
spotPrice = weiToDecimal(
event.params.newSpotPrice.toBigDecimal(),
tokenOut.decimals
2022-04-05 18:20:56 +02:00
)
pool.baseTokenLiquidity = tokenInNewBalance
2022-02-25 10:18:49 +01:00
poolSnapshot.swapVolume = poolSnapshot.swapVolume.plus(ammountIn)
addLiquidity(tokenIn.id, ammountIn)
addPoolSwap(tokenIn.id, ammountIn)
2021-11-19 15:42:17 +01:00
}
2021-11-12 14:22:35 +01:00
2021-11-19 15:42:17 +01:00
// update spot price
pool.spotPrice = spotPrice
2021-11-23 13:59:34 +01:00
poolSnapshot.spotPrice = spotPrice
2022-02-25 10:18:49 +01:00
poolSnapshot.baseTokenLiquidity = pool.baseTokenLiquidity
poolSnapshot.datatokenLiquidity = pool.datatokenLiquidity
poolSnapshot.totalShares = pool.totalShares
2021-11-19 15:42:17 +01:00
2021-11-23 13:59:34 +01:00
poolSnapshot.save()
2021-11-12 14:22:35 +01:00
poolTx.save()
pool.save()
// update datatoken lastPriceToken and lastPriceValue
const datatoken = getToken(Address.fromString(pool.datatoken), true)
datatoken.lastPriceToken = pool.baseToken
datatoken.lastPriceValue = spotPrice
datatoken.save()
2021-11-12 14:22:35 +01:00
}
2021-11-19 15:42:17 +01:00
2021-11-23 13:59:34 +01:00
// setup is just to set token weight(it will mostly be 50:50) and spotPrice
2021-11-19 15:42:17 +01:00
export function handleSetup(event: LOG_SETUP): void {
const pool = getPool(event.address.toHex())
pool.controller = event.params.caller.toHexString()
2022-02-18 12:09:18 +01:00
const token = getToken(event.params.baseToken, false)
2021-11-23 13:59:34 +01:00
pool.baseToken = token.id
pool.baseTokenWeight = weiToDecimal(
2021-11-19 15:42:17 +01:00
event.params.baseTokenWeight.toBigDecimal(),
2022-06-24 14:42:51 +02:00
18
2021-11-19 15:42:17 +01:00
)
// decimals hardcoded because datatokens have 18 decimals
2022-02-18 12:09:18 +01:00
const datatoken = getToken(event.params.datatoken, true)
2021-11-23 13:59:34 +01:00
pool.datatoken = datatoken.id
pool.datatokenWeight = weiToDecimal(
event.params.datatokenWeight.toBigDecimal(),
2021-11-19 15:42:17 +01:00
18
)
// calculate spotPrice
const spotPrice = calcSpotPrice(
pool.id,
pool.baseToken,
pool.datatoken,
token.decimals
)
pool.spotPrice = spotPrice
pool.isFinalized = true
// 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
2022-03-31 16:49:20 +02:00
poolTx.baseTokenValue = weiToDecimal(
event.params.baseTokenAmountIn.toBigDecimal(),
token.decimals
)
2022-06-28 16:47:06 +02:00
log.info('\n\npoolTx: {}\n\n', [event.address.toHex()])
log.info('\n\npoolTx: {}\n\n', [event.block.toString()])
pool.save()
poolTx.save()
const lpFee = getPoolLpSwapFee(event.address)
pool.liquidityProviderSwapFee = lpFee
const publisherMarketFee = getPoolPublisherMarketFee(event.address)
pool.publishMarketSwapFee = publisherMarketFee
pool.save()
const poolSnapshot = getPoolSnapshot(pool.id, event.block.timestamp.toI32())
poolSnapshot.spotPrice = spotPrice
poolSnapshot.baseTokenLiquidity = pool.baseTokenLiquidity
poolSnapshot.datatokenLiquidity = pool.datatokenLiquidity
poolSnapshot.totalShares = pool.totalShares
2021-11-19 15:42:17 +01:00
poolSnapshot.save()
2021-11-19 15:42:17 +01:00
datatoken.save()
}
export function handlerBptTransfer(event: Transfer): void {
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)
2021-11-19 15:42:17 +01:00
// btoken has 18 decimals
const ammount = weiToDecimal(event.params.amt.toBigDecimal(), 18)
2021-11-19 15:42:17 +01:00
if (fromAddress != ZERO_ADDRESS && toAddress != ZERO_ADDRESS) {
poolTx.sharesTransferAmount = poolTx.sharesTransferAmount.plus(ammount)
2021-11-19 15:42:17 +01:00
}
if (fromAddress == ZERO_ADDRESS) {
// add total
const pool = getPool(poolAddress)
pool.totalShares = pool.totalShares.plus(ammount)
2021-11-19 15:42:17 +01:00
// check tx?
if (pool.isFinalized) {
const poolSnapshot = getPoolSnapshot(
poolAddress,
event.block.timestamp.toI32()
)
poolSnapshot.totalShares = pool.totalShares
poolSnapshot.save()
}
pool.save()
} else {
if (poolAddress != fromAddress) {
const fromUser = getPoolShare(poolAddress, fromAddress)
fromUser.shares = fromUser.shares.minus(ammount)
fromUser.save()
}
}
2021-11-19 15:42:17 +01:00
if (toAddress == ZERO_ADDRESS) {
// remove
const pool = getPool(poolAddress)
pool.totalShares = pool.totalShares.minus(ammount)
if (pool.isFinalized) {
const poolSnapshot = getPoolSnapshot(
poolAddress,
event.block.timestamp.toI32()
)
poolSnapshot.totalShares = pool.totalShares
poolSnapshot.save()
}
pool.save()
} else {
if (poolAddress != toAddress) {
const toUser = getPoolShare(poolAddress, toAddress)
toUser.shares = toUser.shares.plus(ammount)
toUser.save()
}
}
2021-11-19 15:42:17 +01:00
poolTx.save()
2021-11-19 15:42:17 +01:00
}
2022-02-17 11:26:05 +01:00
export function handlePublishMarketFeeChanged(
event: PublishMarketFeeChanged
): void {
const pool = getPool(event.address.toHex())
if (pool) {
pool.publishMarketFeeAddress = event.params.newMarketCollector.toHexString()
pool.publishMarketSwapFee = weiToDecimal(
event.params.swapFee.toBigDecimal(),
BigInt.fromI32(18).toI32()
)
pool.save()
}
}
export function handleSwapFeeChanged(event: SwapFeeChanged): void {
const pool = getPool(event.address.toHex())
if (pool) {
pool.liquidityProviderSwapFee = weiToDecimal(
event.params.amount.toBigDecimal(),
BigInt.fromI32(18).toI32()
)
pool.save()
}
}