Skip to content

Commit

Permalink
feat(ui-ux): call endpoint to save tx (#4157)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
chloezxyy authored Feb 2, 2024
1 parent b2d55e5 commit c9e7f5d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
38 changes: 37 additions & 1 deletion mobile-app/app/components/OceanInterface/OceanInterface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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";

Expand Down Expand Up @@ -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);
Expand All @@ -128,6 +131,7 @@ export function OceanInterface(): JSX.Element | null {
const slideAnim = useRef(new Animated.Value(0)).current;
// state
const [tx, setTx] = useState<OceanTransaction | undefined>(transaction);
const [calledTx, setCalledTx] = useState<string | undefined>();
const [err, setError] = useState<string | undefined>(e?.message);
const [txUrl, setTxUrl] = useState<string | undefined>();
// evm tx state
Expand All @@ -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) => {
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit c9e7f5d

Please sign in to comment.