Skip to content

Commit

Permalink
refactor: add address to converter network
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyar committed Nov 26, 2024
1 parent c152752 commit eb32182
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 27 deletions.
1 change: 1 addition & 0 deletions subgraphs/protocol-reserve/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Converter Network entity
"""
type ConverterNetwork @entity {
id: Bytes!
address: Bytes!
tokenConverters: [TokenConverter!]! @derivedFrom(field: "converterNetwork")
}

Expand Down
10 changes: 8 additions & 2 deletions subgraphs/protocol-reserve/src/mappings/converterNetwork.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
21 changes: 20 additions & 1 deletion subgraphs/protocol-reserve/src/operations/create.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
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';
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);
Expand Down
25 changes: 2 additions & 23 deletions subgraphs/protocol-reserve/src/operations/getOrCreate.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion subgraphs/protocol-reserve/template-eth.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
4 changes: 4 additions & 0 deletions subgraphs/protocol-reserve/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down

0 comments on commit eb32182

Please sign in to comment.