-
Notifications
You must be signed in to change notification settings - Fork 44
Add filled form event #767
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
Closed
vyorkin
wants to merge
14
commits into
main
from
feature/add-form-events-to-bridge-and-swap-WLT-6502
Closed
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7b03d12
Add filled form event
vyorkin 6d54f55
Rename
vyorkin f95a1cb
Send event from bridge form as well
vyorkin 401d443
Refactoring, fixes
vyorkin f0fc9bd
Update scope in BridgeForm
vyorkin 30b40a3
Refactoring, fixes
vyorkin 5f2ca4b
Add missing fields, refactoring, fixes
vyorkin f21dcdd
Refactoring, use form snapshots
vyorkin b03c829
Fixes
vyorkin e0ff90f
Refactoring, fixes
vyorkin e770548
Refactoring, fixes
vyorkin 5a086ea
Fix action_type
vyorkin 4b9f2d5
Fix price impact calculation bug
vyorkin b837faa
Use onQuotesReceived callback instead of custom emitter event
vyorkin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import type { | ||
| CustomConfiguration, | ||
| EmptyAddressPosition, | ||
| } from '@zeriontech/transactions'; | ||
| import BigNumber from 'bignumber.js'; | ||
| import type { AddressPosition, Asset } from 'defi-sdk'; | ||
| import { createChain } from 'src/modules/networks/Chain'; | ||
| import { backgroundQueryClient } from 'src/modules/query-client/query-client.background'; | ||
| import { ZerionAPI } from 'src/modules/zerion-api/zerion-api.background'; | ||
| import type { Quote } from 'src/shared/types/Quote'; | ||
| import { | ||
| calculatePriceImpact, | ||
| isHighValueLoss, | ||
| isSignificantValueLoss, | ||
| } from 'src/ui/pages/SwapForm/shared/price-impact'; | ||
| import { getCommonQuantity } from 'src/modules/networks/asset'; | ||
| import { | ||
| assetQuantityToValue, | ||
| createQuantityConverter, | ||
| toMaybeArr, | ||
| } from './helpers'; | ||
|
|
||
| export interface AnalyticsFormData { | ||
| spendAsset: Asset; | ||
| receiveAsset: Asset; | ||
| spendPosition: AddressPosition | EmptyAddressPosition; | ||
| configuration: CustomConfiguration; | ||
| } | ||
|
|
||
| async function fetchAssetFullInfo( | ||
| params: Parameters<typeof ZerionAPI.assetGetFungibleFullInfo>[0] | ||
| ) { | ||
| return backgroundQueryClient.fetchQuery({ | ||
| queryKey: ['ZerionAPI.fetchAssetFullInfo', params], | ||
| queryFn: () => ZerionAPI.assetGetFungibleFullInfo(params), | ||
| // Here this endpoint is used to fetch FDV (Fully Diluted Valuation) for analytics. | ||
| // While FDV can change quickly for new tokens, in practice backend/indexing services typically update this data less frequently. | ||
| // The main purpose of caching here is to reduce redundant requests while the user interacts with the form. | ||
| // This 30-minute cache should provide a good balance between data freshness and efficiency. | ||
| staleTime: 1000 * 60 * 30, | ||
| }); | ||
| } | ||
|
|
||
| export async function formDataToAnalytics( | ||
| scope: 'Swap' | 'Bridge', | ||
| { | ||
| currency, | ||
| formData: { spendAsset, receiveAsset, spendPosition, configuration }, | ||
| quote, | ||
| }: { | ||
| currency: string; | ||
| formData: AnalyticsFormData; | ||
| quote: Quote; | ||
| } | ||
| ) { | ||
| const spendAssetInfo = await fetchAssetFullInfo({ | ||
| fungibleId: spendAsset.asset_code, | ||
| currency, | ||
| }); | ||
| const receiveAssetInfo = await fetchAssetFullInfo({ | ||
| fungibleId: receiveAsset.asset_code, | ||
| currency, | ||
| }); | ||
|
|
||
| const fdvAssetSent = spendAssetInfo.data.fungible.meta.fullyDilutedValuation; | ||
| const fdvAssetReceived = | ||
| receiveAssetInfo.data.fungible.meta.fullyDilutedValuation; | ||
|
|
||
| const zerion_fee_percentage = quote.protocol_fee; | ||
| const feeAmount = quote.protocol_fee_amount; | ||
| const inputChain = createChain(quote.input_chain); | ||
| const outputChain = createChain(quote.output_chain); | ||
| const zerion_fee_usd_amount = assetQuantityToValue( | ||
| { quantity: feeAmount, asset: spendAsset }, | ||
| inputChain | ||
| ); | ||
| const usdAmountSend = assetQuantityToValue( | ||
| { quantity: quote.input_amount_estimation, asset: spendAsset }, | ||
| inputChain | ||
| ); | ||
| const usdAmountReceived = assetQuantityToValue( | ||
| { quantity: quote.output_amount_estimation, asset: receiveAsset }, | ||
| outputChain | ||
| ); | ||
|
|
||
| const inputValue = quote.input_amount_estimation; | ||
| const outputValue = quote.output_amount_estimation; | ||
|
|
||
| const enough_balance = new BigNumber(spendPosition?.quantity || 0).gt( | ||
| quote.input_amount_estimation | ||
| ); | ||
|
|
||
| const convertQuantity = createQuantityConverter(inputChain); | ||
|
|
||
| const bridgeFeeAmountInUsd = | ||
| scope === 'Bridge' | ||
| ? getCommonQuantity({ | ||
| baseQuantity: quote.bridge_fee_amount, | ||
| chain: createChain(quote.input_chain), | ||
| asset: spendAsset, | ||
| }).times(spendAsset.price?.value || 0) | ||
| : null; | ||
|
|
||
| const priceImpact = calculatePriceImpact({ | ||
| inputValue, | ||
| outputValue, | ||
| inputAsset: spendAsset, | ||
| outputAsset: receiveAsset, | ||
| }); | ||
|
|
||
| const isHighPriceImpact = priceImpact && isHighValueLoss(priceImpact); | ||
| const outputAmountColor = | ||
| priceImpact && isSignificantValueLoss(priceImpact) ? 'red' : 'grey'; | ||
|
|
||
| const assetAmountSent = convertQuantity({ | ||
| asset: spendAsset, | ||
| quantity: quote.input_amount_estimation, | ||
| }); | ||
| const assetAmountReceived = convertQuantity({ | ||
| asset: receiveAsset, | ||
| quantity: quote.output_amount_estimation, | ||
| }); | ||
|
|
||
| return { | ||
| usd_amount_sent: toMaybeArr([usdAmountSend]), | ||
| usd_amount_received: toMaybeArr([usdAmountReceived]), | ||
| asset_amount_sent: toMaybeArr([assetAmountSent]), | ||
| asset_amount_received: toMaybeArr([assetAmountReceived]), | ||
| asset_name_sent: toMaybeArr([spendAsset?.name]), | ||
| asset_name_received: toMaybeArr([receiveAsset?.name]), | ||
| asset_address_sent: toMaybeArr([spendAsset?.asset_code]), | ||
| asset_address_received: toMaybeArr([receiveAsset?.asset_code]), | ||
| gas: quote.transaction?.gas, | ||
| network_fee: null, // TODO | ||
| gas_price: null, // TODO | ||
| guaranteed_output_amount: convertQuantity({ | ||
| asset: receiveAsset, | ||
| quantity: quote.guaranteed_output_amount, | ||
| }), | ||
| zerion_fee_percentage, | ||
| zerion_fee_usd_amount, | ||
| input_chain: quote.input_chain, | ||
| output_chain: quote.output_chain ?? quote.input_chain, | ||
| slippage: configuration.slippage, | ||
| contract_type: quote.contract_metadata?.name, | ||
| enough_balance, | ||
| enough_allowance: Boolean(quote.transaction), | ||
| warning_was_shown: isHighPriceImpact, | ||
|
Collaborator
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.
Contributor
Author
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. should be fixed, just checked locally ✅ |
||
| fdv_asset_sent: fdvAssetSent, | ||
| fdv_asset_received: fdvAssetReceived, | ||
| bridge_fee_usd_amount: bridgeFeeAmountInUsd, | ||
| output_amount_color: outputAmountColor, | ||
| }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

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.
action_typecan beTradeorSend🫠🫠🫠Uh oh!
There was an error while loading. Please reload this page.
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.
in general it can, but not in
formFilledOutevent handlerso the scope should be of type
scope: "Swap" | "Bridge"inFormFilledOutParams(if I'm not missing smth)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'll update this part in a minute
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.
updated ✅