Skip to content

Commit

Permalink
Merge pull request #18 from balancer/swap-fee-breakdown
Browse files Browse the repository at this point in the history
breakdown swap fee amounts
  • Loading branch information
mendesfabio authored Feb 11, 2025
2 parents 18b09d8 + 30b8f43 commit ae83cfc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
8 changes: 8 additions & 0 deletions subgraphs/v3-vault/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ type PoolToken @entity {
volume: BigDecimal!
"Total swap fees collected for this token"
totalSwapFee: BigDecimal!
"Total base swap fees collected using static swap fee"
totalSwapFeeBase: BigDecimal!
"Total difference in swap fees collected due to dynamic fees"
totalSwapFeeDelta: BigDecimal!
"Total static swap fees collected for this token"
totalStaticSwapFee: BigDecimal!
"Total dynamic swap fees collected for this token"
Expand Down Expand Up @@ -250,6 +254,10 @@ type Swap @entity(immutable: true) {
swapFeeToken: Bytes!
"Amount of swap fees"
swapFeeAmount: BigDecimal!
"Base fee amount calculated using static swap fee"
swapFeeBaseAmount: BigDecimal!
"Difference between actual fee amount and base fee amount due to dynamic fees"
swapFeeDeltaAmount: BigDecimal!
"Swap fee percentage"
swapFeePercentage: BigDecimal!
"Indicates whether the swap fee is dynamic"
Expand Down
2 changes: 2 additions & 0 deletions subgraphs/v3-vault/src/helpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ export function createPoolToken(
poolToken.balance = ZERO_BD;
poolToken.volume = ZERO_BD;
poolToken.totalSwapFee = ZERO_BD;
poolToken.totalSwapFeeBase = ZERO_BD;
poolToken.totalSwapFeeDelta = ZERO_BD;
poolToken.totalStaticSwapFee = ZERO_BD;
poolToken.totalDynamicSwapFee = ZERO_BD;
poolToken.totalProtocolSwapFee = ZERO_BD;
Expand Down
20 changes: 17 additions & 3 deletions subgraphs/v3-vault/src/mappings/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,15 @@ export function handleSwap(event: SwapEvent): void {
swapFeeToken.decimals
);

let hasDynamicSwapFee = pool.swapFee != swap.swapFeePercentage;
let swapFeeBaseAmount = swapFeeAmount;
let swapFeeDeltaAmount = ZERO_BD;
if (hasDynamicSwapFee) {
let swapFeeDelta = swap.swapFeePercentage.minus(pool.swapFee);
swapFeeBaseAmount = tokenAmountIn.times(pool.swapFee);
swapFeeDeltaAmount = tokenAmountIn.times(swapFeeDelta);
}

swap.pool = event.params.pool;
swap.tokenIn = event.params.tokenIn;
swap.tokenInSymbol = tokenIn.symbol;
Expand All @@ -318,11 +327,12 @@ export function handleSwap(event: SwapEvent): void {
swap.tokenOutSymbol = tokenOut.symbol;
swap.tokenAmountOut = tokenAmountOut;
swap.swapFeeAmount = swapFeeAmount;
swap.swapFeeBaseAmount = swapFeeBaseAmount;
swap.swapFeeDeltaAmount = swapFeeDeltaAmount;
swap.swapFeeToken = event.params.tokenIn;
swap.swapFeePercentage = scaleDown(event.params.swapFeePercentage, 18);
swap.hasDynamicSwapFee = pool.swapFee != swap.swapFeePercentage;
swap.hasDynamicSwapFee = hasDynamicSwapFee;
swap.user = event.transaction.from;

swap.logIndex = event.logIndex;
swap.blockNumber = event.block.number;
swap.blockTimestamp = event.block.timestamp;
Expand Down Expand Up @@ -357,9 +367,13 @@ export function handleSwap(event: SwapEvent): void {
aggregateSwapFeeAmount
);

if (swap.hasDynamicSwapFee) {
if (hasDynamicSwapFee) {
poolTokenIn.totalDynamicSwapFee =
poolTokenIn.totalDynamicSwapFee.plus(swapFeeAmount);
poolTokenIn.totalSwapFeeBase =
poolTokenIn.totalSwapFeeBase.plus(swapFeeBaseAmount);
poolTokenIn.totalSwapFeeDelta =
poolTokenIn.totalSwapFeeDelta.plus(swapFeeDeltaAmount);
} else {
poolTokenIn.totalStaticSwapFee =
poolTokenIn.totalStaticSwapFee.plus(swapFeeAmount);
Expand Down

0 comments on commit ae83cfc

Please sign in to comment.