Skip to content

Commit

Permalink
added ipfs data parsing and removed unused reward/donation code
Browse files Browse the repository at this point in the history
  • Loading branch information
krisbitney committed Dec 19, 2023
1 parent 60af1fc commit 705543b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 63 deletions.
38 changes: 14 additions & 24 deletions packages/subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,8 @@ type DonorCollective @entity {
id: String! # donorAddress + " " + collectiveAddress
donor: Donor! @derivedFrom(field: "collectives")
collective: Collective! @derivedFrom(field: "donors")
contribution: BigInt!
donations: [Donation!]!
}

type Donation @entity {
id: String! # donorAddress + " " + collectiveAddress + " " + timestamp
donor: Donor!
collective: Collective!
timestamp: BigInt!
originationContract: Bytes! # The contract address from which the transaction originated
previousContribution: BigInt! # This is the contribution of the donor to the collective before the supporter update event
contribution: BigInt! # This is the total contribution of the donor to the collective
previousFlowRate: BigInt!
flowRate: BigInt!
isFlowUpdate: Boolean!
}

type Steward @entity {
Expand All @@ -46,17 +33,6 @@ type StewardCollective @entity {
collective: Collective! @derivedFrom(field: "stewards")
actions: Int!
totalEarned: BigInt!
rewards: [Reward!]!
}

type Reward @entity {
id: String! # stewardAddress + " " + collectiveAddress + " " + timestamp
steward: Steward!
collective: Collective!
timestamp: BigInt!
quantity: BigInt!
rewardPerContributor: BigInt!
nft: ProvableNFT!
}

type Collective @entity {
Expand All @@ -75,6 +51,20 @@ type Collective @entity {
totalRewards: BigInt!
}

type IpfsCollective @entity {
id: String! # collective address
name: String!
description: String!
email: String
website: String
twitter: String
instagram: String
threads: String
headerImage: String
logo: String
images: [String!]
}

type PoolSettings @entity {
id: String! # collective address
nftType: BigInt!
Expand Down
87 changes: 66 additions & 21 deletions packages/subgraph/src/mappings/pool.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigInt, log } from '@graphprotocol/graph-ts';
import { BigInt, Bytes, ipfs, json, JSONValueKind, log } from '@graphprotocol/graph-ts';
import {
PoolCreated,
PoolDetailsChanged,
Expand All @@ -13,13 +13,13 @@ import {
import {
Claim,
Collective,
EventData,
IpfsCollective,
PoolSettings,
ProvableNFT,
SafetyLimits,
EventData,
Steward,
StewardCollective,
ProvableNFT,
Reward,
} from '../../generated/schema';

export function handlePoolCreated(event: PoolCreated): void {
Expand All @@ -32,7 +32,7 @@ export function handlePoolCreated(event: PoolCreated): void {

let directPaymentPool = Collective.load(poolAddress.toHexString());
if (directPaymentPool === null) {
directPaymentPool = new Collective(event.address.toHexString());
directPaymentPool = new Collective(poolAddress.toHexString());
const directPaymentPoolSettings = new PoolSettings(poolAddress.toHexString());
const directPaymentPoolLimits = new SafetyLimits(poolAddress.toHexString());

Expand All @@ -51,20 +51,78 @@ export function handlePoolCreated(event: PoolCreated): void {
directPaymentPoolSettings.membersValidator = poolSettings.membersValidator;
directPaymentPoolSettings.uniquenessValidator = poolSettings.uniquenessValidator;
directPaymentPoolSettings.rewardToken = poolSettings.rewardToken;
directPaymentPoolSettings.save();

// Pool Limits
directPaymentPoolLimits.maxTotalPerMonth = poolLimits.maxTotalPerMonth;
directPaymentPoolLimits.maxMemberPerMonth = poolLimits.maxMemberPerMonth;
directPaymentPoolLimits.maxMemberPerDay = poolLimits.maxMemberPerDay;
directPaymentPoolLimits.save();

directPaymentPool.limits = directPaymentPoolLimits.id;
// update and save pool
directPaymentPool.settings = directPaymentPoolSettings.id;
directPaymentPool.limits = directPaymentPoolLimits.id;

directPaymentPoolSettings.save();
directPaymentPoolLimits.save();
directPaymentPool.save();

// IpfsCollective
let ipfsCollective = IpfsCollective.load(ipfsHash);
if (ipfsCollective === null) {
const data = fetchFromIpfsWithRetries(ipfsHash, 3);
if (data === null) {
log.error('Failed to fetch IPFS data using hash {} for collective {}', [ipfsHash, poolAddress.toHexString()]);
return;
}
ipfsCollective = new IpfsCollective(ipfsHash);
// mutates ipfsCollective
parseIpfsData(data, ipfsCollective, ipfsHash, poolAddress.toHexString());
ipfsCollective.save();
}
}
}

// mutates ipfsCollective
function parseIpfsData(data: Bytes, ipfsCollective: IpfsCollective, ipfsHash: string, poolAddress: string): void {
// parse bytes to json
const jsonParseResult = json.try_fromBytes(data);
if (jsonParseResult.isError) {
log.error('Invalid JSON data found at IPFS hash {} for collective {}', [ipfsHash, poolAddress]);
return;
}
const jsonValue = jsonParseResult.value;

// make sure json is object
if (jsonValue.kind != JSONValueKind.OBJECT) {
log.error('Invalid JSON data found at IPFS hash {} for collective {}', [ipfsHash, poolAddress]);
return;
}
const jsonObject = jsonValue.toObject();

ipfsCollective.name = jsonObject.isSet("name") ? jsonObject.get('name')!!.toString() : "";
ipfsCollective.description = jsonObject.isSet("description") ? jsonObject.get('description')!!.toString() : "";
ipfsCollective.email = jsonObject.isSet("email") ? jsonObject.get('email')!!.toString() : null;
ipfsCollective.website = jsonObject.isSet("website") ? jsonObject.get('website')!!.toString() : null;
ipfsCollective.twitter = jsonObject.isSet("twitter") ? jsonObject.get('twitter')!!.toString() : null;
ipfsCollective.instagram = jsonObject.isSet("instagram") ? jsonObject.get('instagram')!!.toString() : null;
ipfsCollective.threads = jsonObject.isSet("threads") ? jsonObject.get('threads')!!.toString() : null;
ipfsCollective.headerImage = jsonObject.isSet("headerImage") ? jsonObject.get('headerImage')!!.toString() : null;
ipfsCollective.logo = jsonObject.isSet("logo") ? jsonObject.get('logo')!!.toString() : null;
ipfsCollective.images = jsonObject.isSet("images")
? jsonObject.get('images')!!.toArray().map<string>((value) => value.toString())
: null;
}

function fetchFromIpfsWithRetries(ipfsHash: string, retries: i32): Bytes | null {
let data = ipfs.cat(ipfsHash);
let i = retries;
while (i > 0 && data === null) {
data = ipfs.cat(ipfsHash);
i--;
}
return data;
}


export function handlePoolDetailsChanged(event: PoolDetailsChanged): void {
const poolAddress = event.params.pool;
const ipfsHash = event.params.ipfs;
Expand Down Expand Up @@ -189,8 +247,6 @@ export function handleRewardClaim(event: EventRewardClaimed): void {
for (let i = 0; i < contributors.length; i++) {
const stewardAddress = contributors[i].toHexString();
const stewardCollectiveId = `${stewardAddress} ${poolAddress}`;
const timestamp = event.block.timestamp;
const rewardId = stewardAddress + " " + poolAddress + " " + timestamp.toString();

// adds steward to event data
eventData.contributors.push(stewardAddress);
Expand Down Expand Up @@ -220,17 +276,6 @@ export function handleRewardClaim(event: EventRewardClaimed): void {
pool.stewards.push(stewardCollectiveId);
}

// create steward reward
let reward = new Reward(rewardId);
reward.steward = stewardAddress;
reward.collective = poolAddress;
reward.timestamp = timestamp;
reward.quantity = eventQuantity;
reward.rewardPerContributor = rewardPerContributor;
reward.nft = nftAddress;
// add Reward to StewardCollective
stewardCollective.rewards.push(rewardId);

steward.save();
stewardCollective.save();
}
Expand Down
21 changes: 3 additions & 18 deletions packages/subgraph/src/mappings/superApp.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { BigInt, log } from '@graphprotocol/graph-ts';
import { SupporterUpdated } from '../../generated/DirectPaymentsPool/DirectPaymentsPool';
import { Collective, Donation, Donor, DonorCollective } from '../../generated/schema';
import { Collective, Donor, DonorCollective } from '../../generated/schema';

export function handleSupport(event: SupporterUpdated): void {
const donorAddress = event.params.supporter.toHexString();
const poolAddress = event.address.toHexString();
const donorCollectiveId = donorAddress + " " + poolAddress;
const timestamp = event.block.timestamp;
const donationId = donorAddress + " " + poolAddress + " " + timestamp.toString()

const contributionDelta = event.params.contribution.minus(event.params.previousContribution);

Expand All @@ -33,30 +32,16 @@ export function handleSupport(event: SupporterUpdated): void {
if (donorCollective == null) {
donorCollective = new DonorCollective(donorCollectiveId);
donorCollective.contribution = BigInt.fromI32(0);
donorCollective.flowRate = BigInt.fromI32(0);
}
// This value is updated in _updateSupporter at line 260 of GoodCollectiveSuperApp.sol before the event is emitted
donorCollective.contribution = event.params.contribution;

// create Donation
const donation = new Donation(donationId);
donation.donor = donorAddress;
donation.collective = poolAddress;
donation.timestamp = timestamp;
donation.originationContract = event.address;
donation.previousContribution = event.params.previousContribution;
donation.contribution = event.params.contribution;
donation.previousFlowRate = event.params.previousFlowRate;
donation.flowRate = event.params.flowRate;
donation.isFlowUpdate = event.params.isFlowUpdate;

// add Donation to DonorCollective
donorCollective.donations.push(donationId);
donorCollective.flowRate = event.params.flowRate;

// add DonorCollective to Donor
donor.collectives.push(donorCollectiveId);

donor.save();
donorCollective.save();
donation.save();
pool.save();
}

0 comments on commit 705543b

Please sign in to comment.