-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
handle magicswap staking contract events
- Loading branch information
1 parent
7e4d79d
commit 596a06c
Showing
7 changed files
with
402 additions
and
144 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters