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

feat(reference): act-1497 - added curl example to request block #1547

Merged
merged 1 commit into from
Sep 12, 2024
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
16 changes: 14 additions & 2 deletions src/components/ParserOpenRPC/RequestBox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface RequestBoxProps {
paramsData: any;
openModal: () => void;
submitRequest: () => void;
isMetamaskNetwork?: boolean;
}

export default function RequestBox({
Expand All @@ -24,10 +25,18 @@ export default function RequestBox({
paramsData,
openModal,
submitRequest,
isMetamaskNetwork = false,
}: RequestBoxProps) {

const exampleRequest = useMemo(() => {
const preparedParams = JSON.stringify(paramsData, null, 2);
return `await window.ethereum.request({\n "method": "${method}",\n "params": ${preparedParams.replace(/"([^"]+)":/g, '$1:')},\n});`;
const preparedShellParams = JSON.stringify(paramsData);
const NETWORK_URL = "https://linea-mainnet.infura.io";
const API_KEY = "<YOUR-API-KEY>";
if (isMetamaskNetwork) {
return `await window.ethereum.request({\n "method": "${method}",\n "params": ${preparedParams.replace(/"([^"]+)":/g, '$1:')},\n});`;
}
return `curl ${NETWORK_URL}/v3/${API_KEY} \\\n -X POST \\\n -H "Content-Type: application/json" \\\n -d '{\n "jsonrpc": "2.0",\n "method": "${method}",\n "params": ${preparedShellParams},\n "id": 1\n }'`;
}, [method, paramsData]);

const exampleResponse = useMemo(() => {
Expand Down Expand Up @@ -57,7 +66,10 @@ export default function RequestBox({
<strong className={styles.cardHeading}>Request</strong>
</div>
<div className={styles.codeWrapper}>
<CodeBlock language="javascript" className="margin-bottom--none">
<CodeBlock
language={isMetamaskNetwork ? "javascript" : "shell"}
className="margin-bottom--none"
>
{exampleRequest}
</CodeBlock>
</div>
Expand Down
3 changes: 3 additions & 0 deletions src/components/ParserOpenRPC/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ export default function ParserOpenRPC({ network, method, extraContent }: ParserP

if (currentMethodData === null) return null;

const isMetamaskNetwork = network === NETWORK_NAMES.metamask;

const { metaMaskAccount, metaMaskProvider, metaMaskConnectHandler } = useContext(MetamaskProviderContext);

const onParamsChangeHandle = (data) => {
Expand Down Expand Up @@ -235,6 +237,7 @@ export default function ParserOpenRPC({ network, method, extraContent }: ParserP
response={reqResult}
openModal={openModal}
submitRequest={onSubmitRequestHandle}
isMetamaskNetwork={isMetamaskNetwork}
/>
</div>
</div>
Expand Down
6 changes: 2 additions & 4 deletions wallet/reference/new-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ sidebar_class_name: "hidden"

import ParserOpenRPC from "@site/src/components/ParserOpenRPC"
import { NETWORK_NAMES } from "@site/src/plugins/plugin-json-rpc"
import Description from "@site/services/reference/_partials/_eth_sendtransaction-description.mdx"

<ParserOpenRPC
network={NETWORK_NAMES.metamask}
method="eth_requestAccounts"
extraContent={<Description />}
network={NETWORK_NAMES.linea}
method="eth_blockNumber"
/>
Loading