Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Broken token payment flow #1093

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ import { checkForSuspiciousAsset } from "popup/helpers/checkForSuspiciousAsset";

export type ManageAssetCurrency = StellarToml.Api.Currency & {
domain: string;
contractId?: string;
name?: string;
};

export interface NewAssetFlags {
Expand Down Expand Up @@ -310,7 +308,7 @@ export const ManageAssetRows = ({
code={code}
issuer={issuer}
image={image}
domain={contractId ? truncateString(contractId) : domain}
domain={domain}
/>
<div className="ManageAssetRows__button">
<PillButton
Expand All @@ -320,7 +318,7 @@ export const ManageAssetRows = ({
}
onClick={() => {
if (contractId) {
handleTokenRowClick(contractId, canonicalAsset);
handleTokenRowClick(issuer, canonicalAsset);
} else {
handleRowClick(
{ code, issuer, image, domain },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Balances } from "@shared/api/types";

import "./styles.scss";
import { formatAmount } from "popup/helpers/formatters";
import { isContractId } from "popup/helpers/soroban";

interface SelectAssetRowsProps {
assetRows: ManageAssetCurrency[];
Expand Down Expand Up @@ -52,61 +53,52 @@ export const SelectAssetRows = ({ assetRows }: SelectAssetRowsProps) => {
return (
<div className="SelectAssetRows__scrollbar">
<div className="SelectAssetRows__content">
{assetRows.map(
({
code = "",
domain,
image = "",
issuer = "",
contractId = "",
name,
}) => {
const isScamAsset = !!blockedDomains.domains[domain];
const _issuer = contractId || issuer;
const canonical = getCanonicalFromAsset(code, _issuer);
{assetRows.map(({ code = "", domain, image = "", issuer = "" }) => {
const isScamAsset = !!blockedDomains.domains[domain];
const isContract = isContractId(issuer);
const canonical = getCanonicalFromAsset(code, issuer);

return (
<div
className="SelectAssetRows__row selectable"
key={canonical}
onClick={() => {
if (assetSelect.isSource) {
dispatch(saveAsset(canonical));
if (contractId) {
dispatch(saveIsToken(true));
} else {
dispatch(saveIsToken(false));
}
history.goBack();
return (
<div
className="SelectAssetRows__row selectable"
key={canonical}
onClick={() => {
if (assetSelect.isSource) {
dispatch(saveAsset(canonical));
if (isContract) {
dispatch(saveIsToken(true));
} else {
dispatch(saveDestinationAsset(canonical));
history.goBack();
dispatch(saveIsToken(false));
}
}}
>
<AssetIcon
assetIcons={code !== "XLM" ? { [canonical]: image } : {}}
code={code}
issuerKey={_issuer}
/>
<div className="SelectAssetRows__row__info">
<div className="SelectAssetRows__row__info__header">
{contractId ? name : code}
<ScamAssetIcon isScamAsset={isScamAsset} />
</div>
<div className="SelectAssetRows__domain">
{formatDomain(domain)}
</div>
history.goBack();
} else {
dispatch(saveDestinationAsset(canonical));
history.goBack();
}
}}
>
<AssetIcon
assetIcons={code !== "XLM" ? { [canonical]: image } : {}}
code={code}
issuerKey={issuer}
/>
<div className="SelectAssetRows__row__info">
<div className="SelectAssetRows__row__info__header">
{code}
<ScamAssetIcon isScamAsset={isScamAsset} />
</div>
<div className="SelectAssetRows__domain">
{formatDomain(domain)}
</div>
{!hideBalances && (
<div>
{formatAmount(getAccountBalance(canonical))} {code}
</div>
)}
</div>
);
},
)}
{!hideBalances && (
<div>
{formatAmount(getAccountBalance(canonical))} {code}
</div>
)}
</div>
);
})}
</div>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Asset,
Memo,
Operation,
SorobanRpc,
TransactionBuilder,
} from "stellar-sdk";
import { Card, Loader, Icon, Button } from "@stellar/design-system";
Expand Down Expand Up @@ -271,14 +270,9 @@ export const TransactionDetails = ({ goBack }: { goBack: () => void }) => {

const handleXferTransaction = async () => {
try {
const preparedTransaction = SorobanRpc.assembleTransaction(
transactionSimulation.raw!,
transactionSimulation.response!,
);

const res = await dispatch(
signFreighterSorobanTransaction({
transactionXDR: preparedTransaction.build().toXDR(),
transactionXDR: transactionSimulation.preparedTransaction!,
network: networkDetails.networkPassphrase,
}),
);
Expand Down
26 changes: 13 additions & 13 deletions extension/src/popup/components/sendPayment/SendSettings/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useEffect } from "react";
import { useSelector, useDispatch } from "react-redux";
import { Address, XdrLargeInt } from "stellar-sdk";
import { Formik, Form, Field, FieldProps } from "formik";
import { Icon, Textarea, Link, Button } from "@stellar/design-system";
import { useTranslation } from "react-i18next";
Expand Down Expand Up @@ -89,11 +88,11 @@ export const SendSettings = ({
Number(assetBalance.decimals),
);

const params = [
new Address(publicKey).toScVal(), // from
new Address(destination).toScVal(), // to
new XdrLargeInt("i128", parsedAmount.toNumber()).toI128(), // amount
];
const params = {
publicKey,
destination,
amount: parsedAmount.toNumber(),
};

try {
const networkDetails = await getNetworkDetails();
Expand All @@ -107,7 +106,7 @@ export const SendSettings = ({
pub_key: publicKey,
memo,
params,
network_url: networkDetails.networkUrl,
network_url: networkDetails.sorobanRpcUrl,
network_passphrase: networkDetails.networkPassphrase,
}),
};
Expand All @@ -118,21 +117,22 @@ export const SendSettings = ({
if (!response.ok) {
throw new Error("failed to simluate token transfer");
}
const { simulationResponse, raw } = await response.json();
const {
preparedTransaction,
simulationResponse,
} = await response.json();

if ("transactionData" in simulationResponse) {
if (preparedTransaction) {
dispatch(
saveSimulation({
response: simulationResponse,
raw,
preparedTransaction,
}),
);
navigateTo(next);
return;
}
throw new Error(
`Failed to simluate transaction, ID: ${simulationResponse.id}`,
);
throw new Error(`Failed to simluate transaction`);
} catch (error) {
throw new Error(
`Failed to simluate transaction: ${JSON.stringify(error)}`,
Expand Down
8 changes: 2 additions & 6 deletions extension/src/popup/ducks/transactionSubmission.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import {
Horizon,
Keypair,
Memo,
MemoType,
Operation,
SorobanRpc,
Transaction,
TransactionBuilder,
xdr,
} from "stellar-sdk";
Expand Down Expand Up @@ -438,7 +434,7 @@ interface InitialState {
transactionData: TransactionData;
transactionSimulation: {
response: SorobanRpc.Api.SimulateTransactionSuccessResponse | null;
raw: Transaction<Memo<MemoType>, Operation[]> | null;
preparedTransaction: string | null;
};
accountBalances: AccountBalancesInterface;
destinationBalances: AccountBalancesInterface;
Expand Down Expand Up @@ -477,7 +473,7 @@ export const initialState: InitialState = {
},
transactionSimulation: {
response: null,
raw: null,
preparedTransaction: null,
},
hardwareWalletData: {
status: ShowOverlayStatus.IDLE,
Expand Down
9 changes: 9 additions & 0 deletions extension/src/popup/helpers/soroban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,12 @@ export const getAttrsFromSorobanHorizonOp = (

return getRootInvocationArgs(invokeHostFn);
};

export const isContractId = (contractId: string) => {
try {
StrKey.decodeContract(contractId);
return true;
} catch (error) {
return false;
}
};
Loading