Skip to content

Commit

Permalink
merge magicswap subgraphs
Browse files Browse the repository at this point in the history
  • Loading branch information
alecananian committed Aug 9, 2024
1 parent ba301ff commit 17cc1f8
Show file tree
Hide file tree
Showing 33 changed files with 173 additions and 84 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ on:
- bridgeworld-kote
- bridgeworld-recruits
- governance-staking
- magicswap-exchange
- magicswapv2
- magicswap
- smol
version:
description: Subgraph version
Expand Down
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
62 changes: 40 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/config/src/arbitrum-sepolia.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
6 changes: 3 additions & 3 deletions packages/config/src/arbitrum.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -91,6 +104,8 @@ type VaultReserveItem @entity {
type Pair @entity {
id: Bytes!

factory: Factory!
version: Version!
token0: Token!
token1: Token!
reserve0: BigDecimal!
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
Collection,
DayData,
Factory,
Global,
LiquidityPosition,
Pair,
PairDayData,
Expand All @@ -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();
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -264,3 +277,8 @@ export const updatePairDayData = (

return pairDayData;
};

export const getMagicUSD = (): BigDecimal => {
const global = getOrCreateGlobal();
return global.magicUSD;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
}
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
Loading

0 comments on commit 17cc1f8

Please sign in to comment.