Skip to content

Commit

Permalink
splits buildAndSimulateSoroswapTx into flows for custom networks, use…
Browse files Browse the repository at this point in the history
…s simulate-tx API for other networks (#1694)
  • Loading branch information
aristidesstaffieri authored Nov 21, 2024
1 parent aad0f96 commit 7e5655c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
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,
};
};

0 comments on commit 7e5655c

Please sign in to comment.