-
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
Changes from 7 commits
7b03d12
6d54f55
f95a1cb
401d443
f0fc9bd
30b40a3
5f2ca4b
f21dcdd
b03c829
e0ff90f
e770548
5a086ea
4b9f2d5
b837faa
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,130 @@ | ||
| 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, | ||
| } from 'src/ui/pages/SwapForm/shared/price-impact'; | ||
| import { getCommonQuantity } from 'src/modules/networks/asset'; | ||
| import { assetQuantityToValue, 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), | ||
| staleTime: 1000 * 60 * 30, // 30 minutes | ||
| }); | ||
| } | ||
|
|
||
| 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 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 = isHighPriceImpact ? 'red' : 'grey'; | ||
|
|
||
| return { | ||
| usd_amount_sent: toMaybeArr([usdAmountSend]), | ||
| usd_amount_received: toMaybeArr([usdAmountReceived]), | ||
| asset_amount_sent: toMaybeArr([quote.input_amount_estimation]), | ||
|
||
| asset_amount_received: toMaybeArr([quote.output_amount_estimation]), | ||
| 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: 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, | ||
| outputAmountColor, | ||
|
||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import type { Asset } from 'defi-sdk'; | ||
| import { isTruthy } from 'is-truthy-ts'; | ||
| import type { Chain } from 'src/modules/networks/Chain'; | ||
| import { getCommonQuantity } from 'src/modules/networks/asset'; | ||
|
|
||
| export function toMaybeArr<T>( | ||
| arr: (T | null | undefined)[] | null | undefined | ||
| ): T[] | undefined { | ||
| return arr?.filter(isTruthy) ?? undefined; | ||
| } | ||
|
|
||
| interface AssetQuantity { | ||
| asset: Asset | null; | ||
| quantity: string | null; | ||
| } | ||
|
|
||
| export function assetQuantityToValue( | ||
| assetWithQuantity: AssetQuantity, | ||
| chain: Chain | ||
| ): number { | ||
| const { asset, quantity } = assetWithQuantity; | ||
| if (asset && 'implementations' in asset && asset.price && quantity !== null) { | ||
| return getCommonQuantity({ asset, chain, baseQuantity: quantity }) | ||
| .times(asset.price.value) | ||
| .toNumber(); | ||
| } | ||
| return 0; | ||
| } |


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 ✅