ocean-subgraph/src/mappings/factoryRouter.ts

155 lines
5.1 KiB
TypeScript
Raw Normal View History

2022-02-18 16:38:51 +01:00
import {
TokenAdded,
2022-02-21 11:45:24 +01:00
TokenRemoved,
2022-02-18 16:38:51 +01:00
OPCFeeChanged,
2022-02-21 11:45:24 +01:00
FactoryRouter,
FixedRateContractAdded,
FixedRateContractRemoved,
DispenserContractAdded,
DispenserContractRemoved
2022-02-18 16:38:51 +01:00
} from '../@types/FactoryRouter/FactoryRouter'
import { BigInt } from '@graphprotocol/graph-ts'
import { FixedRateExchange, Dispenser } from '../@types/templates'
import { getOPC, getTemplates } from './utils/globalUtils'
2022-02-18 16:38:51 +01:00
import { weiToDecimal } from './utils/generic'
2022-06-23 13:51:14 +02:00
import { getToken } from './utils/tokenUtils'
2022-02-18 16:38:51 +01:00
export function handleOPCFeeChanged(event: OPCFeeChanged): void {
const opc = getOPC()
const decimals = BigInt.fromI32(18).toI32()
opc.swapOceanFee = weiToDecimal(
event.params.newSwapOceanFee.toBigDecimal(),
decimals
)
opc.swapNonOceanFee = weiToDecimal(
event.params.newSwapNonOceanFee.toBigDecimal(),
decimals
)
2022-05-17 11:27:48 +02:00
opc.orderFee = weiToDecimal(
2022-02-18 16:38:51 +01:00
event.params.newConsumeFee.toBigDecimal(),
decimals
)
opc.providerFee = weiToDecimal(
event.params.newProviderFee.toBigDecimal(),
decimals
)
opc.save()
}
export function handleTokenAdded(event: TokenAdded): void {
const contract = FactoryRouter.bind(event.address)
const oceanFees = contract.try_getOPCFees()
if (oceanFees.reverted) return
const opc = getOPC()
const decimals = BigInt.fromI32(18).toI32()
opc.swapOceanFee = weiToDecimal(
oceanFees.value.value0.toBigDecimal(),
decimals
)
opc.swapNonOceanFee = weiToDecimal(
oceanFees.value.value1.toBigDecimal(),
decimals
)
2022-05-17 11:27:48 +02:00
const newOrderFee = contract.try_getOPCConsumeFee()
if (newOrderFee.reverted) return
2022-02-18 16:38:51 +01:00
const newProviderFee = contract.try_getOPCProviderFee()
if (newProviderFee.reverted) return
2022-05-17 11:27:48 +02:00
opc.orderFee = weiToDecimal(newOrderFee.value.toBigDecimal(), decimals)
2022-02-18 16:38:51 +01:00
opc.providerFee = weiToDecimal(newProviderFee.value.toBigDecimal(), decimals)
2022-02-21 11:45:24 +01:00
// add token to approvedTokens
let existingTokens: string[]
if (!opc.approvedTokens) existingTokens = []
else existingTokens = opc.approvedTokens as string[]
2022-06-23 13:51:14 +02:00
if (!existingTokens.includes(event.params.token.toHexString())) {
const newToken = getToken(event.params.token, false)
existingTokens.push(newToken.id)
}
2022-02-21 11:45:24 +01:00
opc.approvedTokens = existingTokens
2022-02-18 16:38:51 +01:00
opc.save()
}
2022-02-21 11:45:24 +01:00
export function handleTokenRemoved(event: TokenRemoved): void {
const opc = getOPC()
const newList: string[] = []
let existingTokens: string[]
if (!opc.approvedTokens) existingTokens = []
else existingTokens = opc.approvedTokens as string[]
if (!existingTokens || existingTokens.length < 1) return
while (existingTokens.length > 0) {
const role = existingTokens.shift().toString()
if (!role) break
2022-09-28 12:54:46 +02:00
if (role != event.params.token.toHexString()) newList.push(role)
2022-02-21 11:45:24 +01:00
}
opc.approvedTokens = newList
opc.save()
}
2022-02-21 11:45:24 +01:00
export function handleFixedRateContractAdded(
event: FixedRateContractAdded
): void {
FixedRateExchange.create(event.params.contractAddress)
// add token to approvedTokens
const templates = getTemplates()
let existingContracts: string[]
if (!templates.fixedRateTemplates) existingContracts = []
else existingContracts = templates.fixedRateTemplates as string[]
if (!existingContracts.includes(event.params.contractAddress.toHexString()))
existingContracts.push(event.params.contractAddress.toHexString())
templates.fixedRateTemplates = existingContracts
templates.save()
}
export function handleFixedRateContractRemoved(
event: FixedRateContractRemoved
): void {
const templates = getTemplates()
const newList: string[] = []
let existingContracts: string[]
if (!templates.fixedRateTemplates) existingContracts = []
else existingContracts = templates.fixedRateTemplates as string[]
if (!existingContracts || existingContracts.length < 1) return
while (existingContracts.length > 0) {
const role = existingContracts.shift().toString()
if (!role) break
2022-09-28 12:54:46 +02:00
if (role != event.params.contractAddress.toHexString()) newList.push(role)
2022-02-21 11:45:24 +01:00
}
templates.fixedRateTemplates = newList
templates.save()
}
export function handleDispenserContractAdded(
event: DispenserContractAdded
): void {
Dispenser.create(event.params.contractAddress)
const templates = getTemplates()
let existingContracts: string[]
if (!templates.dispenserTemplates) existingContracts = []
else existingContracts = templates.dispenserTemplates as string[]
if (!existingContracts.includes(event.params.contractAddress.toHexString()))
existingContracts.push(event.params.contractAddress.toHexString())
templates.dispenserTemplates = existingContracts
templates.save()
}
export function handleDispenserContractRemoved(
event: DispenserContractRemoved
): void {
const templates = getTemplates()
const newList: string[] = []
let existingContracts: string[]
if (!templates.dispenserTemplates) existingContracts = []
else existingContracts = templates.dispenserTemplates as string[]
if (!existingContracts || existingContracts.length < 1) return
while (existingContracts.length > 0) {
const role = existingContracts.shift().toString()
if (!role) break
2022-09-28 12:54:46 +02:00
if (role != event.params.contractAddress.toHexString()) newList.push(role)
2022-02-21 11:45:24 +01:00
}
templates.dispenserTemplates = newList
templates.save()
}