Skip to content
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

GrantFund events to use BigDecimal for decimal amounts #47

Merged
merged 3 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ type GrantFund @entity {

type DistributionPeriod @entity {
id: Bytes! # distribution period id converted to Bytes from uint
distributionId: BigInt! # identifies the distribution period
startBlock: BigInt! # block number the distribution period starts
endBlock: BigInt! # block number the distribution period ends
topSlate: FundedSlate # The current top FundedSlate
Expand Down Expand Up @@ -881,8 +882,8 @@ type DelegateChanged @entity(immutable: true) {
type DelegateVotesChanged @entity(immutable: true) {
id: Bytes!
delegate: Bytes! # address
previousBalance: BigInt! # uint256
newBalance: BigInt! # uint256
previousBalance: BigDecimal! # uint256
newBalance: BigDecimal! # uint256
blockNumber: BigInt!
blockTimestamp: BigInt!
transactionHash: Bytes!
Expand All @@ -901,7 +902,7 @@ type DelegateRewardClaimed @entity(immutable: true) {
type FundTreasury @entity(immutable: true) {
id: Bytes!
amount: BigInt! # uint256
treasuryBalance: BigInt! # uint256
treasuryBalance: BigDecimal! # uint256
blockNumber: BigInt!
blockTimestamp: BigInt!
transactionHash: Bytes!
Expand All @@ -921,7 +922,7 @@ type ProposalCreated @entity(immutable: true) {
proposal: Proposal!
proposer: Bytes! # address
targets: [Bytes!]! # address[]
values: [BigInt!]! # uint256[]
values: [BigDecimal!]! # uint256[]
signatures: [String!]! # string[]
calldatas: [Bytes!]! # bytes[]
startBlock: BigInt! # uint256
Expand Down Expand Up @@ -955,7 +956,7 @@ type VoteCast @entity(immutable: true) {
voter: Bytes! # address # TODO: should be Account
proposalId: BigInt! # uint256
support: Int! # uint8
weight: BigInt! # uint256
weight: BigDecimal! # uint256
reason: String! # string
blockNumber: BigInt!
blockTimestamp: BigInt!
Expand Down
6 changes: 2 additions & 4 deletions src/ajna-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
} from "../generated/schema"
import { loadOrCreateAccount } from "./utils/account"
import { addressToBytes, bigIntToBytes, wadToDecimal } from "./utils/convert"
import { getCurrentDistributionId } from "./utils/grants/distribution"
import { loadOrCreateDistributionPeriodVote } from "./utils/grants/voter"

export function handleDelegateChanged(event: DelegateChangedEvent): void {
let entity = new DelegateChanged(
Expand Down Expand Up @@ -39,8 +37,8 @@ export function handleDelegateVotesChanged(
event.transaction.hash.concatI32(event.logIndex.toI32())
)
entity.delegate = event.params.delegate
entity.previousBalance = event.params.previousBalance
entity.newBalance = event.params.newBalance
entity.previousBalance = wadToDecimal(event.params.previousBalance)
entity.newBalance = wadToDecimal(event.params.newBalance)
const changeInBalance = wadToDecimal(event.params.newBalance.minus(event.params.previousBalance))

entity.blockNumber = event.block.number
Expand Down
24 changes: 11 additions & 13 deletions src/grant-fund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
} from "../generated/schema"

import { EXP_18_BD, ONE_BI, THREE_PERCENT_BI, ZERO_ADDRESS, ZERO_BD, ZERO_BI } from './utils/constants'
import { addressArrayToBytesArray, addressToBytes, bigIntToBytes, bytesToBigInt, wadToDecimal } from "./utils/convert"
import { addressArrayToBytesArray, addressToBytes, bigIntArrayToBigDecimalArray, bigIntToBytes, bytesToBigInt, wadToDecimal } from "./utils/convert"
import { getProposalParamsId, getProposalsInSlate, loadOrCreateProposal, removeProposalFromList } from './utils/grants/proposal'
import { getCurrentDistributionId, getCurrentStage, loadOrCreateDistributionPeriod } from './utils/grants/distribution'
import { getFundingStageVotingPower, getFundingVoteId, getFundingVotingPowerUsed, getScreeningStageVotingPower, getScreeningVoteId, loadOrCreateDistributionPeriodVote } from './utils/grants/voter'
Expand All @@ -51,7 +51,7 @@ export function handleDelegateRewardClaimed(
const rewardsClaimed = wadToDecimal(event.params.rewardClaimed)

// update DistributionPeriod entity
const distributionId = bigIntToBytes(getCurrentDistributionId(event.address))
const distributionId = getCurrentDistributionId(event.address)
const distributionPeriod = loadOrCreateDistributionPeriod(distributionId)
distributionPeriod.delegationRewardsClaimed = distributionPeriod.delegationRewardsClaimed.plus(rewardsClaimed)

Expand All @@ -60,7 +60,7 @@ export function handleDelegateRewardClaimed(
grantFund.treasury = grantFund.treasury.minus(rewardsClaimed)
grantFund.totalDelegationRewardsClaimed = grantFund.totalDelegationRewardsClaimed.plus(rewardsClaimed)

delegateRewardClaimed.distribution = distributionId
delegateRewardClaimed.distribution = distributionPeriod.id

// update Account entity
const accountId = addressToBytes(event.params.delegateeAddress)
Expand All @@ -79,7 +79,7 @@ export function handleFundTreasury(event: FundTreasuryEvent): void {
event.transaction.hash.concatI32(event.logIndex.toI32())
)
fundTreasury.amount = event.params.amount
fundTreasury.treasuryBalance = event.params.treasuryBalance
fundTreasury.treasuryBalance = wadToDecimal(event.params.treasuryBalance)

fundTreasury.blockNumber = event.block.number
fundTreasury.blockTimestamp = event.block.timestamp
Expand All @@ -106,13 +106,12 @@ export function handleFundedSlateUpdated(event: FundedSlateUpdatedEvent): void {
fundedSlateUpdated.transactionHash = event.transaction.hash

// update DistributionPeriod entity
const distributionId = bigIntToBytes(event.params.distributionId)
const distributionPeriod = loadOrCreateDistributionPeriod(distributionId)
const distributionPeriod = loadOrCreateDistributionPeriod(event.params.distributionId)
distributionPeriod.topSlate = event.params.fundedSlateHash

// create FundedSlate entity
const fundedSlate = new FundedSlate(fundedSlateUpdated.fundedSlateHash_) as FundedSlate
fundedSlate.distribution = distributionId
fundedSlate.distribution = distributionPeriod.id
fundedSlate.updateBlock = event.block.number

// get the list of proposals in the slate
Expand Down Expand Up @@ -149,7 +148,7 @@ export function handleProposalCreated(event: ProposalCreatedEvent): void {
)
proposalCreated.proposer = event.params.proposer
proposalCreated.targets = addressArrayToBytesArray(event.params.targets)
proposalCreated.values = event.params.values
proposalCreated.values = bigIntArrayToBigDecimalArray(event.params.values)
proposalCreated.signatures = event.params.signatures
proposalCreated.calldatas = event.params.calldatas
proposalCreated.startBlock = event.params.startBlock
Expand Down Expand Up @@ -240,8 +239,7 @@ export function handleDistributionPeriodStarted(
const distributionStarted = new DistributionPeriodStarted(
event.transaction.hash.concatI32(event.logIndex.toI32())
)
const distributionId = bigIntToBytes(event.params.distributionId)
distributionStarted.distribution = distributionId
distributionStarted.distribution = bigIntToBytes(event.params.distributionId)
distributionStarted.startBlock = event.params.startBlock
distributionStarted.endBlock = event.params.endBlock

Expand All @@ -250,7 +248,7 @@ export function handleDistributionPeriodStarted(
distributionStarted.transactionHash = event.transaction.hash

// create DistributionPeriod entities
const distributionPeriod = loadOrCreateDistributionPeriod(distributionId)
const distributionPeriod = loadOrCreateDistributionPeriod(event.params.distributionId)
distributionPeriod.startBlock = distributionStarted.startBlock
distributionPeriod.endBlock = distributionStarted.endBlock

Expand All @@ -275,7 +273,7 @@ export function handleVoteCast(event: VoteCastEvent): void {
voteCast.voter = event.params.voter
voteCast.proposalId = event.params.proposalId
voteCast.support = event.params.support
voteCast.weight = event.params.weight
voteCast.weight = wadToDecimal(event.params.weight)
voteCast.reason = event.params.reason

voteCast.blockNumber = event.block.number
Expand All @@ -294,7 +292,7 @@ export function handleVoteCast(event: VoteCastEvent): void {
const distributionPeriod = DistributionPeriod.load(distributionId) as DistributionPeriod

// load voter's distributionPeriodVotes
const distributionPeriodVote = loadOrCreateDistributionPeriodVote(distributionPeriod.id, voter.id)
const distributionPeriodVote = loadOrCreateDistributionPeriodVote(distributionPeriod.distributionId, voter.id)

// check stage of proposal
const stage = getCurrentStage(voteCast.blockNumber, distributionPeriod)
Expand Down
8 changes: 8 additions & 0 deletions src/utils/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ export function bigIntArrayToIntArray(indexes: BigInt[]): i32[] {
return retval
}

export function bigIntArrayToBigDecimalArray(indexes: BigInt[]): BigDecimal[] {
const retval: BigDecimal[] = [];
for (let i=0; i<indexes.length; ++i) {
retval.push(wadToDecimal(indexes[i]))
}
return retval
}

export function indexToPrice(index: u32): BigDecimal {
const bucketIndex = MAX_BUCKET_INDEX - index;
assert(bucketIndex >= MIN_BUCKET_INDEX && bucketIndex <= MAX_BUCKET_INDEX, 'Invalid bucket index')
Expand Down
10 changes: 6 additions & 4 deletions src/utils/grants/distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GrantFund } from "../../../generated/GrantFund/GrantFund"
import { DistributionPeriod } from "../../../generated/schema"

import { FUNDING_PERIOD_LENGTH, SCREENING_PERIOD_LENGTH, ONE_BI, ZERO_BD, ZERO_BI, CHALLENGE_PERIOD_LENGTH } from "../constants"
import { bigIntToBytes } from "../convert"
import { bigIntToBytes, bytesToBigInt } from "../convert"

export function getDistributionIdAtBlock(blockNumber: BigInt, grantFundAddress: Address): BigInt | null {
const currentDistributionId = getCurrentDistributionId(grantFundAddress)
Expand Down Expand Up @@ -38,11 +38,13 @@ export function getCurrentStage(currentBlockNumber: BigInt, distributionPeriod:
}
}

export function loadOrCreateDistributionPeriod(distributionId: Bytes): DistributionPeriod {
let distributionPeriod = DistributionPeriod.load(distributionId)
export function loadOrCreateDistributionPeriod(distributionId: BigInt): DistributionPeriod {
let id = bigIntToBytes(distributionId)
let distributionPeriod = DistributionPeriod.load(id)
if (distributionPeriod == null) {
// create new distributionPeriod if one hasn't already been stored
distributionPeriod = new DistributionPeriod(distributionId) as DistributionPeriod
distributionPeriod = new DistributionPeriod(id) as DistributionPeriod
distributionPeriod.distributionId = distributionId
distributionPeriod.startBlock = ZERO_BI
distributionPeriod.endBlock = ZERO_BI
distributionPeriod.topSlate = Bytes.empty()
Expand Down
7 changes: 4 additions & 3 deletions src/utils/grants/voter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DistributionPeriodVote, FundingVote } from "../../../generated/schema"
import { GrantFund } from "../../../generated/GrantFund/GrantFund"

import { EXP_18_BD, ZERO_BD, ZERO_BI } from "../constants"
import { wadToDecimal } from "../convert"
import { bigIntToBytes, wadToDecimal } from "../convert"
import { loadOrCreateDistributionPeriod } from "./distribution"

export function getDistributionPeriodVoteId(distributionPeriodId: Bytes, voterId: Bytes): Bytes {
Expand Down Expand Up @@ -77,7 +77,8 @@ export function loadOrCreateFundingVote(fundingVoteId: Bytes): FundingVote {
return fundingVote
}

export function loadOrCreateDistributionPeriodVote(distributionPeriodId: Bytes, voterId: Bytes): DistributionPeriodVote {
export function loadOrCreateDistributionPeriodVote(distributionId: BigInt, voterId: Bytes): DistributionPeriodVote {
const distributionPeriodId = bigIntToBytes(distributionId)
const distributionPeriodVotesId = getDistributionPeriodVoteId(distributionPeriodId, voterId)
let distributionPeriodVotes = DistributionPeriodVote.load(distributionPeriodVotesId)
if (distributionPeriodVotes == null) {
Expand All @@ -91,7 +92,7 @@ export function loadOrCreateDistributionPeriodVote(distributionPeriodId: Bytes,
distributionPeriodVotes.fundingVotes = []

// add to DistributionPeriod entity
const distributionPeriod = loadOrCreateDistributionPeriod(distributionPeriodId)
const distributionPeriod = loadOrCreateDistributionPeriod(distributionId)
distributionPeriod.votes = distributionPeriod.votes.concat([distributionPeriodVotesId])
distributionPeriod.save()
}
Expand Down
2 changes: 1 addition & 1 deletion tests/grant-fund.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ describe("Grant Fund assertions", () => {
"FundTreasury",
`0xa16081f360e3847006db660bae1c6d1b2e17ec2a01000000`,
"treasuryBalance",
`${treasuryBalance}`
`${wadToDecimal(treasuryBalance)}`
);
});

Expand Down