Skip to content

Commit

Permalink
Merge branch 'feature/transaction-hash-tracking' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Ho3einWave committed Nov 26, 2024
2 parents 199a85e + 695e0a4 commit ec9a3ec
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 50 deletions.
44 changes: 20 additions & 24 deletions lib/components/SwapButton/Inprogress.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useEffect } from "react";
import { ModalState, useSwapStore } from "../../store/swap.store";
import { IoClose } from "react-icons/io5";
import { Dex, fromNano } from "@mytonswap/sdk";
import { FaArrowRightArrowLeft } from "react-icons/fa6";
import formatNumber from "../../utils/formatNum";
import { ImSpinner8 } from "react-icons/im";
import "./Inprogress.scss";
import { useEventsStore } from "../../store/events.store";
import { useTranslation } from "react-i18next";
import { sendTransaction } from "../../services/transaction";
import { useEffect } from 'react';
import { ModalState, useSwapStore } from '../../store/swap.store';
import { IoClose } from 'react-icons/io5';
import { Dex, fromNano } from '@mytonswap/sdk';
import { FaArrowRightArrowLeft } from 'react-icons/fa6';
import formatNumber from '../../utils/formatNum';
import { ImSpinner8 } from 'react-icons/im';
import './Inprogress.scss';
import { useEventsStore } from '../../store/events.store';
import { useTranslation } from 'react-i18next';

const Inprogress = () => {
const { t } = useTranslation();
const {
Expand All @@ -22,7 +22,6 @@ const Inprogress = () => {
bestRoute,
receive_rate,
pay_rate,
transactionQueryId,
} = useSwapStore();
const { onSwap } = useEventsStore();
useEffect(() => {
Expand All @@ -34,7 +33,7 @@ const Inprogress = () => {
);
if (event) {
onSwap({
type: "success",
type: 'success',
data: {
pay: pay_token!,
receive: receive_token!,
Expand All @@ -47,14 +46,11 @@ const Inprogress = () => {
hash: transactionHash,
},
});
sendTransaction({
hash: transactionHash,
query_id: transactionQueryId!,
});

setModalState(ModalState.DONE);
} else {
onSwap({
type: "error",
type: 'error',
data: {
pay: pay_token!,
receive: receive_token!,
Expand All @@ -69,8 +65,8 @@ const Inprogress = () => {
});

setErrorMessage({
errorTitle: t("errors.transaction_failed"),
errorMessage: t("errors.unknown_error"),
errorTitle: t('errors.transaction_failed'),
errorMessage: t('errors.unknown_error'),
});
}
}
Expand Down Expand Up @@ -100,7 +96,7 @@ const Inprogress = () => {
</div>
<div className="inprogress-modal-change-rate ">
<div>
{fromNano(pay_amount, pay_token?.decimal)}{" "}
{fromNano(pay_amount, pay_token?.decimal)}{' '}
{pay_token?.symbol}
</div>
<div>
Expand All @@ -110,7 +106,7 @@ const Inprogress = () => {
{bestRoute!.pool_data.receive_show!} {receive_token?.symbol}
</div>
<div className="rate">
{" "}
{' '}
{formatNumber(
Number(bestRoute!.pool_data.receive_show) *
receive_rate!.USD,
Expand All @@ -120,9 +116,9 @@ const Inprogress = () => {
</div>
</div>

<div className="description">{t("transaction.pending")}</div>
<div className="description">{t('transaction.pending')}</div>
<p className="description-2">
{t("transaction.action_in_progress")}
{t('transaction.action_in_progress')}
</p>
<div className="loading-icon">
<ImSpinner8 className="animate-spin icon" />
Expand Down
55 changes: 29 additions & 26 deletions lib/utils/swap.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { BestRoute, MyTonSwapClient } from "@mytonswap/sdk";
import { beginCell, Cell } from "@ton/ton";
import { BestRoute, MyTonSwapClient } from '@mytonswap/sdk';
import { beginCell, Cell } from '@ton/ton';

import {
SendTransactionRequest,
TonConnectUI,
UserRejectsError,
} from "@tonconnect/ui-react";
import { useSwapStore } from "../store/swap.store";
import catchError from "./catchErrors";
import { useOptionsStore } from "../store/options.store";
import { WIDGET_VERSION } from "../constants";
import { reportErrorWithToast } from "../services/errorAnalytics";
import i18n from "../i18n/i18n";
} from '@tonconnect/ui-react';
import { useSwapStore } from '../store/swap.store';
import catchError from './catchErrors';
import { useOptionsStore } from '../store/options.store';
import { WIDGET_VERSION } from '../constants';
import { reportErrorWithToast } from '../services/errorAnalytics';
import i18n from '../i18n/i18n';
import { sendTransaction } from '../services/transaction';
export default async function swap(
tonconnect: TonConnectUI,
bestRoute: BestRoute
) {
const client = new MyTonSwapClient({
headers: { "widget-version": WIDGET_VERSION },
headers: { 'widget-version': WIDGET_VERSION },
});
const app_id = useOptionsStore.getState().options.app_id;
const rawMessageResult = await catchError(() =>
Expand All @@ -26,8 +27,8 @@ export default async function swap(
if (rawMessageResult.error) {
reportErrorWithToast(
rawMessageResult.error,
"Failed to get swap data",
"swap.ts swap createSwap :30"
'Failed to get swap data',
'swap.ts swap createSwap :30'
);
return;
}
Expand All @@ -36,48 +37,50 @@ export default async function swap(

let stateInit: undefined | string = undefined;
if (rawMessage.init) {
const code = Cell.fromBoc(Buffer.from(rawMessage.init.code, "hex"))[0];
const data = Cell.fromBoc(Buffer.from(rawMessage.init.data, "hex"))[0];
const code = Cell.fromBoc(Buffer.from(rawMessage.init.code, 'hex'))[0];
const data = Cell.fromBoc(Buffer.from(rawMessage.init.data, 'hex'))[0];
stateInit = beginCell()
.storeRef(code)
.storeRef(data)
.endCell()
.toBoc()
.toString("base64");
.toString('base64');
}
const messages = [
{
address: rawMessage.to,
amount: rawMessage.value,
stateInit: stateInit,
payload: Cell.fromBoc(Buffer.from(rawMessage.body, "hex"))[0]
payload: Cell.fromBoc(Buffer.from(rawMessage.body, 'hex'))[0]
.toBoc()
.toString("base64"),
.toString('base64'),
},
] satisfies SendTransactionRequest["messages"];
] satisfies SendTransactionRequest['messages'];
tonconnect
.sendTransaction({
messages: messages,
validUntil: Date.now() + 1000 * 60 * 60 * 24,
})
.then((result) => {
const cell = Cell.fromBoc(Buffer.from(result.boc, "base64"))[0];
const stateInstance = useSwapStore.getState();
const hash = cell.hash().toString("hex");
stateInstance.setTransactionHash(hash, rawMessage.query_id);
const cell = Cell.fromBoc(Buffer.from(result.boc, 'base64'))[0];
const hash = cell.hash().toString('hex');
sendTransaction({
hash: hash,
query_id: rawMessage.query_id,
});
})
.catch((e) => {
console.log(e);
const stateInstance = useSwapStore.getState();
if (e instanceof UserRejectsError) {
stateInstance.setErrorMessage({
errorTitle: i18n.t("errors.transaction_canceled"),
errorMessage: i18n.t("errors.transaction_canceled_by_user"),
errorTitle: i18n.t('errors.transaction_canceled'),
errorMessage: i18n.t('errors.transaction_canceled_by_user'),
});
} else {
stateInstance.setErrorMessage({
errorTitle: i18n.t("errors.transaction_failed"),
errorMessage: i18n.t("errors.unknown_error"),
errorTitle: i18n.t('errors.transaction_failed'),
errorMessage: i18n.t('errors.unknown_error'),
});
}
});
Expand Down

0 comments on commit ec9a3ec

Please sign in to comment.