2022-05-25 19:04:26 +02:00
|
|
|
import { Order, Nft, OrderReuse } from '../@types/schema'
|
2022-11-21 15:31:13 +01:00
|
|
|
import { BigInt, BigDecimal, Address } from '@graphprotocol/graph-ts'
|
2022-09-20 22:47:09 +02:00
|
|
|
|
2022-01-14 15:51:23 +01:00
|
|
|
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,
|
2022-05-25 19:04:26 +02:00
|
|
|
CleanedPermissions,
|
2022-07-12 10:11:43 +02:00
|
|
|
OrderReused,
|
|
|
|
ProviderFee
|
2022-01-14 15:51:23 +01:00
|
|
|
} from '../@types/templates/ERC20Template/ERC20Template'
|
|
|
|
|
2021-11-26 09:04:14 +01:00
|
|
|
import { integer } from './utils/constants'
|
|
|
|
import { weiToDecimal } from './utils/generic'
|
2022-02-15 17:13:55 +01:00
|
|
|
import { addOrder } from './utils/globalUtils'
|
2022-03-03 10:55:43 +01:00
|
|
|
import { getToken, getUSDValue } from './utils/tokenUtils'
|
2021-11-26 09:04:14 +01:00
|
|
|
import { getUser } from './utils/userUtils'
|
2022-07-12 10:11:43 +02:00
|
|
|
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
|
|
|
|
|
2022-11-24 14:40:31 +01:00
|
|
|
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())
|
2022-02-17 16:22:02 +01:00
|
|
|
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
|
|
|
|
|
2022-01-28 12:05:04 +01:00
|
|
|
// 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()
|
2022-11-21 15:31:13 +01:00
|
|
|
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
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-21 15:07:10 +02:00
|
|
|
if (event.receipt !== null && event.receipt!.gasUsed) {
|
|
|
|
order.gasUsed = event.receipt!.gasUsed.toBigDecimal()
|
2022-09-20 22:47:09 +02:00
|
|
|
} else {
|
2022-09-21 15:07:10 +02:00
|
|
|
order.gasUsed = BigDecimal.zero()
|
2022-09-20 22:47:09 +02:00
|
|
|
}
|
|
|
|
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()
|
2022-02-15 17:13:55 +01:00
|
|
|
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()
|
|
|
|
}
|
2022-02-17 16:22:02 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-05-25 19:04:26 +02: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())
|
2022-09-20 22:47:09 +02:00
|
|
|
if (event.transaction.gasPrice)
|
|
|
|
reuseOrder.gasPrice = event.transaction.gasPrice
|
|
|
|
else reuseOrder.gasPrice = BigInt.zero()
|
2022-09-21 15:07:10 +02:00
|
|
|
if (event.receipt !== null && event.receipt!.gasUsed) {
|
|
|
|
reuseOrder.gasUsed = event.receipt!.gasUsed.toBigDecimal()
|
|
|
|
} else reuseOrder.gasUsed = BigDecimal.zero()
|
2022-05-25 19:04:26 +02:00
|
|
|
reuseOrder.order = orderId
|
|
|
|
reuseOrder.caller = event.params.caller.toHexString()
|
2022-07-25 15:12:28 +02:00
|
|
|
reuseOrder.createdTimestamp = event.params.timestamp.toI32()
|
2022-05-25 19:04:26 +02:00
|
|
|
reuseOrder.tx = event.transaction.hash.toHex()
|
2022-07-25 15:12:28 +02:00
|
|
|
reuseOrder.block = event.params.number.toI32()
|
2022-05-25 19:04:26 +02:00
|
|
|
|
|
|
|
reuseOrder.save()
|
|
|
|
}
|
|
|
|
|
2022-02-14 18:25:55 +01:00
|
|
|
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)
|
2022-04-29 13:37:07 +02:00
|
|
|
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
|
|
|
}
|
2022-04-29 13:37:07 +02: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()
|
|
|
|
}
|
|
|
|
|
2022-07-12 10:11:43 +02:00
|
|
|
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
|
2022-07-25 15:12:28 +02:00
|
|
|
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()
|
2022-09-20 22:47:09 +02:00
|
|
|
if (event.transaction.gasPrice)
|
|
|
|
orderReuse.gasPrice = event.transaction.gasPrice
|
|
|
|
else orderReuse.gasPrice = BigInt.zero()
|
2022-09-21 15:07:10 +02:00
|
|
|
if (event.receipt !== null && event.receipt!.gasUsed) {
|
|
|
|
orderReuse.gasUsed = event.receipt!.gasUsed.toBigDecimal()
|
|
|
|
} else orderReuse.gasUsed = BigDecimal.zero()
|
2022-07-12 10:11:43 +02:00
|
|
|
orderReuse.save()
|
|
|
|
}
|
|
|
|
}
|