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

[subgraph] various subgraph mapping fixes #2056

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions packages/subgraph/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

- Fix `adjustmentFlowRate` on Pool
- Improve `poolTotalAmountReceivedUntilUpdatedAt` accuracy on PoolMember
- Fix `poolTotalAmountDistributedUntilUpdatedAt` on PoolMember
- Fix `totalNumberOfActivePools` on TokenStatistics
- Fix `totalSubscriptionsWithUnits` on TokenStatistics

## [2.2.0]

- Fix missing Token name/symbol in some cases
Expand Down
6 changes: 3 additions & 3 deletions packages/subgraph/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@superfluid-finance/subgraph",
"description": "Subgraph for the Superfluid Ethereum contracts.",
"version": "2.2.0",
"version": "2.2.1",
"dependencies": {
"@graphprotocol/graph-cli": "0.80.1",
"@graphprotocol/graph-ts": "0.35.1",
"@graphprotocol/graph-cli": "0.96.0",
"@graphprotocol/graph-ts": "0.38.0",
"@superfluid-finance/sdk-core": "0.9.0",
"mustache": "4.2.0"
},
Expand Down
18 changes: 3 additions & 15 deletions packages/subgraph/src/mappingHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ export function getOrInitPoolDistributor(

return poolDistributor;
}

export function updatePoolDistributorTotalAmountFlowedAndDistributed(
event: ethereum.Event,
poolDistributor: PoolDistributor
Expand Down Expand Up @@ -1504,7 +1505,6 @@ export function updateAggregateEntitiesTransferData(
tokenStatistic.save();
}


export function particleRTB(
perUnitSettledValue: BigInt,
perUnitFlowRate: BigInt,
Expand All @@ -1524,7 +1524,7 @@ export function monetaryUnitPoolMemberRTB(pool: Pool, poolMember: PoolMember, cu
);
const poolMemberPerUnitRTB = particleRTB(
poolMember.syncedPerUnitSettledValue,
poolMember.syncedPerUnitFlowRate,
BigInt.fromI32(0),
currentTimestamp,
poolMember.updatedAtTimestamp
);
Expand Down Expand Up @@ -1554,19 +1554,6 @@ export function settlePoolParticle(pool: Pool, block: ethereum.Block): Pool {
return pool;
}

export function settlePoolMemberParticle(poolMember: PoolMember, block: ethereum.Block): PoolMember {
poolMember.syncedPerUnitSettledValue = particleRTB(
poolMember.syncedPerUnitSettledValue,
poolMember.syncedPerUnitFlowRate,
block.timestamp,
poolMember.updatedAtTimestamp
);
poolMember.updatedAtTimestamp = block.timestamp;
poolMember.updatedAtBlockNumber = block.number;

return poolMember;
}

export function syncPoolMemberParticle(pool: Pool, poolMember: PoolMember): PoolMember {
poolMember.syncedPerUnitSettledValue = pool.perUnitSettledValue;
poolMember.syncedPerUnitFlowRate = pool.perUnitFlowRate;
Expand All @@ -1578,5 +1565,6 @@ export function syncPoolMemberParticle(pool: Pool, poolMember: PoolMember): Pool

export function settlePDPoolMemberMU(pool: Pool, poolMember: PoolMember, block: ethereum.Block): void {
poolMember.totalAmountReceivedUntilUpdatedAt = monetaryUnitPoolMemberRTB(pool, poolMember, block.timestamp);
poolMember.poolTotalAmountDistributedUntilUpdatedAt = pool.totalAmountDistributedUntilUpdatedAt;
poolMember = syncPoolMemberParticle(pool, poolMember);
}
35 changes: 24 additions & 11 deletions packages/subgraph/src/mappings/gdav1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,21 @@ export function handleFlowDistributionUpdated(
poolDistributor.flowRate = event.params.newDistributorToPoolFlowRate;
poolDistributor.save();


// Update Pool
let pool = getOrInitPool(event, event.params.pool.toHex());

// @note that we are duplicating update of updatedAtTimestamp/BlockNumber here
// in the two functions
pool = updatePoolParticleAndTotalAmountFlowedAndDistributed(event, pool);
pool.perUnitFlowRate = divideOrZero(event.params.newDistributorToPoolFlowRate, pool.totalUnits);
const previousTotalAmountDistributed = pool.totalAmountDistributedUntilUpdatedAt;

pool.flowRate = event.params.newTotalDistributionFlowRate;
pool.adjustmentFlowRate = event.params.adjustmentFlowRate;
pool.perUnitFlowRate = divideOrZero(pool.flowRate, pool.totalUnits);

const newEffectivePoolFlowRate = pool.perUnitFlowRate.times(pool.totalUnits);
pool.adjustmentFlowRate = pool.flowRate.minus(newEffectivePoolFlowRate);

pool.save();

const flowRateDelta = event.params.newDistributorToPoolFlowRate.minus(
Expand All @@ -255,9 +261,17 @@ export function handleFlowDistributionUpdated(
event.params.newDistributorToPoolFlowRate.equals(BIG_INT_ZERO);

// Update Token Statistics
const tokenStatistic = getOrInitTokenStatistic(
event.params.token,
event.block
);
if (previousTotalAmountDistributed.equals(BIG_INT_ZERO) && pool.totalAmountDistributedUntilUpdatedAt.gt(BIG_INT_ZERO)) {
tokenStatistic.totalNumberOfActivePools = tokenStatistic.totalNumberOfActivePools + 1;
}
tokenStatistic.save();

const eventName = "FlowDistributionUpdated";
updateTokenStatsStreamedUntilUpdatedAt(event.params.token, event.block);
_createTokenStatisticLogEntity(event, event.params.token, eventName);
updateTokenStatisticStreamData(
event.params.token,
event.params.newDistributorToPoolFlowRate,
Expand Down Expand Up @@ -325,14 +339,16 @@ export function handleInstantDistributionUpdated(

// Update Pool
let pool = getOrInitPool(event, event.params.pool.toHex());
const previousTotalAmountDistributed = pool.totalAmountDistributedUntilUpdatedAt;

// @note that we are duplicating update of updatedAtTimestamp/BlockNumber here
// in the two functions
pool = updatePoolParticleAndTotalAmountFlowedAndDistributed(event, pool);

// @note a speculations on what needs to be done
pool.perUnitSettledValue = pool.perUnitSettledValue.plus(divideOrZero(event.params.actualAmount, pool.totalUnits));
const previousTotalAmountDistributed =
pool.totalAmountDistributedUntilUpdatedAt;
const perUnitSettledValueDelta = divideOrZero(event.params.actualAmount, pool.totalUnits);
pool.perUnitSettledValue = pool.perUnitSettledValue.plus(perUnitSettledValueDelta);

pool.totalAmountInstantlyDistributedUntilUpdatedAt =
pool.totalAmountInstantlyDistributedUntilUpdatedAt.plus(
event.params.actualAmount
Expand All @@ -348,12 +364,9 @@ export function handleInstantDistributionUpdated(
event.params.token,
event.block
);

if (previousTotalAmountDistributed.equals(BIG_INT_ZERO)) {
tokenStatistic.totalNumberOfActivePools =
tokenStatistic.totalNumberOfActivePools + 1;
if (previousTotalAmountDistributed.equals(BIG_INT_ZERO) && pool.totalAmountDistributedUntilUpdatedAt.gt(BIG_INT_ZERO)) {
tokenStatistic.totalNumberOfActivePools = tokenStatistic.totalNumberOfActivePools + 1;
}

tokenStatistic.totalAmountDistributedUntilUpdatedAt =
tokenStatistic.totalAmountDistributedUntilUpdatedAt.plus(
event.params.actualAmount
Expand Down
30 changes: 14 additions & 16 deletions packages/subgraph/src/mappings/superfluidPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
updatePoolParticleAndTotalAmountFlowedAndDistributed,
updateTokenStatsStreamedUntilUpdatedAt,
} from "../mappingHelpers";
import { BIG_INT_ZERO, createEventID, initializeEventEntity, membershipWithUnitsExists } from "../utils";
import { BIG_INT_ZERO, createEventID, divideOrZero, initializeEventEntity, membershipWithUnitsExists } from "../utils";

// @note use deltas where applicable

Expand Down Expand Up @@ -53,29 +53,27 @@ export function handleDistributionClaimed(event: DistributionClaimed): void {
export function handleMemberUnitsUpdated(event: MemberUnitsUpdated): void {
let pool = getOrInitPool(event, event.address.toHex());
let poolMember = getOrInitOrUpdatePoolMember(event, event.address, event.params.member);
const totalAmountReceivedFromPoolBeforeUpdate = poolMember.totalAmountReceivedUntilUpdatedAt;

const previousUnits = poolMember.units;
const unitsDelta = event.params.newUnits.minus(previousUnits);
const oldTotalUnits = pool.totalUnits;
const oldPerUnitFlowRate = pool.perUnitFlowRate;
const newTotalUnits = pool.totalUnits.plus(unitsDelta);

pool = updatePoolParticleAndTotalAmountFlowedAndDistributed(event, pool);
settlePDPoolMemberMU(pool, poolMember, event.block);

const existingPoolFlowRate = pool.perUnitFlowRate.times(pool.totalUnits);
let newPerUnitFlowRate: BigInt;
let remainderRate: BigInt;

if (!newTotalUnits.equals(BIG_INT_ZERO)) {
newPerUnitFlowRate = existingPoolFlowRate.div(newTotalUnits);
remainderRate = existingPoolFlowRate.minus(newPerUnitFlowRate.times(newTotalUnits));
} else {
remainderRate = existingPoolFlowRate;
newPerUnitFlowRate = BIG_INT_ZERO;
}
const oldEffectivePoolFlowRate = oldPerUnitFlowRate.times(oldTotalUnits);

const newPerUnitFlowRate = divideOrZero(oldEffectivePoolFlowRate, newTotalUnits);
pool.perUnitFlowRate = newPerUnitFlowRate;
pool.totalUnits = newTotalUnits;

poolMember.syncedPerUnitFlowRate = poolMember.syncedPerUnitFlowRate.plus(remainderRate);
const newEffectivePoolFlowRate = newPerUnitFlowRate.times(newTotalUnits); // This will either be equal or less than the previous one.
pool.adjustmentFlowRate = pool.flowRate.minus(newEffectivePoolFlowRate);

pool.totalUnits = newTotalUnits;
poolMember.syncedPerUnitFlowRate = newPerUnitFlowRate;
poolMember.units = event.params.newUnits;

if (poolMember.isConnected) {
Expand All @@ -99,7 +97,7 @@ export function handleMemberUnitsUpdated(event: MemberUnitsUpdated): void {
updateAggregateDistributionAgreementData(
event.params.member,
event.params.token,
true, // has units
false, // does not have previous units
poolMember.isConnected,
true, // only place we increment subWithUnits
false, // not deleting
Expand All @@ -125,7 +123,7 @@ export function handleMemberUnitsUpdated(event: MemberUnitsUpdated): void {
updateAggregateDistributionAgreementData(
event.params.member,
event.params.token,
false, // has units
true, // has previous units
poolMember.isConnected,
false, // don't increment memberWithUnits
false, // not disconnecting membership
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,6 @@ describe("PoolMember ending up with wrong `totalAmountReceivedUntilUpdatedAt`",
handleFlowDistributionUpdated(firstFlowRateEvent);

// # First flow rate
if (pool) {
pool.updatedAtTimestamp = firstFlowRateEvent.block.timestamp;
}

// TODO: This fails, how has this already flown???
assert.fieldEquals(
Expand Down Expand Up @@ -409,9 +406,6 @@ describe("PoolMember ending up with wrong `totalAmountReceivedUntilUpdatedAt`",
mockedAppManifestAndRealtimeBalanceOf(superTokenAddress, bobAddress.toHexString(), updateBobEvent.block.timestamp);
handleMemberUnitsUpdated(updateBobEvent);

if (pool) {
pool.updatedAtTimestamp = updateBobEvent.block.timestamp;
}
assert.fieldEquals(
"Pool",
poolAddress.toHexString(),
Expand Down Expand Up @@ -445,9 +439,6 @@ describe("PoolMember ending up with wrong `totalAmountReceivedUntilUpdatedAt`",
mockedAppManifestAndRealtimeBalanceOf(superTokenAddress, aliceAddress.toHexString(), updateAliceEvent.block.timestamp);
handleMemberUnitsUpdated(updateAliceEvent);

if (pool) {
pool.updatedAtTimestamp = updateAliceEvent.block.timestamp;
}
assert.fieldEquals(
"PoolMember",
aliceId,
Expand Down
Loading
Loading