ocean-subgraph/src/mappings/fixedRateExchange.ts

180 lines
5.5 KiB
TypeScript
Raw Normal View History

2022-02-11 14:13:21 +01:00
import { BigInt } from '@graphprotocol/graph-ts'
2021-11-10 13:47:44 +01:00
import {
ExchangeActivated,
ExchangeAllowedSwapperChanged,
ExchangeCreated,
ExchangeDeactivated,
ExchangeMintStateChanged,
ExchangeRateChanged,
Swapped
} from '../@types/FixedRateExchange/FixedRateExchange'
import {
FixedRateExchange,
FixedRateExchangeSwap,
FixedRateExchangeUpdate
} from '../@types/schema'
import { getFixedRateExchange, getUpdateOrSwapId } from './utils/fixedRateUtils'
2021-11-19 15:42:17 +01:00
import { weiToDecimal } from './utils/generic'
2021-11-10 13:47:44 +01:00
import { getToken } from './utils/tokenUtils'
import { getUser } from './utils/userUtils'
2021-11-04 16:00:43 +01:00
export function handleExchangeCreated(event: ExchangeCreated): void {
const fixedRateExchange = new FixedRateExchange(
event.params.exchangeId.toHexString()
)
2021-11-10 13:47:44 +01:00
const user = getUser(event.params.exchangeOwner.toHexString())
fixedRateExchange.owner = user.id
fixedRateExchange.datatoken = getToken(
event.params.datatoken.toHexString()
).id
fixedRateExchange.baseToken = getToken(
event.params.baseToken.toHexString()
).id
2021-11-04 16:00:43 +01:00
fixedRateExchange.active = false
2022-02-11 14:13:21 +01:00
fixedRateExchange.price = weiToDecimal(
event.params.fixedRate.toBigDecimal(),
BigInt.fromI32(18).toI32()
)
fixedRateExchange.createdTimestamp = event.block.timestamp.toI32()
fixedRateExchange.tx = event.transaction.hash.toHex()
fixedRateExchange.block = event.block.number.toI32()
2021-11-04 16:00:43 +01:00
fixedRateExchange.save()
}
2021-11-10 13:47:44 +01:00
export function handleRateChange(event: ExchangeRateChanged): void {
const fixedRateExchange = getFixedRateExchange(
event.params.exchangeId.toHex()
)
const newExchangeUpdate = new FixedRateExchangeUpdate(
getUpdateOrSwapId(
event.transaction.hash.toHex(),
event.params.exchangeId.toHex()
)
)
newExchangeUpdate.oldPrice = fixedRateExchange.price
newExchangeUpdate.createdTimestamp = event.block.timestamp.toI32()
2021-12-02 12:08:47 +01:00
newExchangeUpdate.tx = event.transaction.hash.toHex()
2021-11-10 13:47:44 +01:00
newExchangeUpdate.block = event.block.number.toI32()
2021-11-19 15:42:17 +01:00
fixedRateExchange.price = weiToDecimal(
2021-11-10 13:47:44 +01:00
event.params.newRate.toBigDecimal(),
BigInt.fromI32(18).toI32()
)
newExchangeUpdate.newPrice = fixedRateExchange.price
newExchangeUpdate.save()
fixedRateExchange.save()
}
export function handleMintStateChanged(event: ExchangeMintStateChanged): void {
const fixedRateExchange = getFixedRateExchange(
event.params.exchangeId.toHex()
)
fixedRateExchange.withMint = event.params.withMint
fixedRateExchange.save()
}
// TODO: implement fre updates/history for changes
export function handleActivated(event: ExchangeActivated): void {
const fixedRateExchange = getFixedRateExchange(
event.params.exchangeId.toHex()
)
const newExchangeUpdate = new FixedRateExchangeUpdate(
getUpdateOrSwapId(
event.transaction.hash.toHex(),
event.params.exchangeId.toHex()
)
)
newExchangeUpdate.oldActive = fixedRateExchange.active
newExchangeUpdate.newActive = true
newExchangeUpdate.createdTimestamp = event.block.timestamp.toI32()
2021-12-02 12:08:47 +01:00
newExchangeUpdate.tx = event.transaction.hash.toHex()
2021-11-10 13:47:44 +01:00
newExchangeUpdate.block = event.block.number.toI32()
fixedRateExchange.active = true
newExchangeUpdate.save()
fixedRateExchange.save()
}
export function handleDeactivated(event: ExchangeDeactivated): void {
const fixedRateExchange = getFixedRateExchange(
event.params.exchangeId.toHex()
)
const newExchangeUpdate = new FixedRateExchangeUpdate(
getUpdateOrSwapId(
event.transaction.hash.toHex(),
event.params.exchangeId.toHex()
)
)
newExchangeUpdate.oldActive = fixedRateExchange.active
newExchangeUpdate.newActive = false
2021-11-26 09:04:14 +01:00
2021-11-10 13:47:44 +01:00
newExchangeUpdate.createdTimestamp = event.block.timestamp.toI32()
2021-12-02 12:08:47 +01:00
newExchangeUpdate.tx = event.transaction.hash.toHex()
2021-11-10 13:47:44 +01:00
newExchangeUpdate.block = event.block.number.toI32()
fixedRateExchange.active = false
newExchangeUpdate.save()
fixedRateExchange.save()
}
export function handleAllowedSwapperChanged(
event: ExchangeAllowedSwapperChanged
): void {
const fixedRateExchange = getFixedRateExchange(
event.params.exchangeId.toHex()
)
const newExchangeUpdate = new FixedRateExchangeUpdate(
getUpdateOrSwapId(
event.transaction.hash.toHex(),
event.params.exchangeId.toHex()
)
)
newExchangeUpdate.createdTimestamp = event.block.timestamp.toI32()
2021-12-02 12:08:47 +01:00
newExchangeUpdate.tx = event.transaction.hash.toHex()
2021-11-10 13:47:44 +01:00
newExchangeUpdate.block = event.block.number.toI32()
newExchangeUpdate.oldAllowedSwapper = fixedRateExchange.allowedSwapper
fixedRateExchange.allowedSwapper = event.params.allowedSwapper.toHex()
newExchangeUpdate.newAllowedSwapper = fixedRateExchange.allowedSwapper
newExchangeUpdate.save()
fixedRateExchange.save()
}
// TODO: implement market fee, opf fee
export function handleSwap(event: Swapped): void {
const fixedRateExchange = getFixedRateExchange(
event.params.exchangeId.toHex()
)
const swap = new FixedRateExchangeSwap(
getUpdateOrSwapId(
event.transaction.hash.toHex(),
event.params.exchangeId.toHex()
)
)
swap.createdTimestamp = event.block.timestamp.toI32()
2021-12-02 12:08:47 +01:00
swap.tx = event.transaction.hash.toHex()
2021-11-10 13:47:44 +01:00
swap.block = event.block.number.toI32()
swap.exchangeId = event.params.exchangeId.toHex()
swap.by = getUser(event.params.by.toHex()).id
// we need to fetch the decimals of the base token
const baseToken = getToken(fixedRateExchange.baseToken)
2021-11-19 15:42:17 +01:00
swap.baseTokenAmount = weiToDecimal(
2021-11-10 13:47:44 +01:00
event.params.baseTokenSwappedAmount.toBigDecimal(),
BigInt.fromI32(baseToken.decimals).toI32()
)
2021-11-19 15:42:17 +01:00
swap.dataTokenAmount = weiToDecimal(
event.params.datatokenSwappedAmount.toBigDecimal(),
2021-11-10 13:47:44 +01:00
BigInt.fromI32(18).toI32()
)
swap.save()
}