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

fix tokensRequested #40

Merged
merged 7 commits into from
Aug 2, 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
2 changes: 1 addition & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ type DistributionPeriod @entity {
screeningVotesCast: BigDecimal! # number of screening votes cast
votes: [DistributionPeriodVote!]! # Voter info for the distribution period
proposals: [Proposal!]! # List of proposals submitted in the distribution period
totalTokensRequested: BigDecimal! # Total ajna tokens requested by all proposals in the distribution period
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can readd this as an alternative implementation showing the totalTokensRequested of the current list of proposals in the top funded slate

totalTokensDistributed: BigDecimal! # Total ajna tokens distributed to executed proposals in a distribution period
}

type Proposal @entity {
Expand Down
22 changes: 15 additions & 7 deletions src/grant-fund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
DistributionPeriodVote
} from "../generated/schema"

import { ONE_BI, THREE_PERCENT_BI, ZERO_ADDRESS, ZERO_BD } from './utils/constants'
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 { getProposalParamsId, getProposalsInSlate, loadOrCreateProposal, removeProposalFromList } from './utils/grants/proposal'
import { getCurrentDistributionId, getCurrentStage, loadOrCreateDistributionPeriod } from './utils/grants/distribution'
Expand Down Expand Up @@ -165,7 +165,7 @@ export function handleProposalCreated(event: ProposalCreatedEvent): void {
const proposal = loadOrCreateProposal(proposalId)
proposal.description = event.params.description

let totalTokensRequested = ZERO_BD
let totalTokensRequested = ZERO_BI

// create ProposalParams entities for each separate proposal param
for (let i = 0; i < event.params.targets.length; i++) {
Expand All @@ -175,28 +175,32 @@ export function handleProposalCreated(event: ProposalCreatedEvent): void {
proposalParams.value = event.params.values[i]
proposalParams.calldata = event.params.calldatas[i]

// LINKS TO GRAPH DISCORD MESSAGES WITH SOLUTION TO DECODING CALLDATA:
// https://discord.com/channels/438038660412342282/438070183794573313/948230275886878740
// https://discord.com/channels/438038660412342282/438070183794573313/948241208277364756
// decode the calldata to get the recipient and tokens requested
const decoded = ethereum.decode('(address,uint256)', proposalParams.calldata)!
const dataWithoutSelector = Bytes.fromUint8Array(proposalParams.calldata.subarray(4))
const decoded = ethereum.decode('(address,uint256)', dataWithoutSelector)!
proposalParams.recipient = decoded.toTuple()[0].toAddress()
const tokensRequested = decoded.toTuple()[1].toBigInt().toBigDecimal()
proposalParams.tokensRequested = tokensRequested
const tokensRequested = decoded.toTuple()[1].toBigInt()
proposalParams.tokensRequested = wadToDecimal(tokensRequested)
totalTokensRequested = totalTokensRequested.plus(tokensRequested)

// add proposalParams information to proposal
proposal.params = proposal.params.concat([proposalParams.id])
proposal.totalTokensRequested = totalTokensRequested

// save each proposalParams entity to the store
proposalParams.save()
}

proposal.totalTokensRequested = wadToDecimal(totalTokensRequested)

proposalCreated.proposal = proposal.id

// update distribution entity
const distributionId = bigIntToBytes(getCurrentDistributionId(event.address))
const distributionPeriod = DistributionPeriod.load(distributionId)!
distributionPeriod.proposals = distributionPeriod.proposals.concat([proposal.id])
distributionPeriod.totalTokensRequested = distributionPeriod.totalTokensRequested.plus(proposal.totalTokensRequested)

// record proposals distributionId
proposal.distribution = distributionPeriod.id
Expand All @@ -221,7 +225,11 @@ export function handleProposalExecuted(event: ProposalExecutedEvent): void {
const proposal = loadOrCreateProposal(bigIntToBytes(event.params.proposalId))
proposal.executed = true

const distributionPeriod = DistributionPeriod.load(proposal.distribution!)!
distributionPeriod.totalTokensDistributed = distributionPeriod.totalTokensDistributed.plus(proposal.totalTokensRequested)

// save entities to the store
distributionPeriod.save()
proposal.save()
proposalExecuted.save()
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/grants/distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function loadOrCreateDistributionPeriod(distributionId: Bytes): Distribut
distributionPeriod.screeningVotesCast = ZERO_BD
distributionPeriod.votes = []
distributionPeriod.proposals = []
distributionPeriod.totalTokensRequested = ZERO_BD
distributionPeriod.totalTokensDistributed = ZERO_BD
}
return distributionPeriod
}
107 changes: 54 additions & 53 deletions tests/grant-fund.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe("Grant Fund assertions", () => {
assert.fieldEquals(
"DistributionPeriod",
`${expectedDistributionId}`,
"totalTokensRequested",
"totalTokensDistributed",
`${ZERO_BD}`
);

Expand Down Expand Up @@ -197,16 +197,15 @@ describe("Grant Fund assertions", () => {
"transfer(address,uint256)",
"transfer(address,uint256)",
];
const paramsArray: Array<ethereum.Value> = [
ethereum.Value.fromAddress(proposer),
ethereum.Value.fromUnsignedBigInt(ONE_BI),
];
const params = changetype<ethereum.Tuple>(paramsArray)
const encodedparamsOne = ethereum.encode(ethereum.Value.fromTuple(params))!;
const encodedparamsTwo = ethereum.encode(ethereum.Value.fromTuple(params))!;

// use hardcoded output of encode("transfer(address,uint256)")
// as assembly script doesn't support encodeWithSelector
// 0xa9059cbb000000000000000000000000c91f4871cfdd1947df6c23771f230853e0e2740700000000000000000000000000000000000000000000003635c9adc5dea00000
// ==
// (0xc91f4871cfDd1947DF6C23771F230853E0e27407, 1000 * 1e18)
const calldatas = [
encodedparamsOne,
encodedparamsTwo,
Bytes.fromHexString("0xa9059cbb000000000000000000000000c91f4871cfdd1947df6c23771f230853e0e2740700000000000000000000000000000000000000000000003635c9adc5dea00000"),
Bytes.fromHexString("0xa9059cbb000000000000000000000000c91f4871cfdd1947df6c23771f230853e0e2740700000000000000000000000000000000000000000000003635c9adc5dea00000")
];

const distributionId = BigInt.fromI32(234);
Expand Down Expand Up @@ -264,7 +263,7 @@ describe("Grant Fund assertions", () => {
"Proposal",
`${bigIntToBytes(proposalId).toHexString()}`,
"totalTokensRequested",
`${BigInt.fromI32(2)}`
`${wadToDecimal(BigInt.fromString("2000000000000000000000"))}` // 2000 * 1e18
);

});
Expand All @@ -289,16 +288,15 @@ describe("Grant Fund assertions", () => {
"transfer(address,uint256)",
"transfer(address,uint256)",
];
const paramsArray: Array<ethereum.Value> = [
ethereum.Value.fromAddress(proposer),
ethereum.Value.fromUnsignedBigInt(ONE_BI),
];
const params = changetype<ethereum.Tuple>(paramsArray)
const encodedparamsOne = ethereum.encode(ethereum.Value.fromTuple(params))!;
const encodedparamsTwo = ethereum.encode(ethereum.Value.fromTuple(params))!;

// use hardcoded output of encode("transfer(address,uint256)")
// as assembly script doesn't support encodeWithSelector
// 0xa9059cbb000000000000000000000000c91f4871cfdd1947df6c23771f230853e0e2740700000000000000000000000000000000000000000000003635c9adc5dea00000
// ==
// (0xc91f4871cfDd1947DF6C23771F230853E0e27407, 1000 * 1e18)
const calldatas = [
encodedparamsOne,
encodedparamsTwo,
Bytes.fromHexString("0xa9059cbb000000000000000000000000c91f4871cfdd1947df6c23771f230853e0e2740700000000000000000000000000000000000000000000003635c9adc5dea00000"),
Bytes.fromHexString("0xa9059cbb000000000000000000000000c91f4871cfdd1947df6c23771f230853e0e2740700000000000000000000000000000000000000000000003635c9adc5dea00000")
];

const distributionId = BigInt.fromI32(234);
Expand Down Expand Up @@ -346,6 +344,8 @@ describe("Grant Fund assertions", () => {
/*** Assert State ***/
/********************/

const expectedDistributionId = bigIntToBytes(distributionId).toHexString();

// check GrantFund attributes
assert.entityCount("GrantFund", 1);

Expand All @@ -367,7 +367,7 @@ describe("Grant Fund assertions", () => {
"Proposal",
`${bigIntToBytes(proposalId).toHexString()}`,
"totalTokensRequested",
`${BigInt.fromI32(2)}`
`${wadToDecimal(BigInt.fromString("2000000000000000000000"))}` // 2000 * 1e18
);

assert.fieldEquals(
Expand All @@ -383,6 +383,12 @@ describe("Grant Fund assertions", () => {
"proposalId",
`${proposalId}`
);
assert.fieldEquals(
"DistributionPeriod",
`${expectedDistributionId}`,
"totalTokensDistributed",
`${wadToDecimal(BigInt.fromString("2000000000000000000000"))}` // 2000 * 1e18
);

});

Expand All @@ -406,16 +412,15 @@ describe("Grant Fund assertions", () => {
"transfer(address,uint256)",
"transfer(address,uint256)",
];
const paramsArray: Array<ethereum.Value> = [
ethereum.Value.fromAddress(proposer),
ethereum.Value.fromUnsignedBigInt(ONE_BI),
];
const params = changetype<ethereum.Tuple>(paramsArray)
const encodedparamsOne = ethereum.encode(ethereum.Value.fromTuple(params))!;
const encodedparamsTwo = ethereum.encode(ethereum.Value.fromTuple(params))!;

// use hardcoded output of encode("transfer(address,uint256)")
// as assembly script doesn't support encodeWithSelector
// 0xa9059cbb000000000000000000000000c91f4871cfdd1947df6c23771f230853e0e2740700000000000000000000000000000000000000000000003635c9adc5dea00000
// ==
// (0xc91f4871cfDd1947DF6C23771F230853E0e27407, 1000 * 1e18)
const calldatas = [
encodedparamsOne,
encodedparamsTwo,
Bytes.fromHexString("0xa9059cbb000000000000000000000000c91f4871cfdd1947df6c23771f230853e0e2740700000000000000000000000000000000000000000000003635c9adc5dea00000"),
Bytes.fromHexString("0xa9059cbb000000000000000000000000c91f4871cfdd1947df6c23771f230853e0e2740700000000000000000000000000000000000000000000003635c9adc5dea00000")
];

const distributionId = ONE_BI;
Expand Down Expand Up @@ -519,16 +524,15 @@ describe("Grant Fund assertions", () => {
"transfer(address,uint256)",
"transfer(address,uint256)",
];
const paramsArray: Array<ethereum.Value> = [
ethereum.Value.fromAddress(proposer),
ethereum.Value.fromUnsignedBigInt(ONE_BI),
];
const params = changetype<ethereum.Tuple>(paramsArray)
const encodedparamsOne = ethereum.encode(ethereum.Value.fromTuple(params))!;
const encodedparamsTwo = ethereum.encode(ethereum.Value.fromTuple(params))!;

// use hardcoded output of encode("transfer(address,uint256)")
// as assembly script doesn't support encodeWithSelector
// 0xa9059cbb000000000000000000000000c91f4871cfdd1947df6c23771f230853e0e2740700000000000000000000000000000000000000000000003635c9adc5dea00000
// ==
// (0xc91f4871cfDd1947DF6C23771F230853E0e27407, 1000 * 1e18)
const calldatas = [
encodedparamsOne,
encodedparamsTwo,
Bytes.fromHexString("0xa9059cbb000000000000000000000000c91f4871cfdd1947df6c23771f230853e0e2740700000000000000000000000000000000000000000000003635c9adc5dea00000"),
Bytes.fromHexString("0xa9059cbb000000000000000000000000c91f4871cfdd1947df6c23771f230853e0e2740700000000000000000000000000000000000000000000003635c9adc5dea00000")
];

const distributionId = ONE_BI;
Expand Down Expand Up @@ -582,7 +586,6 @@ describe("Grant Fund assertions", () => {
/*** Funding Vote Proposal ***/
/*****************************/

// TODO: need to convert back from WAD
const fundingVotingPower = votesCast.times(votesCast);

mockGetVotesFunding(grantFundAddress, distributionId, voter, fundingVotingPower);
Expand Down Expand Up @@ -724,17 +727,17 @@ describe("Grant Fund assertions", () => {
"transfer(address,uint256)",
"transfer(address,uint256)",
];
const paramsArray: Array<ethereum.Value> = [
ethereum.Value.fromAddress(proposer),
ethereum.Value.fromUnsignedBigInt(ONE_BI),
];
const params = changetype<ethereum.Tuple>(paramsArray)
const encodedparamsOne = ethereum.encode(ethereum.Value.fromTuple(params))!;
const encodedparamsTwo = ethereum.encode(ethereum.Value.fromTuple(params))!;

// use hardcoded output of encode("transfer(address,uint256)")
// as assembly script doesn't support encodeWithSelector
// 0xa9059cbb000000000000000000000000c91f4871cfdd1947df6c23771f230853e0e2740700000000000000000000000000000000000000000000003635c9adc5dea00000
// ==
// (0xc91f4871cfDd1947DF6C23771F230853E0e27407, 1000 * 1e18)
const calldatas = [
encodedparamsOne,
encodedparamsTwo,
Bytes.fromHexString("0xa9059cbb000000000000000000000000c91f4871cfdd1947df6c23771f230853e0e2740700000000000000000000000000000000000000000000003635c9adc5dea00000"),
Bytes.fromHexString("0xa9059cbb000000000000000000000000c91f4871cfdd1947df6c23771f230853e0e2740700000000000000000000000000000000000000000000003635c9adc5dea00000")
];

const distributionId = ONE_BI;
const startBlock = ONE_BI;
const endBlock = startBlock.plus(DISTRIBUTION_PERIOD_LENGTH);
Expand Down Expand Up @@ -773,15 +776,13 @@ describe("Grant Fund assertions", () => {

// mock parameters
const voter = Address.fromString("0x0000000000000000000000000000000000000050");
let votesCast = BigInt.fromI32(-234);
const votesCast = BigInt.fromString("-234000000000000000000")
const reason = ""

// TODO: need to convert back from WAD
const fundingVotingPower = votesCast.times(votesCast);

mockGetVotesFunding(grantFundAddress, distributionId, voter, fundingVotingPower);

votesCast = BigInt.fromI32(-234);
const fundingVoteCastEvent = createVoteCastEvent(voter, proposalId, 0, votesCast, reason, startBlock.plus(SCREENING_PERIOD_LENGTH).plus(BigInt.fromI32(1)), BigInt.fromI32(1));
handleVoteCast(fundingVoteCastEvent);

Expand Down Expand Up @@ -822,7 +823,7 @@ describe("Grant Fund assertions", () => {
"FundedSlate",
`${fundedSlateHash.toHexString()}`,
"totalTokensRequested",
`${BigInt.fromI32(2)}`
`${wadToDecimal(BigInt.fromString("2000000000000000000000"))}` // 1000 * 1e18
);
});
});
Loading