ocean-subgraph/src/mappings/erc20Templates.ts

275 lines
8.9 KiB
TypeScript
Raw Normal View History

import { Order, Nft, OrderReuse } from '../@types/schema'
import { BigInt, BigDecimal, Address } from '@graphprotocol/graph-ts'
import {
NewPaymentCollector,
OrderStarted,
2022-02-17 11:26:05 +01:00
PublishMarketFee,
2022-02-18 12:09:18 +01:00
PublishMarketFeeChanged,
AddedMinter,
AddedPaymentManager,
RemovedMinter,
RemovedPaymentManager,
CleanedPermissions,
OrderReused,
ProviderFee
} from '../@types/templates/ERC20Template/ERC20Template'
2021-11-26 09:04:14 +01:00
import { integer } from './utils/constants'
import { weiToDecimal } from './utils/generic'
import { addOrder } from './utils/globalUtils'
import { getToken, getUSDValue } from './utils/tokenUtils'
2021-11-26 09:04:14 +01:00
import { getUser } from './utils/userUtils'
import { getOrderId } from './utils/orderUtils'
2021-11-26 09:04:14 +01:00
export function handleOrderStarted(event: OrderStarted): void {
const order = new Order(
getOrderId(
event.transaction.hash.toHex(),
event.address.toHex(),
event.transaction.from.toHex()
)
)
2022-02-18 12:09:18 +01:00
const token = getToken(event.address, true)
2022-01-31 11:43:50 +01:00
order.datatoken = token.id
2021-11-26 09:04:14 +01:00
token.orderCount = token.orderCount.plus(integer.ONE)
const consumer = getUser(event.params.consumer.toHex())
order.consumer = consumer.id
if (token.nft) {
const nft = Nft.load(token.nft as string) as Nft
const nftOwner = getUser(nft.owner)
order.nftOwner = nftOwner.id
}
2021-11-26 09:04:14 +01:00
const payer = getUser(event.params.payer.toHex())
payer.totalOrders = payer.totalOrders.plus(integer.ONE)
payer.save()
2021-11-26 09:04:14 +01:00
order.payer = payer.id
order.amount = weiToDecimal(
event.params.amount.toBigDecimal(),
token.decimals
)
2022-01-31 11:43:50 +01:00
order.serviceIndex = event.params.serviceIndex.toI32()
2021-11-26 09:04:14 +01:00
const publishMarket = getUser(event.params.publishMarketAddress.toHex())
order.publishingMarket = publishMarket.id
// const consumeMarket = getUser(event.params..toHex())
// order.consumerMarket = consumeMarket.id
2021-11-26 09:04:14 +01:00
order.createdTimestamp = event.block.timestamp.toI32()
2021-12-02 12:08:47 +01:00
order.tx = event.transaction.hash.toHex()
2021-11-26 09:04:14 +01:00
order.block = event.block.number.toI32()
const tokenId = token.lastPriceToken
if (tokenId) {
const priceToken = getToken(Address.fromString(tokenId), false)
order.lastPriceToken = priceToken.id
order.lastPriceValue = token.lastPriceValue
order.estimatedUSDValue = getUSDValue(
priceToken.id,
order.lastPriceValue,
order.createdTimestamp
)
}
if (event.receipt !== null && event.receipt!.gasUsed) {
order.gasUsed = event.receipt!.gasUsed.toBigDecimal()
} else {
order.gasUsed = BigDecimal.zero()
}
if (event.transaction.gasPrice) {
order.gasPrice = event.transaction.gasPrice
} else {
order.gasPrice = BigInt.zero()
}
2021-11-26 09:04:14 +01:00
order.save()
token.save()
addOrder()
2022-02-17 13:00:54 +01:00
if (token.nft) {
const nft = Nft.load(token.nft as string) as Nft
if (nft) {
nft.orderCount = nft.orderCount.plus(integer.ONE)
nft.save()
}
const owner = getUser(nft.owner)
owner.totalSales = owner.totalSales.plus(integer.ONE)
owner.save()
2022-02-17 13:00:54 +01:00
}
2021-11-26 09:04:14 +01:00
}
export function handlerOrderReused(event: OrderReused): void {
const orderId = getOrderId(
event.params.orderTxId.toHexString(),
event.address.toHex(),
event.params.caller.toHex()
)
const order = Order.load(orderId)
if (!order) return
const reuseOrder = new OrderReuse(event.transaction.hash.toHex())
if (event.transaction.gasPrice)
reuseOrder.gasPrice = event.transaction.gasPrice
else reuseOrder.gasPrice = BigInt.zero()
if (event.receipt !== null && event.receipt!.gasUsed) {
reuseOrder.gasUsed = event.receipt!.gasUsed.toBigDecimal()
} else reuseOrder.gasUsed = BigDecimal.zero()
reuseOrder.order = orderId
reuseOrder.caller = event.params.caller.toHexString()
reuseOrder.createdTimestamp = event.params.timestamp.toI32()
reuseOrder.tx = event.transaction.hash.toHex()
reuseOrder.block = event.params.number.toI32()
reuseOrder.save()
}
export function handlePublishMarketFee(event: PublishMarketFee): void {}
2022-02-17 11:26:05 +01:00
export function handlePublishMarketFeeChanged(
event: PublishMarketFeeChanged
): void {
2022-02-18 12:09:18 +01:00
const token = getToken(event.address, true)
if (!token) return
token.publishMarketFeeAddress =
event.params.PublishMarketFeeAddress.toHexString()
token.publishMarketFeeToken = event.params.PublishMarketFeeToken.toHexString()
let decimals = BigInt.fromI32(18).toI32()
if (
token.publishMarketFeeToken != '0x0000000000000000000000000000000000000000'
) {
const token = getToken(event.params.PublishMarketFeeToken, false)
decimals = BigInt.fromI32(token.decimals).toI32()
2022-02-17 11:26:05 +01:00
}
token.publishMarketFeeAmount = weiToDecimal(
event.params.PublishMarketFeeAmount.toBigDecimal(),
decimals
)
token.save()
// TODO - shold we have a history
2022-02-17 11:26:05 +01:00
}
2022-02-18 12:09:18 +01:00
// roles
// roles
export function handleAddedMinter(event: AddedMinter): void {
const token = getToken(event.address, true)
let existingRoles: string[]
if (!token.minter) existingRoles = []
else existingRoles = token.minter as string[]
if (!existingRoles.includes(event.params.user.toHexString()))
existingRoles.push(event.params.user.toHexString())
token.minter = existingRoles
token.save()
}
export function handleRemovedMinter(event: RemovedMinter): void {
const token = getToken(event.address, true)
const newList: string[] = []
let existingRoles: string[]
if (!token.minter) existingRoles = []
else existingRoles = token.minter as string[]
if (!existingRoles || existingRoles.length < 1) return
while (existingRoles.length > 0) {
const role = existingRoles.shift().toString()
if (!role) break
if (role !== event.params.user.toHexString()) newList.push(role)
}
token.minter = newList
token.save()
}
export function handleAddedPaymentManager(event: AddedPaymentManager): void {
const token = getToken(event.address, true)
let existingRoles: string[]
if (!token.paymentManager) existingRoles = []
else existingRoles = token.paymentManager as string[]
if (!existingRoles.includes(event.params.user.toHexString()))
existingRoles.push(event.params.user.toHexString())
token.paymentManager = existingRoles
token.save()
}
export function handleRemovedPaymentManager(
event: RemovedPaymentManager
): void {
const token = getToken(event.address, true)
const newList: string[] = []
let existingRoles: string[]
if (!token.paymentManager) existingRoles = []
else existingRoles = token.paymentManager as string[]
if (!existingRoles || existingRoles.length < 1) return
while (existingRoles.length > 0) {
const role = existingRoles.shift().toString()
if (!role) break
if (role !== event.params.user.toHexString()) newList.push(role)
}
token.paymentManager = newList
token.save()
}
export function handleCleanedPermissions(event: CleanedPermissions): void {
const token = getToken(event.address, true)
const newList: string[] = []
token.paymentManager = newList
token.minter = newList
const nft = Nft.load(token.nft as string)
if (nft) token.paymentCollector = nft.owner
else token.paymentCollector = '0x0000000000000000000000000000000000000000'
token.save()
}
export function handleNewPaymentCollector(event: NewPaymentCollector): void {
const token = getToken(event.address, true)
token.paymentCollector = event.params._newPaymentCollector.toHexString()
token.save()
}
export function handleProviderFee(event: ProviderFee): void {
const providerFee: string = `{"providerFeeAddress": "${event.params.providerFeeAddress.toHex()}", "providerFeeToken": "${event.params.providerFeeToken.toHex()}", "providerFeeAmount": "${
event.params.providerFeeAmount
}", "providerData": "${event.params.providerData.toHexString()}", "v": "${
event.params.v
}", "r": "${event.params.r.toHexString()}", "s": "${event.params.s.toHexString()}", "validUntil": "${
event.params.validUntil
}"}`
const orderId = getOrderId(
event.transaction.hash.toHex(),
event.address.toHex(),
event.transaction.from.toHex()
)
const order = Order.load(orderId)
if (order) {
order.providerFee = providerFee
order.providerFeeValidUntil = event.params.validUntil
order.save()
return
}
let orderReuse = OrderReuse.load(event.transaction.hash.toHex())
if (orderReuse) {
orderReuse.providerFee = providerFee
orderReuse.providerFeeValidUntil = event.params.validUntil
orderReuse.save()
} else {
orderReuse = new OrderReuse(event.transaction.hash.toHex())
orderReuse.providerFee = providerFee
orderReuse.providerFeeValidUntil = event.params.validUntil
orderReuse.order = orderId
orderReuse.createdTimestamp = event.block.timestamp.toI32()
orderReuse.tx = event.transaction.hash.toHex()
orderReuse.block = event.block.number.toI32()
orderReuse.caller = event.transaction.from.toHex()
if (event.transaction.gasPrice)
orderReuse.gasPrice = event.transaction.gasPrice
else orderReuse.gasPrice = BigInt.zero()
if (event.receipt !== null && event.receipt!.gasUsed) {
orderReuse.gasUsed = event.receipt!.gasUsed.toBigDecimal()
} else orderReuse.gasUsed = BigDecimal.zero()
orderReuse.save()
}
}