-
Notifications
You must be signed in to change notification settings - Fork 10
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
base: v3-canary
Are you sure you want to change the base?
Changes from all commits
04b54bf
bf13fc8
84891ac
e8d56ca
5131340
dbfac77
6ec01d8
6c57ad2
fefc9fb
301f5cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'backend': patch | ||
--- | ||
|
||
adding dynamic swap fee to fx pools |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Abi } from 'abitype'; | ||
import FX from '../../../pool/abi/FxPool.json'; | ||
import { getViemClient, ViemClient } from '../../../sources/viem-client'; | ||
import { Chain, PrismaPoolType } from '@prisma/client'; | ||
import { prisma } from '../../../../prisma/prisma-client'; | ||
import { prismaBulkExecuteOperations } from '../../../../prisma/prisma-util'; | ||
|
||
const update = async (data: { id: string; chain: Chain; typeData: any }[]) => { | ||
// Update the pool type data | ||
const updates = data.map(({ id, chain, typeData }) => | ||
prisma.prismaPool.update({ | ||
where: { id_chain: { id, chain } }, | ||
data: { typeData }, | ||
}), | ||
); | ||
|
||
await prismaBulkExecuteOperations(updates, false); | ||
}; | ||
|
||
export const syncPoolTypeOnchainData = async ( | ||
pools: { id: string; chain: Chain; address: string; type: PrismaPoolType; typeData: any }[], | ||
chain: Chain, | ||
) => { | ||
const viemClient = getViemClient(chain); | ||
|
||
// Get FX pools | ||
const fxPools = pools.filter((pool) => pool.type === 'FX'); | ||
const quoteTokens = await fetchFxQuoteTokens(fxPools, viemClient); | ||
await update(quoteTokens); | ||
|
||
return true; | ||
}; | ||
|
||
export const fetchFxQuoteTokens = async ( | ||
pools: { id: string; chain: Chain; address: string; typeData: any }[], | ||
viemClient: ViemClient, | ||
) => { | ||
// Fetch the tokens from the subgraph | ||
const contracts = pools.map(({ address }) => { | ||
return { | ||
address: address as `0x${string}`, | ||
abi: FX as Abi, | ||
functionName: 'derivatives', | ||
args: [1], | ||
}; | ||
}); | ||
|
||
const results = await viemClient.multicall({ contracts, allowFailure: true }); | ||
|
||
return results | ||
.map((call, index) => { | ||
// If the call failed, return null | ||
if (call.status === 'failure') return null; | ||
|
||
const typeData = { ...pools[index].typeData, quoteToken: (call.result as string).toLowerCase() }; | ||
|
||
return { | ||
id: pools[index].id, | ||
chain: pools[index].chain, | ||
typeData, | ||
}; | ||
}) | ||
.filter((quoteToken): quoteToken is { id: string; chain: Chain; typeData: any } => quoteToken !== null); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,11 +40,12 @@ export async function swapsUsd(swaps: SwapEvent[], chain: Chain): Promise<SwapEv | |
const tokenOut = tokenPrices.find((price) => price.tokenAddress === swap.payload.tokenOut.address); | ||
const feeToken = tokenPrices.find((price) => price.tokenAddress === swap.payload.fee.address); | ||
const surplusToken = tokenPrices.find((price) => price.tokenAddress === swap.payload.surplus?.address); | ||
const feeValueUSD = parseFloat(swap.payload.fee.amount) * (feeToken?.price || 0); | ||
|
||
const payload = { | ||
fee: { | ||
...swap.payload.fee, | ||
valueUSD: String((feeToken?.price || 0) * parseFloat(swap.payload.fee.amount)), | ||
valueUSD: String(feeValueUSD > 0 ? feeValueUSD : swap.payload.fee.valueUSD), | ||
Comment on lines
-47
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you set it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a passthrough for a default USD price coming from the subgraph, in case there is no pricing in the DB. It's set earlier and it important to the FX fee, because it's set based on the subgraph data of latestFx rates. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah gotcha, this is v2. This is just used as fallback so its fine 👍 |
||
}, | ||
tokenIn: { | ||
...swap.payload.tokenIn, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont think this works as a general approach. Some pool types will have mutable and immutable type data, meaning some will be synced at creation via subgraph and some will be synced via on-chain in regular intervals.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how does it matter? do you mean to avoid unnecessary update calls?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It actually doesnt really here as you handle FX pools outside of the "update type data flow".