Feature/templates (#349)

* templates
This commit is contained in:
Alex Coseru 2022-02-21 12:45:24 +02:00 committed by GitHub
parent 992323baaa
commit f5c7eeae00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 234 additions and 88 deletions

View File

@ -365,7 +365,8 @@ type FixedRateExchangeSwap @entity {
type Dispenser @entity { type Dispenser @entity {
"token address" "token address"
id: ID! id: ID!
contract: String!
active: Boolean! active: Boolean!
"if using the enterprise template the owner will always be the erc721 factory, for normal template it will a user" "if using the enterprise template the owner will always be the erc721 factory, for normal template it will a user"
owner: String owner: String
@ -486,6 +487,7 @@ type OPC @entity {
consumeFee: BigDecimal consumeFee: BigDecimal
"fee in percent taken by OPC from providerFees" "fee in percent taken by OPC from providerFees"
providerFee: BigDecimal providerFee: BigDecimal
approvedTokens: [String!]
} }
enum NftUpdateType { enum NftUpdateType {
@ -516,3 +518,11 @@ type NftUpdate @entity {
timestamp: Int! timestamp: Int!
tx: String! tx: String!
} }
type Template @entity{
id: ID!
fixedRateTemplates: [String!]
dispenserTemplates: [String!]
ssTemplates: [String!]
}

View File

@ -30,14 +30,6 @@ async function replaceContractAddresses() {
/__ERC721FACTORYADDRESS__/g, /__ERC721FACTORYADDRESS__/g,
"'" + addresses[network].ERC721Factory + "'" "'" + addresses[network].ERC721Factory + "'"
) )
subgraph = subgraph.replace(
/__FIXEDRATEEXCHANGEADDRESS__/g,
"'" + addresses[network].FixedPrice + "'"
)
subgraph = subgraph.replace(
/__DISPENSERADDRESS__/g,
"'" + addresses[network].Dispenser + "'"
)
subgraph = subgraph.replace( subgraph = subgraph.replace(
/__FACTORYROUTERADDRESS__/g, /__FACTORYROUTERADDRESS__/g,
"'" + addresses[network].Router + "'" "'" + addresses[network].Router + "'"

View File

@ -4,7 +4,7 @@ import {
DispenserDeactivated, DispenserDeactivated,
OwnerWithdrawed, OwnerWithdrawed,
TokensDispensed TokensDispensed
} from '../@types/Dispenser/Dispenser' } from '../@types/templates/Dispenser/Dispenser'
import { DispenserCreated } from '../@types/ERC721Factory/ERC721Factory' import { DispenserCreated } from '../@types/ERC721Factory/ERC721Factory'
import { Dispenser, DispenserTransaction } from '../@types/schema' import { Dispenser, DispenserTransaction } from '../@types/schema'
import { decimal } from './utils/constants' import { decimal } from './utils/constants'
@ -26,6 +26,7 @@ export function handleNewDispenser(event: DispenserCreated): void {
const dispenser = new Dispenser(dispenserID) const dispenser = new Dispenser(dispenserID)
const token = getToken(event.params.datatokenAddress, false) const token = getToken(event.params.datatokenAddress, false)
dispenser.token = token.id dispenser.token = token.id
dispenser.contract = event.address.toHexString()
dispenser.owner = event.params.owner.toHexString() dispenser.owner = event.params.owner.toHexString()
dispenser.maxBalance = weiToDecimal( dispenser.maxBalance = weiToDecimal(

View File

@ -1,13 +1,20 @@
import { import {
NewPool, NewPool,
TokenAdded, TokenAdded,
TokenRemoved,
OPCFeeChanged, OPCFeeChanged,
FactoryRouter FactoryRouter,
SSContractAdded,
SSContractRemoved,
FixedRateContractAdded,
FixedRateContractRemoved,
DispenserContractAdded,
DispenserContractRemoved
} from '../@types/FactoryRouter/FactoryRouter' } from '../@types/FactoryRouter/FactoryRouter'
import { BigInt } from '@graphprotocol/graph-ts' import { BigInt } from '@graphprotocol/graph-ts'
import { Pool } from '../@types/schema' import { Pool } from '../@types/schema'
import { BPool } from '../@types/templates' import { BPool, FixedRateExchange, Dispenser } from '../@types/templates'
import { addPool, getOPC } from './utils/globalUtils' import { addPool, getOPC, getTemplates } from './utils/globalUtils'
import { weiToDecimal } from './utils/generic' import { weiToDecimal } from './utils/generic'
export function handleNewPool(event: NewPool): void { export function handleNewPool(event: NewPool): void {
@ -62,5 +69,119 @@ export function handleTokenAdded(event: TokenAdded): void {
if (newProviderFee.reverted) return if (newProviderFee.reverted) return
opc.consumeFee = weiToDecimal(newConsumeFee.value.toBigDecimal(), decimals) opc.consumeFee = weiToDecimal(newConsumeFee.value.toBigDecimal(), decimals)
opc.providerFee = weiToDecimal(newProviderFee.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() 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()
}

View File

@ -8,7 +8,7 @@ import {
ExchangeRateChanged, ExchangeRateChanged,
Swapped, Swapped,
PublishMarketFeeChanged PublishMarketFeeChanged
} from '../@types/FixedRateExchange/FixedRateExchange' } from '../@types/templates/FixedRateExchange/FixedRateExchange'
import { import {
FixedRateExchange, FixedRateExchange,
FixedRateExchangeSwap, FixedRateExchangeSwap,

View File

@ -2,7 +2,7 @@ import { Dispenser } from '../../@types/schema'
import { getToken } from './tokenUtils' import { getToken } from './tokenUtils'
import { Address } from '@graphprotocol/graph-ts' import { Address } from '@graphprotocol/graph-ts'
import { weiToDecimal } from './generic' import { weiToDecimal } from './generic'
import { Dispenser as DispenserContract } from '../../@types/Dispenser/Dispenser' import { Dispenser as DispenserContract } from '../../@types/templates/Dispenser/Dispenser'
export function getDispenserGraphID( export function getDispenserGraphID(
contractAddress: Address, contractAddress: Address,

View File

@ -1,6 +1,6 @@
import { FixedRateExchange } from '../../@types/schema' 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 { Address, Bytes } from '@graphprotocol/graph-ts'
import { getToken } from './tokenUtils' import { getToken } from './tokenUtils'
import { weiToDecimal } from './generic' import { weiToDecimal } from './generic'

View File

@ -4,7 +4,8 @@ import {
GlobalTotalFixedSwapPair, GlobalTotalFixedSwapPair,
GlobalTotalLiquidityPair, GlobalTotalLiquidityPair,
GlobalTotalPoolSwapPair, GlobalTotalPoolSwapPair,
OPC OPC,
Template
} from '../../@types/schema' } from '../../@types/schema'
const GLOBAL_ID = '1' const GLOBAL_ID = '1'
@ -27,6 +28,15 @@ export function getOPC(): OPC {
return globalStats 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 { export function addOrder(): void {
const globalStats = getGlobalStats() const globalStats = getGlobalStats()
globalStats.orderCount = globalStats.orderCount + 1 globalStats.orderCount = globalStats.orderCount + 1

View File

@ -28,75 +28,7 @@ dataSources:
handler: handleNftCreated handler: handleNftCreated
- event: TokenCreated(indexed address,indexed address,string,string,uint256,address) - event: TokenCreated(indexed address,indexed address,string,string,uint256,address)
handler: handleNewToken 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 - kind: ethereum/contract
name: FactoryRouter name: FactoryRouter
network: __NETWORK__ network: __NETWORK__
@ -121,11 +53,25 @@ dataSources:
handler: handleNewPool handler: handleNewPool
- event: TokenAdded(indexed address,indexed address) - event: TokenAdded(indexed address,indexed address)
handler: handleTokenAdded handler: handleTokenAdded
- event: TokenRemoved(indexed address,indexed address)
handler: handleTokenRemoved
- event: OPCFeeChanged(indexed address,uint256,uint256,uint256,uint256) - event: OPCFeeChanged(indexed address,uint256,uint256,uint256,uint256)
handler: handleOPCFeeChanged 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: templates:
- name: ERC20Template - name: ERC20Template
kind: ethereum/contract kind: ethereum/contract
network: __NETWORK__ network: __NETWORK__
@ -203,7 +149,7 @@ templates:
handler: handlePublishMarketFeeChanged handler: handlePublishMarketFeeChanged
- event: SwapFeeChanged(address,uint256) - event: SwapFeeChanged(address,uint256)
handler: handleSwapFeeChanged handler: handleSwapFeeChanged
- name: ERC721Template - name: ERC721Template
kind: ethereum/contract kind: ethereum/contract
network: __NETWORK__ network: __NETWORK__
@ -250,3 +196,69 @@ templates:
handler: handleRemovedManager handler: handleRemovedManager
- event: CleanedPermissions(indexed address,uint256,uint256) - event: CleanedPermissions(indexed address,uint256,uint256)
handler: handleCleanedPermissions 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