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] buildAndSimulateSoroswapTx uses Horizon directly #1694

Merged
merged 1 commit into from
Nov 21, 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
27 changes: 27 additions & 0 deletions @shared/api/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,33 @@ export const simulateTokenTransfer = async (args: {
};
};

export const simulateTransaction = async (args: {
xdr: string;
networkDetails: NetworkDetails;
}) => {
const { xdr, networkDetails } = args;
const options = {
method: "POST",
headers: {
// eslint-disable-next-line @typescript-eslint/naming-convention
"Content-Type": "application/json",
},
body: JSON.stringify({
xdr,
// eslint-disable-next-line @typescript-eslint/naming-convention
network_url: networkDetails.sorobanRpcUrl,
// eslint-disable-next-line @typescript-eslint/naming-convention
network_passphrase: networkDetails.networkPassphrase,
}),
};
const res = await fetch(`${INDEXER_URL}/simulate-tx`, options);
const response = await res.json();
return {
ok: res.ok,
response,
};
};

export const saveIsBlockaidAnnounced = async ({
isBlockaidAnnounced,
}: {
Expand Down
54 changes: 35 additions & 19 deletions extension/src/popup/helpers/sorobanSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
import BigNumber from "bignumber.js";

import { NetworkDetails } from "@shared/constants/stellar";
import { getSdk } from "@shared/helpers/stellar";
import { getSdk, isCustomNetwork } from "@shared/helpers/stellar";
import { stellarSdkServer } from "@shared/api/helpers/stellarSdkServer";
import { getTokenDetails } from "@shared/api/internal";
import { getTokenDetails, simulateTransaction } from "@shared/api/internal";
import { SoroswapToken } from "@shared/api/types";
import { buildSorobanServer } from "@shared/helpers/soroban/server";
import { isTestnet, xlmToStroop } from "helpers/stellar";
Expand Down Expand Up @@ -290,26 +290,42 @@ export const buildAndSimulateSoroswapTx = async ({
}
const builtTx = tx.build();

// Now we can simulate and see if we have any issues
const simulationTransaction = await sorobanServer.simulateTransaction(
builtTx,
);
if (isCustomNetwork(networkDetails)) {
// Now we can simulate and see if we have any issues
const simulationTransaction = await sorobanServer.simulateTransaction(
builtTx,
);

// If the simulation response is valid, we can prepare the transaction to be submitted to the network
// This is the transaction the user will sign and then submit to complete the swap
const preparedTransaction = Sdk.SorobanRpc.assembleTransaction(
builtTx,
simulationTransaction,
)
.build()
.toXDR();

if (Sdk.SorobanRpc.Api.isSimulationError(simulationTransaction)) {
throw new Error(simulationTransaction.error);
}

return {
simulationTransaction,
preparedTransaction,
};
}

const { ok, response } = await simulateTransaction({
xdr: builtTx.toXDR(),
networkDetails,
});

// If the simulation response is valid, we can prepare the transaction to be submitted to the network
// This is the transaction the user will sign and then submit to complete the swap
const preparedTransaction = Sdk.SorobanRpc.assembleTransaction(
builtTx,
simulationTransaction,
)
.build()
.toXDR();

if (Sdk.SorobanRpc.Api.isSimulationError(simulationTransaction)) {
throw new Error(simulationTransaction.error);
if (!ok) {
throw new Error(response as string);
}

return {
simulationTransaction,
preparedTransaction,
preparedTransaction: response.preparedTransaction,
simulationTransaction: response.simulationResponse,
};
};