diff --git a/schema.graphql b/schema.graphql index d85c7bb..eb48ee0 100644 --- a/schema.graphql +++ b/schema.graphql @@ -365,7 +365,8 @@ type FixedRateExchangeSwap @entity { type Dispenser @entity { "token address" - id: ID! + id: ID! + contract: String! active: Boolean! "if using the enterprise template the owner will always be the erc721 factory, for normal template it will a user" owner: String @@ -486,6 +487,7 @@ type OPC @entity { consumeFee: BigDecimal "fee in percent taken by OPC from providerFees" providerFee: BigDecimal + approvedTokens: [String!] } enum NftUpdateType { @@ -516,3 +518,11 @@ type NftUpdate @entity { timestamp: Int! tx: String! } + + +type Template @entity{ + id: ID! + fixedRateTemplates: [String!] + dispenserTemplates: [String!] + ssTemplates: [String!] +} \ No newline at end of file diff --git a/scripts/generatenetworkssubgraphs.js b/scripts/generatenetworkssubgraphs.js index c6c7052..3f0b559 100644 --- a/scripts/generatenetworkssubgraphs.js +++ b/scripts/generatenetworkssubgraphs.js @@ -30,14 +30,6 @@ async function replaceContractAddresses() { /__ERC721FACTORYADDRESS__/g, "'" + addresses[network].ERC721Factory + "'" ) - subgraph = subgraph.replace( - /__FIXEDRATEEXCHANGEADDRESS__/g, - "'" + addresses[network].FixedPrice + "'" - ) - subgraph = subgraph.replace( - /__DISPENSERADDRESS__/g, - "'" + addresses[network].Dispenser + "'" - ) subgraph = subgraph.replace( /__FACTORYROUTERADDRESS__/g, "'" + addresses[network].Router + "'" diff --git a/src/mappings/dispenser.ts b/src/mappings/dispenser.ts index 98ecc79..79e22ab 100644 --- a/src/mappings/dispenser.ts +++ b/src/mappings/dispenser.ts @@ -4,7 +4,7 @@ import { DispenserDeactivated, OwnerWithdrawed, TokensDispensed -} from '../@types/Dispenser/Dispenser' +} from '../@types/templates/Dispenser/Dispenser' import { DispenserCreated } from '../@types/ERC721Factory/ERC721Factory' import { Dispenser, DispenserTransaction } from '../@types/schema' import { decimal } from './utils/constants' @@ -26,6 +26,7 @@ export function handleNewDispenser(event: DispenserCreated): void { const dispenser = new Dispenser(dispenserID) const token = getToken(event.params.datatokenAddress, false) dispenser.token = token.id + dispenser.contract = event.address.toHexString() dispenser.owner = event.params.owner.toHexString() dispenser.maxBalance = weiToDecimal( diff --git a/src/mappings/factoryRouter.ts b/src/mappings/factoryRouter.ts index 6618f51..a5fef68 100644 --- a/src/mappings/factoryRouter.ts +++ b/src/mappings/factoryRouter.ts @@ -1,13 +1,20 @@ import { NewPool, TokenAdded, + TokenRemoved, OPCFeeChanged, - FactoryRouter + FactoryRouter, + SSContractAdded, + SSContractRemoved, + FixedRateContractAdded, + FixedRateContractRemoved, + DispenserContractAdded, + DispenserContractRemoved } from '../@types/FactoryRouter/FactoryRouter' import { BigInt } from '@graphprotocol/graph-ts' import { Pool } from '../@types/schema' -import { BPool } from '../@types/templates' -import { addPool, getOPC } from './utils/globalUtils' +import { BPool, FixedRateExchange, Dispenser } from '../@types/templates' +import { addPool, getOPC, getTemplates } from './utils/globalUtils' import { weiToDecimal } from './utils/generic' export function handleNewPool(event: NewPool): void { @@ -62,5 +69,119 @@ export function handleTokenAdded(event: TokenAdded): void { if (newProviderFee.reverted) return opc.consumeFee = weiToDecimal(newConsumeFee.value.toBigDecimal(), decimals) opc.providerFee = weiToDecimal(newProviderFee.value.toBigDecimal(), decimals) + + // add token to approvedTokens + let existingTokens: string[] + if (!opc.approvedTokens) existingTokens = [] + else existingTokens = opc.approvedTokens as string[] + if (!existingTokens.includes(event.params.token.toHexString())) + existingTokens.push(event.params.token.toHexString()) + opc.approvedTokens = existingTokens + opc.save() } + +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 + if (role !== event.params.token.toHexString()) newList.push(role) + } + opc.approvedTokens = newList + opc.save() +} + +export function handleSSContractAdded(event: SSContractAdded): void { + // add token to approvedTokens + const templates = getTemplates() + let existingContracts: string[] + if (!templates.ssTemplates) existingContracts = [] + else existingContracts = templates.ssTemplates as string[] + if (!existingContracts.includes(event.params.contractAddress.toHexString())) + existingContracts.push(event.params.contractAddress.toHexString()) + templates.ssTemplates = existingContracts + templates.save() +} +export function handleSSContractRemoved(event: SSContractRemoved): void { + const templates = getTemplates() + const newList: string[] = [] + let existingContracts: string[] + if (!templates.ssTemplates) existingContracts = [] + else existingContracts = templates.ssTemplates as string[] + if (!existingContracts || existingContracts.length < 1) return + while (existingContracts.length > 0) { + const role = existingContracts.shift().toString() + if (!role) break + if (role !== event.params.contractAddress.toHexString()) newList.push(role) + } + templates.ssTemplates = newList + templates.save() +} +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 + if (role !== event.params.contractAddress.toHexString()) newList.push(role) + } + 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 + if (role !== event.params.contractAddress.toHexString()) newList.push(role) + } + templates.dispenserTemplates = newList + templates.save() +} diff --git a/src/mappings/fixedRateExchange.ts b/src/mappings/fixedRateExchange.ts index ae277ab..855158e 100644 --- a/src/mappings/fixedRateExchange.ts +++ b/src/mappings/fixedRateExchange.ts @@ -8,7 +8,7 @@ import { ExchangeRateChanged, Swapped, PublishMarketFeeChanged -} from '../@types/FixedRateExchange/FixedRateExchange' +} from '../@types/templates/FixedRateExchange/FixedRateExchange' import { FixedRateExchange, FixedRateExchangeSwap, diff --git a/src/mappings/utils/dispenserUtils.ts b/src/mappings/utils/dispenserUtils.ts index 3ddeafb..93c51ee 100644 --- a/src/mappings/utils/dispenserUtils.ts +++ b/src/mappings/utils/dispenserUtils.ts @@ -2,7 +2,7 @@ import { Dispenser } from '../../@types/schema' import { getToken } from './tokenUtils' import { Address } from '@graphprotocol/graph-ts' import { weiToDecimal } from './generic' -import { Dispenser as DispenserContract } from '../../@types/Dispenser/Dispenser' +import { Dispenser as DispenserContract } from '../../@types/templates/Dispenser/Dispenser' export function getDispenserGraphID( contractAddress: Address, diff --git a/src/mappings/utils/fixedRateUtils.ts b/src/mappings/utils/fixedRateUtils.ts index 54b6a90..66a7645 100644 --- a/src/mappings/utils/fixedRateUtils.ts +++ b/src/mappings/utils/fixedRateUtils.ts @@ -1,6 +1,6 @@ import { FixedRateExchange } from '../../@types/schema' -import { FixedRateExchange as FixedRateExchangeContract } from '../../@types/FixedRateExchange/FixedRateExchange' +import { FixedRateExchange as FixedRateExchangeContract } from '../../@types/templates/FixedRateExchange/FixedRateExchange' import { Address, Bytes } from '@graphprotocol/graph-ts' import { getToken } from './tokenUtils' import { weiToDecimal } from './generic' diff --git a/src/mappings/utils/globalUtils.ts b/src/mappings/utils/globalUtils.ts index 8360975..db2f4f7 100644 --- a/src/mappings/utils/globalUtils.ts +++ b/src/mappings/utils/globalUtils.ts @@ -4,7 +4,8 @@ import { GlobalTotalFixedSwapPair, GlobalTotalLiquidityPair, GlobalTotalPoolSwapPair, - OPC + OPC, + Template } from '../../@types/schema' const GLOBAL_ID = '1' @@ -27,6 +28,15 @@ export function getOPC(): OPC { return globalStats } +export function getTemplates(): Template { + let templates = Template.load(GLOBAL_ID) + if (!templates) { + templates = new Template(GLOBAL_ID) + templates.save() + } + return templates +} + export function addOrder(): void { const globalStats = getGlobalStats() globalStats.orderCount = globalStats.orderCount + 1 diff --git a/subgraph.template.yaml b/subgraph.template.yaml index 398e3da..ce74523 100644 --- a/subgraph.template.yaml +++ b/subgraph.template.yaml @@ -28,75 +28,7 @@ dataSources: handler: handleNftCreated - event: TokenCreated(indexed address,indexed address,string,string,uint256,address) handler: handleNewToken - - kind: ethereum/contract - name: FixedRateExchange - network: __NETWORK__ - source: - address: __FIXEDRATEEXCHANGEADDRESS__ - abi: FixedRateExchange - startBlock: __STARTBLOCK__ - mapping: - kind: ethereum/events - apiVersion: 0.0.6 - language: wasm/assemblyscript - file: ./src/mappings/fixedRateExchange.ts - entities: - - FixedRateExchange - abis: - - name: FixedRateExchange - file: ./node_modules/@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json - - name: ERC20 - file: ./abis/ERC20.json - eventHandlers: - - event: ExchangeCreated(indexed bytes32,indexed address,indexed address,address,uint256) - handler: handleExchangeCreated - - event: ExchangeRateChanged(indexed bytes32,indexed address,uint256) - handler: handleRateChange - - event: ExchangeMintStateChanged(indexed bytes32,indexed address,bool) - handler: handleMintStateChanged - - event: ExchangeActivated(indexed bytes32,indexed address) - handler: handleActivated - - event: ExchangeDeactivated(indexed bytes32,indexed address) - handler: handleDeactivated - - event: ExchangeAllowedSwapperChanged(indexed bytes32,indexed address) - handler: handleAllowedSwapperChanged - - event: Swapped(indexed bytes32,indexed address,uint256,uint256,address,uint256,uint256,uint256) - handler: handleSwap - - event: PublishMarketFeeChanged(indexed bytes32,address,address,uint256) - handler: handlePublishMarketFeeChanged - - kind: ethereum/contract - name: Dispenser - network: __NETWORK__ - source: - address: __DISPENSERADDRESS__ - abi: Dispenser - startBlock: __STARTBLOCK__ - mapping: - kind: ethereum/events - apiVersion: 0.0.6 - language: wasm/assemblyscript - file: ./src/mappings/dispenser.ts - entities: - - Dispenser - abis: - - name: Dispenser - file: ./node_modules/@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json - - name: ERC20 - file: ./abis/ERC20.json - eventHandlers: - - event: DispenserCreated(indexed address,indexed address,uint256,uint256,address) - handler: handleNewDispenser - - event: DispenserActivated(indexed address) - handler: handleActivate - - event: DispenserDeactivated(indexed address) - handler: handleDeactivate - - event: DispenserAllowedSwapperChanged(indexed address,indexed address) - handler: handleAllowedSwapperChanged - - event: TokensDispensed(indexed address,indexed address,uint256) - handler: handleTokensDispensed - - event: OwnerWithdrawed(indexed address,indexed address,uint256) - handler: handleOwnerWinthdraw - + - kind: ethereum/contract name: FactoryRouter network: __NETWORK__ @@ -121,11 +53,25 @@ dataSources: handler: handleNewPool - event: TokenAdded(indexed address,indexed address) handler: handleTokenAdded + - event: TokenRemoved(indexed address,indexed address) + handler: handleTokenRemoved - event: OPCFeeChanged(indexed address,uint256,uint256,uint256,uint256) handler: handleOPCFeeChanged - + - event: SSContractAdded(indexed address,indexed address) + handler: handleSSContractAdded + - event: SSContractRemoved(indexed address,indexed address) + handler: handleSSContractRemoved + - event: FixedRateContractAdded(indexed address,indexed address) + handler: handleFixedRateContractAdded + - event: FixedRateContractRemoved(indexed address,indexed address) + handler: handleFixedRateContractRemoved + - event: DispenserContractAdded(indexed address,indexed address) + handler: handleDispenserContractAdded + - event: DispenserContractRemoved(indexed address,indexed address) + handler: handleDispenserContractRemoved templates: + - name: ERC20Template kind: ethereum/contract network: __NETWORK__ @@ -203,7 +149,7 @@ templates: handler: handlePublishMarketFeeChanged - event: SwapFeeChanged(address,uint256) handler: handleSwapFeeChanged - + - name: ERC721Template kind: ethereum/contract network: __NETWORK__ @@ -250,3 +196,69 @@ templates: handler: handleRemovedManager - event: CleanedPermissions(indexed address,uint256,uint256) handler: handleCleanedPermissions + + - name: Dispenser + kind: ethereum/contract + network: __NETWORK__ + source: + abi: Dispenser + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/dispenser.ts + entities: + - Dispenser + abis: + - name: Dispenser + file: ./node_modules/@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json + - name: ERC20 + file: ./abis/ERC20.json + eventHandlers: + - event: DispenserCreated(indexed address,indexed address,uint256,uint256,address) + handler: handleNewDispenser + - event: DispenserActivated(indexed address) + handler: handleActivate + - event: DispenserDeactivated(indexed address) + handler: handleDeactivate + - event: DispenserAllowedSwapperChanged(indexed address,indexed address) + handler: handleAllowedSwapperChanged + - event: TokensDispensed(indexed address,indexed address,uint256) + handler: handleTokensDispensed + - event: OwnerWithdrawed(indexed address,indexed address,uint256) + handler: handleOwnerWinthdraw + + - name: FixedRateExchange + kind: ethereum/contract + network: __NETWORK__ + source: + abi: FixedRateExchange + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/fixedRateExchange.ts + entities: + - FixedRateExchange + abis: + - name: FixedRateExchange + file: ./node_modules/@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json + - name: ERC20 + file: ./abis/ERC20.json + eventHandlers: + - event: ExchangeCreated(indexed bytes32,indexed address,indexed address,address,uint256) + handler: handleExchangeCreated + - event: ExchangeRateChanged(indexed bytes32,indexed address,uint256) + handler: handleRateChange + - event: ExchangeMintStateChanged(indexed bytes32,indexed address,bool) + handler: handleMintStateChanged + - event: ExchangeActivated(indexed bytes32,indexed address) + handler: handleActivated + - event: ExchangeDeactivated(indexed bytes32,indexed address) + handler: handleDeactivated + - event: ExchangeAllowedSwapperChanged(indexed bytes32,indexed address) + handler: handleAllowedSwapperChanged + - event: Swapped(indexed bytes32,indexed address,uint256,uint256,address,uint256,uint256,uint256) + handler: handleSwap + - event: PublishMarketFeeChanged(indexed bytes32,address,address,uint256) + handler: handlePublishMarketFeeChanged \ No newline at end of file