-
Notifications
You must be signed in to change notification settings - Fork 49
feat(subgraph): add fee token to subgraph #1037
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alcercu
merged 11 commits into
fix(web)/staking-refetch-and-improving-fetching-timing
from
feat(subgraph)/add-fee-token-to-subgraph
Aug 3, 2023
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
35da777
feat(subgraph): add fee token to subgraph
kemuru 4020a85
Merge branch 'dev' into feat(subgraph)/add-fee-token-to-subgraph
kemuru 500d28d
feat: new params in subgraph
nhestrompia b04fd1f
fix: null errors
kemuru 361bc8e
fix: null value in feeToken
nhestrompia 266e704
Merge branch 'fix(web)/staking-refetch-and-improving-fetching-timing'…
alcercu 2ad8518
fix(subgraph): correctly convert fees to eth
alcercu e8801e8
fix(subgraph): avoid creating a feeToken when using native currency
alcercu 327a567
Merge branch 'fix(web)/staking-refetch-and-improving-fetching-timing'…
alcercu d270bdd
Merge branch 'fix(web)/staking-refetch-and-improving-fetching-timing'…
alcercu 877bd68
fix(subgraph): correctly handle feeToken
alcercu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,53 @@ | ||
import { BigInt, Address } from "@graphprotocol/graph-ts"; | ||
import { FeeToken } from "../../generated/schema"; | ||
import { KlerosCore } from "../../generated/KlerosCore/KlerosCore"; | ||
import { ZERO } from "../utils"; | ||
|
||
export function ensureFeeToken(tokenAddress: Address, klerosCoreAddress: Address): FeeToken { | ||
const hexTokenAddress = tokenAddress.toHexString(); | ||
let feeToken = FeeToken.load(hexTokenAddress); | ||
if (!feeToken) { | ||
feeToken = new FeeToken(hexTokenAddress); | ||
feeToken.totalPaid = ZERO; | ||
feeToken.totalPaidInETH = ZERO; | ||
} | ||
const contract = KlerosCore.bind(klerosCoreAddress); | ||
const currencyRate = contract.currencyRates(tokenAddress); | ||
feeToken.accepted = currencyRate.value0; | ||
feeToken.rateInEth = currencyRate.value1; | ||
feeToken.rateDecimals = currencyRate.value2; | ||
feeToken.save(); | ||
return feeToken; | ||
} | ||
|
||
export function updateFeeTokenRate(tokenAddress: Address, klerosCoreAddress: Address): void { | ||
const feeToken = ensureFeeToken(tokenAddress, klerosCoreAddress); | ||
const contract = KlerosCore.bind(klerosCoreAddress); | ||
const currencyRate = contract.currencyRates(tokenAddress); | ||
feeToken.accepted = currencyRate.value0; | ||
feeToken.rateInEth = currencyRate.value1; | ||
feeToken.rateDecimals = currencyRate.value2; | ||
feeToken.save(); | ||
} | ||
|
||
export function updateFeeTokenPaid(tokenAddress: Address, klerosCoreAddress: Address, amount: BigInt): void { | ||
const feeToken = ensureFeeToken(tokenAddress, klerosCoreAddress); | ||
const ethAmount = convertTokenAmountToEth(tokenAddress, amount, klerosCoreAddress); | ||
feeToken.totalPaid = feeToken.totalPaid.plus(amount); | ||
feeToken.totalPaidInETH = feeToken.totalPaidInETH.plus(ethAmount); | ||
feeToken.save(); | ||
} | ||
|
||
export function convertEthToTokenAmount(tokenAddress: Address, eth: BigInt, klerosCoreAddress: Address): BigInt { | ||
const feeToken = ensureFeeToken(tokenAddress, klerosCoreAddress); | ||
return eth.times(BigInt.fromI32(10 ** feeToken.rateDecimals)).div(feeToken.rateInEth); | ||
} | ||
|
||
export function convertTokenAmountToEth( | ||
tokenAddress: Address, | ||
tokenAmount: BigInt, | ||
klerosCoreAddress: Address | ||
): BigInt { | ||
const feeToken = ensureFeeToken(tokenAddress, klerosCoreAddress); | ||
return tokenAmount.times(feeToken.rateInEth).div(BigInt.fromI32(10 ** feeToken.rateDecimals)); | ||
} |
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -1,36 +1,68 @@ | ||
import { Address, BigInt } from "@graphprotocol/graph-ts"; | ||
import { TokenAndETHShift as TokenAndETHShiftEvent } from "../../generated/KlerosCore/KlerosCore"; | ||
import { TokenAndETHShift } from "../../generated/schema"; | ||
import { Court, Dispute, TokenAndETHShift } from "../../generated/schema"; | ||
import { updatePaidETH, updateRedistributedPNK } from "../datapoint"; | ||
import { ZERO } from "../utils"; | ||
import { convertTokenAmountToEth, updateFeeTokenPaid } from "./FeeToken"; | ||
import { resolveUserDispute } from "./User"; | ||
|
||
export function updateTokenAndEthShiftFromEvent(event: TokenAndETHShiftEvent): void { | ||
const jurorAddress = event.params._account.toHexString(); | ||
const disputeID = event.params._disputeID.toString(); | ||
const shiftID = `${jurorAddress}-${disputeID}`; | ||
const shift = TokenAndETHShift.load(shiftID); | ||
|
||
if (!shift) { | ||
createTokenAndEthShiftFromEvent(event); | ||
resolveUserDispute(jurorAddress, ZERO, event.params._feeAmount, disputeID); | ||
return; | ||
const jurorAddress = event.params._account; | ||
const disputeID = event.params._disputeID; | ||
const dispute = Dispute.load(disputeID.toString()); | ||
if (!dispute) return; | ||
const court = Court.load(dispute.court); | ||
if (!court) return; | ||
const roundIndex = event.params._roundID; | ||
const feeTokenAddress = event.params._feeToken; | ||
let shift = ensureTokenAndEthShift(jurorAddress, disputeID, roundIndex, feeTokenAddress); | ||
const feeAmount = event.params._feeAmount; | ||
const pnkAmount = event.params._pnkAmount; | ||
let ethAmount: BigInt; | ||
if (feeTokenAddress.toHexString() === "0x0000000000000000000000000000000000000000") { | ||
updateFeeTokenPaid(feeTokenAddress, event.address, feeAmount); | ||
ethAmount = convertTokenAmountToEth(feeTokenAddress, feeAmount, event.address); | ||
shift.feeTokenAmount = shift.feeTokenAmount.plus(feeAmount); | ||
} else { | ||
ethAmount = feeAmount; | ||
} | ||
|
||
shift.tokenAmount = shift.tokenAmount.plus(event.params._pnkAmount); | ||
const previousFeeAmount = shift.ethAmount; | ||
const newFeeAmount = shift.ethAmount.plus(event.params._feeAmount); | ||
shift.ethAmount = newFeeAmount; | ||
const previousEthAmount = shift.ethAmount; | ||
const newEthAmount = previousEthAmount.plus(ethAmount); | ||
shift.ethAmount = newEthAmount; | ||
resolveUserDispute(jurorAddress.toHexString(), previousEthAmount, newEthAmount, disputeID.toString()); | ||
court.paidETH = court.paidETH.plus(ethAmount); | ||
updatePaidETH(ethAmount, event.block.timestamp); | ||
if (pnkAmount.gt(ZERO)) { | ||
court.paidPNK = court.paidPNK.plus(pnkAmount); | ||
updateRedistributedPNK(pnkAmount, event.block.timestamp); | ||
} | ||
shift.pnkAmount = shift.pnkAmount.plus(pnkAmount); | ||
shift.save(); | ||
resolveUserDispute(jurorAddress, previousFeeAmount, newFeeAmount, disputeID); | ||
court.save(); | ||
} | ||
|
||
export function createTokenAndEthShiftFromEvent(event: TokenAndETHShiftEvent): void { | ||
const jurorAddress = event.params._account.toHexString(); | ||
const disputeID = event.params._disputeID.toString(); | ||
const shiftID = `${jurorAddress}-${disputeID}`; | ||
const shift = new TokenAndETHShift(shiftID); | ||
shift.juror = jurorAddress; | ||
shift.dispute = disputeID; | ||
shift.tokenAmount = event.params._pnkAmount; | ||
shift.ethAmount = event.params._feeAmount; | ||
shift.save(); | ||
export function ensureTokenAndEthShift( | ||
jurorAddress: Address, | ||
disputeID: BigInt, | ||
roundIndex: BigInt, | ||
feeTokenAddress: Address | ||
): TokenAndETHShift { | ||
const shiftID = `${jurorAddress.toHexString()}-${disputeID.toString()}-${roundIndex.toString()}`; | ||
let shift = TokenAndETHShift.load(shiftID); | ||
if (!shift) { | ||
shift = new TokenAndETHShift(shiftID); | ||
if (feeTokenAddress !== Address.fromI32(0)) { | ||
shift.isNativeCurrency = false; | ||
shift.feeToken = feeTokenAddress.toHexString(); | ||
} else { | ||
shift.isNativeCurrency = true; | ||
} | ||
shift.feeTokenAmount = ZERO; | ||
shift.ethAmount = ZERO; | ||
shift.juror = jurorAddress.toHexString(); | ||
shift.dispute = disputeID.toString(); | ||
shift.pnkAmount = ZERO; | ||
shift.save(); | ||
} | ||
return shift; | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.