Skip to content

Commit

Permalink
handle magicswap staking contract events
Browse files Browse the repository at this point in the history
  • Loading branch information
alecananian committed Oct 10, 2024
1 parent 7e4d79d commit 596a06c
Show file tree
Hide file tree
Showing 7 changed files with 402 additions and 144 deletions.
320 changes: 177 additions & 143 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"devDependencies": {
"@goldskycom/cli": "^9.1.2",
"@graphprotocol/graph-cli": "^0.80.0",
"@graphprotocol/graph-cli": "^0.86.0",
"@trivago/prettier-plugin-sort-imports": "^4.2.0",
"husky": "^9.0.10",
"lint-staged": "^15.0.2",
Expand Down
1 change: 1 addition & 0 deletions packages/config/src/arbitrum-sepolia.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"magicswapv2_nft_vault_factory_v2_address": "0x496f5f13816cdddd6934be1ca6a579b3838889ce",
"magicswapv2_uniswapv2_factory_address": "0xbdfab57b9446ab3512579b7a901b74fa49fda752",
"magicswapv2_router_address": "0xc8d7261feb0b648bc489224a18d6cdc905a0c5ab",
"magicswapv2_staking_address": "0xb39b382b81aedd8c5003ff141b51c6398dba82ff",
"magic_usd_price_feed_address": "0xe6616a09e029d477183cba6bb6133643b522c15c",
"weth_address": "0x980b62da83eff3d4576c647993b0c1d7faf17c73"
}
1 change: 1 addition & 0 deletions subgraphs/magicswap/abis/StakingContract.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[],"name":"AlreadySubscribed","type":"error"},{"inputs":[],"name":"AlreadyUnsubscribed","type":"error"},{"inputs":[{"internalType":"bytes","name":"innerErorr","type":"bytes"}],"name":"BatchError","type":"error"},{"inputs":[],"name":"FullyPacked","type":"error"},{"inputs":[],"name":"IncentiveOverflow","type":"error"},{"inputs":[],"name":"InsufficientStakedAmount","type":"error"},{"inputs":[],"name":"InvalidIndex","type":"error"},{"inputs":[],"name":"InvalidInput","type":"error"},{"inputs":[],"name":"InvalidTimeFrame","type":"error"},{"inputs":[],"name":"NoToken","type":"error"},{"inputs":[],"name":"NonZero","type":"error"},{"inputs":[],"name":"NotStaked","type":"error"},{"inputs":[],"name":"NotSubscribed","type":"error"},{"inputs":[],"name":"OnlyCreator","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"id","type":"uint256"},{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Claim","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token","type":"address"},{"indexed":true,"internalType":"address","name":"rewardToken","type":"address"},{"indexed":true,"internalType":"address","name":"creator","type":"address"},{"indexed":false,"internalType":"uint256","name":"id","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"startTime","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"endTime","type":"uint256"}],"name":"IncentiveCreated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"id","type":"uint256"},{"indexed":false,"internalType":"int256","name":"changeAmount","type":"int256"},{"indexed":false,"internalType":"uint256","name":"newStartTime","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"newEndTime","type":"uint256"}],"name":"IncentiveUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token","type":"address"},{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Stake","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"id","type":"uint256"},{"indexed":true,"internalType":"address","name":"user","type":"address"}],"name":"Subscribe","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token","type":"address"},{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Unstake","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"id","type":"uint256"},{"indexed":true,"internalType":"address","name":"user","type":"address"}],"name":"Unsubscribe","type":"event"},{"inputs":[{"internalType":"uint256","name":"incentiveId","type":"uint256"}],"name":"accrueRewards","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes[]","name":"datas","type":"bytes[]"}],"name":"batch","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256[]","name":"incentiveIds","type":"uint256[]"}],"name":"claimAllRewards","outputs":[{"internalType":"uint256[]","name":"rewards","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256[]","name":"incentiveIds","type":"uint256[]"}],"name":"claimRewards","outputs":[{"internalType":"uint256[]","name":"rewards","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"address","name":"rewardToken","type":"address"},{"internalType":"uint112","name":"rewardAmount","type":"uint112"},{"internalType":"uint32","name":"startTime","type":"uint32"},{"internalType":"uint32","name":"endTime","type":"uint32"},{"internalType":"bool","name":"isRewardRounded","type":"bool"}],"name":"createIncentive","outputs":[{"internalType":"uint256","name":"incentiveId","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"incentiveCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"incentives","outputs":[{"internalType":"address","name":"creator","type":"address"},{"internalType":"address","name":"token","type":"address"},{"internalType":"address","name":"rewardToken","type":"address"},{"internalType":"uint32","name":"endTime","type":"uint32"},{"internalType":"bool","name":"isRewardRounded","type":"bool"},{"internalType":"uint256","name":"rewardPerLiquidity","type":"uint256"},{"internalType":"uint32","name":"lastRewardTime","type":"uint32"},{"internalType":"uint112","name":"rewardRemaining","type":"uint112"},{"internalType":"uint112","name":"liquidityStaked","type":"uint112"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"rewardPerLiquidityLast","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint112","name":"amount","type":"uint112"},{"internalType":"uint256[]","name":"incentiveIds","type":"uint256[]"},{"internalType":"bool","name":"transferExistingRewards","type":"bool"}],"name":"stakeAndSubscribeToIncentives","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint112","name":"amount","type":"uint112"},{"internalType":"bool","name":"transferExistingRewards","type":"bool"}],"name":"stakeToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"incentiveId","type":"uint256"}],"name":"subscribeToIncentive","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint112","name":"amount","type":"uint112"},{"internalType":"bool","name":"transferExistingRewards","type":"bool"}],"name":"unstakeToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"incentiveIndex","type":"uint256"},{"internalType":"bool","name":"ignoreRewards","type":"bool"}],"name":"unsubscribeFromIncentive","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"incentiveId","type":"uint256"},{"internalType":"int112","name":"changeAmount","type":"int112"},{"internalType":"uint32","name":"newStartTime","type":"uint32"},{"internalType":"uint32","name":"newEndTime","type":"uint32"}],"name":"updateIncentive","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"userStakes","outputs":[{"internalType":"uint112","name":"liquidity","type":"uint112"},{"internalType":"uint144","name":"subscribedIncentiveIds","type":"uint144"}],"stateMutability":"view","type":"function"}]
41 changes: 41 additions & 0 deletions subgraphs/magicswap/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ type Pair @entity {
liquidityPositions: [LiquidityPosition!]! @derivedFrom(field: "pair")
hourData: [PairHourData!]! @derivedFrom(field: "pair")
dayData: [PairDayData!]! @derivedFrom(field: "pair")
incentives: [Incentive!]! @derivedFrom(field: "pair")
}

type PairHourData @entity {
Expand Down Expand Up @@ -215,3 +216,43 @@ type TransactionItem @entity(immutable: true) {
tokenId: BigInt!
amount: Int!
}

type Incentive @entity {
id: Bytes!

incentiveId: BigInt!
pair: Pair!
creator: Bytes!
rewardTokenAddress: Bytes!
rewardToken: Token
rewardAmount: BigInt!
isRewardRounded: Boolean!
startTime: BigInt!
endTime: BigInt!
}

type UserStake @entity {
id: Bytes!

user: User!
pair: Pair!
amount: BigInt!
}

type UserIncentive @entity {
id: Bytes!

user: User!
incentive: Incentive!
isSubscribed: Boolean!

claims: [UserIncentiveClaim!]! @derivedFrom(field: "userIncentive")
}

type UserIncentiveClaim @entity(immutable: true) {
id: Bytes!

userIncentive: UserIncentive!
timestamp: BigInt!
amount: BigInt!
}
146 changes: 146 additions & 0 deletions subgraphs/magicswap/src/mappings/staking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import { BigInt, Bytes } from "@graphprotocol/graph-ts";

import {
Claim,
IncentiveCreated,
IncentiveUpdated,
Stake,
Subscribe,
Unstake,
Unsubscribe,
} from "../../generated/StakingContract/StakingContract";
import {
Incentive,
Pair,
Token,
UserIncentive,
UserIncentiveClaim,
UserStake,
} from "../../generated/schema";
import { getOrCreateUser } from "../helpers";

export function handleIncentiveCreated(event: IncentiveCreated): void {
const params = event.params;
const pair = Pair.load(params.token);
if (!pair) {
// Ignoring incentive for unknown token
return;
}

const incentive = new Incentive(Bytes.fromI32(params.id.toI32()));
incentive.incentiveId = params.id;
incentive.pair = pair.id;
incentive.creator = params.creator;
incentive.rewardTokenAddress = params.rewardToken;
incentive.rewardAmount = params.amount;
incentive.isRewardRounded = false;
incentive.startTime = params.startTime;
incentive.endTime = params.endTime;

const rewardToken = Token.load(params.rewardToken);
if (rewardToken) {
incentive.rewardToken = rewardToken.id;
}

incentive.save();
}

export function handleIncentiveUpdated(event: IncentiveUpdated): void {
const params = event.params;
const incentive = Incentive.load(Bytes.fromI32(params.id.toI32()));
if (!incentive) {
// Ignoring update for unknown incentive
return;
}

incentive.rewardAmount = incentive.rewardAmount.plus(params.changeAmount);
incentive.startTime = params.newStartTime;
incentive.endTime = params.newEndTime;
incentive.save();
}

export function handleStake(event: Stake): void {
const params = event.params;
const pair = Pair.load(params.token);
if (!pair) {
// Ignoring stake for unknown token
return;
}

const stakeId = params.token.concat(params.user);
let stake = UserStake.load(stakeId);
if (!stake) {
stake = new UserStake(stakeId);
stake.user = getOrCreateUser(params.user).id;
stake.pair = params.token;
stake.amount = BigInt.zero();
}

stake.amount = stake.amount.plus(params.amount);
stake.save();
}

export function handleUnstake(event: Unstake): void {
const params = event.params;
const stake = UserStake.load(params.token.concat(params.user));
if (!stake) {
// Ignoring unstake for unknown stake
return;
}

stake.amount = stake.amount.minus(params.amount);
stake.save();
}

export function handleSubscribe(event: Subscribe): void {
const params = event.params;
const incentive = Incentive.load(Bytes.fromI32(params.id.toI32()));
if (!incentive) {
// Ignoring subscription for unknown incentive
return;
}

const userIncentiveId = incentive.id.concat(params.user);
let userIncentive = UserIncentive.load(userIncentiveId);
if (!userIncentive) {
userIncentive = new UserIncentive(userIncentiveId);
userIncentive.user = getOrCreateUser(params.user).id;
userIncentive.incentive = incentive.id;
}

userIncentive.isSubscribed = true;
userIncentive.save();
}

export function handleUnsubscribe(event: Unsubscribe): void {
const params = event.params;
const userIncentive = UserIncentive.load(
Bytes.fromI32(params.id.toI32()).concat(params.user)
);
if (!userIncentive) {
// Ignoring unsubscription for unknown user incentive
return;
}

userIncentive.isSubscribed = false;
userIncentive.save();
}

export function handleClaim(event: Claim): void {
const params = event.params;
const userIncentive = UserIncentive.load(
Bytes.fromI32(params.id.toI32()).concat(params.user)
);
if (!userIncentive) {
// Ignoring claim for unknown user incentive
return;
}

const claim = new UserIncentiveClaim(
event.transaction.hash.concatI32(event.transactionLogIndex.toI32())
);
claim.userIncentive = userIncentive.id;
claim.timestamp = event.block.timestamp;
claim.amount = params.amount;
claim.save();
}
35 changes: 35 additions & 0 deletions subgraphs/magicswap/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,41 @@ dataSources:
- event: NFTNFTLiquidityRemoved(indexed address,address,(address,address[],uint256[],uint256[]),(address,address[],uint256[],uint256[]))
handler: handleNftNftLiquidityRemoved
file: ./src/mappings/router.ts
- name: StakingContract
kind: ethereum/contract
network: {{ network }}
source:
address: "{{ magicswapv2_staking_address }}"
abi: StakingContract
startBlock: {{ magicswapv2_start_block }}
mapping:
kind: ethereum/events
apiVersion: 0.0.6
language: wasm/assemblyscript
entities:
- Incentive
- UserIncentive
- UserIncentiveClaim
- UserStake
abis:
- name: StakingContract
file: ./abis/StakingContract.json
eventHandlers:
- event: IncentiveCreated(indexed address,indexed address,indexed address,uint256,uint256,uint256,uint256)
handler: handleIncentiveCreated
- event: IncentiveUpdated(indexed uint256,int256,uint256,uint256)
handler: handleIncentiveUpdated
- event: Stake(indexed address,indexed address,uint256)
handler: handleStake
- event: Unstake(indexed address,indexed address,uint256)
handler: handleUnstake
- event: Subscribe(indexed uint256,indexed address)
handler: handleSubscribe
- event: Unsubscribe(indexed uint256,indexed address)
handler: handleUnsubscribe
- event: Claim(indexed uint256,indexed address,uint256)
handler: handleClaim
file: ./src/mappings/staking.ts
- name: ChainlinkAggregator
kind: ethereum/contract
network: {{ network }}
Expand Down

0 comments on commit 596a06c

Please sign in to comment.