Skip to content

Commit

Permalink
schema: implement relayer handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
facuspagnuolo committed Aug 15, 2023
1 parent 534fe9a commit cca1ef8
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@mimic-fi/v3-fee-controller": "0.1.0",
"@mimic-fi/v3-price-oracle": "0.1.0",
"@mimic-fi/v3-registry": "0.1.0",
"@mimic-fi/v3-relayer": "0.1.0",
"@mimic-fi/v3-relayer": "0.1.1",
"@mimic-fi/v3-smart-vault": "0.1.0",
"@mimic-fi/v3-tasks": "0.1.0"
},
Expand Down
19 changes: 19 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type Movement @entity {
added: Boolean!
connector: String!
smartVault: SmartVault!
relayedExecution: RelayedExecution
}

type Transaction @entity {
Expand All @@ -101,6 +102,7 @@ type Transaction @entity {
type: TransactionType!
fee: BigInt!
smartVault: SmartVault!
relayedExecution: RelayedExecution
}

enum TransactionType {
Expand All @@ -112,6 +114,23 @@ enum TransactionType {
Execute
}

type RelayedExecution @entity {
id: ID!
hash: String!
sender: String!
executedAt: BigInt!
smartVault: SmartVault!
task: Task!
index: BigInt!
succeeded: Boolean!
result: Bytes!
gasUsed: BigInt!
gasPrice: BigInt!
costNative: BigInt!
movements: [Movement!] @derivedFrom(field: "relayedExecution")
transactions: [Transaction!] @derivedFrom(field: "relayedExecution")
}

type Implementation @entity {
id: ID!
name: String!
Expand Down
23 changes: 22 additions & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,49 @@ set -o errexit
# Arbitrum
registry_arbitrum=0x1675BF3F75046aCd131caD845eb8FF3Bed49a643
deployer_arbitrum=0x849B7B1102B0dcf6eC10f98b81C8D1c38f7cbf24
relayer_arbitrum=0xD7252C026c3cA28D73B4DeeF62FE6ADe86eC17A9
block_arbitrum=117042327

# Avalanche
registry_avalanche=0x0000000000000000000000000000000000000000
deployer_avalanche=0x0000000000000000000000000000000000000000
relayer_avalanche=0x0000000000000000000000000000000000000000
block_avalanche=0

# BSC
registry_bsc=0x0000000000000000000000000000000000000000
deployer_bsc=0x0000000000000000000000000000000000000000
relayer_bsc=0x0000000000000000000000000000000000000000
block_bsc=0

# Fantom
registry_fantom=0x0000000000000000000000000000000000000000
deployer_fantom=0x0000000000000000000000000000000000000000
relayer_fantom=0x0000000000000000000000000000000000000000
block_fantom=0

# Gnosis
registry_gnosis=0x0000000000000000000000000000000000000000
deployer_gnosis=0x0000000000000000000000000000000000000000
relayer_gnosis=0x0000000000000000000000000000000000000000
block_gnosis=0

# Mainnet
registry_mainnet=0x0000000000000000000000000000000000000000
deployer_mainnet=0x0000000000000000000000000000000000000000
relayer_mainnet=0x0000000000000000000000000000000000000000
block_mainnet=0

# Optimism
registry_optimism=0x0000000000000000000000000000000000000000
deployer_optimism=0x0000000000000000000000000000000000000000
relayer_optimism=0x0000000000000000000000000000000000000000
block_optimism=0

# Polygon
registry_polygon=0x0000000000000000000000000000000000000000
deployer_polygon=0x0000000000000000000000000000000000000000
relayer_polygon=0x0000000000000000000000000000000000000000
block_polygon=0

# Validate network
Expand All @@ -50,7 +58,7 @@ if [[ -z $NETWORK || ! " ${networks[@]} " =~ " ${NETWORK} " ]]; then
exit 1
fi

# Use mainnet network in case of local deployment
# Update network name properly
if [[ "$NETWORK" = "localhost" ]]; then
ENV='mainnet'
elif [[ "$NETWORK" = "polygon" ]]; then
Expand Down Expand Up @@ -94,6 +102,18 @@ if [[ -z $DEPLOYER_ADDRESS ]]; then
exit 1
fi

# Load relayer address
if [[ -z $RELAYER_ADDRESS ]]; then
RELAYER_ADDRESS_VAR=relayer_$NETWORK
RELAYER_ADDRESS=${!RELAYER_ADDRESS_VAR}
fi

# Validate relayer address
if [[ -z $RELAYER_ADDRESS ]]; then
echo 'Please make sure a Relayer address is provided'
exit 1
fi

#################################################################
##### FINALIZE ######
#################################################################
Expand All @@ -110,6 +130,7 @@ cp subgraph.template.yaml subgraph.yaml
sed -i -e "s/{{network}}/${ENV}/g" subgraph.yaml
sed -i -e "s/{{registryAddress}}/${REGISTRY_ADDRESS}/g" subgraph.yaml
sed -i -e "s/{{deployerAddress}}/${DEPLOYER_ADDRESS}/g" subgraph.yaml
sed -i -e "s/{{relayerAddress}}/${RELAYER_ADDRESS}/g" subgraph.yaml
sed -i -e "s/{{blockNumber}}/${BLOCK_NUMBER}/g" subgraph.yaml
rm -f subgraph.yaml-e

Expand Down
54 changes: 54 additions & 0 deletions src/Relayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Address, log } from '@graphprotocol/graph-ts'

import { TaskExecuted } from '../types/Relayer/Relayer'
import { Movement, RelayedExecution, Transaction } from '../types/schema'
import { Task as TaskContract } from '../types/templates/Task/Task'

export function handleTaskExecuted(event: TaskExecuted): void {
let executionId = event.transaction.hash.toHexString() + '#' + event.params.index.toString()
let execution = new RelayedExecution(executionId)
execution.hash = event.transaction.hash.toHexString()
execution.sender = event.transaction.from.toHexString()
execution.executedAt = event.block.timestamp
execution.smartVault = getSmartVault(event.params.task).toHexString()
execution.task = event.params.task.toHexString()
execution.index = event.params.index
execution.succeeded = event.params.success
execution.result = event.params.result
execution.gasUsed = event.params.gas
execution.gasPrice = event.transaction.gasPrice
execution.costNative = event.transaction.gasPrice.times(event.params.gas)
execution.save()

for (let i: i32 = 0; true; i++) {
const movementId = event.transaction.hash.toHexString() + '#' + i.toString()
let movement = Movement.load(movementId)
if (movement == null) break
if (movement.relayedExecution == null) {
movement.relayedExecution = executionId
movement.save()
}
}

for (let i: i32 = 0; true; i++) {
const transactionId = event.transaction.hash.toHexString() + '#' + i.toString()
let transaction = Transaction.load(transactionId)
if (transaction == null) break
if (transaction.relayedExecution == null) {
transaction.relayedExecution = executionId
transaction.save()
}
}
}

export function getSmartVault(address: Address): Address {
let taskContract = TaskContract.bind(address)
let smartVaultCall = taskContract.try_smartVault()

if (!smartVaultCall.reverted) {
return smartVaultCall.value
}

log.warning('smartVault() call reverted for task {}', [address.toHexString()])
return Address.zero()
}
26 changes: 22 additions & 4 deletions src/SmartVault.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, BigInt, ethereum, log } from '@graphprotocol/graph-ts'
import { Address, BigInt, Bytes, ethereum, log } from '@graphprotocol/graph-ts'

import { Movement, Transaction, SmartVault } from '../types/schema'
import { SmartVault as SmartVaultContract } from '../types/templates/SmartVault/SmartVault'
Expand Down Expand Up @@ -42,7 +42,7 @@ export function handleUnwrap(event: Unwrapped): void {
}

function createTransaction(event: ethereum.Event, type: string, fee: BigInt): void {
let transactionId = event.transaction.hash.toHexString() + '#' + event.transactionLogIndex.toString()
let transactionId = getNextTransactionId(event.transaction.hash)
let transaction = new Transaction(transactionId)
transaction.hash = event.transaction.hash.toHexString()
transaction.sender = event.transaction.from.toHexString()
Expand All @@ -54,7 +54,7 @@ function createTransaction(event: ethereum.Event, type: string, fee: BigInt): vo
}

export function handleBalanceConnectorUpdated(event: BalanceConnectorUpdated): void {
let movementId = event.transaction.hash.toHexString() + '#' + event.transactionLogIndex.toString()
let movementId = getNextMovementId(event.transaction.hash)
let movement = new Movement(movementId)
movement.hash = event.transaction.hash.toHexString()
movement.sender = event.transaction.from.toHexString()
Expand Down Expand Up @@ -125,4 +125,22 @@ export function getPriceOracle(address: Address): Address {

log.warning('priceOracle() call reverted for smart vault {}', [address.toHexString()])
return Address.zero()
}
}

function getNextMovementId(hash: Bytes): string {
for (let i: i32 = 0; true; i++) {
const movementId = hash.toHexString() + '#' + i.toString()
if (Movement.load(movementId) == null) return movementId
}

throw Error('Could not find next movement ID')
}

function getNextTransactionId(hash: Bytes): string {
for (let i: i32 = 0; true; i++) {
const transactionId = hash.toHexString() + '#' + i.toString()
if (Transaction.load(transactionId) == null) return transactionId
}

throw Error('Could not find next transaction ID')
}
20 changes: 20 additions & 0 deletions subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ dataSources:
- event: TaskDeployed(string,string,address,address)
handler: handleTaskDeployed
file: ./src/Deployer.ts
- kind: ethereum/contract
name: Relayer
network: {{network}}
source:
address: '{{relayerAddress}}'
abi: Relayer
startBlock: {{blockNumber}}
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
entities:
- Relayer
abis:
- name: Relayer
file: ./node_modules/@mimic-fi/v3-relayer/artifacts/contracts/interfaces/IRelayer.sol/IRelayer.json
eventHandlers:
- event: TaskExecuted(indexed address,indexed address,bytes,bool,bytes,uint256,uint256)
handler: handleTaskExecuted
file: ./src/Relayer.ts
templates:
- kind: ethereum/contract
name: Authorizer
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -983,10 +983,10 @@
"@openzeppelin/contracts" "4.7.0"
solmate "^6.7.0"

"@mimic-fi/[email protected].0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@mimic-fi/v3-relayer/-/v3-relayer-0.1.0.tgz#8fc6b6365c6b232921b7ad82d45fe96eeda7593b"
integrity sha512-BIleQF5rljWxs9weXCbbcuUkZAzpytyCjpu2+P4JRPvvxkgK6D/O7FmYLSMuCT8AQZlft8Irp57nEEVWwxMF7A==
"@mimic-fi/[email protected].1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@mimic-fi/v3-relayer/-/v3-relayer-0.1.1.tgz#01e8d5e8defc3ab47f8da321a92791075b305031"
integrity sha512-JG9hVbVgPA6IpszgPV2E0ZxEaNkHw8GrDPCeH4HjJuAtfl2nrYsDMSGoZqg+MxeuT4KeGrfX2LmlJCHPLamSag==
dependencies:
"@mimic-fi/v3-helpers" "0.1.0"
"@mimic-fi/v3-smart-vault" "0.1.0"
Expand Down

0 comments on commit cca1ef8

Please sign in to comment.