|
| 1 | +// tslint:disable: only-arrow-functions prefer-for-of |
| 2 | + |
| 3 | +import { BigInt } from "@graphprotocol/graph-ts"; |
| 4 | + |
| 5 | +import { Gateway, LogBurn, LogMint } from "../generated/BCHGateway/Gateway"; |
| 6 | +import { Transaction } from "../generated/schema"; |
| 7 | +import { getDayData, getIntegrator, getRenVM, I32, oneBigInt } from "./common"; |
| 8 | + |
| 9 | +const periods: string[] = ["HOUR", "DAY", "WEEK", "MONTH", "YEAR"]; |
| 10 | + |
| 11 | +export function handleLogMint(event: LogMint): void { |
| 12 | + |
| 13 | + const txid = event.transaction.hash.toHex() + "-" + event.logIndex.toString(); |
| 14 | + const tx = new Transaction(txid); |
| 15 | + |
| 16 | + const contract = Gateway.bind(event.address); |
| 17 | + const to = event.transaction.to; |
| 18 | + |
| 19 | + tx.createdTimestamp = event.block.timestamp; |
| 20 | + tx.asset = "BCH"; |
| 21 | + tx.amount = event.params._amount; |
| 22 | + tx.feeRate = BigInt.fromI32(contract.mintFee()); |
| 23 | + tx.type = "mint"; |
| 24 | + tx.adapterAddress = to; |
| 25 | + tx.save(); |
| 26 | + |
| 27 | + // Nov 2 2018 is 1541116800 for dayStartTimestamp and 17837 for dayID |
| 28 | + // Nov 3 2018 would be 1541116800 + 86400 and 17838. And so on, for each exchange |
| 29 | + const timestamp: I32 = event.block.timestamp.toI32(); |
| 30 | + |
| 31 | + // Update Global Values |
| 32 | + const renVM = getRenVM(); |
| 33 | + renVM.totalTxCountBCH = renVM.totalTxCountBCH.plus(oneBigInt()); |
| 34 | + renVM.totalVolumeBCH = renVM.totalVolumeBCH.plus(tx.amount); |
| 35 | + renVM.totalLockedBCH = renVM.totalLockedBCH.plus(tx.amount); |
| 36 | + renVM.save(); |
| 37 | + |
| 38 | + const integrator = getIntegrator(tx.adapterAddress); |
| 39 | + integrator.totalTxCountBCH = integrator.totalTxCountBCH.plus(oneBigInt()); |
| 40 | + integrator.totalVolumeBCH = integrator.totalVolumeBCH.plus(tx.amount); |
| 41 | + integrator.totalLockedBCH = integrator.totalLockedBCH.plus(tx.amount); |
| 42 | + integrator.save(); |
| 43 | + |
| 44 | + for (let i = 0; i < periods.length; i++) { |
| 45 | + const dayData = getDayData(timestamp, periods[i]); |
| 46 | + |
| 47 | + // save info |
| 48 | + dayData.periodTxCountBCH = dayData.periodTxCountBCH.plus(oneBigInt()); |
| 49 | + dayData.periodVolumeBCH = dayData.periodVolumeBCH.plus(tx.amount); |
| 50 | + dayData.periodLockedBCH = dayData.periodLockedBCH.plus(tx.amount); |
| 51 | + |
| 52 | + dayData.totalTxCountBCH = renVM.totalTxCountBCH; |
| 53 | + dayData.totalVolumeBCH = renVM.totalVolumeBCH; |
| 54 | + dayData.totalLockedBCH = renVM.totalLockedBCH; |
| 55 | + |
| 56 | + dayData.save(); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +export function handleLogBurn(event: LogBurn): void { |
| 61 | + const txid = event.transaction.hash.toHex() + "-" + event.logIndex.toString(); |
| 62 | + const tx = new Transaction(txid); |
| 63 | + |
| 64 | + const contract = Gateway.bind(event.address); |
| 65 | + const to = event.transaction.to; |
| 66 | + |
| 67 | + tx.createdTimestamp = event.block.timestamp; |
| 68 | + tx.asset = "RenBCH"; |
| 69 | + tx.amount = event.params._amount; |
| 70 | + tx.feeRate = BigInt.fromI32(contract.burnFee()); |
| 71 | + tx.type = "burn"; |
| 72 | + tx.adapterAddress = to; |
| 73 | + tx.save(); |
| 74 | + |
| 75 | + // Nov 2 2018 is 1541116800 for dayStartTimestamp and 17837 for dayID |
| 76 | + // Nov 3 2018 would be 1541116800 + 86400 and 17838. And so on, for each exchange |
| 77 | + const timestamp: I32 = event.block.timestamp.toI32(); |
| 78 | + |
| 79 | + // Update Global Values |
| 80 | + const renVM = getRenVM(); |
| 81 | + renVM.totalTxCountBCH = renVM.totalTxCountBCH.plus(oneBigInt()); |
| 82 | + renVM.totalVolumeBCH = renVM.totalVolumeBCH.plus(tx.amount); |
| 83 | + renVM.totalLockedBCH = renVM.totalLockedBCH.minus(tx.amount); |
| 84 | + renVM.save(); |
| 85 | + |
| 86 | + for (let i = 0; i < periods.length; i++) { |
| 87 | + const dayData = getDayData(timestamp, periods[i]); |
| 88 | + |
| 89 | + // save info |
| 90 | + dayData.periodTxCountBCH = dayData.periodTxCountBCH.plus(oneBigInt()); |
| 91 | + dayData.periodVolumeBCH = dayData.periodVolumeBCH.plus(tx.amount); |
| 92 | + dayData.periodLockedBCH = dayData.periodLockedBCH.minus(tx.amount); |
| 93 | + |
| 94 | + dayData.totalTxCountBCH = renVM.totalTxCountBCH; |
| 95 | + dayData.totalVolumeBCH = renVM.totalVolumeBCH; |
| 96 | + dayData.totalLockedBCH = renVM.totalLockedBCH; |
| 97 | + |
| 98 | + dayData.save(); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments