Skip to content

Commit d36335c

Browse files
authored
Merge pull request #27 from balancer/add-pool-states
fix dynamic swap fee rounding, add pool pause and recovery mode
2 parents f6425cb + 9f82c52 commit d36335c

File tree

10 files changed

+93
-13
lines changed

10 files changed

+93
-13
lines changed

subgraphs/v3-vault/schema.graphql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ type Pool @entity {
4848
transactionHash: Bytes!
4949
"Indicates whether the Pool has been initialized"
5050
isInitialized: Boolean!
51+
"Indicates whether the Pool is currently paused"
52+
isPaused: Boolean!
53+
"Indicates whether the Pool is in recovery mode"
54+
isInRecoveryMode: Boolean!
55+
5156

5257
"Account empowered to pause/unpause the pool"
5358
pauseManager: Bytes!

subgraphs/v3-vault/src/helpers/math.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function mulDown(a: BigInt, b: BigInt): BigInt {
99
return product.div(ONE);
1010
}
1111

12-
export function computeAggregateSwapFee(
12+
export function mulDownSwapFee(
1313
swapFeeAmountRaw: BigInt,
1414
swapFeePercentage: BigDecimal
1515
): BigInt {

subgraphs/v3-vault/src/mappings/vault.ts

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import {
66
LiquidityAddedToBuffer,
77
LiquidityRemoved,
88
LiquidityRemovedFromBuffer,
9+
PoolPausedStateChanged,
10+
PoolRecoveryModeStateChanged,
911
PoolRegistered,
1012
Swap as SwapEvent,
13+
SwapFeePercentageChanged,
1114
Unwrap,
1215
Wrap,
1316
} from "../types/Vault/Vault";
@@ -37,7 +40,7 @@ import { BPT } from "../types/templates";
3740
import { ERC20 } from "../types/Vault/ERC20";
3841
import { VaultExtension } from "../types/Vault/VaultExtension";
3942
import { ERC4626 } from "../types/Vault/ERC4626";
40-
import { computeAggregateSwapFee } from "../helpers/math";
43+
import { mulDownSwapFee } from "../helpers/math";
4144

4245
/************************************
4346
******* POOLS REGISTRATIONS ********
@@ -54,6 +57,8 @@ export function handlePoolRegistered(event: PoolRegistered): void {
5457
pool.pauseWindowEndTime = event.params.pauseWindowEndTime;
5558
pool.totalShares = ZERO_BD;
5659
pool.isInitialized = false;
60+
pool.isInRecoveryMode = false;
61+
pool.isPaused = false;
5762
pool.swapsCount = ZERO_BI;
5863
pool.holdersCount = ZERO_BI;
5964
pool.protocolSwapFee = vault.protocolSwapFee;
@@ -189,10 +194,7 @@ export function handleLiquidityAdded(event: LiquidityAdded): void {
189194
);
190195

191196
let aggregateSwapFeeAmount = scaleDown(
192-
computeAggregateSwapFee(
193-
event.params.swapFeeAmountsRaw[i],
194-
pool.protocolSwapFee
195-
),
197+
mulDownSwapFee(event.params.swapFeeAmountsRaw[i], pool.protocolSwapFee),
196198
poolToken.decimals
197199
);
198200

@@ -259,10 +261,7 @@ export function handleLiquidityRemoved(event: LiquidityRemoved): void {
259261
);
260262

261263
let aggregateSwapFeeAmount = scaleDown(
262-
computeAggregateSwapFee(
263-
event.params.swapFeeAmountsRaw[i],
264-
pool.protocolSwapFee
265-
),
264+
mulDownSwapFee(event.params.swapFeeAmountsRaw[i], pool.protocolSwapFee),
266265
poolToken.decimals
267266
);
268267

@@ -331,8 +330,14 @@ export function handleSwap(event: SwapEvent): void {
331330
let swapFeeDeltaAmount = ZERO_BD;
332331
if (hasDynamicSwapFee) {
333332
let swapFeeDelta = swapFeePercentage.minus(pool.swapFee);
334-
swapFeeBaseAmount = tokenAmountIn.times(pool.swapFee);
335-
swapFeeDeltaAmount = tokenAmountIn.times(swapFeeDelta);
333+
swapFeeBaseAmount = scaleDown(
334+
mulDownSwapFee(event.params.amountIn, pool.swapFee),
335+
tokenIn.decimals
336+
);
337+
swapFeeDeltaAmount = scaleDown(
338+
mulDownSwapFee(event.params.amountIn, swapFeeDelta),
339+
tokenIn.decimals
340+
);
336341
}
337342

338343
swap.pool = event.params.pool;
@@ -369,7 +374,7 @@ export function handleSwap(event: SwapEvent): void {
369374
}
370375

371376
let aggregateSwapFeeAmount = scaleDown(
372-
computeAggregateSwapFee(event.params.swapFeeAmount, pool.protocolSwapFee),
377+
mulDownSwapFee(event.params.swapFeeAmount, pool.protocolSwapFee),
373378
poolTokenIn.decimals
374379
);
375380

@@ -594,3 +599,31 @@ export function handleWrap(event: Wrap): void {
594599
);
595600
buffer.save();
596601
}
602+
603+
/************************************
604+
********** POOLS STATE *************
605+
************************************/
606+
607+
export function handleSwapFeePercentageChanged(
608+
event: SwapFeePercentageChanged
609+
): void {
610+
let pool = Pool.load(event.params.pool) as Pool;
611+
pool.swapFee = scaleDown(event.params.swapFeePercentage, 18);
612+
pool.save();
613+
}
614+
615+
export function handlePoolRecoveryModeStateChanged(
616+
event: PoolRecoveryModeStateChanged
617+
): void {
618+
let pool = Pool.load(event.params.pool) as Pool;
619+
pool.isInRecoveryMode = event.params.recoveryMode;
620+
pool.save();
621+
}
622+
623+
export function handlePoolPausedStateChanged(
624+
event: PoolPausedStateChanged
625+
): void {
626+
let pool = Pool.load(event.params.pool) as Pool;
627+
pool.isPaused = event.params.paused;
628+
pool.save();
629+
}

subgraphs/v3-vault/subgraph.arbitrum-one.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ dataSources:
4444
handler: handleWrap
4545
- event: Unwrap(indexed address,uint256,uint256,bytes32)
4646
handler: handleUnwrap
47+
- event: SwapFeePercentageChanged(indexed address,uint256)
48+
handler: handleSwapFeePercentageChanged
49+
- event: PoolRecoveryModeStateChanged(indexed address,bool)
50+
handler: handlePoolRecoveryModeStateChanged
51+
- event: PoolPausedStateChanged(indexed address,bool)
52+
handler: handlePoolPausedStateChanged
4753
file: ./src/mappings/vault.ts
4854
- kind: ethereum
4955
name: ProtocolFeeController

subgraphs/v3-vault/subgraph.base.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ dataSources:
4444
handler: handleWrap
4545
- event: Unwrap(indexed address,uint256,uint256,bytes32)
4646
handler: handleUnwrap
47+
- event: SwapFeePercentageChanged(indexed address,uint256)
48+
handler: handleSwapFeePercentageChanged
49+
- event: PoolRecoveryModeStateChanged(indexed address,bool)
50+
handler: handlePoolRecoveryModeStateChanged
51+
- event: PoolPausedStateChanged(indexed address,bool)
52+
handler: handlePoolPausedStateChanged
4753
file: ./src/mappings/vault.ts
4854
- kind: ethereum
4955
name: ProtocolFeeController

subgraphs/v3-vault/subgraph.gnosis.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ dataSources:
4444
handler: handleWrap
4545
- event: Unwrap(indexed address,uint256,uint256,bytes32)
4646
handler: handleUnwrap
47+
- event: SwapFeePercentageChanged(indexed address,uint256)
48+
handler: handleSwapFeePercentageChanged
49+
- event: PoolRecoveryModeStateChanged(indexed address,bool)
50+
handler: handlePoolRecoveryModeStateChanged
51+
- event: PoolPausedStateChanged(indexed address,bool)
52+
handler: handlePoolPausedStateChanged
4753
file: ./src/mappings/vault.ts
4854
- kind: ethereum
4955
name: ProtocolFeeController

subgraphs/v3-vault/subgraph.sepolia.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ dataSources:
4444
handler: handleWrap
4545
- event: Unwrap(indexed address,uint256,uint256,bytes32)
4646
handler: handleUnwrap
47+
- event: SwapFeePercentageChanged(indexed address,uint256)
48+
handler: handleSwapFeePercentageChanged
49+
- event: PoolRecoveryModeStateChanged(indexed address,bool)
50+
handler: handlePoolRecoveryModeStateChanged
51+
- event: PoolPausedStateChanged(indexed address,bool)
52+
handler: handlePoolPausedStateChanged
4753
file: ./src/mappings/vault.ts
4854
- kind: ethereum
4955
name: ProtocolFeeController

subgraphs/v3-vault/subgraph.sonic.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ dataSources:
4444
handler: handleWrap
4545
- event: Unwrap(indexed address,uint256,uint256,bytes32)
4646
handler: handleUnwrap
47+
- event: SwapFeePercentageChanged(indexed address,uint256)
48+
handler: handleSwapFeePercentageChanged
49+
- event: PoolRecoveryModeStateChanged(indexed address,bool)
50+
handler: handlePoolRecoveryModeStateChanged
51+
- event: PoolPausedStateChanged(indexed address,bool)
52+
handler: handlePoolPausedStateChanged
4753
file: ./src/mappings/vault.ts
4854
- kind: ethereum
4955
name: ProtocolFeeController

subgraphs/v3-vault/subgraph.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ dataSources:
4444
handler: handleWrap
4545
- event: Unwrap(indexed address,uint256,uint256,bytes32)
4646
handler: handleUnwrap
47+
- event: SwapFeePercentageChanged(indexed address,uint256)
48+
handler: handleSwapFeePercentageChanged
49+
- event: PoolRecoveryModeStateChanged(indexed address,bool)
50+
handler: handlePoolRecoveryModeStateChanged
51+
- event: PoolPausedStateChanged(indexed address,bool)
52+
handler: handlePoolPausedStateChanged
4753
file: ./src/mappings/vault.ts
4854
- kind: ethereum
4955
name: ProtocolFeeController

subgraphs/v3-vault/template.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ dataSources:
4444
handler: handleWrap
4545
- event: Unwrap(indexed address,uint256,uint256,bytes32)
4646
handler: handleUnwrap
47+
- event: SwapFeePercentageChanged(indexed address,uint256)
48+
handler: handleSwapFeePercentageChanged
49+
- event: PoolRecoveryModeStateChanged(indexed address,bool)
50+
handler: handlePoolRecoveryModeStateChanged
51+
- event: PoolPausedStateChanged(indexed address,bool)
52+
handler: handlePoolPausedStateChanged
4753
file: ./src/mappings/vault.ts
4854

4955
- kind: ethereum

0 commit comments

Comments
 (0)