From 1504d0a005e89769a3ef2a89adc2f0cb73b9d9c9 Mon Sep 17 00:00:00 2001 From: Facu Spagnuolo Date: Mon, 14 Aug 2023 18:43:48 +0200 Subject: [PATCH] schema: implement smart vault handlers --- schema.graphql | 36 +++++++++++++++++ src/Deployer.ts | 1 + src/SmartVault.ts | 92 +++++++++++++++++++++++++++++++++++++++++- subgraph.template.yaml | 21 ++++++++++ 4 files changed, 149 insertions(+), 1 deletion(-) diff --git a/schema.graphql b/schema.graphql index 831cde0..3eeb6c0 100644 --- a/schema.graphql +++ b/schema.graphql @@ -16,6 +16,10 @@ type SmartVault @entity { environment: Environment! authorizer: Authorizer! priceOracle: PriceOracle! + paused: Boolean! + tasks: [Task!] @derivedFrom(field: "smartVault") + movements: [Movement!] @derivedFrom(field: "smartVault") + transactions: [Transaction!] @derivedFrom(field: "smartVault") } type Authorizer @entity { @@ -73,6 +77,38 @@ type Task @entity { smartVault: SmartVault! } +type Movement @entity { + id: ID! + hash: String! + index: BigInt! + sender: String! + smartVault: SmartVault! + connector: String! + token: ERC20! + amount: BigInt! + added: Boolean! +} + +type Transaction @entity { + id: ID! + hash: String! + index: BigInt! + sender: String! + smartVault: SmartVault! + type: TransactionType! + fee: BigInt! + executedAt: BigInt! +} + +enum TransactionType { + Wrap + Unwrap + Collect + Withdraw + Call + Execute +} + type Implementation @entity { id: ID! name: String! diff --git a/src/Deployer.ts b/src/Deployer.ts index 16ac441..062b3a1 100644 --- a/src/Deployer.ts +++ b/src/Deployer.ts @@ -51,6 +51,7 @@ export function handleSmartVaultDeployed(event: SmartVaultDeployed): void { smartVault.registry = getRegistry(event.params.instance).toHexString() smartVault.authorizer = getAuthorizer(event.params.instance).toHexString() smartVault.priceOracle = getPriceOracle(event.params.instance).toHexString() + smartVault.paused = false smartVault.save() } diff --git a/src/SmartVault.ts b/src/SmartVault.ts index ca0edc2..6e4fd8d 100644 --- a/src/SmartVault.ts +++ b/src/SmartVault.ts @@ -1,6 +1,96 @@ -import { Address, log } from '@graphprotocol/graph-ts' +import { Address, BigInt, ethereum, log } from '@graphprotocol/graph-ts' +import { Movement, Transaction, SmartVault } from '../types/schema' import { SmartVault as SmartVaultContract } from '../types/templates/SmartVault/SmartVault' +import { + BalanceConnectorUpdated, + Called, + Collected, + Executed, + Paused, + PriceOracleSet, + Unpaused, + Unwrapped, + Withdrawn, + Wrapped, +} from '../types/templates/SmartVault/SmartVault' + +import { loadOrCreateERC20 } from './ERC20' + +export function handleExecuted(event: Executed): void { + createTransaction(event, 'Execute', BigInt.zero()) +} + +export function handleCall(event: Called): void { + createTransaction(event, 'Call', BigInt.zero()) +} + +export function handleCollect(event: Collected): void { + createTransaction(event, 'Collect', BigInt.zero()) +} + +export function handleWithdraw(event: Withdrawn): void { + createTransaction(event, 'Withdraw', event.params.fee) +} + +export function handleWrap(event: Wrapped): void { + createTransaction(event, 'Wrap', BigInt.zero()) +} + +export function handleUnwrap(event: Unwrapped): void { + createTransaction(event, 'Unwrap', BigInt.zero()) +} + +function createTransaction(event: ethereum.Event, type: string, fee: BigInt): void { + let transactionId = event.transaction.hash.toHexString() + '#' + event.transactionLogIndex.toString() + let transaction = new Transaction(transactionId) + transaction.hash = event.transaction.hash.toHexString() + transaction.index = event.transactionLogIndex + transaction.sender = event.transaction.from.toHexString() + transaction.smartVault = event.address.toHexString() + transaction.type = type + transaction.fee = fee + transaction.executedAt = event.block.timestamp + transaction.save() +} + +export function handleBalanceConnectorUpdated(event: BalanceConnectorUpdated): void { + let movementId = event.transaction.hash.toHexString() + '#' + event.transactionLogIndex.toString() + let movement = new Movement(movementId) + movement.hash = event.transaction.hash.toHexString() + movement.index = event.transactionLogIndex + movement.sender = event.transaction.from.toHexString() + movement.smartVault = event.address.toHexString() + movement.connector = event.params.id.toHexString() + movement.token = loadOrCreateERC20(event.params.token).id + movement.amount = event.params.amount + movement.added = event.params.added + movement.save() +} + +export function handlePaused(event: Paused): void { + let smartVault = SmartVault.load(event.address.toHexString()) + if (smartVault == null) return log.warning('Missing smart vault entity {}', [event.address.toHexString()]) + + smartVault.paused = true + smartVault.save() +} + +export function handleUnpaused(event: Unpaused): void { + let smartVault = SmartVault.load(event.address.toHexString()) + if (smartVault == null) return log.warning('Missing smart vault entity {}', [event.address.toHexString()]) + + smartVault.paused = false + smartVault.save() +} + +export function handlePriceOracleSet(event: PriceOracleSet): void { + let smartVault = SmartVault.load(event.address.toHexString()) + if (smartVault == null) return log.warning('Missing smart vault entity {}', [event.address.toHexString()]) + + smartVault.priceOracle = event.params.priceOracle.toHexString() + smartVault.save() +} export function getRegistry(address: Address): Address { let smartVaultContract = SmartVaultContract.bind(address) diff --git a/subgraph.template.yaml b/subgraph.template.yaml index 842c49d..75f9e28 100644 --- a/subgraph.template.yaml +++ b/subgraph.template.yaml @@ -111,6 +111,27 @@ templates: abis: - name: SmartVault file: ./node_modules/@mimic-fi/v3-smart-vault/artifacts/contracts/interfaces/ISmartVault.sol/ISmartVault.json + eventHandlers: + - event: Paused() + handler: handlePause + - event: Unpaused() + handler: handleUnpaused + - event: PriceOracleSet(indexed address) + handler: handlePriceOracleSet + - event: BalanceConnectorUpdated(indexed bytes32,indexed address,uint256,bool) + handler: handleBalanceConnectorUpdated + - event: Executed(indexed address,bytes,bytes) + handler: handleExecuted + - event: Called(indexed address,bytes,uint256,bytes) + handler: handleCalled + - event: Wrapped(uint256) + handler: handleWrapped + - event: Unwrapped(uint256) + handler: handleUnwrapped + - event: Collected(indexed address,indexed address,uint256) + handler: handleCollected + - event: Withdrawn(indexed address,indexed address,uint256,uint256) + handler: handleWithdrawn file: ./src/SmartVault.ts - kind: ethereum/contract name: Task