Skip to content

Commit dc02f5b

Browse files
committed
Add support for initializable contract deployed with deployer
1 parent b68aedc commit dc02f5b

6 files changed

+79
-13
lines changed

abis/beefy/Initializable.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"anonymous": false,
4+
"inputs": [{ "indexed": false, "internalType": "uint8", "name": "version", "type": "uint8" }],
5+
"name": "Initialized",
6+
"type": "event"
7+
}
8+
]

src/common/interaction.ts

-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ export function handleProductTransfer(event: TransferEvent): void {
4444
function updateAccountBalance(tokenAddress: Bytes, accountAddress: Bytes, amountDiff: BigInt): BalanceDiff {
4545
const account = createAccount(accountAddress)
4646
const token = getToken(tokenAddress)
47-
token.save()
48-
4947
const balance = getTokenBalance(token, account)
5048
const before = balance.amount
5149
const after = balance.amount.plus(amountDiff)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { BeefyERC20Product as BeefyERC20ProductTemplate } from "../../../generated/templates"
2+
import { Initialized as InitializedEvent } from "../../../generated/ContractDeployer/Initializable"
3+
import { IERC20 as IERC20Contract } from "../../../generated/templates/BeefyERC20Product/IERC20"
4+
import { Address, log } from "@graphprotocol/graph-ts"
5+
import { fetchAndSaveTokenData } from "../utils/token"
6+
7+
export function handleContractDeployedInitializableInitialized(event: InitializedEvent): void {
8+
const tokenAddress = event.address
9+
10+
// detect if we are creating an erc20 token
11+
const tokenContract = IERC20Contract.bind(Address.fromBytes(tokenAddress))
12+
13+
const tokenDecimalsRes = tokenContract.try_decimals()
14+
if (tokenDecimalsRes.reverted) {
15+
log.info("Contract {} is not an ERC20 token, decimals() reverted", [tokenAddress.toHexString()])
16+
return
17+
}
18+
19+
const tokenNameRes = tokenContract.try_name()
20+
if (tokenNameRes.reverted) {
21+
log.info("Contract {} is not an ERC20 token, name() reverted", [tokenAddress.toHexString()])
22+
return
23+
}
24+
25+
const tokenSymbolRes = tokenContract.try_symbol()
26+
if (tokenSymbolRes.reverted) {
27+
log.info("Contract {} is not an ERC20 token, symbol() reverted", [tokenAddress.toHexString()])
28+
return
29+
}
30+
31+
log.debug("Creating BeefyERC20Product template for {} from contract-deployer", [tokenAddress.toHexString()])
32+
33+
fetchAndSaveTokenData(tokenAddress)
34+
BeefyERC20ProductTemplate.create(tokenAddress)
35+
}
+13-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { BeefyERC20Product as BeefyERC20ProductTemplate } from "../../../generated/templates"
1+
import {
2+
BeefyERC20Product as BeefyERC20ProductTemplate,
3+
ContractDeployerInitializable as ContractDeployerInitializableTemplate,
4+
} from "../../../generated/templates"
25
import { ContractDeployed as ContractDeployedEvent } from "../../../generated/ContractDeployer/ContractDeployer"
36
import { IERC20 as IERC20Contract } from "../../../generated/templates/BeefyERC20Product/IERC20"
47
import { fetchAndSaveTokenData } from "../utils/token"
@@ -9,27 +12,30 @@ export function handleContractDeployedWithDeployer(event: ContractDeployedEvent)
912

1013
// detect if we are creating an erc20 token
1114
const tokenContract = IERC20Contract.bind(Address.fromBytes(address))
12-
1315
const tokenDecimalsRes = tokenContract.try_decimals()
1416
if (tokenDecimalsRes.reverted) {
1517
log.info("Contract {} is not an ERC20 token, decimals() reverted", [address.toHexString()])
1618
return
1719
}
18-
1920
const tokenNameRes = tokenContract.try_name()
2021
if (tokenNameRes.reverted) {
2122
log.info("Contract {} is not an ERC20 token, name() reverted", [address.toHexString()])
2223
return
2324
}
24-
2525
const tokenSymbolRes = tokenContract.try_symbol()
2626
if (tokenSymbolRes.reverted) {
2727
log.info("Contract {} is not an ERC20 token, symbol() reverted", [address.toHexString()])
2828
return
2929
}
3030

31-
log.debug("Creating BeefyERC20Product template for {} from contract-deployer", [address.toHexString()])
31+
// if any of the calls return null, this is most likely an initializable contract
32+
if (tokenDecimalsRes.value == null || tokenNameRes.value == null || tokenSymbolRes.value == null) {
33+
log.info("Contract {} is probably innitializable, one of the metadata calls returned null", [address.toHexString()])
3234

33-
fetchAndSaveTokenData(address)
34-
BeefyERC20ProductTemplate.create(address)
35+
ContractDeployerInitializableTemplate.create(address)
36+
} else {
37+
log.debug("Creating BeefyERC20Product template for {} from contract-deployer", [address.toHexString()])
38+
fetchAndSaveTokenData(address)
39+
BeefyERC20ProductTemplate.create(address)
40+
}
3541
}

src/common/utils/token.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ export function fetchAndSaveTokenData(tokenAddress: Bytes): Token {
1010

1111
// if any of these calls revert, we will just use the default values to avoid a subgraph crash
1212
const tokenDecimalsRes = tokenContract.try_decimals()
13-
const tokenDecimals = tokenDecimalsRes.reverted ? 18 : tokenDecimalsRes.value
14-
1513
const tokenNameRes = tokenContract.try_name()
16-
const tokenName = tokenNameRes.reverted ? "Unknown" : tokenNameRes.value
17-
1814
const tokenSymbolRes = tokenContract.try_symbol()
15+
16+
const tokenDecimals = tokenDecimalsRes.reverted ? 18 : tokenDecimalsRes.value
17+
const tokenName = tokenNameRes.reverted ? "Unknown" : tokenNameRes.value
1918
const tokenSymbol = tokenSymbolRes.reverted ? "UNKNOWN" : tokenSymbolRes.value
2019

2120
const token = getToken(tokenAddress)

subgraph.template.yaml

+20
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ dataSources:
196196
file: ./abis/beefy/ContractDeployer.json
197197
- name: IERC20
198198
file: ./abis/IERC20/IERC20.json
199+
- name: Initializable
200+
file: ./abis/beefy/Initializable.json
199201
eventHandlers:
200202
- event: ContractDeployed(indexed bytes32,address)
201203
handler: handleContractDeployedWithDeployer
@@ -268,6 +270,24 @@ templates:
268270
handler: handleClassicVaultInitialized
269271
{{/beefyClassicVaultFactoryAddress}}
270272

273+
{{#beefyContractDeployerAddress}}
274+
- name: ContractDeployerInitializable
275+
kind: ethereum/contract
276+
network: {{network}}
277+
source:
278+
abi: Initializable
279+
mapping:
280+
kind: ethereum/events
281+
apiVersion: 0.0.7 # 0xgraph's version
282+
language: wasm/assemblyscript
283+
file: ./src/common/mapping/contract-deployer-initializable.ts
284+
entities: *contractDeployerEntities
285+
abis: *contractDeployerAbis
286+
eventHandlers:
287+
- event: Initialized(uint8)
288+
handler: handleContractDeployedInitializableInitialized
289+
{{/beefyContractDeployerAddress}}
290+
271291
- name: BeefyERC20Product
272292
kind: ethereum/contract
273293
network: {{network}}

0 commit comments

Comments
 (0)