From 17cc1f8a642807aa991e4696aef73c849a7c4f2f Mon Sep 17 00:00:00 2001 From: Alec Ananian <1013230+alecananian@users.noreply.github.com> Date: Fri, 9 Aug 2024 15:54:48 -0700 Subject: [PATCH] merge magicswap subgraphs --- .github/workflows/deploy.yaml | 3 +- README.md | 8 +-- package-lock.json | 62 ++++++++++++------- packages/config/src/arbitrum-sepolia.json | 8 +-- packages/config/src/arbitrum.json | 6 +- .../abis/ChainlinkAggregator.json | 0 .../abis/ERC20.json | 0 .../abis/MagicswapV2Router.json | 0 .../abis/NftVault.json | 0 .../abis/NftVaultFactory.json | 0 .../abis/UniswapV2Factory.json | 0 .../abis/UniswapV2Pair.json | 0 .../matchstick.yaml | 0 .../{magicswapv2 => magicswap}/package.json | 4 +- .../{magicswapv2 => magicswap}/schema.graphql | 21 ++++++- .../{magicswapv2 => magicswap}/src/const.ts | 0 .../{magicswapv2 => magicswap}/src/helpers.ts | 42 +++++++++---- .../src/mappings/factory.ts | 12 ++-- .../src/mappings/pair.ts | 35 +++++------ .../src/mappings/price.ts | 8 +-- .../src/mappings/router.ts | 0 .../src/mappings/vault.ts | 0 .../{magicswapv2 => magicswap}/src/utils.ts | 0 .../{magicswapv2 => magicswap}/template.yaml | 48 +++++++++++++- .../tests/helpers.test.ts | 0 .../tests/helpers/constants.ts | 0 .../tests/helpers/pair.ts | 0 .../tests/helpers/price.ts | 0 .../tests/helpers/token.ts | 0 .../tests/helpers/vault.ts | 0 .../tests/pair.test.ts | 0 .../tests/price.test.ts | 0 .../tests/vault.test.ts | 0 33 files changed, 173 insertions(+), 84 deletions(-) rename subgraphs/{magicswapv2 => magicswap}/abis/ChainlinkAggregator.json (100%) rename subgraphs/{magicswapv2 => magicswap}/abis/ERC20.json (100%) rename subgraphs/{magicswapv2 => magicswap}/abis/MagicswapV2Router.json (100%) rename subgraphs/{magicswapv2 => magicswap}/abis/NftVault.json (100%) rename subgraphs/{magicswapv2 => magicswap}/abis/NftVaultFactory.json (100%) rename subgraphs/{magicswapv2 => magicswap}/abis/UniswapV2Factory.json (100%) rename subgraphs/{magicswapv2 => magicswap}/abis/UniswapV2Pair.json (100%) rename subgraphs/{magicswapv2 => magicswap}/matchstick.yaml (100%) rename subgraphs/{magicswapv2 => magicswap}/package.json (86%) rename subgraphs/{magicswapv2 => magicswap}/schema.graphql (94%) rename subgraphs/{magicswapv2 => magicswap}/src/const.ts (100%) rename subgraphs/{magicswapv2 => magicswap}/src/helpers.ts (86%) rename subgraphs/{magicswapv2 => magicswap}/src/mappings/factory.ts (91%) rename subgraphs/{magicswapv2 => magicswap}/src/mappings/pair.ts (93%) rename subgraphs/{magicswapv2 => magicswap}/src/mappings/price.ts (54%) rename subgraphs/{magicswapv2 => magicswap}/src/mappings/router.ts (100%) rename subgraphs/{magicswapv2 => magicswap}/src/mappings/vault.ts (100%) rename subgraphs/{magicswapv2 => magicswap}/src/utils.ts (100%) rename subgraphs/{magicswapv2 => magicswap}/template.yaml (81%) rename subgraphs/{magicswapv2 => magicswap}/tests/helpers.test.ts (100%) rename subgraphs/{magicswapv2 => magicswap}/tests/helpers/constants.ts (100%) rename subgraphs/{magicswapv2 => magicswap}/tests/helpers/pair.ts (100%) rename subgraphs/{magicswapv2 => magicswap}/tests/helpers/price.ts (100%) rename subgraphs/{magicswapv2 => magicswap}/tests/helpers/token.ts (100%) rename subgraphs/{magicswapv2 => magicswap}/tests/helpers/vault.ts (100%) rename subgraphs/{magicswapv2 => magicswap}/tests/pair.test.ts (100%) rename subgraphs/{magicswapv2 => magicswap}/tests/price.test.ts (100%) rename subgraphs/{magicswapv2 => magicswap}/tests/vault.test.ts (100%) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 9e127ff8..482bddd1 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -15,8 +15,7 @@ on: - bridgeworld-kote - bridgeworld-recruits - governance-staking - - magicswap-exchange - - magicswapv2 + - magicswap - smol version: description: Subgraph version diff --git a/README.md b/README.md index 3e491cae..81d3b3f1 100644 --- a/README.md +++ b/README.md @@ -28,13 +28,9 @@ Corruption balances and removals for [Bridgeworld](https://bridgeworld.treasure. Tracking MAGIC deposited for [TreasureDAO Governance Staking](https://governance-staking.treasure.lol) (gMAGIC) -### magicswap-exchange +### magicswap -Fork of [Sushiswap subgraph](https://github.com/sushiswap/subgraphs) for [MagicSwap](https://magicswap.lol) - -### magicswapv2 - -Exchange data for [Magicswap v2](https://v2.magicswap.lol) +Exchange data for [Magicswap](https://v2.magicswap.lol) ### smol diff --git a/package-lock.json b/package-lock.json index 8c0ed4ad..2d718658 100644 --- a/package-lock.json +++ b/package-lock.json @@ -882,9 +882,9 @@ } }, "node_modules/@goldskycom/cli": { - "version": "9.3.0", - "resolved": "https://registry.npmjs.org/@goldskycom/cli/-/cli-9.3.0.tgz", - "integrity": "sha512-/2IH3n8je77udgqbLvUt7tK+yOFayFXoqmBfmcC07R4fbnuuLBT3vsIRtg7jVT+bJrhbL2PkyS2N9ymBucgGCA==", + "version": "9.4.0", + "resolved": "https://registry.npmjs.org/@goldskycom/cli/-/cli-9.4.0.tgz", + "integrity": "sha512-TWf3fqh3/rUd/SJm5+vMBDdcMiYLeUoxOC2DPORPnllix8wW+3xPbyjVrm0UhAUHyacrwk1aVsbzbux9c4MKXA==", "dev": true, "dependencies": { "cuid": "^2.1.8", @@ -1688,9 +1688,9 @@ "license": "MIT" }, "node_modules/@types/node": { - "version": "22.1.0", - "resolved": "https://registry.npmjs.org/@types/node/-/node-22.1.0.tgz", - "integrity": "sha512-AOmuRF0R2/5j1knA3c6G3HOk523Ga+l+ZXltX8SF1+5oqcXijjfTd8fY3XRZqSihEu9XhtQnKYLmkFaoxgsJHw==", + "version": "22.2.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-22.2.0.tgz", + "integrity": "sha512-bm6EG6/pCpkxDf/0gDNDdtDILMOHgaQBVOJGdwsqClnxA3xL6jtMv76rLBc006RVMWbmaf0xbmom4Z/5o2nRkQ==", "dev": true, "license": "MIT", "dependencies": { @@ -4888,22 +4888,22 @@ "license": "MIT" }, "node_modules/lint-staged": { - "version": "15.2.7", - "resolved": "https://registry.npmjs.org/lint-staged/-/lint-staged-15.2.7.tgz", - "integrity": "sha512-+FdVbbCZ+yoh7E/RosSdqKJyUM2OEjTciH0TFNkawKgvFp1zbGlEC39RADg+xKBG1R4mhoH2j85myBQZ5wR+lw==", + "version": "15.2.8", + "resolved": "https://registry.npmjs.org/lint-staged/-/lint-staged-15.2.8.tgz", + "integrity": "sha512-PUWFf2zQzsd9EFU+kM1d7UP+AZDbKFKuj+9JNVTBkhUFhbg4MAt6WfyMMwBfM4lYqd4D2Jwac5iuTu9rVj4zCQ==", "dev": true, "license": "MIT", "dependencies": { "chalk": "~5.3.0", "commander": "~12.1.0", - "debug": "~4.3.4", + "debug": "~4.3.6", "execa": "~8.0.1", - "lilconfig": "~3.1.1", - "listr2": "~8.2.1", + "lilconfig": "~3.1.2", + "listr2": "~8.2.4", "micromatch": "~4.0.7", "pidtree": "~0.6.0", "string-argv": "~0.3.2", - "yaml": "~2.4.2" + "yaml": "~2.5.0" }, "bin": { "lint-staged": "bin/lint-staged.js" @@ -4938,6 +4938,24 @@ "node": ">=18" } }, + "node_modules/lint-staged/node_modules/debug": { + "version": "4.3.6", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.6.tgz", + "integrity": "sha512-O/09Bd4Z1fBrU4VzkhFqVgpPzaGbw6Sm9FEkBT1A/YBXQFGuuSxa1dN2nxgxS34JmKXqYx8CZAwEVoJFImUXIg==", + "dev": true, + "license": "MIT", + "dependencies": { + "ms": "2.1.2" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, "node_modules/lint-staged/node_modules/execa": { "version": "8.0.1", "resolved": "https://registry.npmjs.org/execa/-/execa-8.0.1.tgz", @@ -5083,9 +5101,9 @@ } }, "node_modules/lint-staged/node_modules/yaml": { - "version": "2.4.5", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.4.5.tgz", - "integrity": "sha512-aBx2bnqDzVOyNKfsysjA2ms5ZlnjSAW2eG3/L5G/CSujfjLJTJsEw1bGw8kCf04KodQWk1pxlGnZ56CRxiawmg==", + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.5.0.tgz", + "integrity": "sha512-2wWLbGbYDiSqqIKoPjar3MPgB94ErzCtrNE1FdqGuaO0pi2JGjmE8aW8TDZwzU7vuxcGRdL/4gPQwQ7hD5AMSw==", "dev": true, "license": "ISC", "bin": { @@ -5569,12 +5587,12 @@ "dev": true, "license": "ISC" }, - "node_modules/magicswap-exchange": { - "resolved": "subgraphs/magicswap-exchange", + "node_modules/magicswap": { + "resolved": "subgraphs/magicswap", "link": true }, - "node_modules/magicswapv2": { - "resolved": "subgraphs/magicswapv2", + "node_modules/magicswap-exchange": { + "resolved": "subgraphs/magicswap-exchange", "link": true }, "node_modules/make-error": { @@ -7839,11 +7857,11 @@ "version": "1.0.0", "license": "MIT" }, - "subgraphs/magicswap-exchange": { + "subgraphs/magicswap": { "version": "1.0.0", "license": "MIT" }, - "subgraphs/magicswapv2": { + "subgraphs/magicswap-exchange": { "version": "1.0.0", "license": "MIT" }, diff --git a/packages/config/src/arbitrum-sepolia.json b/packages/config/src/arbitrum-sepolia.json index fa8751a8..b7593819 100644 --- a/packages/config/src/arbitrum-sepolia.json +++ b/packages/config/src/arbitrum-sepolia.json @@ -68,13 +68,13 @@ "transmolgrifier_start_block": 6249003, "smol_renderer_address": "0x79973af48c424de5f199a7c0369c5e9cdeb493fc", - "magicswap_factory_address": "0x9b58fc8c7B224Ae8479DA7E6eD37CA4Ac58099a9", - "magicswap_factory_start_block": 654981, + "magicswapv1_start_block": 654981, + "magicswapv1_uniswapv2_factory_address": "0x9b58fc8c7B224Ae8479DA7E6eD37CA4Ac58099a9", + "magicswapv2_start_block": 49484001, "magicswapv2_nft_vault_factory_address": "0x040d5778553aa5c465dfdba889e914b12895d423", "magicswapv2_nft_vault_factory_v2_address": "0x496f5f13816cdddd6934be1ca6a579b3838889ce", "magicswapv2_uniswapv2_factory_address": "0xbdfab57b9446ab3512579b7a901b74fa49fda752", "magicswapv2_router_address": "0xc8d7261feb0b648bc489224a18d6cdc905a0c5ab", "magic_usd_price_feed_address": "0xe6616a09e029d477183cba6bb6133643b522c15c", - "weth_address": "0x980b62da83eff3d4576c647993b0c1d7faf17c73", - "magicswapv2_start_block": 49484001 + "weth_address": "0x980b62da83eff3d4576c647993b0c1d7faf17c73" } diff --git a/packages/config/src/arbitrum.json b/packages/config/src/arbitrum.json index bdbe3dfd..aaeafc33 100644 --- a/packages/config/src/arbitrum.json +++ b/packages/config/src/arbitrum.json @@ -68,15 +68,15 @@ "transmolgrifier_start_block": 173093282, "smol_renderer_address": "0x2d79de1b69bfdde8708acaef7bf52b6bbc83afd5", - "magicswap_factory_address": "0x015e379Ce0Ff195228b3A9eBDFA13F9afC155Dd7", - "magicswap_factory_start_block": 20547275, + "magicswapv1_start_block": 20547275, + "magicswapv1_uniswapv2_factory_address": "0x015e379Ce0Ff195228b3A9eBDFA13F9afC155Dd7", + "magicswapv2_start_block": 232580414, "magicswapv2_nft_vault_factory_address": "0x0000000000000000000000000000000000000000", "magicswapv2_nft_vault_factory_v2_address": "0xd67a39241fcdaf57dfbe764fa62d14e9b681f8e2", "magicswapv2_uniswapv2_factory_address": "0x77fa938998e196701c324149f771efd6e980df0a", "magicswapv2_router_address": "0xb740d5804ea2061432469119cfa40cbb4586dd17", "magic_usd_price_feed_address": "0x5ab0b1e2604d4b708721bc3cd1ce962958b4297e", "weth_address": "0x82af49447d8a07e3bd95bd0d56f35241523fbab1", - "magicswapv2_start_block": 232580414, "governance_staking_address": "0xc0e641c7ea263166a238285556ff61fdf37a4c79", "governance_staking_start_block": 60986774 diff --git a/subgraphs/magicswapv2/abis/ChainlinkAggregator.json b/subgraphs/magicswap/abis/ChainlinkAggregator.json similarity index 100% rename from subgraphs/magicswapv2/abis/ChainlinkAggregator.json rename to subgraphs/magicswap/abis/ChainlinkAggregator.json diff --git a/subgraphs/magicswapv2/abis/ERC20.json b/subgraphs/magicswap/abis/ERC20.json similarity index 100% rename from subgraphs/magicswapv2/abis/ERC20.json rename to subgraphs/magicswap/abis/ERC20.json diff --git a/subgraphs/magicswapv2/abis/MagicswapV2Router.json b/subgraphs/magicswap/abis/MagicswapV2Router.json similarity index 100% rename from subgraphs/magicswapv2/abis/MagicswapV2Router.json rename to subgraphs/magicswap/abis/MagicswapV2Router.json diff --git a/subgraphs/magicswapv2/abis/NftVault.json b/subgraphs/magicswap/abis/NftVault.json similarity index 100% rename from subgraphs/magicswapv2/abis/NftVault.json rename to subgraphs/magicswap/abis/NftVault.json diff --git a/subgraphs/magicswapv2/abis/NftVaultFactory.json b/subgraphs/magicswap/abis/NftVaultFactory.json similarity index 100% rename from subgraphs/magicswapv2/abis/NftVaultFactory.json rename to subgraphs/magicswap/abis/NftVaultFactory.json diff --git a/subgraphs/magicswapv2/abis/UniswapV2Factory.json b/subgraphs/magicswap/abis/UniswapV2Factory.json similarity index 100% rename from subgraphs/magicswapv2/abis/UniswapV2Factory.json rename to subgraphs/magicswap/abis/UniswapV2Factory.json diff --git a/subgraphs/magicswapv2/abis/UniswapV2Pair.json b/subgraphs/magicswap/abis/UniswapV2Pair.json similarity index 100% rename from subgraphs/magicswapv2/abis/UniswapV2Pair.json rename to subgraphs/magicswap/abis/UniswapV2Pair.json diff --git a/subgraphs/magicswapv2/matchstick.yaml b/subgraphs/magicswap/matchstick.yaml similarity index 100% rename from subgraphs/magicswapv2/matchstick.yaml rename to subgraphs/magicswap/matchstick.yaml diff --git a/subgraphs/magicswapv2/package.json b/subgraphs/magicswap/package.json similarity index 86% rename from subgraphs/magicswapv2/package.json rename to subgraphs/magicswap/package.json index 02264b84..00cee809 100644 --- a/subgraphs/magicswapv2/package.json +++ b/subgraphs/magicswap/package.json @@ -1,7 +1,7 @@ { - "name": "magicswapv2", + "name": "magicswap", "version": "1.0.0", - "description": "Subgraph that powers Magicswap v2", + "description": "Subgraph that powers Magicswap", "license": "MIT", "scripts": { "generate:dev": "npm run prepare:arbitrum-sepolia --prefix=../../packages/constants && mustache ../../node_modules/@treasure/subgraph-config/src/arbitrum-sepolia.json template.yaml > subgraph.yaml && graph codegen", diff --git a/subgraphs/magicswapv2/schema.graphql b/subgraphs/magicswap/schema.graphql similarity index 94% rename from subgraphs/magicswapv2/schema.graphql rename to subgraphs/magicswap/schema.graphql index 1f6978b4..9b4c7a12 100644 --- a/subgraphs/magicswapv2/schema.graphql +++ b/subgraphs/magicswap/schema.graphql @@ -9,21 +9,34 @@ enum TransactionType { Withdrawal } +enum Version { + V1 + V2 +} + +type Global @entity { + id: Bytes! + + userCount: BigInt! + magicUSD: BigDecimal! +} + type Factory @entity { id: Bytes! + address: Bytes! + version: Version! pairCount: BigInt! volumeUSD: BigDecimal! reserveNFT: BigDecimal! reserveUSD: BigDecimal! txCount: BigInt! - userCount: BigInt! - - magicUSD: BigDecimal! lpFee: BigDecimal! protocolFee: BigDecimal! protocolFeeBeneficiary: Bytes + + pairs: [Pair!]! @derivedFrom(field: "factory") } type DayData @entity { @@ -91,6 +104,8 @@ type VaultReserveItem @entity { type Pair @entity { id: Bytes! + factory: Factory! + version: Version! token0: Token! token1: Token! reserve0: BigDecimal! diff --git a/subgraphs/magicswapv2/src/const.ts b/subgraphs/magicswap/src/const.ts similarity index 100% rename from subgraphs/magicswapv2/src/const.ts rename to subgraphs/magicswap/src/const.ts diff --git a/subgraphs/magicswapv2/src/helpers.ts b/subgraphs/magicswap/src/helpers.ts similarity index 86% rename from subgraphs/magicswapv2/src/helpers.ts rename to subgraphs/magicswap/src/helpers.ts index 11d059ad..368615f5 100644 --- a/subgraphs/magicswapv2/src/helpers.ts +++ b/subgraphs/magicswap/src/helpers.ts @@ -18,6 +18,7 @@ import { Collection, DayData, Factory, + Global, LiquidityPosition, Pair, PairDayData, @@ -31,19 +32,32 @@ import { exponentToBigDecimal } from "./utils"; export const NFT_TYPES = ["ERC721", "ERC1155"]; -export const getOrCreateFactory = (): Factory => { - let factory = Factory.load(MAGICSWAP_V2_FACTORY_ADDRESS); +export const getOrCreateGlobal = (): Global => { + const id = Bytes.fromI32(1); + let global = Global.load(id); + if (!global) { + global = new Global(id); + global.userCount = ZERO_BI; + global.magicUSD = ZERO_BD; + } + + return global; +}; + +export const getOrCreateFactory = (address: Bytes): Factory => { + let factory = Factory.load(address); if (!factory) { - factory = new Factory(MAGICSWAP_V2_FACTORY_ADDRESS); + const isV2 = address.equals(MAGICSWAP_V2_FACTORY_ADDRESS); + factory = new Factory(address); + factory.address = address; + factory.version = isV2 ? "V2" : "V1"; factory.pairCount = ZERO_BI; factory.volumeUSD = ZERO_BD; factory.reserveUSD = ZERO_BD; factory.reserveNFT = ZERO_BD; factory.txCount = ZERO_BI; - factory.userCount = ZERO_BI; - factory.magicUSD = ZERO_BD; - factory.lpFee = ZERO_BD; - factory.protocolFee = ZERO_BD; + factory.lpFee = isV2 ? ZERO_BD : BigDecimal.fromString("0.00375"); + factory.protocolFee = isV2 ? ZERO_BD : BigDecimal.fromString("0.00125"); factory.save(); } @@ -71,9 +85,9 @@ export const getOrCreateUser = (address: Address): User => { user.liquidityPositionCount = ZERO_BI; user.save(); - const factory = getOrCreateFactory(); - factory.userCount = factory.userCount.plus(ONE_BI); - factory.save(); + const global = getOrCreateGlobal(); + global.userCount = global.userCount.plus(ONE_BI); + global.save(); } return user; @@ -219,7 +233,7 @@ export const getDerivedMagic = (token: Token): BigDecimal => { export const timestampToDate = (timestamp: BigInt): BigInt => BigInt.fromI32((timestamp.toI32() / 86400) * 86400); -export const updateDayData = (timestamp: BigInt): DayData => { +export const updateDayData = (factory: Factory, timestamp: BigInt): DayData => { const date = timestampToDate(timestamp); const id = Bytes.fromI32(date.toI32()); let dayData = DayData.load(id); @@ -229,7 +243,6 @@ export const updateDayData = (timestamp: BigInt): DayData => { dayData.volumeUSD = ZERO_BD; } - const factory = getOrCreateFactory(); dayData.reserveUSD = factory.reserveUSD; dayData.reserveNFT = factory.reserveNFT; dayData.txCount = factory.txCount; @@ -264,3 +277,8 @@ export const updatePairDayData = ( return pairDayData; }; + +export const getMagicUSD = (): BigDecimal => { + const global = getOrCreateGlobal(); + return global.magicUSD; +}; diff --git a/subgraphs/magicswapv2/src/mappings/factory.ts b/subgraphs/magicswap/src/mappings/factory.ts similarity index 91% rename from subgraphs/magicswapv2/src/mappings/factory.ts rename to subgraphs/magicswap/src/mappings/factory.ts index 92198b7a..e45e5ef2 100644 --- a/subgraphs/magicswapv2/src/mappings/factory.ts +++ b/subgraphs/magicswap/src/mappings/factory.ts @@ -25,11 +25,13 @@ const updateTotalFee = (pair: Pair): void => { export function handlePairCreated(event: PairCreated): void { const params = event.params; - const factory = getOrCreateFactory(); + const factory = getOrCreateFactory(event.address); const token0 = getOrCreateToken(params.token0); const token1 = getOrCreateToken(params.token1); const pair = new Pair(params.pair); + pair.factory = factory.id; + pair.version = factory.version; pair.token0 = token0.id; pair.token1 = token1.id; pair.reserve0 = ZERO_BD; @@ -62,7 +64,7 @@ export function handlePairCreated(event: PairCreated): void { export function handleDefaultFeesSet(event: DefaultFeesSet): void { const params = event.params; - const factory = getOrCreateFactory(); + const factory = getOrCreateFactory(event.address); factory.protocolFee = basisPointToBigDecimal(params.fees.protocolFee); factory.lpFee = basisPointToBigDecimal(params.fees.lpFee); factory.save(); @@ -71,7 +73,7 @@ export function handleDefaultFeesSet(event: DefaultFeesSet): void { export function handleProtocolFeeBeneficiarySet( event: ProtocolFeeBeneficiarySet ): void { - const factory = getOrCreateFactory(); + const factory = getOrCreateFactory(event.address); factory.protocolFeeBeneficiary = event.params.beneficiary; factory.save(); } @@ -89,7 +91,7 @@ export function handleLpFeesSet(event: LpFeesSet): void { if (params.overrideFee) { pair.lpFee = basisPointToBigDecimal(params.lpFee); } else { - const factory = getOrCreateFactory(); + const factory = getOrCreateFactory(event.address); pair.lpFee = factory.lpFee; } @@ -110,7 +112,7 @@ export function handleProtocolFeesSet(event: ProtocolFeesSet): void { if (params.overrideFee) { pair.protocolFee = basisPointToBigDecimal(params.protocolFee); } else { - const factory = getOrCreateFactory(); + const factory = getOrCreateFactory(event.address); pair.protocolFee = factory.protocolFee; } diff --git a/subgraphs/magicswapv2/src/mappings/pair.ts b/subgraphs/magicswap/src/mappings/pair.ts similarity index 93% rename from subgraphs/magicswapv2/src/mappings/pair.ts rename to subgraphs/magicswap/src/mappings/pair.ts index cce2b56f..59957c29 100644 --- a/subgraphs/magicswapv2/src/mappings/pair.ts +++ b/subgraphs/magicswap/src/mappings/pair.ts @@ -14,7 +14,9 @@ import { TWO_BD, ZERO_BI } from "../const"; import { ONE_BI } from "../const"; import { getDerivedMagic, + getMagicUSD, getOrCreateFactory, + getOrCreateGlobal, getOrCreateLiquidityPosition, getOrCreateTransaction, getOrCreateUser, @@ -48,8 +50,6 @@ export function handleBurn(event: Burn): void { return; } - const factory = getOrCreateFactory(); - let amount0 = tokenAmountToBigDecimal(token0, params.amount0); if (token0.isNFT) { amount0 = amount0.truncate(0); @@ -63,7 +63,7 @@ export function handleBurn(event: Burn): void { const amountUSD = amount0 .times(token0.derivedMAGIC) .plus(amount1.times(token1.derivedMAGIC)) - .times(factory.magicUSD); + .times(getMagicUSD()); // Update Token 0 token0.txCount = token0.txCount.plus(ONE_BI); @@ -78,6 +78,7 @@ export function handleBurn(event: Burn): void { pair.save(); // Update Factory + const factory = getOrCreateFactory(pair.factory); factory.reserveUSD = factory.reserveUSD.minus(amountUSD); if (token0.isNFT) { factory.reserveNFT = factory.reserveNFT.minus(amount0); @@ -89,7 +90,7 @@ export function handleBurn(event: Burn): void { factory.save(); // Update time interval stats - updateDayData(event.block.timestamp); + updateDayData(factory, event.block.timestamp); updatePairDayData(pair, event.block.timestamp); // Update Transaction @@ -130,14 +131,12 @@ export function handleMint(event: Mint): void { return; } - const factory = getOrCreateFactory(); - const amount0 = tokenAmountToBigDecimal(token0, params.amount0); const amount1 = tokenAmountToBigDecimal(token1, params.amount1); const amountUSD = amount0 .times(token0.derivedMAGIC) .plus(amount1.times(token1.derivedMAGIC)) - .times(factory.magicUSD); + .times(getMagicUSD()); // Update Token 0 token0.txCount = token0.txCount.plus(ONE_BI); @@ -152,6 +151,7 @@ export function handleMint(event: Mint): void { pair.save(); // Update Factory + const factory = getOrCreateFactory(pair.factory); factory.reserveUSD = factory.reserveUSD.plus(amountUSD); if (token0.isNFT) { factory.reserveNFT = factory.reserveNFT.plus(amount0); @@ -163,7 +163,7 @@ export function handleMint(event: Mint): void { factory.save(); // Update time interval stats - updateDayData(event.block.timestamp); + updateDayData(factory, event.block.timestamp); updatePairDayData(pair, event.block.timestamp); // Update Transaction @@ -209,11 +209,10 @@ export function handleSwap(event: Swap): void { params.amount1In.plus(params.amount1Out) ); - const factory = getOrCreateFactory(); - + const magicUSD = getMagicUSD(); const isAmount1Out = params.amount1Out.gt(ZERO_BI); - const amount0Usd = amount0.times(token0.derivedMAGIC).times(factory.magicUSD); - const amount1Usd = amount1.times(token1.derivedMAGIC).times(factory.magicUSD); + const amount0Usd = amount0.times(token0.derivedMAGIC).times(magicUSD); + const amount1Usd = amount1.times(token1.derivedMAGIC).times(magicUSD); const amountUSD = isAmount1Out ? amount0Usd : amount1Usd; // Update Token 0 @@ -236,12 +235,13 @@ export function handleSwap(event: Swap): void { pair.save(); // Update Factory + const factory = getOrCreateFactory(pair.factory); factory.volumeUSD = factory.volumeUSD.plus(amountUSD); factory.txCount = factory.txCount.plus(ONE_BI); factory.save(); // Update time interval stats - const dayData = updateDayData(event.block.timestamp); + const dayData = updateDayData(factory, event.block.timestamp); dayData.volumeUSD = dayData.volumeUSD.plus(amountUSD); dayData.save(); @@ -299,8 +299,6 @@ export function handleSync(event: Sync): void { pair.reserve0 = tokenAmountToBigDecimal(token0, params.reserve0); pair.reserve1 = tokenAmountToBigDecimal(token1, params.reserve1); - const factory = getOrCreateFactory(); - token0.derivedMAGIC = token0.isMAGIC ? pair.reserve1.div(pair.reserve0) : getDerivedMagic(token0); @@ -311,15 +309,16 @@ export function handleSync(event: Sync): void { : getDerivedMagic(token1); token1.save(); + const magicUSD = getMagicUSD(); if (token0.isMAGIC) { - pair.reserveUSD = pair.reserve0.times(factory.magicUSD).times(TWO_BD); + pair.reserveUSD = pair.reserve0.times(magicUSD).times(TWO_BD); } else if (token1.isMAGIC) { - pair.reserveUSD = pair.reserve1.times(factory.magicUSD).times(TWO_BD); + pair.reserveUSD = pair.reserve1.times(magicUSD).times(TWO_BD); } else { pair.reserveUSD = pair.reserve0 .times(token0.derivedMAGIC) .plus(pair.reserve1.times(token1.derivedMAGIC)) - .times(factory.magicUSD); + .times(magicUSD); } pair.save(); diff --git a/subgraphs/magicswapv2/src/mappings/price.ts b/subgraphs/magicswap/src/mappings/price.ts similarity index 54% rename from subgraphs/magicswapv2/src/mappings/price.ts rename to subgraphs/magicswap/src/mappings/price.ts index aff5eee7..c5bce0fc 100644 --- a/subgraphs/magicswapv2/src/mappings/price.ts +++ b/subgraphs/magicswap/src/mappings/price.ts @@ -1,9 +1,9 @@ import { AnswerUpdated } from "../../generated/ChainlinkAggregator/ChainlinkAggregator"; -import { getOrCreateFactory } from "../helpers"; +import { getOrCreateGlobal } from "../helpers"; import { amountToBigDecimal } from "../utils"; export function handleMagicUSDUpdated(event: AnswerUpdated): void { - const factory = getOrCreateFactory(); - factory.magicUSD = amountToBigDecimal(event.params.current, 8); - factory.save(); + const global = getOrCreateGlobal(); + global.magicUSD = amountToBigDecimal(event.params.current, 8); + global.save(); } diff --git a/subgraphs/magicswapv2/src/mappings/router.ts b/subgraphs/magicswap/src/mappings/router.ts similarity index 100% rename from subgraphs/magicswapv2/src/mappings/router.ts rename to subgraphs/magicswap/src/mappings/router.ts diff --git a/subgraphs/magicswapv2/src/mappings/vault.ts b/subgraphs/magicswap/src/mappings/vault.ts similarity index 100% rename from subgraphs/magicswapv2/src/mappings/vault.ts rename to subgraphs/magicswap/src/mappings/vault.ts diff --git a/subgraphs/magicswapv2/src/utils.ts b/subgraphs/magicswap/src/utils.ts similarity index 100% rename from subgraphs/magicswapv2/src/utils.ts rename to subgraphs/magicswap/src/utils.ts diff --git a/subgraphs/magicswapv2/template.yaml b/subgraphs/magicswap/template.yaml similarity index 81% rename from subgraphs/magicswapv2/template.yaml rename to subgraphs/magicswap/template.yaml index d4b7c62e..a797450b 100644 --- a/subgraphs/magicswapv2/template.yaml +++ b/subgraphs/magicswap/template.yaml @@ -50,7 +50,7 @@ dataSources: - event: VaultCreated(string,string,indexed address,indexed uint256,(address,uint8,bool,uint256[])[],address) handler: handleVaultCreated file: ./src/mappings/vault.ts - - name: UniswapV2Factory + - name: MagicswapV2UniswapV2Factory kind: ethereum/contract network: {{ network }} source: @@ -62,7 +62,9 @@ dataSources: apiVersion: 0.0.6 language: wasm/assemblyscript entities: + - Factory - Pair + - Token abis: - name: UniswapV2Factory file: ./abis/UniswapV2Factory.json @@ -91,6 +93,39 @@ dataSources: - event: RoyaltiesFeesSet(indexed address,address,uint256) handler: handleRoyaltiesFeesSet file: ./src/mappings/factory.ts + - name: MagicswapV1UniswapV2Factory + kind: ethereum/contract + network: {{ network }} + source: + address: "{{ magicswapv1_uniswapv2_factory_address }}" + abi: UniswapV2Factory + startBlock: {{ magicswapv1_start_block }} + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + entities: + - Factory + - Pair + - Token + abis: + - name: UniswapV2Factory + file: ./abis/UniswapV2Factory.json + - name: ERC20 + file: ./abis/ERC20.json + eventHandlers: + - event: PairCreated(indexed address,indexed address,address,uint256) + handler: handlePairCreated + calls: + ERC20.name0: ERC20[event.params.token0].name() + ERC20.name1: ERC20[event.params.token1].name() + ERC20.symbol0: ERC20[event.params.token0].symbol() + ERC20.symbol1: ERC20[event.params.token1].symbol() + ERC20.totalSupply0: ERC20[event.params.token0].totalSupply() + ERC20.totalSupply1: ERC20[event.params.token1].totalSupply() + ERC20.decimals0: ERC20[event.params.token0].decimals() + ERC20.decimals1: ERC20[event.params.token1].decimals() + file: ./src/mappings/factory.ts - name: MagicswapV2Router kind: ethereum/contract network: {{ network }} @@ -103,7 +138,7 @@ dataSources: apiVersion: 0.0.6 language: wasm/assemblyscript entities: - - Pair + - Transaction abis: - name: MagicswapV2Router file: ./abis/MagicswapV2Router.json @@ -148,7 +183,12 @@ templates: apiVersion: 0.0.6 language: wasm/assemblyscript entities: + - DayData + - Factory - Pair + - PairDayData + - Token + - Transaction abis: - name: UniswapV2Pair file: ./abis/UniswapV2Pair.json @@ -174,7 +214,9 @@ templates: apiVersion: 0.0.6 language: wasm/assemblyscript entities: - - Token + - Transaction + - TransactionItem + - VaultReserveItem abis: - name: NftVault file: ./abis/NftVault.json diff --git a/subgraphs/magicswapv2/tests/helpers.test.ts b/subgraphs/magicswap/tests/helpers.test.ts similarity index 100% rename from subgraphs/magicswapv2/tests/helpers.test.ts rename to subgraphs/magicswap/tests/helpers.test.ts diff --git a/subgraphs/magicswapv2/tests/helpers/constants.ts b/subgraphs/magicswap/tests/helpers/constants.ts similarity index 100% rename from subgraphs/magicswapv2/tests/helpers/constants.ts rename to subgraphs/magicswap/tests/helpers/constants.ts diff --git a/subgraphs/magicswapv2/tests/helpers/pair.ts b/subgraphs/magicswap/tests/helpers/pair.ts similarity index 100% rename from subgraphs/magicswapv2/tests/helpers/pair.ts rename to subgraphs/magicswap/tests/helpers/pair.ts diff --git a/subgraphs/magicswapv2/tests/helpers/price.ts b/subgraphs/magicswap/tests/helpers/price.ts similarity index 100% rename from subgraphs/magicswapv2/tests/helpers/price.ts rename to subgraphs/magicswap/tests/helpers/price.ts diff --git a/subgraphs/magicswapv2/tests/helpers/token.ts b/subgraphs/magicswap/tests/helpers/token.ts similarity index 100% rename from subgraphs/magicswapv2/tests/helpers/token.ts rename to subgraphs/magicswap/tests/helpers/token.ts diff --git a/subgraphs/magicswapv2/tests/helpers/vault.ts b/subgraphs/magicswap/tests/helpers/vault.ts similarity index 100% rename from subgraphs/magicswapv2/tests/helpers/vault.ts rename to subgraphs/magicswap/tests/helpers/vault.ts diff --git a/subgraphs/magicswapv2/tests/pair.test.ts b/subgraphs/magicswap/tests/pair.test.ts similarity index 100% rename from subgraphs/magicswapv2/tests/pair.test.ts rename to subgraphs/magicswap/tests/pair.test.ts diff --git a/subgraphs/magicswapv2/tests/price.test.ts b/subgraphs/magicswap/tests/price.test.ts similarity index 100% rename from subgraphs/magicswapv2/tests/price.test.ts rename to subgraphs/magicswap/tests/price.test.ts diff --git a/subgraphs/magicswapv2/tests/vault.test.ts b/subgraphs/magicswap/tests/vault.test.ts similarity index 100% rename from subgraphs/magicswapv2/tests/vault.test.ts rename to subgraphs/magicswap/tests/vault.test.ts