Skip to content

Commit

Permalink
[Rpc endpoint] Bug fixes (#911)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeesunikim authored Jul 30, 2024
1 parent f583236 commit b8da7cd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 29 deletions.
61 changes: 43 additions & 18 deletions src/app/(sidebar)/endpoints/[[...pages]]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export default function Endpoints() {
.find((page) => pathname.includes(page.route))
?.nestedItems?.find((i) => i.route === pathname);

const rpcPage = ENDPOINTS_PAGES_RPC.navItems.find((page) =>
pathname.includes(page.route),
const rpcPage = ENDPOINTS_PAGES_RPC.navItems.find(
(page) => pathname === page.route,
);

const page = isRpcEndpoint ? rpcPage : horizonPage;
Expand Down Expand Up @@ -128,7 +128,7 @@ export default function Endpoints() {
const [urlParams, setUrlParams] = useState("");

const getRpcPostPayloadProps = (endpoint: string) => {
const defaultRpcRequestBody = {
const defaultRpcRequestBody: AnyObject = {
jsonrpc: "2.0",
id: 8675309,
method: pageData?.rpcMethod,
Expand All @@ -138,17 +138,31 @@ export default function Endpoints() {
case Routes.ENDPOINTS_GET_EVENTS: {
const filteredParams = params.filters ? JSON.parse(params.filters) : {};

// do not display the empty string unless its field is filled
const filteredContractIds = filteredParams.contract_ids
? filteredParams.contract_ids.filter((topic: string) => topic.length)
: [];
// [filter] do not display the empty string unless its field is filled
// [map] Parse the JSON string to JSON
const filteredTopics = filteredParams.topics
? filteredParams.topics
.filter((topic: string) => topic.length)
.map((item: string) => JSON.parse(item))
: [];

return {
...defaultRpcRequestBody,
params: {
startLedger: params.ledger ?? "",
cursor: params.cursor,
limit: params.limit,
startLedger: Number(params.startLedger),
pagination: {
cursor: params.cursor,
limit: Number(params.limit) || undefined,
},
filters: [
{
type: filteredParams.type ?? "",
contractIds: filteredParams.contract_ids ?? "",
topics: filteredParams.topics ?? "",
contractIds: filteredContractIds ?? [],
topics: filteredTopics ?? [],
},
],
},
Expand All @@ -159,7 +173,7 @@ export default function Endpoints() {
return {
...defaultRpcRequestBody,
params: {
keys: params.transaction ?? "",
keys: params.tx ?? "",
},
};
}
Expand All @@ -177,9 +191,11 @@ export default function Endpoints() {
return {
...defaultRpcRequestBody,
params: {
startLedger: params.ledger ?? "",
cursor: params.cursor,
limit: params.limit,
startLedger: Number(params.startLedger),
pagination: {
cursor: params.cursor,
limit: Number(params.limit) || undefined,
},
},
};
}
Expand All @@ -188,7 +204,7 @@ export default function Endpoints() {
return {
...defaultRpcRequestBody,
params: {
transaction: params.transaction ?? "",
transaction: params.tx ?? "",
},
};
}
Expand All @@ -197,9 +213,9 @@ export default function Endpoints() {
return {
...defaultRpcRequestBody,
params: {
transaction: params.transaction ?? "",
transaction: params.tx ?? "",
resourceConfig: {
instructionLeeway: params.resourceConfig,
instructionLeeway: Number(params.resourceConfig) || undefined,
},
},
};
Expand Down Expand Up @@ -262,7 +278,7 @@ export default function Endpoints() {
isValidReqFields = missingReqFields.length === 0;

// Checking if there are any errors
isValid = formError.tx?.result === "success" || isEmptyObject(formError);
isValid = isEmptyObject(formError);

// Asset components
const assetParams = [
Expand Down Expand Up @@ -540,6 +556,7 @@ export default function Endpoints() {

const renderPostPayload = () => {
let renderedProps = getPostPayload();
const defaultRowsLength = 5;

if (pageData?.requestMethod === "POST") {
if (pathname === Routes.ENDPOINTS_TRANSACTIONS_POST) {
Expand All @@ -552,14 +569,22 @@ export default function Endpoints() {
}

if (renderedProps) {
const requiredParams = renderedProps.params
? Object.values(renderedProps.params).filter((val) => val !== undefined)
: undefined;

const rows = requiredParams
? requiredParams.length + defaultRowsLength + 2
: defaultRowsLength;

return (
<div className="Endpoints__txTextarea">
<Textarea
id="tx"
fieldSize="md"
label="Payload"
value={renderedProps ? JSON.stringify(renderedProps, null, 2) : ""}
rows={5}
rows={rows}
disabled
spellCheck={false}
/>
Expand Down Expand Up @@ -695,7 +720,7 @@ export default function Endpoints() {

const error = component.validate?.(value, isRequired);

if (error) {
if (error && error.result !== "success") {
setFormError({ ...formError, [f]: error });
} else if (formError[f]) {
const updatedErrors = { ...formError };
Expand Down
16 changes: 16 additions & 0 deletions src/components/formComponentTemplateEndpoints.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,22 @@ export const formComponentTemplateEndpoints = (
),
validate: validate.getPositiveIntError,
};
case "startLedger":
return {
render: (templ: TemplateRenderProps) => (
<PositiveIntPicker
key={id}
id={id}
label="Start Ledger Sequence"
placeholder="Ex: 1714814"
labelSuffix={!templ.isRequired ? "optional" : undefined}
value={templ.value || ""}
error={templ.error}
onChange={templ.onChange}
/>
),
validate: validate.getPositiveIntError,
};
case "limit":
return {
render: (templ: TemplateRenderProps) => (
Expand Down
22 changes: 11 additions & 11 deletions src/constants/endpointsPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ export const ENDPOINTS_PAGES_RPC: EndpointsPagesProps = {
docsUrl:
"https://developers.stellar.org/docs/data/rpc/api-reference/methods/getEvents",
docsLabel: "getEvents",
endpointUrlTemplate: "/{?ledger,cursor,limit,filters}",
endpointUrlTemplate: "{?startLedger,cursor,limit,filters}",
requestMethod: "POST",
requiredParams: "ledger,filters",
requiredParams: "startLedger,filters",
rpcMethod: "getEvents",
isStreaming: false,
},
Expand Down Expand Up @@ -92,9 +92,9 @@ export const ENDPOINTS_PAGES_RPC: EndpointsPagesProps = {
docsUrl:
"https://developers.stellar.org/docs/data/rpc/api-reference/methods/getLedgerEntries",
docsLabel: "getLedgerEntries",
endpointUrlTemplate: "/{?transaction}",
endpointUrlTemplate: "{?tx}",
requestMethod: "POST",
requiredParams: "transaction",
requiredParams: "tx",
rpcMethod: "getLedgerEntries",
isStreaming: false,
},
Expand All @@ -119,7 +119,7 @@ export const ENDPOINTS_PAGES_RPC: EndpointsPagesProps = {
docsUrl:
"https://developers.stellar.org/docs/data/rpc/api-reference/methods/getTransaction",
docsLabel: "getTransaction",
endpointUrlTemplate: "/{?transaction}",
endpointUrlTemplate: "{?transaction}",
requestMethod: "POST",
requiredParams: "transaction",
rpcMethod: "getTransaction",
Expand All @@ -133,9 +133,9 @@ export const ENDPOINTS_PAGES_RPC: EndpointsPagesProps = {
docsUrl:
"https://developers.stellar.org/docs/data/rpc/api-reference/methods/getTransactions",
docsLabel: "getTransactions",
endpointUrlTemplate: "{?startLedger,cursor,limit}",
requestMethod: "POST",
requiredParams: "ledger",
endpointUrlTemplate: "/{?ledger,cursor,limit}",
requiredParams: "startLedger",
rpcMethod: "getTransactions",
isStreaming: false,
},
Expand All @@ -160,9 +160,9 @@ export const ENDPOINTS_PAGES_RPC: EndpointsPagesProps = {
docsUrl:
"https://developers.stellar.org/docs/data/rpc/api-reference/methods/sendTransaction",
docsLabel: "sendTransaction",
endpointUrlTemplate: "/{?transaction}",
endpointUrlTemplate: "{?tx}",
requestMethod: "POST",
requiredParams: "/{?transaction}",
requiredParams: "tx",
rpcMethod: "sendTransaction",
isStreaming: false,
},
Expand All @@ -174,9 +174,9 @@ export const ENDPOINTS_PAGES_RPC: EndpointsPagesProps = {
docsUrl:
"https://developers.stellar.org/docs/data/rpc/api-reference/methods/simulateTransaction",
docsLabel: "simulateTransaction",
endpointUrlTemplate: "/{?transaction,resourceConfig}",
endpointUrlTemplate: "{?tx,resourceConfig}",
requestMethod: "POST",
requiredParams: "transaction",
requiredParams: "tx",
rpcMethod: "simulateTransaction",
isStreaming: false,
},
Expand Down

0 comments on commit b8da7cd

Please sign in to comment.