Skip to content

Commit

Permalink
Merge pull request #482 from secretkeylabs/jordankzf/swaps-mixpanel-usd
Browse files Browse the repository at this point in the history
Always use USD for Swaps MixPanel Tracking
  • Loading branch information
jordankzf authored Aug 12, 2024
2 parents cd7493d + 4c0fbde commit 5fa9fd6
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 32 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@react-spring/web": "^9.6.1",
"@sats-connect/core": "0.1.2",
"@scure/btc-signer": "1.2.1",
"@secretkeylabs/xverse-core": "18.4.1",
"@secretkeylabs/xverse-core": "18.5.0",
"@stacks/connect": "7.4.1",
"@stacks/stacks-blockchain-api-types": "6.1.1",
"@stacks/transactions": "6.13.1",
Expand Down
13 changes: 11 additions & 2 deletions src/app/hooks/queries/useCoinRates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ const useGetRates = (fiatCurrency: SupportedCurrency, networkType: NetworkType)
const btcFiatRate = await fetchBtcToCurrencyRate(networkType, {
fiatCurrency,
});

const btcUsdRate =
fiatCurrency === 'USD'
? btcFiatRate
: await fetchBtcToCurrencyRate(networkType, {
fiatCurrency: 'USD' as SupportedCurrency,
});

const stxBtcRate = await fetchStxToBtcRate(networkType);
return { stxBtcRate, btcFiatRate };
return { stxBtcRate, btcFiatRate, btcUsdRate };
} catch (e: any) {
return Promise.reject(e);
}
Expand All @@ -34,8 +42,9 @@ const useCoinRates = () => {

const stxBtcRate = data?.stxBtcRate.toString() || '0';
const btcFiatRate = data?.btcFiatRate.toString() || '0';
const btcUsdRate = data?.btcUsdRate.toString() || '0';

return { stxBtcRate, btcFiatRate };
return { stxBtcRate, btcFiatRate, btcUsdRate };
};

export default useCoinRates;
2 changes: 1 addition & 1 deletion src/app/screens/coinDashboard/coinHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export default function CoinHeader({ currency, fungibleToken }: Props) {
return;
}
trackMixPanel(AnalyticsEvents.InitiateSwapFlow, {
token: fungibleToken ? fungibleToken.name ?? fungibleToken.principal : currency,
selectedToken: fungibleToken ? fungibleToken.name ?? fungibleToken.principal : currency,
});
navigate(`/swap?from=${fungibleToken ? fungibleToken.principal : currency}`);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function TokenFromBottomSheet({ visible, title, onSelectCoin, onC
onPress={() => {
onSelectCoin(token);
trackMixPanel(AnalyticsEvents.SelectTokenToSwapFrom, {
token: 'Bitcoin',
selectedToken: 'Bitcoin',
});
onClose();
}}
Expand All @@ -61,7 +61,7 @@ export default function TokenFromBottomSheet({ visible, title, onSelectCoin, onC
onPress={() => {
onSelectCoin(token);
trackMixPanel(AnalyticsEvents.SelectTokenToSwapFrom, {
token: token.name,
selectedToken: token.name,
});
onClose();
}}
Expand Down
4 changes: 2 additions & 2 deletions src/app/screens/swap/components/tokenToBottomSheet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default function TokenToBottomSheet({
onPress={() => {
onSelectCoin(token);
trackMixPanel(AnalyticsEvents.SelectTokenToSwapTo, {
token: 'Bitcoin',
selectedToken: 'Bitcoin',
});
handleClose();
}}
Expand All @@ -177,7 +177,7 @@ export default function TokenToBottomSheet({
onPress={() => {
onSelectCoin(token);
trackMixPanel(AnalyticsEvents.SelectTokenToSwapTo, {
token: token.name ?? token.ticker,
selectedToken: token.name ?? token.ticker,
});
handleClose();
}}
Expand Down
8 changes: 4 additions & 4 deletions src/app/screens/swap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export default function SwapScreen() {

const { unfilteredData } = useRuneFungibleTokensQuery();
const { data: btcBalance } = useBtcWalletData();
const { btcFiatRate } = useCoinRates();
const { btcFiatRate, btcUsdRate } = useCoinRates();
const navigate = useNavigate();
const { t } = useTranslation('translation');
const location = useLocation();
Expand Down Expand Up @@ -173,9 +173,9 @@ export default function SwapScreen() {
trackSwapMixPanel(AnalyticsEvents.FetchSwapQuote, {
fromToken,
toToken,
amount,
amount: fromToken === 'BTC' ? btcToSats(new BigNumber(amount)).toString() : amount,
quote,
btcFiatRate,
btcUsdRate,
runeFloorPrice,
});

Expand Down Expand Up @@ -371,7 +371,7 @@ export default function SwapScreen() {
toToken,
amount,
quote,
btcFiatRate,
btcUsdRate,
runeFloorPrice,
});
};
Expand Down
24 changes: 13 additions & 11 deletions src/app/screens/swap/mixpanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ function trackSwapMixPanel(
toToken,
amount,
quote,
btcFiatRate,
btcUsdRate,
runeFloorPrice,
}: {
provider?: Provider;
fromToken?: FungibleToken | 'BTC';
toToken?: Token;
amount: string;
quote?: Quote;
btcFiatRate: string;
btcUsdRate: string;
runeFloorPrice?: number;
},
) {
Expand All @@ -33,23 +33,25 @@ function trackSwapMixPanel(

const fromAmount =
fromToken === 'BTC'
? getBtcFiatEquivalent(new BigNumber(amount), new BigNumber(btcFiatRate)).toFixed(2)
? getBtcFiatEquivalent(new BigNumber(amount), new BigNumber(btcUsdRate)).toFixed(2)
: new BigNumber(fromToken?.tokenFiatRate ?? 0).multipliedBy(amount).toFixed(2);

const toAmount =
toToken?.protocol === 'btc'
? getBtcFiatEquivalent(
new BigNumber(quote?.receiveAmount ?? 0),
new BigNumber(btcFiatRate),
).toFixed(2)
: new BigNumber(quote?.receiveAmount ?? 0).multipliedBy(runeFloorPrice ?? 0).toFixed(2);
const receiveAmount = quote?.receiveAmount ? new BigNumber(quote?.receiveAmount) : undefined;
const toAmount = receiveAmount
? getBtcFiatEquivalent(
toToken?.protocol === 'btc'
? receiveAmount
: receiveAmount.multipliedBy(runeFloorPrice ?? 0),
new BigNumber(btcUsdRate),
).toFixed(2)
: undefined;

trackMixPanel(eventName, {
...(provider ? { provider: provider?.name } : {}),
...(from ? { from } : {}),
...(to ? { to } : {}),
fromAmount,
toAmount,
...(toAmount ? { toAmount } : {}),
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/app/screens/swap/quoteSummary/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export default function QuoteSummary({
}: QuoteSummaryProps) {
const { t } = useTranslation('translation');
const theme = useTheme();
const { btcFiatRate } = useCoinRates();
const { btcFiatRate, btcUsdRate } = useCoinRates();
const { btcAddress, ordinalsAddress, btcPublicKey, ordinalsPublicKey } = useSelectedAccount();
const { loading: isPlaceOrderLoading, error: placeOrderError, placeOrder } = usePlaceOrder();
const {
Expand Down Expand Up @@ -204,7 +204,7 @@ export default function QuoteSummary({
toToken,
amount,
quote,
btcFiatRate,
btcUsdRate,
runeFloorPrice,
});

Expand Down

0 comments on commit 5fa9fd6

Please sign in to comment.