From af73724a652723df5c318b8ffa6c97320cc0bd1c Mon Sep 17 00:00:00 2001 From: Juan Manuel Rodriguez Defago Date: Fri, 22 May 2020 17:24:12 -0300 Subject: [PATCH] Added connector contract data, and instaConnectors chief data --- abis/Connector.json | 33 ++++++++++++ abis/InstaEvent.json | 80 ++++++++++++++++++++++++++++ schema.graphql | 19 +++++++ src/index.ts | 4 +- src/mappings/instaConnectors.ts | 69 +++++++++++++++++++++--- src/utils/helpers/index.ts | 3 +- src/utils/helpers/instaConnectors.ts | 15 +++++- subgraph.yaml | 6 +++ 8 files changed, 220 insertions(+), 9 deletions(-) create mode 100644 abis/Connector.json create mode 100644 abis/InstaEvent.json diff --git a/abis/Connector.json b/abis/Connector.json new file mode 100644 index 0000000..b081865 --- /dev/null +++ b/abis/Connector.json @@ -0,0 +1,33 @@ +[ + { + "inputs": [], + "name": "connectorID", + "outputs": [ + { + "internalType": "uint256", + "name": "_type", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_id", + "type": "uint256" + } + ], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [], + "name": "name", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "view", + "type": "function" + } +] diff --git a/abis/InstaEvent.json b/abis/InstaEvent.json new file mode 100644 index 0000000..56f7e58 --- /dev/null +++ b/abis/InstaEvent.json @@ -0,0 +1,80 @@ +[ + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint64", + "name": "connectorType", + "type": "uint64" + }, + { + "indexed": true, + "internalType": "uint64", + "name": "connectorID", + "type": "uint64" + }, + { + "indexed": true, + "internalType": "uint64", + "name": "accountID", + "type": "uint64" + }, + { + "indexed": true, + "internalType": "bytes32", + "name": "eventCode", + "type": "bytes32" + }, + { + "indexed": false, + "internalType": "bytes", + "name": "eventData", + "type": "bytes" + } + ], + "name": "LogEvent", + "type": "event" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_connectorType", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_connectorID", + "type": "uint256" + }, + { + "internalType": "bytes32", + "name": "_eventCode", + "type": "bytes32" + }, + { + "internalType": "bytes", + "name": "_eventData", + "type": "bytes" + } + ], + "name": "emitEvent", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "instaList", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + } +] diff --git a/schema.graphql b/schema.graphql index 6279dab..0288686 100644 --- a/schema.graphql +++ b/schema.graphql @@ -45,10 +45,13 @@ type InstaConnector @entity { "Address of the InstaConnector contract. This contract has a list of connectors tracked." id: ID! + chiefs: [Chief!]! @derivedFrom(field: "instaConnector") + connectors: [Connector!]! @derivedFrom(field: "instaConnector") } type Connector @entity { + "Connector ID includes the ID of the InstaConnector it belongs to and the internal ID of the connector" id: ID! isStatic: Boolean! @@ -56,4 +59,20 @@ type Connector @entity { isEnabled: Boolean! instaConnector: InstaConnector! + + name: String! + + connectorID: BigInt! + + connectorType: BigInt! + + address: String! +} + +type Chief @entity { + id: ID! + + isActive: Boolean! + + instaConnector: InstaConnector! } diff --git a/src/index.ts b/src/index.ts index 6a60e42..d5c35f8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,5 +11,7 @@ export { export { handleLogEnableConnector, handleLogDisableConnector, - handleLogEnableStaticConnector + handleLogEnableStaticConnector, + handleLogAddController, + handleLogRemoveController } from "./mappings/instaConnectors"; diff --git a/src/mappings/instaConnectors.ts b/src/mappings/instaConnectors.ts index 9def748..4bcdfd9 100644 --- a/src/mappings/instaConnectors.ts +++ b/src/mappings/instaConnectors.ts @@ -1,18 +1,31 @@ import { LogDisable, LogEnable, - LogEnableStatic + LogEnableStatic, + LogAddController, + LogRemoveController } from "../../generated/templates/InstaConnectors/InstaConnectors"; -import { getOrCreateConnector } from "../utils/helpers"; +import { Connector as ConnectorContract } from "../../generated/templates/InstaConnectors/Connector"; +import { getOrCreateConnector, getOrCreateChief } from "../utils/helpers"; // - event: LogDisable(indexed address) // handler: handleLogDisableConnector export function handleLogDisableConnector(event: LogDisable): void { - let connector = getOrCreateConnector(event.params.connector.toHexString()) + let contract = ConnectorContract.bind(event.params.connector); + let connectorIDResult = contract.connectorID(); + let instaConnectorAddress = event.address.toHexString(); + let entityId = instaConnectorAddress + .concat("-") + .concat(connectorIDResult.value1.toString()); + let connector = getOrCreateConnector(entityId); connector.isEnabled = false; connector.instaConnector = event.address.toHexString(); + connector.name = contract.name(); + connector.address = event.params.connector.toHexString(); + connector.connectorType = connectorIDResult.value0; + connector.connectorID = connectorIDResult.value1; connector.save(); } @@ -21,11 +34,21 @@ export function handleLogDisableConnector(event: LogDisable): void { // handler: handleLogEnableConnector export function handleLogEnableConnector(event: LogEnable): void { - let connector = getOrCreateConnector(event.params.connector.toHexString()) + let contract = ConnectorContract.bind(event.params.connector); + let connectorIDResult = contract.connectorID(); + let instaConnectorAddress = event.address.toHexString(); + let entityId = instaConnectorAddress + .concat("-") + .concat(connectorIDResult.value1.toString()); + let connector = getOrCreateConnector(entityId); connector.isEnabled = true; connector.isStatic = false; connector.instaConnector = event.address.toHexString(); + connector.name = contract.name(); + connector.address = event.params.connector.toHexString(); + connector.connectorType = connectorIDResult.value0; + connector.connectorID = connectorIDResult.value1; connector.save(); } @@ -34,11 +57,45 @@ export function handleLogEnableConnector(event: LogEnable): void { // handler: handleLogEnableStaticConnector export function handleLogEnableStaticConnector(event: LogEnableStatic): void { - let connector = getOrCreateConnector(event.params.connector.toHexString()) + let contract = ConnectorContract.bind(event.params.connector); + let connectorIDResult = contract.connectorID(); + let instaConnectorAddress = event.address.toHexString(); + let entityId = instaConnectorAddress + .concat("-") + .concat(connectorIDResult.value1.toString()); + let connector = getOrCreateConnector(entityId); connector.isEnabled = true; connector.isStatic = true; - connector.instaConnector = event.address.toHexString(); + connector.instaConnector = instaConnectorAddress; + connector.name = contract.name(); + connector.address = event.params.connector.toHexString(); + connector.connectorType = connectorIDResult.value0; + connector.connectorID = connectorIDResult.value1; connector.save(); } + +// - event: LogAddController(indexed address) +// handler: handleLogAddController + +export function handleLogAddController(event: LogAddController): void { + let chief = getOrCreateChief(event.params.addr.toHexString()) + + chief.isActive = true; + chief.instaConnector = event.address.toHexString(); + + chief.save() +} + +// - event: LogRemoveController(indexed address) +// handler: handleLogRemoveController + +export function handleLogRemoveController(event: LogRemoveController): void { + let chief = getOrCreateChief(event.params.addr.toHexString()) + + chief.isActive = false; + chief.instaConnector = event.address.toHexString(); + + chief.save() +} diff --git a/src/utils/helpers/index.ts b/src/utils/helpers/index.ts index dc6505a..3a1ed99 100644 --- a/src/utils/helpers/index.ts +++ b/src/utils/helpers/index.ts @@ -7,5 +7,6 @@ export { export { getOrCreateConnector, - getOrCreateInstaConnector + getOrCreateInstaConnector, + getOrCreateChief } from "./instaConnectors"; diff --git a/src/utils/helpers/instaConnectors.ts b/src/utils/helpers/instaConnectors.ts index a3ada51..75fc42c 100644 --- a/src/utils/helpers/instaConnectors.ts +++ b/src/utils/helpers/instaConnectors.ts @@ -1,5 +1,5 @@ import { InstaConnectors as ConnectorsTemplate } from "../../../generated/templates"; -import { InstaConnector, Connector } from "../../../generated/schema"; +import { InstaConnector, Connector, Chief } from "../../../generated/schema"; import { Address } from "@graphprotocol/graph-ts"; export function getOrCreateInstaConnector( @@ -30,3 +30,16 @@ export function getOrCreateConnector( return connector as Connector; } + +export function getOrCreateChief( + id: String, + createIfNotFound: boolean = true +): Chief { + let chief = Chief.load(id); + + if (chief == null && createIfNotFound) { + chief = new Chief(id); + } + + return chief as Chief; +} diff --git a/subgraph.yaml b/subgraph.yaml index 3961994..3a265ff 100644 --- a/subgraph.yaml +++ b/subgraph.yaml @@ -55,6 +55,8 @@ templates: abis: - name: InstaConnectors file: ./abis/InstaConnectors.json + - name: Connector + file: ./abis/Connector.json eventHandlers: - event: LogDisable(indexed address) handler: handleLogDisableConnector @@ -62,6 +64,10 @@ templates: handler: handleLogEnableConnector - event: LogEnableStatic(indexed address) handler: handleLogEnableStaticConnector + - event: LogAddController(indexed address) + handler: handleLogAddController + - event: LogRemoveController(indexed address) + handler: handleLogRemoveController # - LogAddController(indexed address) # - LogDisable(indexed address) # - LogEnable(indexed address)