Skip to content

Commit 4cf723c

Browse files
Merge pull request #868 from threshold-network/feat/tbtc-deposit-telemetry-to-backend
feat: add deposit data telemetry to be retained in the backend
2 parents f2230cd + 3ec5ec1 commit 4cf723c

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

src/enums/api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export enum ApiUrl {
77

88
export enum endpointUrl {
99
TRM_WALLET_SCREENING = "/trm/screen",
10+
TBTC_DEPOSIT_DATA = "/deposit-data",
1011
CURVE_ETHEREUM_POOL = "/getPools/ethereum/factory",
1112
COINGECKO_SIMPLE_PRICE = "/simple/price",
1213
COINGECKO_VS_CURRENCY = "vs_currencies",

src/hooks/tbtc/useDepositTelemetry.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import axios from "axios"
12
import { DepositReceipt } from "@keep-network/tbtc-v2.ts"
23
import { useCallback } from "react"
34
import { BitcoinNetwork } from "../../threshold-ts/types"
45
import { verifyDepositAddress } from "../../utils/verifyDepositAddress"
56
import { useCaptureMessage } from "../sentry"
7+
import { ApiUrl, endpointUrl } from "../../enums"
68

79
export const useDepositTelemetry = (network: BitcoinNetwork) => {
810
const captureMessage = useCaptureMessage()
@@ -40,6 +42,28 @@ export const useDepositTelemetry = (network: BitcoinNetwork) => {
4042
"verification.status": status,
4143
}
4244
)
45+
46+
const requestBody = {
47+
depositAddress,
48+
depositor: depositor.identifierHex,
49+
blindingFactor: blindingFactor.toString(),
50+
walletPublicKeyHash: walletPublicKeyHash.toString(),
51+
refundPublicKeyHash: refundPublicKeyHash.toString(),
52+
refundLocktime: refundLocktime.toString(),
53+
extraData: extraData?.toString(),
54+
verificationStatus: status,
55+
verificationResponse: response,
56+
}
57+
58+
try {
59+
await axios.post(
60+
`${ApiUrl.TBTC_EXPLORER}${endpointUrl.TBTC_DEPOSIT_DATA}`,
61+
requestBody,
62+
{ timeout: 10000 }
63+
)
64+
} catch (error) {
65+
throw new Error("Failed to submit deposit telemetry", { cause: error })
66+
}
4367
},
4468
[verifyDepositAddress, network, captureMessage]
4569
)

src/pages/tBTC/Bridge/Minting/ProvideData.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import TbtcFees from "../components/TbtcFees"
4141
import { useIsActive } from "../../../../hooks/useIsActive"
4242
import { PosthogButtonId } from "../../../../types/posthog"
4343
import SubmitTxButton from "../../../../components/SubmitTxButton"
44+
import { Toast } from "../../../../components/Toast"
4445
import { Deposit } from "@keep-network/tbtc-v2.ts"
4546
import { useModal } from "../../../../hooks/useModal"
4647
import { ModalType } from "../../../../enums"
@@ -162,16 +163,18 @@ const MintingProcessForm = withFormik<MintingProcessFormProps, FormValues>({
162163
export const ProvideDataComponent: FC<{
163164
onPreviousStepClick: (previosuStep: MintingStep) => void
164165
}> = ({ onPreviousStepClick }) => {
165-
const { updateState } = useTbtcState()
166+
const { updateState, resetDepositData } = useTbtcState()
166167
const [isSubmitButtonLoading, setSubmitButtonLoading] = useState(false)
168+
const [telemetryFailed, setTelemetryFailed] = useState(false)
167169
const [stagedDepositValues, setStagedDepositValues] =
168170
useState<FormValues | null>(null)
169171
const formRef = useRef<FormikProps<FormValues>>(null)
170172
const threshold = useThreshold()
171173
const { account, chainId } = useIsActive()
172174
const { isNonEVMActive, nonEVMPublicKey, nonEVMChainName } =
173175
useNonEVMConnection()
174-
const { setDepositDataInLocalStorage } = useTBTCDepositDataFromLocalStorage()
176+
const { setDepositDataInLocalStorage, removeDepositDataFromLocalStorage } =
177+
useTBTCDepositDataFromLocalStorage()
175178
const depositTelemetry = useDepositTelemetry(threshold.tbtc.bitcoinNetwork)
176179
const connectedAccount = account || nonEVMPublicKey
177180
const { openModal, closeModal: closeModalFromHook, modalType } = useModal()
@@ -217,6 +220,7 @@ export const ProvideDataComponent: FC<{
217220
const chainName =
218221
nonEVMChainName || getEthereumNetworkNameFromChainId(chainId)
219222

223+
setTelemetryFailed(false)
220224
setSubmitButtonLoading(true)
221225

222226
let deposit: Deposit
@@ -263,7 +267,16 @@ export const ProvideDataComponent: FC<{
263267
networkName
264268
)
265269

266-
depositTelemetry(receipt, btcDepositAddress)
270+
try {
271+
await depositTelemetry(receipt, btcDepositAddress)
272+
} catch (error) {
273+
threshold.tbtc.removeDepositData()
274+
resetDepositData()
275+
removeDepositDataFromLocalStorage(networkName)
276+
setSubmitButtonLoading(false)
277+
setTelemetryFailed(true)
278+
return
279+
}
267280

268281
// if the user has NOT declined the json file, ask the user if they want to accept the new file
269282
if (shouldDownloadDepositReceipt) {
@@ -296,6 +309,15 @@ export const ProvideDataComponent: FC<{
296309

297310
return (
298311
<>
312+
{telemetryFailed && (
313+
<Toast
314+
title="We couldn't submit deposit telemetry. Please try again."
315+
status="error"
316+
duration={8000}
317+
top={3}
318+
zIndex={1}
319+
/>
320+
)}
299321
<BridgeProcessCardTitle onPreviousStepClick={onPreviousStepClick} />
300322
<BridgeProcessCardSubTitle
301323
stepText="Step 1"

0 commit comments

Comments
 (0)