Skip to content

Commit ec66ad2

Browse files
committed
use declarative eth calls
- it is a new subgraph feature - we hope that it makes our subgraph index much faster
1 parent 0056560 commit ec66ad2

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

packages/subgraph/src/mappingHelpers.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,16 +1002,15 @@ function updateATSBalanceAndUpdatedAt(
10021002
// If the balance has been updated in this block without an RPC, it's better to be safe than sorry and just get the final accurate state from the RPC.
10031003
const hasBalanceBeenUpdatedInThisBlock = accountTokenSnapshot.updatedAtBlockNumber === block.number;
10041004

1005-
if (balanceDelta && isAccountWithOnlyVeryPredictableBalanceSources && !hasBalanceBeenUpdatedInThisBlock) {
1005+
if (balanceDelta !== null && isAccountWithOnlyVeryPredictableBalanceSources && !hasBalanceBeenUpdatedInThisBlock) {
10061006
accountTokenSnapshot.balanceUntilUpdatedAt = accountTokenSnapshot.balanceUntilUpdatedAt.plus(balanceDelta);
10071007
} else {
10081008
// if the account has any subscriptions with units we assume that
10091009
// the balance data requires a RPC call for balance because we did not
10101010
// have claim events there and we do not count distributions
10111011
// for subscribers
1012-
const newBalanceResult = superTokenContract.try_realtimeBalanceOf(
1013-
Address.fromString(accountTokenSnapshot.account),
1014-
block.timestamp
1012+
const newBalanceResult = superTokenContract.try_realtimeBalanceOfNow(
1013+
Address.fromString(accountTokenSnapshot.account)
10151014
);
10161015

10171016
const balanceBeforeUpdate = accountTokenSnapshot.balanceUntilUpdatedAt;

packages/subgraph/subgraph.template.yaml

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
specVersion: 1.0.0
1+
specVersion: 1.2.0
22
description: Subgraph for the Superfluid Protocol V1 contracts.
33
repository: https://github.com/superfluid-finance/protocol-monorepo
44
schema:
@@ -21,7 +21,7 @@ dataSources:
2121
entities:
2222
- CustomSuperTokenCreatedEvent
2323
- SuperTokenCreatedEvent
24-
- SuperTokenLogicCreatedEvent
24+
- SuperTokenLogicCreatedEventß
2525
- Token
2626
- TokenStatistic
2727
abis:
@@ -37,12 +37,24 @@ dataSources:
3737
- event: SuperTokenCreated(indexed address)
3838
handler: handleSuperTokenCreated
3939
receipt: true
40+
calls:
41+
getHost: ISuperToken[event.params.token].getHost()
42+
getUnderlyingToken: ISuperToken[event.params.token].getUnderlyingToken()
43+
getSymbol: ISuperToken[event.params.token].symbol()
44+
getDecimals: ISuperToken[event.params.token].decimals()
4045
- event: CustomSuperTokenCreated(indexed address)
4146
handler: handleCustomSuperTokenCreated
4247
receipt: true
48+
calls:
49+
getHost: ISuperToken[event.params.token].getHost()
50+
getUnderlyingToken: ISuperToken[event.params.token].getUnderlyingToken()
51+
getSymbol: ISuperToken[event.params.token].symbol()
52+
getDecimals: ISuperToken[event.params.token].decimals()
4353
- event: SuperTokenLogicCreated(indexed address)
4454
handler: handleSuperTokenLogicCreated
4555
receipt: true
56+
calls:
57+
getHost: ISuperToken[event.params.token].getHost()
4658
- kind: ethereum/contract
4759
name: Host
4860
network: {{ network }}
@@ -126,6 +138,9 @@ dataSources:
126138
- event: FlowUpdated(indexed address,indexed address,indexed address,int96,int256,int256,bytes)
127139
handler: handleFlowUpdated
128140
receipt: true
141+
calls:
142+
balanceOfSender: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.sender)
143+
balanceOfReceiver: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.receiver)
129144
- event: FlowUpdatedExtension(indexed address,uint256)
130145
handler: handleFlowUpdatedExtension
131146
receipt: true
@@ -179,9 +194,12 @@ dataSources:
179194
- event: IndexDistributionClaimed(indexed address,indexed address,indexed uint32,address,uint256)
180195
handler: handleIndexDistributionClaimed
181196
receipt: true
197+
calls:
198+
balanceOfSubscriber: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.subscriber)
182199
- event: IndexUpdated(indexed address,indexed address,indexed uint32,uint128,uint128,uint128,uint128,bytes)
183200
handler: handleIndexUpdated
184201
receipt: true
202+
# TODO
185203
- event: IndexSubscribed(indexed address,indexed address,indexed uint32,address,bytes)
186204
handler: handleIndexSubscribed
187205
receipt: true
@@ -194,15 +212,25 @@ dataSources:
194212
- event: SubscriptionApproved(indexed address,indexed address,address,uint32,bytes)
195213
handler: handleSubscriptionApproved
196214
receipt: true
215+
calls:
216+
balanceOfSubscriber: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.subscriber)
197217
- event: SubscriptionDistributionClaimed(indexed address,indexed address,address,uint32,uint256)
198218
handler: handleSubscriptionDistributionClaimed
199219
receipt: true
220+
calls:
221+
balanceOfPublisher: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.publisher)
222+
balanceOfSubscriber: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.subscriber)
200223
- event: SubscriptionRevoked(indexed address,indexed address,address,uint32,bytes)
201224
handler: handleSubscriptionRevoked
202225
receipt: true
226+
calls:
227+
balanceOfSubscriber: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.subscriber)
203228
- event: SubscriptionUnitsUpdated(indexed address,indexed address,address,uint32,uint128,bytes)
204229
handler: handleSubscriptionUnitsUpdated
205230
receipt: true
231+
calls:
232+
balanceOfPublisher: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.publisher)
233+
balanceOfSubscriber: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.subscriber)
206234
- kind: ethereum/contract
207235
name: GeneralDistributionAgreementV1
208236
network: {{ network }}
@@ -247,15 +275,23 @@ dataSources:
247275
- event: FlowDistributionUpdated(indexed address,indexed address,indexed address,address,int96,int96,int96,address,int96,bytes)
248276
handler: handleFlowDistributionUpdated
249277
receipt: true
278+
calls:
279+
balanceOfDistributor: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.distributor)
250280
- event: InstantDistributionUpdated(indexed address,indexed address,indexed address,address,uint256,uint256,bytes)
251281
handler: handleInstantDistributionUpdated
252282
receipt: true
283+
calls:
284+
balanceOfDistributor: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.distributor)
253285
- event: PoolConnectionUpdated(indexed address,indexed address,indexed address,bool,bytes)
254286
handler: handlePoolConnectionUpdated
255287
receipt: true
288+
calls:
289+
balanceOfAccount: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.account)
256290
- event: PoolCreated(indexed address,indexed address,address)
257291
handler: handlePoolCreated
258292
receipt: true
293+
calls:
294+
balanceOfAdmin: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.admin)
259295
- kind: ethereum/contract
260296
name: ResolverV1
261297
network: {{ network }}
@@ -327,9 +363,17 @@ templates:
327363
- event: AgreementLiquidatedBy(address,indexed address,bytes32,indexed address,indexed address,uint256,uint256)
328364
handler: handleAgreementLiquidatedBy
329365
receipt: true
366+
calls:
367+
balanceOfLiquidator: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.liquidatorAccount)
368+
balanceOfPenalty: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.penaltyAccount)
369+
balanceOfBond: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.bondAccount)
330370
- event: AgreementLiquidatedV2(indexed address,bytes32,indexed address,indexed address,address,uint256,int256,bytes)
331371
handler: handleAgreementLiquidatedV2
332372
receipt: true
373+
calls:
374+
balanceOfLiquidator: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.liquidatorAccount)
375+
balanceOfTarget: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.targetAccount)
376+
balanceOfRewardReceiver: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.rewardAmountReceiver)
333377
- event: Burned(indexed address,indexed address,uint256,bytes,bytes)
334378
handler: handleBurned
335379
receipt: true
@@ -342,12 +386,18 @@ templates:
342386
- event: TokenUpgraded(indexed address,uint256)
343387
handler: handleTokenUpgraded
344388
receipt: true
389+
# Decided not to do calls here as the RPC call might not always happen.
345390
- event: TokenDowngraded(indexed address,uint256)
346391
handler: handleTokenDowngraded
347392
receipt: true
393+
# Decided not to do calls here as the RPC call might not always happen.
348394
- event: Transfer(indexed address,indexed address,uint256)
349395
handler: handleTransfer
350396
receipt: true
397+
calls:
398+
# Decided to parallelize here even though the RPC call might not always happen in the handler.
399+
balanceOfFrom: ISuperToken[event.address].realtimeBalanceOfNow(event.params.from)
400+
balanceOfTo: ISuperToken[event.address].realtimeBalanceOfNow(event.params.to)
351401
- event: Approval(indexed address,indexed address,uint256)
352402
handler: handleApproval
353403
receipt: true
@@ -452,6 +502,10 @@ templates:
452502
- event: MemberUnitsUpdated(indexed address,indexed address,uint128,uint128)
453503
handler: handleMemberUnitsUpdated
454504
receipt: true
505+
calls:
506+
balanceOfMember: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.member)
455507
- event: DistributionClaimed(indexed address,indexed address,int256,int256)
456508
handler: handleDistributionClaimed
457509
receipt: true
510+
calls:
511+
balanceOfMember: ISuperToken[event.params.token].realtimeBalanceOfNow(event.params.member)

packages/subgraph/tests/mockedFunctions.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export function mockedHandleFlowUpdatedRPCCalls(
110110

111111
mockedResolverGet(resolverAddress, "supertokens.v1." + tokenSymbol, ZERO_ADDRESS.toHexString());
112112

113-
// updateATSStreamedAndBalanceUntilUpdatedAt => updateATSBalanceAndUpdatedAt => try_realtimeBalanceOf(sender)
113+
// updateATSStreamedAndBalanceUntilUpdatedAt => updateATSBalanceAndUpdatedAt => try_realtimeBalanceOfNow(sender)
114114
mockedRealtimeBalanceOf(
115115
superToken,
116116
sender,
@@ -119,7 +119,7 @@ export function mockedHandleFlowUpdatedRPCCalls(
119119
flowRate,
120120
BIG_INT_ZERO
121121
);
122-
// updateATSStreamedAndBalanceUntilUpdatedAt => updateATSBalanceAndUpdatedAt => try_realtimeBalanceOf(receiver)
122+
// updateATSStreamedAndBalanceUntilUpdatedAt => updateATSBalanceAndUpdatedAt => try_realtimeBalanceOfNow(receiver)
123123
mockedRealtimeBalanceOf(
124124
superToken,
125125
receiver,
@@ -274,17 +274,17 @@ export function mockedRealtimeBalanceOf(
274274
): void {
275275
createMockedFunction(
276276
Address.fromString(superTokenAddress),
277-
"realtimeBalanceOf",
278-
"realtimeBalanceOf(address,uint256):(int256,uint256,uint256)"
277+
"realtimeBalanceOfNow",
278+
"realtimeBalanceOfNow(address):(int256,uint256,uint256,uint256)"
279279
)
280280
.withArgs([
281-
getETHAddress(accountAddress),
282-
getETHUnsignedBigInt(timestamp),
281+
getETHAddress(accountAddress)
283282
])
284283
.returns([
285284
getETHSignedBigInt(expectedAvailableBalance),
286285
getETHUnsignedBigInt(expectedDeposit),
287286
getETHUnsignedBigInt(expectedOwedDeposit),
287+
getETHUnsignedBigInt(timestamp)
288288
]);
289289
}
290290

0 commit comments

Comments
 (0)