dispenser updates (#341)

This commit is contained in:
Alex Coseru 2022-02-20 14:36:26 +02:00 committed by GitHub
parent cbdbc64a42
commit 68db058782
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 13 deletions

View File

@ -11,7 +11,7 @@ import { decimal } from './utils/constants'
import {
getDispenser,
getDispenserGraphID,
updateDispenserBalance
updateDispenserDetails
} from './utils/dispenserUtils'
import { weiToDecimal } from './utils/generic'
import { addDispenser } from './utils/globalUtils'
@ -45,6 +45,7 @@ export function handleNewDispenser(event: DispenserCreated): void {
dispenser.save()
addDispenser()
updateDispenserDetails(event.address, event.params.datatokenAddress)
}
export function handleActivate(event: DispenserActivated): void {
@ -88,13 +89,7 @@ export function handleTokensDispensed(event: TokensDispensed): void {
const dispenserTransaction = new DispenserTransaction(id)
const dispenser = getDispenser(dispenserID)
dispenser.balance = updateDispenserBalance(
event.address,
event.params.datatokenAddress
)
dispenser.save()
updateDispenserDetails(event.address, event.params.datatokenAddress)
dispenserTransaction.dispenser = dispenser.id
const user = getUser(event.params.userAddress.toHex())
dispenserTransaction.user = user.id

View File

@ -1,6 +1,6 @@
import { Dispenser } from '../../@types/schema'
import { getToken } from './tokenUtils'
import { Address, BigDecimal } from '@graphprotocol/graph-ts'
import { Address } from '@graphprotocol/graph-ts'
import { weiToDecimal } from './generic'
import { Dispenser as DispenserContract } from '../../@types/Dispenser/Dispenser'
@ -25,16 +25,21 @@ export function getDispenser(dispenserID: string): Dispenser {
return dispenser
}
export function updateDispenserBalance(
export function updateDispenserDetails(
contractAddress: Address,
datatokenAddress: Address
): BigDecimal {
): void {
const dispenserID = getDispenserGraphID(contractAddress, datatokenAddress)
const dispenser = getDispenser(dispenserID)
const contract = DispenserContract.bind(contractAddress)
const dispenserDetails = contract.try_status(datatokenAddress)
if (dispenserDetails == null) return BigDecimal.fromString('0')
if (dispenserDetails == null) return
const token = getToken(datatokenAddress, true)
return weiToDecimal(
dispenser.balance = weiToDecimal(
dispenserDetails.value.value5.toBigDecimal(),
token.decimals
)
dispenser.isMinter = dispenserDetails.value.value2
dispenser.active = dispenserDetails.value.value0
dispenser.save()
}