Skip to content
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
66 changes: 57 additions & 9 deletions src/route/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ import { ERROR } from "../helper/error";
import * as StellarHelpers from "../helper/stellar";
import * as OnrampHelpers from "../helper/onramp";
import * as HorizonRpcHelpers from "../helper/horizon-rpc";
import { getStellarRpcUrls } from "../helper/soroban-rpc";
import { StellarRpcConfig } from "../config";

const mockStellarRpcConfig = {
freighterRpcPubnetUrl: "https://rpc-pubnet.stellar.org",
freighterRpcTestnetUrl: "https://rpc-testnet.stellar.org",
freighterRpcFuturenetUrl: "https://rpc-futurenet.stellar.org",
} as StellarRpcConfig;

jest.mock("@blockaid/client", () => {
return class Blockaid {
Expand Down Expand Up @@ -996,6 +988,40 @@ describe("API routes", () => {
await server.close();
});
});
describe("/submit-tx", () => {
afterEach(() => {
jest.restoreAllMocks();
});

it("can submit a transaction", async () => {
jest
.spyOn(HorizonRpcHelpers, "submitTransaction")
.mockResolvedValue({ data: { hash: "tx-hash" }, error: null } as any);

const server = await getDevServer();
const url = new URL(
`http://localhost:${
(server?.server?.address() as any).port
}/api/v1/submit-tx`,
);
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
signed_xdr: TEST_SOROBAN_TX,
network_passphrase: Networks.TESTNET,
}),
};
const response = await fetch(url.href, options);
const data = await response.json();

expect(response.status).toEqual(200);
expect(data).toEqual({ hash: "tx-hash" });
await server.close();
});
});
describe("/simulate-tx", () => {
const simResponse = "simulated xdr";
const preparedTransaction = "assembled tx xdr";
Expand Down Expand Up @@ -1048,7 +1074,6 @@ describe("API routes", () => {
},
body: JSON.stringify({
xdr: TEST_SOROBAN_TX,
network_url: getStellarRpcUrls(mockStellarRpcConfig).TESTNET,
network_passphrase: Networks.TESTNET,
}),
};
Expand All @@ -1061,6 +1086,29 @@ describe("API routes", () => {
await server.close();
});

it("returns an error for an unknown network passphrase", async () => {
const server = await getDevServer();
const url = new URL(
`http://localhost:${
(server?.server?.address() as any).port
}/api/v1/simulate-tx`,
);
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
xdr: TEST_SOROBAN_TX,
network_passphrase: "unknown-passphrase",
}),
};
const response = await fetch(url.href, options);

expect(response.status).toEqual(400);
await server.close();
});

it("can fetch an onramp token", async () => {
jest.spyOn(OnrampHelpers, "fetchOnrampSessionToken").mockReturnValueOnce(
Promise.resolve({
Expand Down
16 changes: 9 additions & 7 deletions src/route/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,6 @@ export async function initApiServer(
required: ["signed_xdr", "network_passphrase"],
properties: {
signed_xdr: { type: "string" },
network_url: { type: "string" },
network_passphrase: { type: "string" },
},
},
Expand All @@ -1269,7 +1268,6 @@ export async function initApiServer(
request: FastifyRequest<{
Body: {
signed_xdr: string;
network_url?: string;
network_passphrase: string;
};
}>,
Expand Down Expand Up @@ -1299,10 +1297,9 @@ export async function initApiServer(
schema: {
body: {
type: "object",
required: ["xdr", "network_url", "network_passphrase"],
required: ["xdr", "network_passphrase"],
properties: {
xdr: { type: "string" },
network_url: { type: "string" },
network_passphrase: { type: "string" },
},
},
Expand All @@ -1311,18 +1308,23 @@ export async function initApiServer(
request: FastifyRequest<{
Body: {
xdr: string;
network_url: string;
network_passphrase: string;
};
}>,
reply,
) => {
const { xdr, network_url, network_passphrase } = request.body;
const { xdr, network_passphrase } = request.body;

try {
const Sdk = getSdk(network_passphrase as Networks);
const networkName = networkPassphraseToName(network_passphrase);
if (!networkName) {
throw new Error(
`Unknown network passphrase: ${network_passphrase}`,
);
}
const tx = Sdk.TransactionBuilder.fromXDR(xdr, network_passphrase);
const server = await mercuryClient.getRpcServer(network_url);
const server = await getServer(networkName, stellarRpcConfig);

const simulationResponse = await server.simulateTransaction(tx);
const preparedTransaction = Sdk.rpc
Expand Down
Loading