Skip to content

Commit

Permalink
add funds available to DistributionPeriod; improve treasury tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike committed Jul 27, 2023
1 parent 52775a8 commit fdf5c6e
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 13 deletions.
1 change: 1 addition & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ type DistributionPeriod @entity {
endBlock: BigInt! # block number the distribution period ends
topSlate: FundedSlate # The current top FundedSlate
slatesSubmitted: [FundedSlate!]! # FundedSlate[] slates submitted in the distribution period
fundsAvailable: BigDecimal! # Total ajna tokens available for distribution in the distribution period
delegationRewardsClaimed: BigDecimal! # Total delegation rewards claimed in the distribution period
fundingVotePowerUsed: BigDecimal! # Total funding vote power used
screeningVotesCast: BigDecimal! # number of screening votes cast
Expand Down
13 changes: 8 additions & 5 deletions src/grant-fund.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, Bytes, ethereum } from '@graphprotocol/graph-ts'
import { Address, BigInt, Bytes, ethereum } from '@graphprotocol/graph-ts'

import {
DelegateRewardClaimed as DelegateRewardClaimedEvent,
Expand Down Expand Up @@ -26,12 +26,12 @@ import {
DistributionPeriodVote
} from "../generated/schema"

import { ONE_BI, ZERO_ADDRESS, ZERO_BD } from './utils/constants'
import { ONE_BI, THREE_PERCENT_BI, 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 { getCurrentDistributionId, getCurrentStage, loadOrCreateDistributionPeriod } from './utils/grants/distribution'
import { getFundingStageVotingPower, getFundingVoteId, getFundingVotingPowerUsed, getScreeningStageVotingPower, getScreeningVoteId, loadOrCreateDistributionPeriodVote } from './utils/grants/voter'
import { loadOrCreateGrantFund } from './utils/grants/fund'
import { getTreasury, loadOrCreateGrantFund } from './utils/grants/fund'
import { loadOrCreateAccount } from './utils/account'

export function handleDelegateRewardClaimed(
Expand Down Expand Up @@ -86,8 +86,7 @@ export function handleFundTreasury(event: FundTreasuryEvent): void {

// update GrantFund entity
const grantFund = loadOrCreateGrantFund(event.address)
// TODO: simply set this to treasuryBalance?
grantFund.treasury = grantFund.treasury.plus(wadToDecimal(event.params.amount))
grantFund.treasury = wadToDecimal(getTreasury(event.address))

// save entities to the store
grantFund.save()
Expand Down Expand Up @@ -280,7 +279,11 @@ export function handleDistributionPeriodStarted(

// update GrantFund entity
const grantFund = loadOrCreateGrantFund(event.address)
const treasury = getTreasury(event.address)
grantFund.distributionPeriods = grantFund.distributionPeriods.concat([distributionPeriod.id])
grantFund.treasury = wadToDecimal(treasury)

distributionPeriod.fundsAvailable = wadToDecimal(treasury.times(THREE_PERCENT_BI))

// save entities to store
distributionPeriod.save()
Expand Down
15 changes: 8 additions & 7 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { BigInt, Address, BigDecimal, TypedMap } from '@graphprotocol/graph-ts'
export const ZERO_ADDRESS = Address.fromString('0x0000000000000000000000000000000000000000')

// BigInt constants
export const ZERO_BI = BigInt.zero()
export const ONE_BI = BigInt.fromI32(1)
export const TEN_BI = BigInt.fromI32(10)
export const ONE_PERCENT_BI = BigInt.fromString("10000000000000000") // 0.01 * 1e18
export const FIVE_PERCENT_BI = BigInt.fromString("50000000000000000") // 0.05 * 1e18
export const HALF_WAD_BI = BigInt.fromString("500000000000000000")
export const ONE_WAD_BI = BigInt.fromString("1000000000000000000")
export const ZERO_BI = BigInt.zero()
export const ONE_BI = BigInt.fromI32(1)
export const TEN_BI = BigInt.fromI32(10)
export const ONE_PERCENT_BI = BigInt.fromString("10000000000000000") // 0.01 * 1e18
export const THREE_PERCENT_BI = BigInt.fromString("30000000000000000") // 0.03 * 1e18
export const FIVE_PERCENT_BI = BigInt.fromString("50000000000000000") // 0.05 * 1e18
export const HALF_WAD_BI = BigInt.fromString("500000000000000000")
export const ONE_WAD_BI = BigInt.fromString("1000000000000000000")

// BigDecimal constants
export const ZERO_BD = BigDecimal.zero()
Expand Down
1 change: 1 addition & 0 deletions src/utils/grants/distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function loadOrCreateDistributionPeriod(distributionId: Bytes): Distribut
distributionPeriod.topSlate = Bytes.empty()
distributionPeriod.slatesSubmitted = []
distributionPeriod.delegationRewardsClaimed = ZERO_BD
distributionPeriod.fundsAvailable = ZERO_BD
distributionPeriod.fundingVotePowerUsed = ZERO_BD
distributionPeriod.screeningVotesCast = ZERO_BD
distributionPeriod.votes = []
Expand Down
7 changes: 7 additions & 0 deletions src/utils/grants/fund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ export function loadOrCreateGrantFund(grantFundAddress: Address): GrantFund {
}
return grantFund
}

export function getTreasury(grantFundAddress: Address): BigInt {
const grantFundContract = GrantFundContract.bind(grantFundAddress)
const getTreasuryResult = grantFundContract.treasury()

return getTreasuryResult
}
13 changes: 12 additions & 1 deletion tests/grant-fund.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
ZERO_BI,
} from "../src/utils/constants";
import { addressToBytes, bigIntToBytes, decimalToWad, wadToDecimal } from "../src/utils/convert";
import { mockGetDistributionId, mockGetVotesFunding, mockGetVotesScreening } from "./utils/common";
import { mockGetDistributionId, mockGetTreasury, mockGetVotesFunding, mockGetVotesScreening } from "./utils/common";
import { getDistributionPeriodVoteId, getFundingVoteId } from "../src/utils/grants/voter";

// Tests structure (matchstick-as >=0.5.0)
Expand Down Expand Up @@ -94,6 +94,9 @@ describe("Grant Fund assertions", () => {
const distributionId = ONE_BI;
const startBlock = ONE_BI;
const endBlock = startBlock.plus(DISTRIBUTION_PERIOD_LENGTH);
const grantFundAddress = Address.fromString("0xa16081f360e3847006db660bae1c6d1b2e17ec2a")

mockGetTreasury(grantFundAddress, ONE_WAD_BI);

const newDistributionPeriodStartedEvent = createDistributionPeriodStartedEvent(
distributionId,
Expand Down Expand Up @@ -148,6 +151,9 @@ describe("Grant Fund assertions", () => {
// mock parameters
const amount = ONE_WAD_BI;
const treasuryBalance = ONE_WAD_BI;
const grantFundAddress = Address.fromString("0xa16081f360e3847006db660bae1c6d1b2e17ec2a")

mockGetTreasury(grantFundAddress, treasuryBalance)

const newFundTreasuryEvent = createFundTreasuryEvent(
amount,
Expand Down Expand Up @@ -197,6 +203,8 @@ describe("Grant Fund assertions", () => {
endBlock
);
newDistributionPeriodStartedEvent.address = grantFundAddress
mockGetTreasury(grantFundAddress, ONE_WAD_BI);

handleDistributionPeriodStarted(newDistributionPeriodStartedEvent);
mockGetDistributionId(grantFundAddress, distributionId);

Expand Down Expand Up @@ -259,6 +267,9 @@ describe("Grant Fund assertions", () => {
endBlock
);
newDistributionPeriodStartedEvent.address = grantFundAddress

mockGetTreasury(grantFundAddress, ONE_WAD_BI);

handleDistributionPeriodStarted(newDistributionPeriodStartedEvent);
mockGetDistributionId(grantFundAddress, distributionId);

Expand Down
8 changes: 8 additions & 0 deletions tests/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,14 @@ export function mockGetVotesFunding(grantFund: Address, distributionId: BigInt,
])
}

export function mockGetTreasury(grantFund: Address, expectedTreasury: BigInt): void {
createMockedFunction(grantFund, 'treasury', 'treasury():(uint256)')
.withArgs([])
.returns([
ethereum.Value.fromUnsignedBigInt(expectedTreasury),
])
}

/*******************************/
/*** Position Mock Functions ***/
/*******************************/
Expand Down

0 comments on commit fdf5c6e

Please sign in to comment.