From c9e7f5d13145d9b11ebbcb21ec931337b326dc3d Mon Sep 17 00:00:00 2001 From: Chloe <44501120+chloezxyy@users.noreply.github.com> Date: Fri, 2 Feb 2024 10:43:39 +0800 Subject: [PATCH] feat(ui-ux): call endpoint to save tx (#4157) * feat(ui-ux): call endpoint to save tx * add comment * update domain * remove comment * fix: add feature flag to save tx only in mainnet * fix: add try catch when calling endpoint * fix: call saveTx only once per tx * fix: check if tx has been broadcasted before calling api --- .../OceanInterface/OceanInterface.tsx | 38 ++++++++++++++++++- package-lock.json | 18 ++++----- package.json | 4 +- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/mobile-app/app/components/OceanInterface/OceanInterface.tsx b/mobile-app/app/components/OceanInterface/OceanInterface.tsx index ad2528e8d4..3b27f61f4d 100644 --- a/mobile-app/app/components/OceanInterface/OceanInterface.tsx +++ b/mobile-app/app/components/OceanInterface/OceanInterface.tsx @@ -10,7 +10,7 @@ import { import { CTransactionSegWit } from "@defichain/jellyfish-transaction/dist"; import { WhaleApiClient } from "@defichain/whale-api-client"; import { Transaction } from "@defichain/whale-api-client/dist/api/transactions"; -import { getEnvironment } from "@waveshq/walletkit-core"; +import { EnvironmentNetwork, getEnvironment } from "@waveshq/walletkit-core"; import { RootState } from "@store"; import { firstTransactionSelector, @@ -29,6 +29,7 @@ import { } from "@shared-contexts/NativeLoggingProvider"; import { getReleaseChannel } from "@api/releaseChannel"; import { useAppDispatch } from "@hooks/useAppDispatch"; +import { useFeatureFlagContext } from "@contexts/FeatureFlagContext"; import { TransactionDetail } from "./TransactionDetail"; import { TransactionError } from "./TransactionError"; @@ -119,6 +120,8 @@ export function OceanInterface(): JSX.Element | null { const { wallet, address } = useWalletContext(); const { getTransactionUrl } = useDeFiScanContext(); const { network } = useNetworkContext(); + const { isFeatureAvailable } = useFeatureFlagContext(); + const isSaveTxEnabled = isFeatureAvailable("save_tx"); // store const { height, err: e } = useSelector((state: RootState) => state.ocean); @@ -128,6 +131,7 @@ export function OceanInterface(): JSX.Element | null { const slideAnim = useRef(new Animated.Value(0)).current; // state const [tx, setTx] = useState(transaction); + const [calledTx, setCalledTx] = useState(); const [err, setError] = useState(e?.message); const [txUrl, setTxUrl] = useState(); // evm tx state @@ -149,6 +153,38 @@ export function OceanInterface(): JSX.Element | null { return vmmap.output; }; + useEffect(() => { + const saveTx = async (txId: string) => { + try { + await fetch( + `https://3paxhqj3np.ap-southeast-1.awsapprunner.com/transaction/${txId}`, + { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + transaction: txId, + }), + }, + ); + // store called transaction + setCalledTx(txId); + } catch (e) { + /* empty - don't do anything even if saveTx is not called */ + } + }; + if ( + tx?.broadcasted && // only call tx when tx is done + calledTx !== tx?.tx.txId && // to ensure that api is only called once per tx + tx?.tx.txId !== undefined && + network === EnvironmentNetwork.MainNet && + isSaveTxEnabled // feature flag + ) { + saveTx(tx.tx.txId); + } + }, [tx?.tx.txId, calledTx, tx?.broadcasted, network, isSaveTxEnabled]); + useEffect(() => { // get evm tx id and url (if any) const fetchEvmTx = async (txId: string) => { diff --git a/package-lock.json b/package-lock.json index bdbd7b42f9..7fa9825413 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,8 +23,8 @@ "@reduxjs/toolkit": "^1.9.7", "@shopify/flash-list": "1.4.3", "@waveshq/standard-defichain-jellyfishsdk": "^2.20.0", - "@waveshq/walletkit-core": "^1.3.9", - "@waveshq/walletkit-ui": "^1.3.9", + "@waveshq/walletkit-core": "^1.3.10", + "@waveshq/walletkit-ui": "^1.3.10", "bignumber.js": "^9.1.2", "buffer": "^6.0.3", "classnames": "^2.3.2", @@ -10380,24 +10380,24 @@ } }, "node_modules/@waveshq/walletkit-core": { - "version": "1.3.9", - "resolved": "https://registry.npmjs.org/@waveshq/walletkit-core/-/walletkit-core-1.3.9.tgz", - "integrity": "sha512-0mp7Zj8gCgbmrR7AcGMaXEepsCPnmBmTisx5ltHBdF6K80WExR3M//j0XbW1eCROdCL4LcT0r+rG1JAJIj+nRw==", + "version": "1.3.10", + "resolved": "https://registry.npmjs.org/@waveshq/walletkit-core/-/walletkit-core-1.3.10.tgz", + "integrity": "sha512-bK8fV9v+8m7W0cbN9fFesi3wiOff+ev6y9LzQFhSiH0pcm8Bnk7kb4DL3paqL9tqUi6nw8RbFbYloLYLEuMuRg==", "dependencies": { "bignumber.js": "^9.1.2", "ethers": "^5.7.2" } }, "node_modules/@waveshq/walletkit-ui": { - "version": "1.3.9", - "resolved": "https://registry.npmjs.org/@waveshq/walletkit-ui/-/walletkit-ui-1.3.9.tgz", - "integrity": "sha512-SYNkpfQ4G5VjdSxaAtYucRmPj4uP5fzwKearYG+q6nuVFWIKOtnEEBLTiqUAr13njdbywGVLTZaAM2inTZMFSQ==", + "version": "1.3.10", + "resolved": "https://registry.npmjs.org/@waveshq/walletkit-ui/-/walletkit-ui-1.3.10.tgz", + "integrity": "sha512-4Hwm6BQAcYqeApHlSmAWpNoHq+M15BVeijkQpgw3nKpqQ/wyNEtl6I/MdBHqYPRcJkZjDGSg1M1HZxnuyqvpRA==", "dependencies": { "@stickyjs/jest": "^1.3.2", "@waveshq/standard-defichain-jellyfishsdk": "^2.14.0", "@waveshq/standard-web": "^2.14.0", "@waveshq/standard-web-linter": "^2.14.0", - "@waveshq/walletkit-core": "1.3.9", + "@waveshq/walletkit-core": "1.3.10", "bignumber.js": "^9.1.2", "dayjs": "^1.11.10", "immer": "^9.0.21", diff --git a/package.json b/package.json index f4224c59e4..7f3db33084 100644 --- a/package.json +++ b/package.json @@ -34,8 +34,8 @@ "@reduxjs/toolkit": "^1.9.7", "@shopify/flash-list": "1.4.3", "@waveshq/standard-defichain-jellyfishsdk": "^2.20.0", - "@waveshq/walletkit-core": "^1.3.9", - "@waveshq/walletkit-ui": "^1.3.9", + "@waveshq/walletkit-core": "^1.3.10", + "@waveshq/walletkit-ui": "^1.3.10", "bignumber.js": "^9.1.2", "buffer": "^6.0.3", "classnames": "^2.3.2",