Skip to content

Commit

Permalink
eliminated grantFundAddressTable
Browse files Browse the repository at this point in the history
  • Loading branch information
EdNoepel committed Jul 17, 2023
1 parent d3a79e3 commit 8654ccb
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 157 deletions.
20 changes: 10 additions & 10 deletions src/grant-fund.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, BigInt, Bytes, ethereum, log } from '@graphprotocol/graph-ts'
import { Address, Bytes, ethereum, log } from '@graphprotocol/graph-ts'

import {
DelegateRewardClaimed as DelegateRewardClaimedEvent,
Expand Down Expand Up @@ -27,9 +27,9 @@ import {

import { ZERO_ADDRESS, ZERO_BD } from './utils/constants'
import { addressArrayToBytesArray, addressToBytes, bigIntToBytes, bytesToBigInt, wadToDecimal } from "./utils/convert"
import { getProposalParamsId, getProposalsInSlate, loadOrCreateProposal, removeProposalFromList } from './utils/grants/proposal'
import { getProposalParamsId, getProposalsInSlate, removeProposalFromList } from './utils/grants/proposal'
import { getCurrentDistributionId, getCurrentStage, loadOrCreateDistributionPeriod } from './utils/grants/distribution'
import { getDistributionPeriodVoteId, getFundingStageVotingPower, getFundingVoteId, getScreeningStageVotingPower, getScreeningVoteId, loadOrCreateDistributionPeriodVote, loadOrCreateVoter } from './utils/grants/voter'
import { getFundingStageVotingPower, getFundingVoteId, getScreeningStageVotingPower, getScreeningVoteId, loadOrCreateDistributionPeriodVote, loadOrCreateVoter } from './utils/grants/voter'
import { loadOrCreateGrantFund } from './utils/grants/fund'

export function handleDelegateRewardClaimed(
Expand All @@ -48,7 +48,7 @@ export function handleDelegateRewardClaimed(
const rewardsClaimed = wadToDecimal(event.params.rewardClaimed)

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

Expand Down Expand Up @@ -108,7 +108,7 @@ export function handleFundedSlateUpdated(event: FundedSlateUpdatedEvent): void {
fundedSlate.updateBlock = event.block.number

// get the list of proposals in the slate
const proposalsInSlate = getProposalsInSlate(fundedSlateUpdated.distributionId_)
const proposalsInSlate = getProposalsInSlate(event.address, fundedSlateUpdated.distributionId_)
const proposals = fundedSlate.proposals
for (let i = 0; i < proposalsInSlate.length; i++) {
const proposalId = proposalsInSlate[i]
Expand Down Expand Up @@ -188,7 +188,7 @@ export function handleProposalCreated(event: ProposalCreatedEvent): void {
const grantFund = loadOrCreateGrantFund(event.address)

// update distribution entity
const distributionId = getCurrentDistributionId()
const distributionId = getCurrentDistributionId(event.address)
const distributionPeriod = loadOrCreateDistributionPeriod(bigIntToBytes(distributionId))
distributionPeriod.proposals = distributionPeriod.proposals.concat([proposal.id])
distributionPeriod.totalTokensRequested = distributionPeriod.totalTokensRequested.plus(proposal.totalTokensRequested)
Expand Down Expand Up @@ -260,7 +260,7 @@ export function handleDistributionPeriodStarted(
const grantFund = loadOrCreateGrantFund(event.address)
grantFund.distributionPeriods = grantFund.distributionPeriods.concat([distributionPeriod.id])

log.info("distribution period {} started", [distributionId.toString()])
log.info("distribution period {} started", [distributionId.toHexString()])

// save entities to store
distributionPeriod.save()
Expand Down Expand Up @@ -291,7 +291,7 @@ export function handleVoteCast(event: VoteCastEvent): void {
if (proposal != null) {
// TODO: need to be able to access the distributionId at that block height or call getDistributionIdAtBlock()?
// load distribution entity
const distributionId = bigIntToBytes(getCurrentDistributionId())
const distributionId = bigIntToBytes(getCurrentDistributionId(event.address))
const distributionPeriod = DistributionPeriod.load(distributionId) as DistributionPeriod

// load voter's distributionPeriodVotes
Expand All @@ -316,7 +316,7 @@ export function handleVoteCast(event: VoteCastEvent): void {

// update voter's distributionPeriodVote entity if it hasn't been recorded yet
if (distributionPeriodVote.screeningStageVotingPower === ZERO_BD) {
distributionPeriodVote.screeningStageVotingPower = getScreeningStageVotingPower(bytesToBigInt(distributionId), Address.fromBytes(voter.id))
distributionPeriodVote.screeningStageVotingPower = getScreeningStageVotingPower(event.address, bytesToBigInt(distributionId), Address.fromBytes(voter.id))
}

// add additional screening votes to voter's distributionPeriodVote entity
Expand All @@ -337,7 +337,7 @@ export function handleVoteCast(event: VoteCastEvent): void {

// update voter's distributionPeriodVote entity if it hasn't been recorded yet
if (distributionPeriodVote.screeningStageVotingPower === ZERO_BD) {
distributionPeriodVote.fundingStageVotingPower = getFundingStageVotingPower(bytesToBigInt(distributionId), Address.fromBytes(voter.id))
distributionPeriodVote.fundingStageVotingPower = getFundingStageVotingPower(event.address, bytesToBigInt(distributionId), Address.fromBytes(voter.id))
}

// save fundingVote to the store
Expand Down
4 changes: 0 additions & 4 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ poolInfoUtilsAddressTable.set('ganache', Address.fromString('0x4f05DA51eAAB00e58
export const positionManagerAddressTable = new TypedMap<string, Address>()
positionManagerAddressTable.set('goerli', Address.fromString('0x23E2EFF19bd50BfCF0364B7dCA01004D5cce41f9'))
positionManagerAddressTable.set('ganache', Address.fromString('0x6c5c7fD98415168ada1930d44447790959097482'))
// TODO: eliminate; should be able to get from event
export const grantFundAddressTable = new TypedMap<string, Address>()
grantFundAddressTable.set('goerli', Address.fromString('0x881b4dFF6C72babA6f5eA60f34A61410c1EA1ec2'))
grantFundAddressTable.set('ganache', Address.fromString('0xE340B87CEd1af1AbE1CE8D617c84B7f168e3b18b'))

// GrantFund constants
export const CHALLENGE_PERIOD_LENGTH = BigInt.fromI32(50400)
Expand Down
13 changes: 7 additions & 6 deletions src/utils/grants/distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@ import { Address, BigInt, Bytes, dataSource, log } from "@graphprotocol/graph-ts
import { GrantFund } from "../../../generated/GrantFund/GrantFund"
import { DistributionPeriod } from "../../../generated/schema"

import { FUNDING_PERIOD_LENGTH, ONE_BI, ZERO_BD, ZERO_BI, grantFundAddressTable } from "../constants"
import { FUNDING_PERIOD_LENGTH, ONE_BI, ZERO_BD, ZERO_BI } from "../constants"
import { bigIntToBytes } from "../convert"

export function getDistributionIdAtBlock(blockNumber: BigInt): BigInt {
const currentDistributionId = getCurrentDistributionId()
export function getDistributionIdAtBlock(blockNumber: BigInt, grantFundAddress: Address): BigInt | null {
const currentDistributionId = getCurrentDistributionId(grantFundAddress)
for (let i = currentDistributionId.toI32(); i > 0; i--) {
const distributionPeriod = DistributionPeriod.load(Bytes.fromI32(i))!

if (blockNumber > distributionPeriod.startBlock && blockNumber < distributionPeriod.endBlock) {
return BigInt.fromI32(i)
}
}
return null
}

export function getCurrentDistributionId(): BigInt {
const grantFundAddress = grantFundAddressTable.get(dataSource.network())!
export function getCurrentDistributionId(grantFundAddress: Address): BigInt {
const grantFundContract = GrantFund.bind(grantFundAddress)
const distributionIdResult = grantFundContract.getDistributionId()
log.info("current distribution id is {}", [distributionIdResult.toString()])
return BigInt.fromI32(distributionIdResult)
}

Expand Down Expand Up @@ -49,7 +50,7 @@ export function loadOrCreateDistributionPeriod(distributionId: Bytes): Distribut
distributionPeriod.screeningVotesCast = ZERO_BD
distributionPeriod.proposals = []
distributionPeriod.totalTokensRequested = ZERO_BD
log.info("created new distribution period {}", [distributionId.toString()])
log.info("created new distribution period {}", [distributionId.toHexString()])
}
return distributionPeriod
}
5 changes: 2 additions & 3 deletions src/utils/grants/proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Address, BigInt, Bytes, dataSource, log } from "@graphprotocol/graph-ts
import { Proposal, ProposalParams } from "../../../generated/schema"
import { GrantFund } from "../../../generated/GrantFund/GrantFund"

import { ZERO_ADDRESS, ONE_BI, ZERO_BD, ZERO_BI, grantFundAddressTable } from "../constants"
import { ZERO_ADDRESS, ONE_BI, ZERO_BD, ZERO_BI } from "../constants"
import { bytesToBigInt } from "../convert"

export function getProposalParamsId(proposalId: Bytes, paramIndex: number): Bytes {
Expand Down Expand Up @@ -41,8 +41,7 @@ export function removeProposalFromList(proposalId: Bytes, proposalList: Array<By
/*** Contract Calls ***/
/**********************/

export function getProposalsInSlate(distributionId: BigInt): Array<BigInt> {
const grantFundAddress = grantFundAddressTable.get(dataSource.network())!
export function getProposalsInSlate(grantFundAddress: Address, distributionId: BigInt): Array<BigInt> {
const grantFundContract = GrantFund.bind(grantFundAddress)
const getProposalsInSlateResult = grantFundContract.getTopTenProposals(distributionId.toI32())

Expand Down
8 changes: 3 additions & 5 deletions src/utils/grants/voter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Address, BigDecimal, BigInt, Bytes, dataSource } from "@graphprotocol/g
import { DistributionPeriodVote, Voter } from "../../../generated/schema"
import { GrantFund } from "../../../generated/GrantFund/GrantFund"

import { ZERO_BD, ZERO_BI, grantFundAddressTable } from "../constants"
import { ZERO_BD, ZERO_BI } from "../constants"
import { wadToDecimal } from "../convert"

export function getDistributionPeriodVoteId(distributionPeriodId: Bytes, voterId: Bytes): Bytes {
Expand Down Expand Up @@ -55,16 +55,14 @@ export function loadOrCreateDistributionPeriodVote(distributionPeriodId: Bytes,
/*** Contract Calls ***/
/**********************/

export function getFundingStageVotingPower(distributionId: BigInt, voter: Address): BigDecimal {
const grantFundAddress = grantFundAddressTable.get(dataSource.network())!
export function getFundingStageVotingPower(grantFundAddress: Address, distributionId: BigInt, voter: Address): BigDecimal {
const grantFundContract = GrantFund.bind(grantFundAddress)
const votingPower = grantFundContract.getVotesFunding(distributionId.toI32(), voter)

return wadToDecimal(votingPower)
}

export function getScreeningStageVotingPower(distributionId: BigInt, voter: Address): BigDecimal {
const grantFundAddress = grantFundAddressTable.get(dataSource.network())!
export function getScreeningStageVotingPower(grantFundAddress: Address, distributionId: BigInt, voter: Address): BigDecimal {
const grantFundContract = GrantFund.bind(grantFundAddress)
const votingPower = grantFundContract.getVotesScreening(distributionId.toI32(), voter)

Expand Down
Loading

0 comments on commit 8654ccb

Please sign in to comment.