From 7146b3ae37a4d38b51c3fbbaf904f1c72b5b762d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Pr=C3=A9vost?= <998369+prevostc@users.noreply.github.com> Date: Fri, 21 Jun 2024 13:19:38 +0200 Subject: [PATCH] Add ignored contracts to account for classic boosts --- schema.graphql | 9 +++++++ src/classic/lifecycle.ts | 6 ++--- src/common/entity/ignored.ts | 15 +++++++++++ src/common/interaction.ts | 8 +++++- tests/utils/time.test.ts | 51 ++++++++++++++++++++++++++++++++++++ tsconfig.json | 2 +- 6 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 src/common/entity/ignored.ts create mode 100644 tests/utils/time.test.ts diff --git a/schema.graphql b/schema.graphql index 0ecf46e..910f325 100644 --- a/schema.graphql +++ b/schema.graphql @@ -14,6 +14,15 @@ type Token @entity(immutable: true) { decimals: BigInt! } +""" +A contract where transfers from/to are ignored +This is used to account for contracts that are not ERC20 compliant +but allow staking one token like beefy boosts +""" +type IgnoredContract @entity { + id: Bytes! +} + # account details type Account @entity { #account address diff --git a/src/classic/lifecycle.ts b/src/classic/lifecycle.ts index f528d08..8134d57 100644 --- a/src/classic/lifecycle.ts +++ b/src/classic/lifecycle.ts @@ -5,6 +5,7 @@ import { BoostDeployed as BoostCreated } from "../../generated/ClassicBoostFacto import { ClassicVault as ClassicVaultContract } from "../../generated/ClassicVaultFactory/ClassicVault" import { BeefyERC20Product as BeefyERC20ProductTemplate, ClassicVault as ClassicVaultTemplate } from "../../generated/templates" import { fetchAndSaveTokenData } from "../common/utils/token" +import { getIgnoredContract } from "../common/entity/ignored" export function handleClassicVaultOrStrategyCreated(event: VaultOrStrategyCreated): void { const address = event.params.proxy @@ -24,10 +25,9 @@ export function handleClassicVaultOrStrategyCreated(event: VaultOrStrategyCreate } export function handleClassicBoostCreated(event: BoostCreated): void { - // TODO: this is wrong const address = event.params.boost - fetchAndSaveTokenData(address) - BeefyERC20ProductTemplate.create(address) + const ignored = getIgnoredContract(address) + ignored.save() } export function handleClassicVaultInitialized(event: VaultInitialized): void { diff --git a/src/common/entity/ignored.ts b/src/common/entity/ignored.ts new file mode 100644 index 0000000..757ecc6 --- /dev/null +++ b/src/common/entity/ignored.ts @@ -0,0 +1,15 @@ +import { Bytes } from "@graphprotocol/graph-ts" +import { IgnoredContract } from "../../../generated/schema" + +export function shouldIgnoreContract(contractAddress: Bytes): boolean { + return IgnoredContract.load(contractAddress) !== null +} + +export function getIgnoredContract(contractAddress: Bytes): IgnoredContract { + let ignoredcontract = IgnoredContract.load(contractAddress) + if (!ignoredcontract) { + ignoredcontract = new IgnoredContract(contractAddress) + } + + return ignoredcontract +} diff --git a/src/common/interaction.ts b/src/common/interaction.ts index 620f991..10fbbb7 100644 --- a/src/common/interaction.ts +++ b/src/common/interaction.ts @@ -1,11 +1,17 @@ -import { BigInt, Bytes } from "@graphprotocol/graph-ts" +import { BigInt, Bytes, log } from "@graphprotocol/graph-ts" import { Transfer as TransferEvent } from "../../generated/templates/BeefyERC20Product/IERC20" import { BURN_ADDRESS, SHARE_TOKEN_MINT_ADDRESS } from "../config" import { getAccount } from "./entity/account" import { getTokenBalance } from "./entity/balance" import { getToken } from "./entity/token" +import { shouldIgnoreContract } from "./entity/ignored" export function handleProductTransfer(event: TransferEvent): void { + if (shouldIgnoreContract(event.params.from) || shouldIgnoreContract(event.params.to)) { + log.debug("Ignoring transfer from/to ignored contract: {}", [event.transaction.hash.toHexString()]) + return + } + if (event.params.from.notEqual(SHARE_TOKEN_MINT_ADDRESS) && event.params.from.notEqual(BURN_ADDRESS)) { updateAccountBalance(event.address, event.params.from, event.params.value.neg()) } diff --git a/tests/utils/time.test.ts b/tests/utils/time.test.ts new file mode 100644 index 0000000..e7aa2fc --- /dev/null +++ b/tests/utils/time.test.ts @@ -0,0 +1,51 @@ +import { assert, test, describe } from "matchstick-as/assembly/index" +import { BigInt } from "@graphprotocol/graph-ts" +import { DAY, HOUR, MONTH, QUARTER, WEEK, YEAR, getIntervalFromTimestamp, getPreviousIntervalFromTimestamp } from "../../src/common/utils/time" + +describe("time.getIntervalFromTimestamp", () => { + test("Support all the different periods", () => { + const timestamp = BigInt.fromString("1712744972") + + // simple periods + let res = getIntervalFromTimestamp(timestamp, HOUR) + assert.assertTrue(res.equals(BigInt.fromString("1712743200"))) + + res = getIntervalFromTimestamp(timestamp, DAY) + assert.assertTrue(res.equals(BigInt.fromString("1712707200"))) + + res = getIntervalFromTimestamp(timestamp, WEEK) + assert.assertTrue(res.equals(BigInt.fromString("1712448000"))) + + res = getIntervalFromTimestamp(timestamp, MONTH) + assert.assertTrue(res.equals(BigInt.fromString("1711929600"))) + + res = getIntervalFromTimestamp(timestamp, QUARTER) + assert.assertTrue(res.equals(BigInt.fromString("1711929600"))) + + res = getIntervalFromTimestamp(timestamp, YEAR) + assert.assertTrue(res.equals(BigInt.fromString("1704067200"))) + }) + + test("can query the previous interval as well", () => { + const timestamp = BigInt.fromString("1712744972") + + // simple periods + let res = getPreviousIntervalFromTimestamp(timestamp, HOUR) + assert.assertTrue(res.equals(BigInt.fromString("1712739600"))) + + res = getPreviousIntervalFromTimestamp(timestamp, DAY) + assert.assertTrue(res.equals(BigInt.fromString("1712620800"))) + + res = getPreviousIntervalFromTimestamp(timestamp, WEEK) + assert.assertTrue(res.equals(BigInt.fromString("1711843200"))) + + res = getPreviousIntervalFromTimestamp(timestamp, MONTH) + assert.assertTrue(res.equals(BigInt.fromString("1709251200"))) + + res = getPreviousIntervalFromTimestamp(timestamp, QUARTER) + assert.assertTrue(res.equals(BigInt.fromString("1704067200"))) + + res = getPreviousIntervalFromTimestamp(timestamp, YEAR) + assert.assertTrue(res.equals(BigInt.fromString("1672531200"))) + }) +}) diff --git a/tsconfig.json b/tsconfig.json index 586dc2c..dc4055e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "@graphprotocol/graph-ts/tsconfig", "compilerOptions": { - "types": ["@graphprotocol/graph-ts"], + "types": ["@graphprotocol/graph-ts", "assemblyscript"], "strict": true, "strictNullChecks": true }