Skip to content

Commit

Permalink
schema: implement smart vault handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
facuspagnuolo committed Aug 15, 2023
1 parent ee1164b commit f4e5e89
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 2 deletions.
35 changes: 35 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -73,6 +77,37 @@ type Task @entity {
smartVault: SmartVault!
}

type Movement @entity {
id: ID!
hash: String!
sender: String!
executedAt: BigInt!
token: ERC20!
amount: BigInt!
added: Boolean!
connector: String!
smartVault: SmartVault!
}

type Transaction @entity {
id: ID!
hash: String!
sender: String!
executedAt: BigInt!
type: TransactionType!
fee: BigInt!
smartVault: SmartVault!
}

enum TransactionType {
Wrap
Unwrap
Collect
Withdraw
Call
Execute
}

type Implementation @entity {
id: ID!
name: String!
Expand Down
10 changes: 9 additions & 1 deletion src/Deployer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Address, Bytes, crypto, log } from '@graphprotocol/graph-ts'

import { Authorizer as AuthorizerTemplate, PriceOracle as PriceOracleTemplate } from '../types/templates'
import {
Authorizer as AuthorizerTemplate,
PriceOracle as PriceOracleTemplate,
SmartVault as SmartVaultTemplate,
} from '../types/templates'

import { Authorizer, Environment, PriceOracle, SmartVault, Task } from '../types/schema'
import { AuthorizerDeployed, PriceOracleDeployed, SmartVaultDeployed, TaskDeployed } from '../types/Deployer/Deployer'

Expand Down Expand Up @@ -51,7 +56,10 @@ 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()

SmartVaultTemplate.create(event.params.instance)
}

export function handleTaskDeployed(event: TaskDeployed): void {
Expand Down
91 changes: 90 additions & 1 deletion src/SmartVault.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,95 @@
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.sender = event.transaction.from.toHexString()
transaction.executedAt = event.block.timestamp
transaction.smartVault = event.address.toHexString()
transaction.type = type
transaction.fee = fee
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.sender = event.transaction.from.toHexString()
movement.executedAt = event.block.timestamp
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)
Expand Down
21 changes: 21 additions & 0 deletions subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,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
Expand Down

0 comments on commit f4e5e89

Please sign in to comment.