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

adds route and helper to submit transaction to the horizon rpc #13

Merged
merged 5 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 32 additions & 1 deletion src/helper/horizon-rpc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BigNumber from "bignumber.js";
import { AssetType, Horizon } from "stellar-sdk";
import { AssetType, Horizon, TransactionBuilder } from "stellar-sdk";

export const BASE_RESERVE = 0.5;
export const BASE_RESERVE_MIN_COUNT = 2;
Expand Down Expand Up @@ -229,3 +229,34 @@ export const fetchAccountHistory = async (
throw new Error(JSON.stringify(error));
}
};

export const submitTransaction = async (
signedXDR: string,
networkUrl: string,
networkPassphrase: string
): Promise<{
data: Horizon.HorizonApi.SubmitTransactionResponse | null;
error: unknown;
}> => {
const tx = TransactionBuilder.fromXDR(signedXDR, networkPassphrase);
const server = new Horizon.Server(networkUrl);

try {
const data = await server.submitTransaction(tx);
return {
data,
error: null,
};
} catch (e: any) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you're just picking up this code from the Freighter codebase, but maybe we can type this error now. I think it should be BadResponseError from stellar-sdk

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah so I actually tried this but it turns out in TS, you can only annotate the error from the catch block as an any or unknown. https://byby.dev/ts-try-catch-error-type#:~:text=The%20only%20type%20annotations%20that,on%20it%20without%20type%20checking.

So I did annotate it in the response shape from this function but not sure if I can actually annotate the error as it come in from the catch block.

if (e.response.status === 504) {
// in case of 504, keep retrying this tx until submission succeeds or we get a different error
// https://developers.stellar.org/api/errors/http-status-codes/horizon-specific/timeout
// https://developers.stellar.org/docs/encyclopedia/error-handling
return await submitTransaction(signedXDR, networkUrl, networkPassphrase);
}
return {
data: null,
error: e,
};
}
};
46 changes: 46 additions & 0 deletions src/route/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
isNetwork,
NetworkNames,
} from "../helper/validate";
import { submitTransaction } from "../helper/horizon-rpc";

const API_VERSION = "v1";

Expand Down Expand Up @@ -279,6 +280,51 @@ export function initApiServer(
},
});

instance.route({
method: "POST",
url: "/submit-tx",
schema: {
body: {
type: "object",
properties: {
signed_xdr: { type: "string" },
network_url: { type: "string" },
network_passphrase: { type: "string" },
},
},
response: {
200: {
type: "object",
properties: {
data: { type: "object" },
},
},
},
},
handler: async (
request: FastifyRequest<{
Body: {
signed_xdr: string;
network_url: string;
network_passphrase: string;
};
}>,
reply
) => {
const { signed_xdr, network_url, network_passphrase } = request.body;
const { data, error } = await submitTransaction(
signed_xdr,
network_url,
network_passphrase
);
if (error) {
reply.code(400).send(error);
} else {
reply.code(200).send(data);
}
},
});

next();
},
{ prefix: `/api/${API_VERSION}` }
Expand Down
Loading