From 6498fb0512003909e9354e11b20284f9e31ec938 Mon Sep 17 00:00:00 2001 From: Juan Manuel Rodriguez Defago Date: Wed, 3 Jun 2020 18:05:32 -0300 Subject: [PATCH] Changed ID of smart accounts from address to accountID --- README.md | 8 +++- schema.graphql | 6 ++- src/mappings/instaAccount.ts | 75 ++++++++++++++++++++++----------- src/mappings/instaConnectors.ts | 8 +--- src/mappings/instaIndex.ts | 19 ++++++--- src/utils/helpers/instaIndex.ts | 7 ++- subgraph.yaml | 4 ++ 7 files changed, 87 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index d320ac3..8819fb2 100644 --- a/README.md +++ b/README.md @@ -8,11 +8,11 @@ Below is a general description of the entities we store. We tried to make it as #### SmartAccount -The SmartAccount entity holds the owner and creator address for that SmartAccount, as well as the AccountModule used as a template for the account (which also holds the connectors that the account has, since it was cloned from that module), the shield status, whether the current owner is enabled for authentication or not, and all the events both withing the account scope (modifications to the shield status, disabling and enabling of the owner, and casts), and events within the scope of connectors that the account interacted using casts. +The SmartAccount entity holds the address and ID of the SmartAccount, the owner and creator address for that SmartAccount, as well as the AccountModule used as a template for the account (which also holds the connectors that the account has, since it was cloned from that module), the shield status, whether the current owner is enabled for authentication or not, and all the events both withing the account scope (modifications to the shield status, disabling and enabling of the owner, and casts), and events within the scope of connectors that the account interacted using casts. ```graphql type SmartAccount @entity { - "The ID used for the SmartAccount entity is the address of said smart account" + "The ID used for the SmartAccount entity is the accountID for that SmartAccount on the InstaList contract" id: ID! "Latest enabled owner" @@ -23,8 +23,12 @@ type SmartAccount @entity { origin: Bytes! + "AccountID as BigInt according to the InstaList contract" accountID: BigInt! + "Address of the SmartAccount" + address: Bytes! + "Account module from which this account was created. It holds the connectors linked to this account." accountModule: AccountModule diff --git a/schema.graphql b/schema.graphql index a2de227..9aa7085 100644 --- a/schema.graphql +++ b/schema.graphql @@ -9,7 +9,7 @@ type User @entity { } type SmartAccount @entity { - "The ID used for the SmartAccount entity is the address of said smart account" + "The ID used for the SmartAccount entity is the accountID for that SmartAccount on the InstaList contract" id: ID! "Latest enabled owner" @@ -20,8 +20,12 @@ type SmartAccount @entity { origin: Bytes! + "AccountID as BigInt according to the InstaList contract" accountID: BigInt! + "Address of the SmartAccount" + address: Bytes! + "Account module from which this account was created. It holds the connectors linked to this account." accountModule: AccountModule diff --git a/src/mappings/instaAccount.ts b/src/mappings/instaAccount.ts index a7da073..b75e16f 100644 --- a/src/mappings/instaAccount.ts +++ b/src/mappings/instaAccount.ts @@ -5,6 +5,7 @@ import { LogSwitchShield, CastCall } from "../../generated/templates/InstaAccount/InstaAccount"; +import { InstaList } from "../../generated/InstaIndex/InstaList"; import { getOrCreateSmartAccount, getOrCreateCast, @@ -12,32 +13,39 @@ import { getOrCreateDisableEvent, getOrCreateEnableEvent, getOrCreateSwitchShieldEvent, - getOrCreateUser + getOrCreateUser, + getOrCreateInstaIndex } from "../utils/helpers"; -import { log, Bytes } from '@graphprotocol/graph-ts'; +import { log, Bytes, Address } from "@graphprotocol/graph-ts"; //- event: LogCast(indexed address,indexed address,uint256) // handler: handleLogCast export function handleLogCast(event: LogCast): void { - let account = getOrCreateSmartAccount(event.address.toHexString(), false); + let instaIndex = getOrCreateInstaIndex(); + let instaListContract = InstaList.bind(instaIndex.instaListAddress as Address); + let accountID = instaListContract.accountID(event.address); + let account = getOrCreateSmartAccount(accountID.toString(), false); if (account == null) { log.error("LOGCAST - Indexed address for smart account is wrong? {}", [ - event.address.toHexString() + accountID.toString() ]); } else { - let eventId = event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString()); - let castEvent = getOrCreateCastEvent(eventId) + let eventId = event.transaction.hash + .toHexString() + .concat("-") + .concat(event.logIndex.toString()); + let castEvent = getOrCreateCastEvent(eventId); let cast = getOrCreateCast(eventId); castEvent.account = account.id; castEvent.origin = event.params.origin; - castEvent.sender = event.params.sender + castEvent.sender = event.params.sender; castEvent.value = event.params.value; cast.account = account.id; cast.origin = event.params.origin; - cast.sender = event.params.sender + cast.sender = event.params.sender; cast.value = event.params.value; castEvent.save(); @@ -49,22 +57,28 @@ export function handleLogCast(event: LogCast): void { // handler: handleLogDisableSmartAccountOwner export function handleLogDisableSmartAccountOwner(event: LogDisable): void { - let account = getOrCreateSmartAccount(event.address.toHexString(), false); + let instaIndex = getOrCreateInstaIndex(); + let instaListContract = InstaList.bind(instaIndex.instaListAddress as Address); + let accountID = instaListContract.accountID(event.address); + let account = getOrCreateSmartAccount(accountID.toString(), false); if (account == null) { log.error("DISABLE - Indexed address for smart account is wrong? {}", [ - event.address.toHexString() + accountID.toString() ]); } else { - let eventId = event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString()); + let eventId = event.transaction.hash + .toHexString() + .concat("-") + .concat(event.logIndex.toString()); let user = getOrCreateUser(event.params.user.toHexString()); - let disableEvent = getOrCreateDisableEvent(eventId) + let disableEvent = getOrCreateDisableEvent(eventId); disableEvent.account = account.id; disableEvent.user = user.id; account.owner = user.id; account.isEnabled = false; - account.save() + account.save(); disableEvent.save(); } } @@ -73,22 +87,28 @@ export function handleLogDisableSmartAccountOwner(event: LogDisable): void { // handler: handleLogEnableSmartAccountOwner export function handleLogEnableSmartAccountOwner(event: LogEnable): void { - let account = getOrCreateSmartAccount(event.address.toHexString(), false); + let instaIndex = getOrCreateInstaIndex(); + let instaListContract = InstaList.bind(instaIndex.instaListAddress as Address); + let accountID = instaListContract.accountID(event.address); + let account = getOrCreateSmartAccount(accountID.toString(), false); if (account == null) { log.error("ENABLE - Indexed address for smart account is wrong? {}", [ - event.address.toHexString() + accountID.toString() ]); } else { - let eventId = event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString()); + let eventId = event.transaction.hash + .toHexString() + .concat("-") + .concat(event.logIndex.toString()); let user = getOrCreateUser(event.params.user.toHexString()); - let enableEvent = getOrCreateEnableEvent(eventId) + let enableEvent = getOrCreateEnableEvent(eventId); enableEvent.account = account.id; enableEvent.user = user.id; account.owner = user.id; account.isEnabled = true; - account.save() + account.save(); enableEvent.save(); } } @@ -97,14 +117,21 @@ export function handleLogEnableSmartAccountOwner(event: LogEnable): void { // handler: handleLogSwitchShield export function handleLogSwitchShield(event: LogSwitchShield): void { - let account = getOrCreateSmartAccount(event.address.toHexString(), false); + let instaIndex = getOrCreateInstaIndex(); + let instaListContract = InstaList.bind(instaIndex.instaListAddress as Address); + let accountID = instaListContract.accountID(event.address); + let account = getOrCreateSmartAccount(accountID.toString(), false); if (account == null) { - log.error("SWITCH SHIELD - Indexed address for smart account is wrong? {}", [ - event.address.toHexString() - ]); + log.error( + "SWITCH SHIELD - Indexed address for smart account is wrong? {}", + [accountID.toString()] + ); } else { - let eventId = event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString()); - let switchEvent = getOrCreateSwitchShieldEvent(eventId) + let eventId = event.transaction.hash + .toHexString() + .concat("-") + .concat(event.logIndex.toString()); + let switchEvent = getOrCreateSwitchShieldEvent(eventId); switchEvent.account = account.id; switchEvent.shield = event.params._shield; diff --git a/src/mappings/instaConnectors.ts b/src/mappings/instaConnectors.ts index 8a355ac..368ee73 100644 --- a/src/mappings/instaConnectors.ts +++ b/src/mappings/instaConnectors.ts @@ -6,8 +6,7 @@ import { LogRemoveController } from "../../generated/templates/InstaConnectors/InstaConnectors"; import { LogEvent } from "../../generated/InstaEvents/InstaEvents"; -import { InstaList } from "../../generated/InstaEvents/InstaList"; -import { log, Address } from "@graphprotocol/graph-ts"; +import { log } from "@graphprotocol/graph-ts"; import { Connector as ConnectorContract } from "../../generated/templates/InstaConnectors/Connector"; import { getOrCreateConnector, @@ -124,15 +123,12 @@ export function handleLogEvent(event: LogEvent): void { if (connector == null) { log.error("Connector '{}' doesn't exist.", [entityId]); } else { - let index = getOrCreateInstaIndex(); - let instaListContract = InstaList.bind(index.instaListAddress as Address); - let accountAddress = instaListContract.accountAddr(event.params.accountID); let eventId = event.transaction.hash .toHexString() .concat("-") .concat(event.logIndex.toString()); let connectorEvent = getOrCreateConnectorEvent(eventId); - connectorEvent.account = accountAddress.toHexString(); + connectorEvent.account = event.params.accountID.toString(); connectorEvent.connector = connector.id; connectorEvent.eventCode = event.params.eventCode; connectorEvent.eventData = event.params.eventData; diff --git a/src/mappings/instaIndex.ts b/src/mappings/instaIndex.ts index c1f1653..0aaf43a 100644 --- a/src/mappings/instaIndex.ts +++ b/src/mappings/instaIndex.ts @@ -24,19 +24,23 @@ import { // event LogAccountCreated(address sender, address indexed owner, address indexed account, address indexed origin); export function handleLogAccountCreated(event: LogAccountCreated): void { - let smartAccount = getOrCreateSmartAccount( - event.params.account.toHexString() - ); let owner = getOrCreateUser(event.params.owner.toHexString()); let sender = getOrCreateUser(event.params.sender.toHexString()); let index = getOrCreateInstaIndex(); let instaListContract = InstaList.bind(index.instaListAddress as Address); + let dsaID = instaListContract.accountID(event.params.account); + let smartAccount = getOrCreateSmartAccount( + dsaID.toString(), + true, + event.params.account as Address + ); smartAccount.owner = owner.id; smartAccount.creator = sender.id; smartAccount.origin = event.params.origin; smartAccount.isEnabled = true; - smartAccount.accountID = instaListContract.accountID(event.params.account); + smartAccount.accountID = dsaID; + smartAccount.address = event.params.account; smartAccount.save(); } @@ -110,8 +114,13 @@ export function handleSetBasics(call: SetBasicsCall): void { } export function handleBuild(call: BuildCall): void { + let index = getOrCreateInstaIndex(); + let instaListContract = InstaList.bind(index.instaListAddress as Address); + let dsaID = instaListContract.accountID(call.outputs._account); let smartAccount = getOrCreateSmartAccount( - call.outputs._account.toHexString() + dsaID.toString(), + true, + call.outputs._account as Address ); let owner = getOrCreateUser(call.inputs._owner.toHexString()); diff --git a/src/utils/helpers/instaIndex.ts b/src/utils/helpers/instaIndex.ts index a0fd868..4b20ddf 100644 --- a/src/utils/helpers/instaIndex.ts +++ b/src/utils/helpers/instaIndex.ts @@ -27,7 +27,8 @@ export function getOrCreateUser( export function getOrCreateSmartAccount( id: String, - createIfNotFound: boolean = true + createIfNotFound: boolean = true, + address: Address | null = null ): SmartAccount { let smartAccount = SmartAccount.load(id); @@ -35,7 +36,9 @@ export function getOrCreateSmartAccount( smartAccount = new SmartAccount(id); smartAccount.shield = false; - AccountTemplate.create(Address.fromString(id)) + if(address != null) { + AccountTemplate.create(address as Address) + } } return smartAccount as SmartAccount; diff --git a/subgraph.yaml b/subgraph.yaml index 340f6e6..92964ea 100644 --- a/subgraph.yaml +++ b/subgraph.yaml @@ -83,6 +83,8 @@ templates: file: ./abis/InstaConnectors.json - name: Connector file: ./abis/Connector.json + - name: InstaList + file: ./abis/InstaList.json eventHandlers: - event: LogDisable(indexed address) handler: handleLogDisableConnector @@ -109,6 +111,8 @@ templates: abis: - name: InstaAccount file: ./abis/InstaAccount.json + - name: InstaList + file: ./abis/InstaList.json eventHandlers: - event: LogCast(indexed address,indexed address,uint256) handler: handleLogCast