diff --git a/components/MangoProvider.tsx b/components/MangoProvider.tsx index 87e8b997..75d948c5 100644 --- a/components/MangoProvider.tsx +++ b/components/MangoProvider.tsx @@ -11,6 +11,8 @@ import useLocalStorageState from 'hooks/useLocalStorageState' import { DEFAULT_PRIORITY_FEE_LEVEL } from './settings/RpcSettings' import { useHiddenMangoAccounts } from 'hooks/useHiddenMangoAccounts' import { notify } from 'utils/notifications' +import { usePlausible } from 'next-plausible' +import { TelemetryEvents } from 'utils/telemetry' const set = mangoStore.getState().set const actions = mangoStore.getState().actions @@ -22,6 +24,7 @@ const HydrateStore = () => { const connection = mangoStore((s) => s.connection) const slowNetwork = useNetworkSpeed() const { wallet } = useWallet() + const telemetry = usePlausible() const [, setLastWalletName] = useLocalStorageState(LAST_WALLET_NAME, '') @@ -113,7 +116,7 @@ const HydrateStore = () => { localStorage.getItem(PRIORITY_FEE_KEY) ?? DEFAULT_PRIORITY_FEE_LEVEL.value, ) - actions.estimatePriorityFee(priorityFeeMultiplier) + actions.estimatePriorityFee(priorityFeeMultiplier, telemetry) } }, (slowNetwork ? 60 : 10) * SECONDS, diff --git a/store/mangoStore.ts b/store/mangoStore.ts index a27ecc84..bbea6a2d 100644 --- a/store/mangoStore.ts +++ b/store/mangoStore.ts @@ -88,6 +88,7 @@ import groupBy from 'lodash/groupBy' import sampleSize from 'lodash/sampleSize' import { fetchTokenStatsData, processTokenStatsData } from 'apis/mngo' import { OrderTypes } from 'utils/tradeForm' +import { usePlausible } from 'next-plausible' const GROUP = new PublicKey('78b8f4cGCwmZ9ysPFMWLaLTkkaYnUjwMJYStWe5RTSSX') @@ -126,6 +127,8 @@ const initMangoClient = ( prependedGlobalAdditionalInstructions: [], multipleProviders: [], }, + //for analytics use + telemetry: ReturnType | null, ): MangoClient => { return MangoClient.connect(provider, CLUSTER, MANGO_V4_ID[CLUSTER], { prioritizationFee: opts.prioritizationFee, @@ -133,6 +136,12 @@ const initMangoClient = ( opts.prependedGlobalAdditionalInstructions, idsSource: 'api', postSendTxCallback: ({ txid }: { txid: string }) => { + if (telemetry) { + telemetry('postSendTx', { + props: { fee: opts.prioritizationFee }, + }) + } + notify({ title: 'Transaction sent', description: 'Waiting for confirmation', @@ -294,6 +303,7 @@ export type MangoStore = { width: number height: number } + telemetry: ReturnType | null actions: { fetchActivityFeed: ( mangoAccountPk: string, @@ -323,7 +333,10 @@ export type MangoStore = { setPrependedGlobalAdditionalInstructions: ( instructions: TransactionInstruction[], ) => void - estimatePriorityFee: (feeMultiplier: number) => Promise + estimatePriorityFee: ( + feeMultiplier: number, + telemetry: ReturnType | null, + ) => Promise } } @@ -363,11 +376,15 @@ const mangoStore = create()( emptyWallet, options, ) - const client = initMangoClient(provider, { - prioritizationFee: DEFAULT_PRIORITY_FEE, - prependedGlobalAdditionalInstructions: [], - multipleProviders: backupProviders, - }) + const client = initMangoClient( + provider, + { + prioritizationFee: DEFAULT_PRIORITY_FEE, + prependedGlobalAdditionalInstructions: [], + multipleProviders: backupProviders, + }, + null, + ) return { activityFeed: { @@ -482,6 +499,7 @@ const mangoStore = create()( width: 0, height: 0, }, + telemetry: null, actions: { fetchActivityFeed: async ( mangoAccountPk: string, @@ -1007,12 +1025,16 @@ const mangoStore = create()( ) const priorityFee = get().priorityFee ?? DEFAULT_PRIORITY_FEE - const client = initMangoClient(provider, { - prioritizationFee: priorityFee, - prependedGlobalAdditionalInstructions: - get().prependedGlobalAdditionalInstructions, - multipleProviders: backupProviders, - }) + const client = initMangoClient( + provider, + { + prioritizationFee: priorityFee, + prependedGlobalAdditionalInstructions: + get().prependedGlobalAdditionalInstructions, + multipleProviders: backupProviders, + }, + null, + ) set((s) => { s.client = client @@ -1036,11 +1058,15 @@ const mangoStore = create()( const provider = client.program.provider as AnchorProvider provider.opts.skipPreflight = true - const newClient = initMangoClient(provider, { - prioritizationFee: get().priorityFee, - prependedGlobalAdditionalInstructions: instructions, - multipleProviders: [], - }) + const newClient = initMangoClient( + provider, + { + prioritizationFee: get().priorityFee, + prependedGlobalAdditionalInstructions: instructions, + multipleProviders: [], + }, + null, + ) set((s) => { s.client = newClient @@ -1121,23 +1147,29 @@ const mangoStore = create()( options, ) newProvider.opts.skipPreflight = true - const newClient = initMangoClient(newProvider, { - prependedGlobalAdditionalInstructions: - get().prependedGlobalAdditionalInstructions, - prioritizationFee: DEFAULT_PRIORITY_FEE, - multipleProviders: [], - }) + const newClient = initMangoClient( + newProvider, + { + prependedGlobalAdditionalInstructions: + get().prependedGlobalAdditionalInstructions, + prioritizationFee: DEFAULT_PRIORITY_FEE, + multipleProviders: [], + }, + null, + ) set((state) => { state.connection = newConnection state.client = newClient }) }, - estimatePriorityFee: async (feeMultiplier) => { + estimatePriorityFee: async (feeMultiplier, telemetry) => { const set = get().set const group = mangoStore.getState().group const client = mangoStore.getState().client const mangoAccount = get().mangoAccount.current + const currentFee = get().priorityFee + const currentTelemetry = get().telemetry if (!mangoAccount || !group || !client) return const altResponse = await connection.getAddressLookupTable( @@ -1176,16 +1208,25 @@ const mangoStore = create()( const provider = client.program.provider as AnchorProvider provider.opts.skipPreflight = true - const newClient = initMangoClient(provider, { - prioritizationFee: feeEstimate, - prependedGlobalAdditionalInstructions: - get().prependedGlobalAdditionalInstructions, - multipleProviders: [], - }) - set((state) => { - state.priorityFee = feeEstimate - state.client = newClient - }) + + if (currentFee !== feeEstimate || !currentTelemetry) { + const newClient = initMangoClient( + provider, + { + prioritizationFee: feeEstimate, + prependedGlobalAdditionalInstructions: + get().prependedGlobalAdditionalInstructions, + multipleProviders: [], + }, + telemetry, + ) + + set((state) => { + state.priorityFee = feeEstimate + state.client = newClient + state.telemetry = telemetry + }) + } }, }, } diff --git a/utils/telemetry.ts b/utils/telemetry.ts index cc02d005..b33f5e28 100644 --- a/utils/telemetry.ts +++ b/utils/telemetry.ts @@ -6,4 +6,5 @@ export type TelemetryEvents = { rewardsRenderUnsupported: { message: string } rewardsClaim: { rewards: number } rewardsClaimError: { message: string } + postSendTx: { fee: number } }