diff --git a/schema.graphql b/schema.graphql index 8b3167e..1cf9225 100644 --- a/schema.graphql +++ b/schema.graphql @@ -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 diff --git a/src/grant-fund.ts b/src/grant-fund.ts index 53adee0..4106dd1 100644 --- a/src/grant-fund.ts +++ b/src/grant-fund.ts @@ -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, @@ -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( @@ -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() @@ -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() diff --git a/src/utils/constants.ts b/src/utils/constants.ts index b356635..db8f8eb 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -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() diff --git a/src/utils/grants/distribution.ts b/src/utils/grants/distribution.ts index e84c646..8a336f6 100644 --- a/src/utils/grants/distribution.ts +++ b/src/utils/grants/distribution.ts @@ -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 = [] diff --git a/src/utils/grants/fund.ts b/src/utils/grants/fund.ts index 3d76224..bef8b7c 100644 --- a/src/utils/grants/fund.ts +++ b/src/utils/grants/fund.ts @@ -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 +} diff --git a/tests/grant-fund.test.ts b/tests/grant-fund.test.ts index 5a9fc4a..b2cac76 100644 --- a/tests/grant-fund.test.ts +++ b/tests/grant-fund.test.ts @@ -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) @@ -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, @@ -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, @@ -197,6 +203,8 @@ describe("Grant Fund assertions", () => { endBlock ); newDistributionPeriodStartedEvent.address = grantFundAddress + mockGetTreasury(grantFundAddress, ONE_WAD_BI); + handleDistributionPeriodStarted(newDistributionPeriodStartedEvent); mockGetDistributionId(grantFundAddress, distributionId); @@ -259,6 +267,9 @@ describe("Grant Fund assertions", () => { endBlock ); newDistributionPeriodStartedEvent.address = grantFundAddress + + mockGetTreasury(grantFundAddress, ONE_WAD_BI); + handleDistributionPeriodStarted(newDistributionPeriodStartedEvent); mockGetDistributionId(grantFundAddress, distributionId); diff --git a/tests/utils/common.ts b/tests/utils/common.ts index 82297bc..ca3bed5 100644 --- a/tests/utils/common.ts +++ b/tests/utils/common.ts @@ -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 ***/ /*******************************/