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 2 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
4 changes: 2 additions & 2 deletions src/ajna-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,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
8 changes: 4 additions & 4 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 Down Expand Up @@ -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 Down Expand Up @@ -149,7 +149,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 @@ -275,7 +275,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 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
3 changes: 2 additions & 1 deletion 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 @@ -43,6 +43,7 @@ export function loadOrCreateDistributionPeriod(distributionId: Bytes): Distribut
if (distributionPeriod == null) {
// create new distributionPeriod if one hasn't already been stored
distributionPeriod = new DistributionPeriod(distributionId) as DistributionPeriod
distributionPeriod.distributionId = bytesToBigInt(distributionId)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to just pass the raw distributionId to this method instead of converting back - wondering how accurate the bytes conversion is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that, but there's a local distributionId bytes in all the callers which is used for other purposes. I'll create another local distributionIdRaw on the stack and see how that looks.

Copy link
Contributor Author

@EdNoepel EdNoepel Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this as PR #48.

distributionPeriod.startBlock = ZERO_BI
distributionPeriod.endBlock = ZERO_BI
distributionPeriod.topSlate = Bytes.empty()
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