Skip to content

Commit

Permalink
fix proposal calldata decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike committed Jul 27, 2023
1 parent 27918da commit 9b3faf0
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 26 deletions.
19 changes: 6 additions & 13 deletions src/grant-fund.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, BigInt, Bytes, ethereum } from '@graphprotocol/graph-ts'
import { Address, BigInt, Bytes, ethereum, log } from '@graphprotocol/graph-ts'

import {
DelegateRewardClaimed as DelegateRewardClaimedEvent,
Expand Down Expand Up @@ -182,18 +182,11 @@ export function handleProposalCreated(event: ProposalCreatedEvent): void {
proposalParams.calldata = event.params.calldatas[i]

// decode the calldata to get the recipient and tokens requested
const dataWithoutFunctionSelector = Bytes.fromUint8Array(proposalParams.calldata.subarray(3))
const decoded = ethereum.decode('(address,uint256)', dataWithoutFunctionSelector)
if (decoded != null) {
proposalParams.recipient = decoded.toTuple()[0].toAddress()
const tokensRequested = decoded.toTuple()[1].toBigInt().toBigDecimal()
proposalParams.tokensRequested = tokensRequested
totalTokensRequested = totalTokensRequested.plus(tokensRequested)
}
else {
proposalParams.recipient = ZERO_ADDRESS
proposalParams.tokensRequested = ZERO_BD
}
const decoded = ethereum.decode('(address,uint256)', proposalParams.calldata)!
proposalParams.recipient = decoded.toTuple()[0].toAddress()
const tokensRequested = decoded.toTuple()[1].toBigInt().toBigDecimal()
proposalParams.tokensRequested = tokensRequested
totalTokensRequested = totalTokensRequested.plus(tokensRequested)

// add proposalParams information to proposal
proposal.params = proposal.params.concat([proposalParams.id])
Expand Down
102 changes: 89 additions & 13 deletions tests/grant-fund.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
log,
logStore,
} from "matchstick-as/assembly/index";
import { Address, BigInt, Bytes, dataSource } from "@graphprotocol/graph-ts";
import { Address, BigInt, Bytes, dataSource, ethereum } from "@graphprotocol/graph-ts";
import {
handleDelegateRewardClaimed,
handleFundTreasury,
Expand Down Expand Up @@ -183,16 +183,26 @@ describe("Grant Fund assertions", () => {
const proposer = Address.fromString(
"0x0000000000000000000000000000000000000025"
);

// encode mock calldatas
const targets = [ajnaTokenAddress, ajnaTokenAddress];
const values = [ZERO_BI, ZERO_BI];
const signatures = [
"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))!;
const calldatas = [
Bytes.fromHexString("0x000000"),
Bytes.fromHexString("0x000000"),
encodedparamsOne,
encodedparamsTwo,
];

const distributionId = BigInt.fromI32(234);
const startBlock = ONE_BI;
const endBlock = startBlock.plus(DISTRIBUTION_PERIOD_LENGTH);
Expand Down Expand Up @@ -227,12 +237,30 @@ describe("Grant Fund assertions", () => {

// check Proposal attributes
assert.entityCount("Proposal", 1);
assert.entityCount("ProposalParams", 2);

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

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

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

assert.fieldEquals(
"Proposal",
`${bigIntToBytes(proposalId).toHexString()}`,
"distribution",
`${expectedDistributionId}`
);

assert.fieldEquals(
"Proposal",
`${bigIntToBytes(proposalId).toHexString()}`,
"totalTokensRequested",
`${BigInt.fromI32(2)}`
);

});

test("ProposalExecuted", () => {
Expand All @@ -247,16 +275,26 @@ describe("Grant Fund assertions", () => {
const proposer = Address.fromString(
"0x0000000000000000000000000000000000000025"
);

// encode mock calldatas
const targets = [ajnaTokenAddress, ajnaTokenAddress];
const values = [ZERO_BI, ZERO_BI];
const signatures = [
"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))!;
const calldatas = [
Bytes.fromHexString("0x000000"),
Bytes.fromHexString("0x000000"),
encodedparamsOne,
encodedparamsTwo,
];

const distributionId = BigInt.fromI32(234);
const startBlock = ONE_BI;
const endBlock = startBlock.plus(DISTRIBUTION_PERIOD_LENGTH);
Expand Down Expand Up @@ -307,6 +345,7 @@ describe("Grant Fund assertions", () => {

// check Proposal attributes
assert.entityCount("Proposal", 1);
assert.entityCount("ProposalParams", 2);

// check ProposalExecuted attributes
assert.entityCount("ProposalExecuted", 1);
Expand All @@ -324,16 +363,26 @@ describe("Grant Fund assertions", () => {
const proposer = Address.fromString(
"0x0000000000000000000000000000000000000025"
);

// encode mock calldatas
const targets = [ajnaTokenAddress, ajnaTokenAddress];
const values = [ZERO_BI, ZERO_BI];
const signatures = [
"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))!;
const calldatas = [
Bytes.fromHexString("0x000000"),
Bytes.fromHexString("0x000000"),
encodedparamsOne,
encodedparamsTwo,
];

const distributionId = ONE_BI;
const startBlock = ONE_BI;
const endBlock = startBlock.plus(DISTRIBUTION_PERIOD_LENGTH);
Expand Down Expand Up @@ -390,6 +439,7 @@ describe("Grant Fund assertions", () => {

// check Proposal attributes
assert.entityCount("Proposal", 1);
assert.entityCount("ProposalParams", 2);

// check Proposal attributes
assert.entityCount("DistributionPeriodVote", 1);
Expand Down Expand Up @@ -426,16 +476,26 @@ describe("Grant Fund assertions", () => {
const proposer = Address.fromString(
"0x0000000000000000000000000000000000000025"
);

// encode mock calldatas
const targets = [ajnaTokenAddress, ajnaTokenAddress];
const values = [ZERO_BI, ZERO_BI];
const signatures = [
"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))!;
const calldatas = [
Bytes.fromHexString("0x000000"),
Bytes.fromHexString("0x000000"),
encodedparamsOne,
encodedparamsTwo,
];

const distributionId = ONE_BI;
const startBlock = ONE_BI;
const endBlock = startBlock.plus(DISTRIBUTION_PERIOD_LENGTH);
Expand Down Expand Up @@ -505,6 +565,7 @@ describe("Grant Fund assertions", () => {

// check Proposal attributes
assert.entityCount("Proposal", 1);
assert.entityCount("ProposalParams", 2);

assert.entityCount("DistributionPeriod", 1);
assert.entityCount("DistributionPeriodVote", 1);
Expand Down Expand Up @@ -604,15 +665,24 @@ describe("Grant Fund assertions", () => {
const proposer = Address.fromString(
"0x0000000000000000000000000000000000000025"
);

// encode mock calldatas
const targets = [ajnaTokenAddress, ajnaTokenAddress];
const values = [ZERO_BI, ZERO_BI];
const signatures = [
"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))!;
const calldatas = [
Bytes.fromHexString("0x000000"),
Bytes.fromHexString("0x000000"),
encodedparamsOne,
encodedparamsTwo,
];
const distributionId = ONE_BI;
const startBlock = ONE_BI;
Expand Down Expand Up @@ -687,19 +757,25 @@ describe("Grant Fund assertions", () => {

// check Proposal attributes
assert.entityCount("Proposal", 1);
assert.entityCount("ProposalParams", 2);

assert.entityCount("DistributionPeriod", 1);
assert.entityCount("FundedSlate", 1);

logStore();

// // check FundedSlate attributes
// check FundedSlate attributes
// assert.fieldEquals(
// "FundedSlate",
// `${fundedSlateHash.toHexString()}`,
// "totalFundingVotesReceived",
// `${votesCast}`
// );

assert.fieldEquals(
"FundedSlate",
`${fundedSlateHash.toHexString()}`,
"totalTokensRequested",
`${BigInt.fromI32(2)}`
);
});
});

0 comments on commit 9b3faf0

Please sign in to comment.