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

adding dynamic swap fee to fx pools #1101

Open
wants to merge 4 commits into
base: v3-canary
Choose a base branch
from

Conversation

gmbronco
Copy link
Collaborator

@gmbronco gmbronco commented Oct 24, 2024

closes #802

Copy link

changeset-bot bot commented Oct 24, 2024

🦋 Changeset detected

Latest commit: 760c57c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
backend Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Collaborator

@franzns franzns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should think about how a negative fee and APR affects our UI and if this is what we want to show

// Replica of the subgraph logic:
// https://github.com/balancer/balancer-subgraph-v2/blob/60453224453bd07a0a3a22a8ad6cc26e65fd809f/src/mappings/vault.ts#L551-L564
if (swap.poolId.poolType === 'FX') {
const USDC_ADDRESS = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can we hardcode this? Isnt this different for each chain?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we only compare against USDC? No other FOREX such as EURC?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like all the markets are against USDC, but true that it needs to be done for other chains as well.

Copy link

@schystz schystz Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @franzns, @gmbronco, would it be possible to add a quoteToken property on the Pool model? This new prop will hold the quote token address, whether USDC, USDC.e, or any token.

This is how we assign the quoteToken value in our balancer-subgraph fork:

function handleNewFXPool(event: ethereum.Event, permissionless: boolean): void {
...
  pool.quoteToken = getFXPoolQuoteToken(poolAddress);
...
}

function getFXPoolQuoteToken(poolAddress: Address): Bytes {
  let poolContract = FXPool.bind(poolAddress);
  // FXPool **always** holds the quote token in the second position
  let call = poolContract.try_derivatives(BigInt.fromI32(1));
  if (call.reverted) {
    log.error('Failed to get quote token for FXPool: {}', [poolAddress.toHexString()]);
    return stringToBytes('0x');
  }
  return call.value;
}

We then refer to quoteToken instead of hardcoded USDC addresses to determine base and quote:

let isTokenInBase = tokenOutAddress === pool.quoteToken;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @franzns, @gmbronco, would it be possible to add a quoteToken property on the Pool model? This new prop will hold the quote token address, whether USDC, USDC.e, or any token.

This is how we assign the quoteToken value in our balancer-subgraph fork:

function handleNewFXPool(event: ethereum.Event, permissionless: boolean): void {
...
  pool.quoteToken = getFXPoolQuoteToken(poolAddress);
...
}

function getFXPoolQuoteToken(poolAddress: Address): Bytes {
  let poolContract = FXPool.bind(poolAddress);
  // FXPool **always** holds the quote token in the second position
  let call = poolContract.try_derivatives(BigInt.fromI32(1));
  if (call.reverted) {
    log.error('Failed to get quote token for FXPool: {}', [poolAddress.toHexString()]);
    return stringToBytes('0x');
  }
  return call.value;
}

We then refer to quoteToken instead of hardcoded USDC addresses to determine base and quote:

let isTokenInBase = tokenOutAddress === pool.quoteToken;

Yes, we should definitely do that. We already have pool type specific data where we can add the quote token. Is the quote token immutable?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the quote token is immutable for FX pools

}
}

feeUSD = String(feeFloatUSD);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think we talked about the option of a possible negative APR. Would that mean that his fee might be negative? What are the downstream effects if so?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there upside / downside? i assume it's just a property of FX pools. unless you mean if some other parts of the app will need to be updated. that i'm not sure. Is there anything that depends on the apr?

@schystz
Copy link

schystz commented Oct 31, 2024

Hi @gmbronco, don't we also need to update how the fees24h is computed in pool.snapshot-service.ts? (note: this file seems to exist in two places)

The code assumes a static pool swap fee, but FX pools have a dynamic swap fee

fees24h: volume24h * parseFloat(poolAtBlock.swapFee)

I'm thinking something like this might work:

fees24h: parseFloat(pool.totalSwapFee) - parseFloat(pool24hAgo.totalSwapFee)

@franzns
Copy link
Collaborator

franzns commented Oct 31, 2024

Hi @gmbronco, don't we also need to update how the fees24h is computed in pool.snapshot-service.ts? (note: this file seems to exist in two places)

The code assumes a static pool swap fee, but FX pools have a dynamic swap fee

fees24h: volume24h * parseFloat(poolAtBlock.swapFee)

I'm thinking something like this might work:

fees24h: parseFloat(pool.totalSwapFee) - parseFloat(pool24hAgo.totalSwapFee)

Good catch. We'll need to think about that as we'll change the snapshot generation not to rely on subgraph pricing anymore. We can calculate fees24h based on the swap events for example which would be more accurate.

@schystz
Copy link

schystz commented Oct 31, 2024

Hi @gmbronco, don't we also need to update how the fees24h is computed in pool.snapshot-service.ts? (note: this file seems to exist in two places)
The code assumes a static pool swap fee, but FX pools have a dynamic swap fee

fees24h: volume24h * parseFloat(poolAtBlock.swapFee)

I'm thinking something like this might work:

fees24h: parseFloat(pool.totalSwapFee) - parseFloat(pool24hAgo.totalSwapFee)

Good catch. We'll need to think about that as we'll change the snapshot generation not to rely on subgraph pricing anymore. We can calculate fees24h based on the swap events for example which would be more accurate.

Great, that makes sense to me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support FX pools (apr, swap fee)
3 participants