ocean-subgraph/src/mappings/dispenser.ts

115 lines
3.6 KiB
TypeScript
Raw Normal View History

2021-11-10 13:47:44 +01:00
import {
DispenserActivated,
DispenserAllowedSwapperChanged,
DispenserDeactivated,
OwnerWithdrawed,
TokensDispensed
2022-02-21 11:45:24 +01:00
} from '../@types/templates/Dispenser/Dispenser'
2021-11-10 13:47:44 +01:00
import { DispenserCreated } from '../@types/ERC721Factory/ERC721Factory'
import { Dispenser, DispenserTransaction } from '../@types/schema'
2021-11-15 13:04:26 +01:00
import { decimal } from './utils/constants'
import {
getDispenser,
getDispenserGraphID,
2022-02-20 13:36:26 +01:00
updateDispenserDetails
} from './utils/dispenserUtils'
import { weiToDecimal } from './utils/generic'
import { addDispenser } from './utils/globalUtils'
import { getToken } from './utils/tokenUtils'
2021-11-10 13:47:44 +01:00
import { getUser } from './utils/userUtils'
export function handleNewDispenser(event: DispenserCreated): void {
const dispenserID = getDispenserGraphID(
event.address,
event.params.datatokenAddress
)
const dispenser = new Dispenser(dispenserID)
2022-02-18 12:09:18 +01:00
const token = getToken(event.params.datatokenAddress, false)
dispenser.token = token.id
2022-02-21 11:45:24 +01:00
dispenser.contract = event.address.toHexString()
dispenser.owner = event.params.owner.toHexString()
dispenser.maxBalance = weiToDecimal(
event.params.maxBalance.toBigDecimal(),
token.decimals
)
dispenser.maxTokens = weiToDecimal(
event.params.maxTokens.toBigDecimal(),
token.decimals
)
dispenser.active = true
dispenser.allowedSwapper = event.params.allowedSwapper.toHex()
dispenser.createdTimestamp = event.block.timestamp.toI32()
dispenser.tx = event.transaction.hash.toHex()
dispenser.block = event.block.number.toI32()
dispenser.save()
addDispenser()
2022-02-20 13:36:26 +01:00
updateDispenserDetails(event.address, event.params.datatokenAddress)
2021-11-10 13:47:44 +01:00
}
export function handleActivate(event: DispenserActivated): void {
const dispenserID = getDispenserGraphID(
event.address,
event.params.datatokenAddress
)
const dispenser = getDispenser(dispenserID)
2021-11-10 13:47:44 +01:00
dispenser.active = true
dispenser.save()
}
export function handleDeactivate(event: DispenserDeactivated): void {
const dispenserID = getDispenserGraphID(
event.address,
event.params.datatokenAddress
)
const dispenser = getDispenser(dispenserID)
2021-11-10 13:47:44 +01:00
dispenser.active = true
dispenser.save()
}
export function handleAllowedSwapperChanged(
event: DispenserAllowedSwapperChanged
): void {
const dispenserID = getDispenserGraphID(event.address, event.params.datatoken)
const dispenser = getDispenser(dispenserID)
2021-11-10 13:47:44 +01:00
dispenser.allowedSwapper = event.params.newAllowedSwapper.toHex()
dispenser.save()
}
export function handleTokensDispensed(event: TokensDispensed): void {
const dispenserID = getDispenserGraphID(
event.address,
event.params.datatokenAddress
)
2021-11-10 13:47:44 +01:00
const id = event.transaction.hash
.toHexString()
.concat('-')
.concat(dispenserID)
2021-11-10 13:47:44 +01:00
const dispenserTransaction = new DispenserTransaction(id)
const dispenser = getDispenser(dispenserID)
2022-02-20 13:36:26 +01:00
updateDispenserDetails(event.address, event.params.datatokenAddress)
2021-11-10 13:47:44 +01:00
dispenserTransaction.dispenser = dispenser.id
const user = getUser(event.params.userAddress.toHex())
dispenserTransaction.user = user.id
dispenserTransaction.createdTimestamp = event.block.timestamp.toI32()
2021-12-02 12:08:47 +01:00
dispenserTransaction.tx = event.transaction.hash.toHex()
2021-11-10 13:47:44 +01:00
dispenserTransaction.block = event.block.number.toI32()
const token = getToken(event.params.datatokenAddress, true)
dispenserTransaction.amount = weiToDecimal(
event.params.amount.toBigDecimal(),
token.decimals
)
2021-11-10 13:47:44 +01:00
dispenserTransaction.save()
}
export function handleOwnerWinthdraw(event: OwnerWithdrawed): void {
const dispenserID = getDispenserGraphID(event.address, event.params.datatoken)
const dispenser = getDispenser(dispenserID)
2021-11-19 15:42:17 +01:00
dispenser.balance = decimal.ZERO
2021-11-10 13:47:44 +01:00
dispenser.save()
}