From 34cb74d39c3c07cf1c514d38cf79ba98ed0f8f0d Mon Sep 17 00:00:00 2001 From: Aristides Staffieri Date: Thu, 21 Nov 2024 10:37:48 -0700 Subject: [PATCH] splits buildAndSimulateSoroswapTx into flows for custom networks, uses simulate-tx API for other networks --- @shared/api/internal.ts | 27 +++++++++++ extension/src/popup/helpers/sorobanSwap.ts | 54 ++++++++++++++-------- 2 files changed, 62 insertions(+), 19 deletions(-) diff --git a/@shared/api/internal.ts b/@shared/api/internal.ts index b244bb19cb..038767a284 100644 --- a/@shared/api/internal.ts +++ b/@shared/api/internal.ts @@ -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, }: { diff --git a/extension/src/popup/helpers/sorobanSwap.ts b/extension/src/popup/helpers/sorobanSwap.ts index 65cc8db178..ce7b3a7708 100644 --- a/extension/src/popup/helpers/sorobanSwap.ts +++ b/extension/src/popup/helpers/sorobanSwap.ts @@ -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"; @@ -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, }; };