From eb321825158df49a0c6511da434ede4eff4ed0bd Mon Sep 17 00:00:00 2001 From: Corey Rice Date: Tue, 26 Nov 2024 12:36:07 -0300 Subject: [PATCH] refactor: add address to converter network --- subgraphs/protocol-reserve/schema.graphql | 1 + .../src/mappings/converterNetwork.ts | 10 ++++++-- .../protocol-reserve/src/operations/create.ts | 21 +++++++++++++++- .../src/operations/getOrCreate.ts | 25 ++----------------- subgraphs/protocol-reserve/template-eth.yaml | 5 +++- subgraphs/protocol-reserve/template.yaml | 4 +++ 6 files changed, 39 insertions(+), 27 deletions(-) diff --git a/subgraphs/protocol-reserve/schema.graphql b/subgraphs/protocol-reserve/schema.graphql index 904d09dd..9ea11641 100644 --- a/subgraphs/protocol-reserve/schema.graphql +++ b/subgraphs/protocol-reserve/schema.graphql @@ -10,6 +10,7 @@ Converter Network entity """ type ConverterNetwork @entity { id: Bytes! + address: Bytes! tokenConverters: [TokenConverter!]! @derivedFrom(field: "converterNetwork") } diff --git a/subgraphs/protocol-reserve/src/mappings/converterNetwork.ts b/subgraphs/protocol-reserve/src/mappings/converterNetwork.ts index deee2444..bd12d513 100644 --- a/subgraphs/protocol-reserve/src/mappings/converterNetwork.ts +++ b/subgraphs/protocol-reserve/src/mappings/converterNetwork.ts @@ -1,12 +1,18 @@ +import { ethereum } from '@graphprotocol/graph-ts'; import { ConverterAdded, ConverterRemoved, } from '../../generated/ConverterNetwork/ConverterNetwork'; -import { getOrCreateConverterNetwork, getOrCreateTokenConverter } from '../operations/getOrCreate'; +import { getOrCreateTokenConverter } from '../operations/getOrCreate'; +import { createConverterNetwork } from '../operations/create'; import { getConverterNetworkId } from '../utilities/ids'; +// eslint-disable-next-line @typescript-eslint/no-unused-vars +export function handleInitializationConverterNetwork(block: ethereum.Block): void { + createConverterNetwork(); +} + export function handleConverterAdded(event: ConverterAdded): void { - getOrCreateConverterNetwork(event.address); const tokenConverter = getOrCreateTokenConverter(event.params.converter); tokenConverter.converterNetwork = getConverterNetworkId(event.address); tokenConverter.save(); diff --git a/subgraphs/protocol-reserve/src/operations/create.ts b/subgraphs/protocol-reserve/src/operations/create.ts index 960140ff..e036b125 100644 --- a/subgraphs/protocol-reserve/src/operations/create.ts +++ b/subgraphs/protocol-reserve/src/operations/create.ts @@ -1,5 +1,6 @@ import { Address } from '@graphprotocol/graph-ts'; +import { ConverterNetwork } from '../../generated/schema'; import { TokenConverter as TokenConverterContract } from '../../generated/BTCBPrimeConverter/TokenConverter'; import { RiskFund } from '../../generated/ConverterNetwork/RiskFund'; import { TokenConverter, TokenConverterConfig } from '../../generated/schema'; @@ -7,8 +8,26 @@ import { ERC20 } from '../../generated/templates'; import { zeroBigInt32 } from '../constants'; import { riskFundAddress, riskFundConverterAddress } from '../constants/addresses'; import { valueOrNotAvailableAddressIfReverted } from '../utilities'; -import { getTokenConverterConfigId, getTokenConverterId } from '../utilities/ids'; +import { + getTokenConverterConfigId, + getTokenConverterId, + getConverterNetworkId, +} from '../utilities/ids'; import { getOrCreateToken } from './getOrCreate'; +import { converterNetworkAddress } from '../constants/addresses'; + +/** + * ConverterNetwork is hardcoded in the subgraph definition + * + * @param converterNetworkAddress + * @returns + */ +export function createConverterNetwork(): ConverterNetwork { + const converterNetwork = new ConverterNetwork(getConverterNetworkId(converterNetworkAddress)); + converterNetwork.address = converterNetworkAddress; + + return converterNetwork; +} export function createTokenConverter(tokenConverterAddress: Address): TokenConverter { const tokenConverterContract = TokenConverterContract.bind(tokenConverterAddress); diff --git a/subgraphs/protocol-reserve/src/operations/getOrCreate.ts b/subgraphs/protocol-reserve/src/operations/getOrCreate.ts index 61d078cb..9a1a4dd0 100644 --- a/subgraphs/protocol-reserve/src/operations/getOrCreate.ts +++ b/subgraphs/protocol-reserve/src/operations/getOrCreate.ts @@ -1,13 +1,8 @@ import { Address } from '@graphprotocol/graph-ts'; import { ERC20 } from '../../generated/RiskFundConverter/ERC20'; -import { - ConverterNetwork, - Token, - TokenConverter, - TokenConverterConfig, -} from '../../generated/schema'; -import { getAssetId, getConverterNetworkId } from '../utilities/ids'; +import { Token, TokenConverter, TokenConverterConfig } from '../../generated/schema'; +import { getAssetId } from '../utilities/ids'; import { createTokenConverter, createTokenConverterConfig } from './create'; import { getTokenConverter, getTokenConverterConfig } from './get'; @@ -27,22 +22,6 @@ export function getOrCreateTokenConverter(tokenConverterAddress: Address): Token return tokenConverter; } -/** - * ConverterNetwork is hardcoded in the subgraph definition - * - * @param converterNetworkAddress - * @returns - */ -export function getOrCreateConverterNetwork(converterNetworkAddress: Address): ConverterNetwork { - let converterNetwork = ConverterNetwork.load(getConverterNetworkId(converterNetworkAddress)); - - if (!converterNetwork) { - converterNetwork = new ConverterNetwork(getConverterNetworkId(converterNetworkAddress)); - } - - return converterNetwork; -} - export function getOrCreateTokenConverterConfig( tokenConverterAddress: Address, tokenAddressIn: Address, diff --git a/subgraphs/protocol-reserve/template-eth.yaml b/subgraphs/protocol-reserve/template-eth.yaml index 4e7a7a6f..e8a74322 100644 --- a/subgraphs/protocol-reserve/template-eth.yaml +++ b/subgraphs/protocol-reserve/template-eth.yaml @@ -28,7 +28,10 @@ dataSources: handler: handleConverterAdded - event: ConverterRemoved(indexed address) handler: handleConverterRemoved - + blockHandlers: + - handler: handleInitializationConverterNetwork + filter: + kind: once - name: WBTCPrimeConverter kind: ethereum/contract network: {{ network }} diff --git a/subgraphs/protocol-reserve/template.yaml b/subgraphs/protocol-reserve/template.yaml index 88243a23..5720d7a6 100644 --- a/subgraphs/protocol-reserve/template.yaml +++ b/subgraphs/protocol-reserve/template.yaml @@ -28,6 +28,10 @@ dataSources: handler: handleConverterAdded - event: ConverterRemoved(indexed address) handler: handleConverterRemoved + blockHandlers: + - handler: handleInitializationConverterNetwork + filter: + kind: once - name: BTCBPrimeConverter kind: ethereum/contract network: {{ network }}