Skip to content

Commit

Permalink
feat(swap): track valora fee (#5368)
Browse files Browse the repository at this point in the history
### Description

As the title says.

### Test plan

- Updated tests

### Related issues

- Fixes RET-1061

### Backwards compatibility

Yes

### Network scalability

If a new NetworkId and/or Network are added in the future, the changes
in this PR will:

- [x] Continue to work without code changes, OR trigger a compilation
error (guaranteeing we find it when a new network is added)
  • Loading branch information
jeanregisser authored May 2, 2024
1 parent 0776d57 commit 5f6afd1
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/analytics/Properties.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,7 @@ type SwapQuoteEvent = SwapEvent & {
*/
estimatedPriceImpact: string | null
price: string
appFeePercentageIncludedInPrice: string | null | undefined
provider: string
}

Expand Down Expand Up @@ -1288,6 +1289,7 @@ interface SwapEventsProperties {
swapApproveTxId: string
estimatedSellTokenUsdValue?: number
estimatedBuyTokenUsdValue?: number
estimatedAppFeeUsdValue: number | undefined
areSwapTokensShuffled: boolean
}
[SwapEvents.swap_execute_error]: SwapQuoteEvent &
Expand All @@ -1301,6 +1303,7 @@ interface SwapEventsProperties {
swapApproveTxId: string
estimatedSellTokenUsdValue?: number
estimatedBuyTokenUsdValue?: number
estimatedAppFeeUsdValue: number | undefined
areSwapTokensShuffled: boolean
}
[SwapEvents.swap_learn_more]: undefined
Expand Down
14 changes: 11 additions & 3 deletions src/swap/SwapScreen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { getDynamicConfigParams, getExperimentParams, getFeatureGate } from 'src
import { StatsigFeatureGates } from 'src/statsig/types'
import SwapScreen from 'src/swap/SwapScreen'
import { swapStart } from 'src/swap/slice'
import { Field } from 'src/swap/types'
import { FetchQuoteResponse, Field } from 'src/swap/types'
import { NO_QUOTE_ERROR_MESSAGE } from 'src/swap/useSwapQuote'
import { NetworkId } from 'src/transactions/types'
import { publicClient } from 'src/viem'
Expand Down Expand Up @@ -181,10 +181,12 @@ const renderScreen = ({
}
}

const defaultQuote = {
const defaultQuote: FetchQuoteResponse = {
unvalidatedSwapTransaction: {
chainId: 44787,
price: '1.2345678',
guaranteedPrice: '1.1234567',
appFeePercentageIncludedInPrice: undefined,
sellTokenAddress: mockCeloAddress,
buyTokenAddress: mockCusdAddress,
sellAmount: '1234000000000000000',
Expand All @@ -195,7 +197,7 @@ const defaultQuote = {
value: '0',
data: '0x0',
gas: '1800000',
gasPrice: '500000000',
estimatedGasUse: undefined,
estimatedPriceImpact: '0.1',
},
details: {
Expand Down Expand Up @@ -995,6 +997,8 @@ describe('SwapScreen', () => {
preparedTransactions,
receivedAt: quoteReceivedTimestamp,
price: defaultQuote.unvalidatedSwapTransaction.price,
appFeePercentageIncludedInPrice:
defaultQuote.unvalidatedSwapTransaction.appFeePercentageIncludedInPrice,
provider: defaultQuote.details.swapProvider,
estimatedPriceImpact: defaultQuote.unvalidatedSwapTransaction.estimatedPriceImpact,
allowanceTarget: defaultQuote.unvalidatedSwapTransaction.allowanceTarget,
Expand Down Expand Up @@ -1046,6 +1050,8 @@ describe('SwapScreen', () => {
preparedTransactions: [preparedTransactions[1]], // no approval transaction
receivedAt: expect.any(Number),
price: defaultQuote.unvalidatedSwapTransaction.price,
appFeePercentageIncludedInPrice:
defaultQuote.unvalidatedSwapTransaction.appFeePercentageIncludedInPrice,
provider: defaultQuote.details.swapProvider,
estimatedPriceImpact: defaultQuote.unvalidatedSwapTransaction.estimatedPriceImpact,
allowanceTarget: defaultQuote.unvalidatedSwapTransaction.allowanceTarget,
Expand Down Expand Up @@ -1091,6 +1097,8 @@ describe('SwapScreen', () => {
preparedTransactions,
receivedAt: quoteReceivedTimestamp,
price: defaultQuote.unvalidatedSwapTransaction.price,
appFeePercentageIncludedInPrice:
defaultQuote.unvalidatedSwapTransaction.appFeePercentageIncludedInPrice,
provider: defaultQuote.details.swapProvider,
estimatedPriceImpact: defaultQuote.unvalidatedSwapTransaction.estimatedPriceImpact,
allowanceTarget: defaultQuote.unvalidatedSwapTransaction.allowanceTarget,
Expand Down
4 changes: 3 additions & 1 deletion src/swap/SwapScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ export function SwapScreen({ route }: Props) {
}

const swapAmountParam = updatedField === Field.FROM ? 'sellAmount' : 'buyAmount'
const { estimatedPriceImpact, price, allowanceTarget } = quote
const { estimatedPriceImpact, price, appFeePercentageIncludedInPrice, allowanceTarget } = quote

const resultType = quote.preparedTransactions.type
switch (resultType) {
Expand All @@ -434,6 +434,7 @@ export function SwapScreen({ route }: Props) {
allowanceTarget,
estimatedPriceImpact,
price,
appFeePercentageIncludedInPrice,
provider: quote.provider,
web3Library: 'viem',
...getSwapTxsAnalyticsProperties(
Expand All @@ -454,6 +455,7 @@ export function SwapScreen({ route }: Props) {
),
receivedAt: quote.receivedAt,
price: quote.price,
appFeePercentageIncludedInPrice: quote.appFeePercentageIncludedInPrice,
provider: quote.provider,
estimatedPriceImpact,
allowanceTarget,
Expand Down
6 changes: 6 additions & 0 deletions src/swap/saga.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ const mockSwapFromParams = (toTokenId: string, feeCurrency?: Address): PayloadAc
},
]),
price: '1',
appFeePercentageIncludedInPrice: '0.6',
provider: '0x',
estimatedPriceImpact: '0.1',
allowanceTarget: mockAllowanceTarget,
Expand Down Expand Up @@ -145,6 +146,7 @@ const mockSwapEthereum: PayloadAction<SwapInfo> = {
},
]),
price: '1',
appFeePercentageIncludedInPrice: '0.6',
provider: '0x',
estimatedPriceImpact: '0.1',
allowanceTarget: mockAllowanceTarget,
Expand Down Expand Up @@ -430,6 +432,7 @@ describe(swapSubmitSaga, () => {
amount: swapPrepared.payload.userInput.swapAmount[Field.TO],
amountType: 'buyAmount',
price: '1',
appFeePercentageIncludedInPrice: '0.6',
allowanceTarget: '0xdef1c0ded9bec7f1a1670819833240f027b25eff',
estimatedPriceImpact: '0.1',
provider: '0x',
Expand All @@ -440,6 +443,7 @@ describe(swapSubmitSaga, () => {
quoteToTransactionElapsedTimeInMs: 10_000,
estimatedBuyTokenUsdValue: 100,
estimatedSellTokenUsdValue: 100,
estimatedAppFeeUsdValue: 0.6,
web3Library: 'viem',
gas: 1384480,
maxGasFee: 0.01661376,
Expand Down Expand Up @@ -663,6 +667,7 @@ describe(swapSubmitSaga, () => {
amount: mockSwap.payload.userInput.swapAmount[Field.TO],
amountType: 'buyAmount',
price: '1',
appFeePercentageIncludedInPrice: '0.6',
allowanceTarget: '0xdef1c0ded9bec7f1a1670819833240f027b25eff',
estimatedPriceImpact: '0.1',
provider: '0x',
Expand All @@ -673,6 +678,7 @@ describe(swapSubmitSaga, () => {
quoteToTransactionElapsedTimeInMs: 10_000,
estimatedBuyTokenUsdValue: 100,
estimatedSellTokenUsdValue: 100,
estimatedAppFeeUsdValue: 0.6,
web3Library: 'viem',
gas: 1384480,
maxGasFee: 0.01661376,
Expand Down
4 changes: 4 additions & 0 deletions src/swap/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export function* swapSubmitSaga(action: PayloadAction<SwapInfo>) {
const {
provider,
price,
appFeePercentageIncludedInPrice,
allowanceTarget,
estimatedPriceImpact,
preparedTransactions: serializablePreparedTransactions,
Expand Down Expand Up @@ -206,6 +207,7 @@ export function* swapSubmitSaga(action: PayloadAction<SwapInfo>) {
amount,
amountType,
price,
appFeePercentageIncludedInPrice,
allowanceTarget,
estimatedPriceImpact,
provider,
Expand All @@ -214,6 +216,8 @@ export function* swapSubmitSaga(action: PayloadAction<SwapInfo>) {
swapApproveTxId: swapApproveContext.id,
estimatedSellTokenUsdValue,
estimatedBuyTokenUsdValue,
estimatedAppFeeUsdValue:
(Number(appFeePercentageIncludedInPrice) / 100) * Number(estimatedSellTokenUsdValue),
web3Library: 'viem' as const,
areSwapTokensShuffled,
...getSwapTxsAnalyticsProperties(preparedTransactions, fromToken.networkId, tokensById),
Expand Down
1 change: 1 addition & 0 deletions src/swap/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface SwapInfo {
preparedTransactions: SerializableTransactionRequest[]
receivedAt: number
price: string
appFeePercentageIncludedInPrice: string | undefined
provider: string
estimatedPriceImpact: string | null
allowanceTarget: string
Expand Down

0 comments on commit 5f6afd1

Please sign in to comment.