2022-08-03 17:20:25 +02:00
|
|
|
import { BigInt, Address, BigDecimal } from '@graphprotocol/graph-ts'
|
2021-11-10 13:47:44 +01:00
|
|
|
import {
|
|
|
|
ExchangeActivated,
|
|
|
|
ExchangeAllowedSwapperChanged,
|
|
|
|
ExchangeCreated,
|
|
|
|
ExchangeDeactivated,
|
|
|
|
ExchangeMintStateChanged,
|
|
|
|
ExchangeRateChanged,
|
2022-02-17 11:26:05 +01:00
|
|
|
Swapped,
|
2022-02-23 18:12:51 +01:00
|
|
|
PublishMarketFeeChanged,
|
|
|
|
TokenCollected
|
2022-02-21 11:45:24 +01:00
|
|
|
} from '../@types/templates/FixedRateExchange/FixedRateExchange'
|
2021-11-10 13:47:44 +01:00
|
|
|
import {
|
|
|
|
FixedRateExchange,
|
|
|
|
FixedRateExchangeSwap,
|
|
|
|
FixedRateExchangeUpdate
|
|
|
|
} from '../@types/schema'
|
2022-02-20 09:00:16 +01:00
|
|
|
import {
|
|
|
|
getFixedRateExchange,
|
|
|
|
getUpdateOrSwapId,
|
|
|
|
getFixedRateGraphID,
|
|
|
|
updateFixedRateExchangeSupply
|
|
|
|
} from './utils/fixedRateUtils'
|
2021-11-19 15:42:17 +01:00
|
|
|
import { weiToDecimal } from './utils/generic'
|
2022-02-20 14:51:01 +01:00
|
|
|
import { addFixedRateExchange, addFixedSwap } from './utils/globalUtils'
|
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 {
|
2022-02-20 09:00:16 +01:00
|
|
|
const fixedRateId = getFixedRateGraphID(
|
|
|
|
event.params.exchangeId.toHexString(),
|
|
|
|
event.address
|
2021-11-04 16:00:43 +01:00
|
|
|
)
|
2022-02-20 09:00:16 +01:00
|
|
|
const fixedRateExchange = new FixedRateExchange(fixedRateId)
|
2021-11-10 13:47:44 +01:00
|
|
|
const user = getUser(event.params.exchangeOwner.toHexString())
|
|
|
|
fixedRateExchange.owner = user.id
|
2022-02-20 09:00:16 +01:00
|
|
|
fixedRateExchange.contract = event.address.toHexString()
|
|
|
|
fixedRateExchange.exchangeId = event.params.exchangeId.toHexString()
|
2022-02-18 12:09:18 +01:00
|
|
|
fixedRateExchange.datatoken = getToken(event.params.datatoken, true).id
|
|
|
|
fixedRateExchange.baseToken = getToken(event.params.baseToken, false).id
|
2022-08-01 13:58:09 +02:00
|
|
|
fixedRateExchange.datatokenSupply = BigDecimal.zero()
|
|
|
|
fixedRateExchange.baseTokenSupply = BigDecimal.zero()
|
|
|
|
fixedRateExchange.datatokenBalance = BigDecimal.zero()
|
|
|
|
fixedRateExchange.baseTokenBalance = BigDecimal.zero()
|
|
|
|
fixedRateExchange.totalSwapValue = BigDecimal.zero()
|
2022-01-13 17:24:57 +01:00
|
|
|
fixedRateExchange.active = false
|
2022-02-11 14:13:21 +01:00
|
|
|
fixedRateExchange.price = weiToDecimal(
|
|
|
|
event.params.fixedRate.toBigDecimal(),
|
|
|
|
BigInt.fromI32(18).toI32()
|
|
|
|
)
|
2022-01-13 17:24:57 +01:00
|
|
|
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()
|
2022-02-15 17:13:55 +01:00
|
|
|
|
|
|
|
addFixedRateExchange()
|
2022-02-20 09:00:16 +01:00
|
|
|
updateFixedRateExchangeSupply(event.params.exchangeId, event.address)
|
2021-11-04 16:00:43 +01:00
|
|
|
}
|
2021-11-10 13:47:44 +01:00
|
|
|
|
|
|
|
export function handleRateChange(event: ExchangeRateChanged): void {
|
2022-02-20 09:00:16 +01:00
|
|
|
const fixedRateId = getFixedRateGraphID(
|
|
|
|
event.params.exchangeId.toHexString(),
|
|
|
|
event.address
|
2021-11-10 13:47:44 +01:00
|
|
|
)
|
2022-02-20 09:00:16 +01:00
|
|
|
const fixedRateExchange = getFixedRateExchange(fixedRateId)
|
2021-11-10 13:47:44 +01:00
|
|
|
const newExchangeUpdate = new FixedRateExchangeUpdate(
|
2022-02-20 09:00:16 +01:00
|
|
|
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
|
2021-11-10 13:47:44 +01:00
|
|
|
)
|
|
|
|
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()
|
2022-08-01 13:58:09 +02:00
|
|
|
newExchangeUpdate.exchangeId = fixedRateId
|
2022-08-03 17:20:25 +02:00
|
|
|
|
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 {
|
2022-02-20 09:00:16 +01:00
|
|
|
const fixedRateId = getFixedRateGraphID(
|
|
|
|
event.params.exchangeId.toHexString(),
|
|
|
|
event.address
|
2021-11-10 13:47:44 +01:00
|
|
|
)
|
2022-02-20 09:00:16 +01:00
|
|
|
const fixedRateExchange = getFixedRateExchange(fixedRateId)
|
2021-11-10 13:47:44 +01:00
|
|
|
fixedRateExchange.withMint = event.params.withMint
|
|
|
|
fixedRateExchange.save()
|
|
|
|
}
|
|
|
|
|
|
|
|
export function handleActivated(event: ExchangeActivated): void {
|
2022-02-20 09:00:16 +01:00
|
|
|
const fixedRateId = getFixedRateGraphID(
|
|
|
|
event.params.exchangeId.toHexString(),
|
|
|
|
event.address
|
2021-11-10 13:47:44 +01:00
|
|
|
)
|
2022-02-20 09:00:16 +01:00
|
|
|
const fixedRateExchange = getFixedRateExchange(fixedRateId)
|
2021-11-10 13:47:44 +01:00
|
|
|
const newExchangeUpdate = new FixedRateExchangeUpdate(
|
2022-02-20 09:00:16 +01:00
|
|
|
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
|
2021-11-10 13:47:44 +01:00
|
|
|
)
|
2022-08-01 13:58:09 +02:00
|
|
|
newExchangeUpdate.exchangeId = fixedRateId
|
2021-11-10 13:47:44 +01:00
|
|
|
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 {
|
2022-02-20 09:00:16 +01:00
|
|
|
const fixedRateId = getFixedRateGraphID(
|
|
|
|
event.params.exchangeId.toHexString(),
|
|
|
|
event.address
|
2021-11-10 13:47:44 +01:00
|
|
|
)
|
2022-02-20 09:00:16 +01:00
|
|
|
const fixedRateExchange = getFixedRateExchange(fixedRateId)
|
2021-11-10 13:47:44 +01:00
|
|
|
const newExchangeUpdate = new FixedRateExchangeUpdate(
|
2022-02-20 09:00:16 +01:00
|
|
|
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
|
2021-11-10 13:47:44 +01:00
|
|
|
)
|
|
|
|
newExchangeUpdate.oldActive = fixedRateExchange.active
|
|
|
|
newExchangeUpdate.newActive = false
|
2021-11-26 09:04:14 +01:00
|
|
|
|
2022-08-01 13:58:09 +02:00
|
|
|
newExchangeUpdate.exchangeId = fixedRateId
|
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 {
|
2022-02-20 09:00:16 +01:00
|
|
|
const fixedRateId = getFixedRateGraphID(
|
|
|
|
event.params.exchangeId.toHexString(),
|
|
|
|
event.address
|
2021-11-10 13:47:44 +01:00
|
|
|
)
|
2022-02-20 09:00:16 +01:00
|
|
|
const fixedRateExchange = getFixedRateExchange(fixedRateId)
|
2021-11-10 13:47:44 +01:00
|
|
|
const newExchangeUpdate = new FixedRateExchangeUpdate(
|
2022-02-20 09:00:16 +01:00
|
|
|
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
|
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()
|
|
|
|
newExchangeUpdate.oldAllowedSwapper = fixedRateExchange.allowedSwapper
|
2022-08-03 17:20:25 +02:00
|
|
|
newExchangeUpdate.exchangeId = fixedRateId
|
2021-11-10 13:47:44 +01:00
|
|
|
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 {
|
2022-02-20 09:00:16 +01:00
|
|
|
const fixedRateId = getFixedRateGraphID(
|
|
|
|
event.params.exchangeId.toHexString(),
|
|
|
|
event.address
|
2021-11-10 13:47:44 +01:00
|
|
|
)
|
2022-02-20 09:00:16 +01:00
|
|
|
const fixedRateExchange = getFixedRateExchange(fixedRateId)
|
2021-11-10 13:47:44 +01:00
|
|
|
|
|
|
|
const swap = new FixedRateExchangeSwap(
|
2022-02-20 09:00:16 +01:00
|
|
|
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
|
2021-11-10 13:47:44 +01:00
|
|
|
)
|
|
|
|
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()
|
|
|
|
|
2022-02-20 09:00:16 +01:00
|
|
|
swap.exchangeId = fixedRateId
|
2021-11-10 13:47:44 +01:00
|
|
|
swap.by = getUser(event.params.by.toHex()).id
|
|
|
|
|
|
|
|
// we need to fetch the decimals of the base token
|
2022-02-18 12:09:18 +01:00
|
|
|
const baseToken = getToken(
|
|
|
|
Address.fromString(fixedRateExchange.baseToken),
|
|
|
|
false
|
|
|
|
)
|
2022-08-01 13:58:09 +02:00
|
|
|
|
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(
|
2022-01-28 12:05:04 +01:00
|
|
|
event.params.datatokenSwappedAmount.toBigDecimal(),
|
2021-11-10 13:47:44 +01:00
|
|
|
BigInt.fromI32(18).toI32()
|
|
|
|
)
|
|
|
|
|
|
|
|
swap.save()
|
2022-08-01 13:58:09 +02:00
|
|
|
|
2022-02-20 09:00:16 +01:00
|
|
|
updateFixedRateExchangeSupply(event.params.exchangeId, event.address)
|
2022-08-01 13:58:09 +02:00
|
|
|
|
|
|
|
if (event.params.tokenOutAddress.toHexString() == fixedRateExchange.baseToken)
|
2022-02-20 14:51:01 +01:00
|
|
|
addFixedSwap(
|
|
|
|
event.params.tokenOutAddress.toHexString(),
|
|
|
|
swap.dataTokenAmount
|
|
|
|
)
|
2022-08-01 13:58:09 +02:00
|
|
|
else addFixedSwap(fixedRateExchange.baseToken, swap.baseTokenAmount)
|
2022-03-03 10:55:43 +01:00
|
|
|
// update datatoken lastPriceToken and lastPriceValue
|
|
|
|
const datatoken = getToken(
|
|
|
|
Address.fromString(fixedRateExchange.datatoken),
|
|
|
|
true
|
|
|
|
)
|
|
|
|
datatoken.lastPriceToken = fixedRateExchange.baseToken
|
|
|
|
datatoken.lastPriceValue = fixedRateExchange.price
|
|
|
|
datatoken.save()
|
2021-11-10 13:47:44 +01:00
|
|
|
}
|
2022-02-17 11:26:05 +01:00
|
|
|
|
|
|
|
export function handlePublishMarketFeeChanged(
|
|
|
|
event: PublishMarketFeeChanged
|
|
|
|
): void {
|
2022-02-20 09:00:16 +01:00
|
|
|
const fixedRateId = getFixedRateGraphID(
|
|
|
|
event.params.exchangeId.toHexString(),
|
|
|
|
event.address
|
2022-02-17 11:26:05 +01:00
|
|
|
)
|
2022-02-20 09:00:16 +01:00
|
|
|
const fixedRateExchange = getFixedRateExchange(fixedRateId)
|
2022-02-17 11:26:05 +01:00
|
|
|
if (fixedRateExchange) {
|
|
|
|
fixedRateExchange.publishMarketFeeAddress =
|
|
|
|
event.params.newMarketCollector.toHexString()
|
|
|
|
fixedRateExchange.publishMarketSwapFee = weiToDecimal(
|
|
|
|
event.params.swapFee.toBigDecimal(),
|
|
|
|
BigInt.fromI32(18).toI32()
|
|
|
|
)
|
|
|
|
fixedRateExchange.save()
|
|
|
|
}
|
|
|
|
}
|
2022-02-23 18:12:51 +01:00
|
|
|
|
|
|
|
export function handleTokenCollected(event: TokenCollected): void {
|
|
|
|
const fixedRateId = getFixedRateGraphID(
|
|
|
|
event.params.exchangeId.toHexString(),
|
|
|
|
event.address
|
|
|
|
)
|
|
|
|
const fixedRateExchange = getFixedRateExchange(fixedRateId)
|
|
|
|
|
|
|
|
if (event.params.token.toHexString() == fixedRateExchange.baseToken) {
|
|
|
|
const baseToken = getToken(event.params.token, false)
|
|
|
|
fixedRateExchange.baseTokenBalance =
|
|
|
|
fixedRateExchange.baseTokenBalance.minus(
|
|
|
|
weiToDecimal(event.params.amount.toBigDecimal(), baseToken.decimals)
|
|
|
|
)
|
|
|
|
|
|
|
|
fixedRateExchange.save()
|
|
|
|
}
|
|
|
|
}
|