Skip to content

Commit b4a1b8a

Browse files
committed
Add API and cURL code snippets for contract interactions
Introduces API and cURL environments to the code segment component and provides example code snippets for reading, writing, and listening to contract events via API and cURL. Updates the contract overview and function components to support these new environments and ensures cURL snippets are only included when available. Also refines code formatting and improves default environment selection.
1 parent 74e3f47 commit b4a1b8a

File tree

3 files changed

+106
-24
lines changed

3 files changed

+106
-24
lines changed

apps/dashboard/src/@/components/blocks/code/code-segment.client.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
"use client";
2+
23
import type React from "react";
34
import { type Dispatch, type SetStateAction, useMemo } from "react";
45
import { CodeClient } from "@/components/ui/code/code.client";
56
import { TabButtons } from "@/components/ui/tabs";
67
import { cn } from "@/lib/utils";
78

89
export type CodeEnvironment =
10+
| "api"
911
| "javascript"
1012
| "typescript"
1113
| "react"
1214
| "react-native"
1315
| "unity"
14-
| "api";
16+
| "curl";
1517

1618
type SupportedEnvironment = {
1719
environment: CodeEnvironment;
@@ -21,6 +23,10 @@ type SupportedEnvironment = {
2123
type CodeSnippet = Partial<Record<CodeEnvironment, string>>;
2224

2325
const Environments: SupportedEnvironment[] = [
26+
{
27+
environment: "api",
28+
title: "API",
29+
},
2430
{
2531
environment: "javascript",
2632
title: "JavaScript",
@@ -37,14 +43,14 @@ const Environments: SupportedEnvironment[] = [
3743
environment: "react-native",
3844
title: "React Native",
3945
},
40-
{
41-
environment: "api",
42-
title: "API",
43-
},
4446
{
4547
environment: "unity",
4648
title: "Unity",
4749
},
50+
{
51+
environment: "curl",
52+
title: "cURL",
53+
},
4854
];
4955

5056
interface CodeSegmentProps {
@@ -130,8 +136,10 @@ export const CodeSegment: React.FC<CodeSegmentProps> = ({
130136
: activeEnvironment === "unity"
131137
? "cpp"
132138
: activeEnvironment === "api"
133-
? "bash"
134-
: activeEnvironment
139+
? "javascript"
140+
: activeEnvironment === "curl"
141+
? "bash"
142+
: activeEnvironment
135143
}
136144
/>
137145
)}

apps/dashboard/src/@/components/contracts/code-overview.tsx

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ export default function Component() {
7070
// Get your contract
7171
var contract = await ThirdwebManager.Instance.GetContract(
7272
address: "{{contract_address}}",
73-
chainId: {{chainId}},
74-
abi: "optional-abi"
73+
chainId: {{chainId}},
7574
);
7675
7776
// Listen to contract events
@@ -116,12 +115,15 @@ export default function Component() {
116115
// Get your contract
117116
var contract = await ThirdwebManager.Instance.GetContract(
118117
address: "{{contract_address}}",
119-
chainId: {{chainId}},
120-
abi: "optional-abi"
118+
chainId: {{chainId}}
121119
);
122120
123121
// Read from the contract
124-
var result = await contract.Read<T>(contract, "{{function}}", {{args}});`,
122+
var result = await contract.Read<T>(
123+
contract,
124+
"{{function}}",
125+
{{args}}
126+
);`,
125127
},
126128
setup: {
127129
javascript: `import { createThirdwebClient, getContract } from "thirdweb";
@@ -190,8 +192,7 @@ function App() {
190192
// Get your contract
191193
var contract = await ThirdwebManager.Instance.GetContract(
192194
address: "{{contract_address}}",
193-
chainId: {{chainId}},
194-
abi: "optional-abi"
195+
chainId: {{chainId}}
195196
);`,
196197
},
197198
write: {
@@ -241,14 +242,74 @@ export default function Component() {
241242
// Get your contract
242243
var contract = await ThirdwebManager.Instance.GetContract(
243244
address: "{{contract_address}}",
244-
chainId: {{chainId}},
245-
abi: "optional-abi"
245+
chainId: {{chainId}}
246246
);
247247
248248
// Write to the contract
249-
var transactionReceipt = await contract.Write(wallet, contract, "{{function}}", weiValue, {{args}});`,
249+
var transactionReceipt = await contract.Write(
250+
wallet,
251+
contract,
252+
"{{function}}",
253+
weiValue,
254+
{{args}}
255+
);`,
250256
},
251257
api: {
258+
read: `const response = await fetch('https://api.thirdweb.com/v1/contracts/read', {
259+
method: 'POST',
260+
headers: {
261+
'Content-Type': 'application/json',
262+
'x-secret-key': '<YOUR_SECRET_KEY>'
263+
},
264+
body: JSON.stringify({
265+
calls: [
266+
{
267+
contractAddress: "{{contract_address}}",
268+
method: "{{function}}",
269+
params: [{{args}}]
270+
}
271+
],
272+
chainId: {{chainId}}
273+
})
274+
});
275+
276+
const data = await response.json();`,
277+
write: `const response = await fetch('https://api.thirdweb.com/v1/contracts/write', {
278+
method: 'POST',
279+
headers: {
280+
'Content-Type': 'application/json',
281+
'x-secret-key': '<YOUR_SECRET_KEY>'
282+
},
283+
body: JSON.stringify({
284+
calls: [
285+
{
286+
contractAddress: "{{contract_address}}",
287+
method: "{{function}}",
288+
params: [{{args}}]
289+
}
290+
],
291+
chainId: {{chainId}},
292+
from: "<YOUR_WALLET_ADDRESS>"
293+
})
294+
});
295+
296+
const data = await response.json();`,
297+
events: `const response = await fetch('https://api.thirdweb.com/v1/contracts/events', {
298+
method: 'POST',
299+
headers: {
300+
'Content-Type': 'application/json',
301+
'x-secret-key': '<YOUR_SECRET_KEY>'
302+
},
303+
body: JSON.stringify({
304+
contractAddress: "{{contract_address}}",
305+
eventName: "{{function}}",
306+
chainId: {{chainId}}
307+
})
308+
});
309+
310+
const data = await response.json();`,
311+
},
312+
curl: {
252313
read: `# OR use secret key (backend only, not frontend): --header 'x-secret-key: <YOUR_SECRET_KEY>'
253314
curl https://api.thirdweb.com/v1/contracts/read \\
254315
--request POST \\
@@ -264,8 +325,7 @@ curl https://api.thirdweb.com/v1/contracts/read \\
264325
],
265326
"chainId": {{chainId}}
266327
}'`,
267-
write: `# Note: Secret key should only be used in backend environments, not frontend
268-
curl -X POST https://api.thirdweb.com/v1/contracts/write \\
328+
write: `curl -X POST https://api.thirdweb.com/v1/contracts/write \\
269329
-H "Content-Type: application/json" \\
270330
-H "x-secret-key: <YOUR_SECRET_KEY>" \\
271331
-d '{
@@ -277,13 +337,12 @@ curl -X POST https://api.thirdweb.com/v1/contracts/write \\
277337
}
278338
],
279339
"chainId": {{chainId}},
280-
"from": "0x1234567890123456789012345678901234567890"
340+
"from": "<YOUR_WALLET_ADDRESS>"
281341
}'`,
282-
events: `# OR use secret key (backend only, not frontend): --header 'x-secret-key: <YOUR_SECRET_KEY>'
283-
curl https://api.thirdweb.com/v1/contracts/events \\
342+
events: `curl https://api.thirdweb.com/v1/contracts/events \\
284343
--request POST \\
285344
--header 'Content-Type: application/json' \\
286-
--header 'x-client-id: <YOUR_CLIENT_ID>' \\
345+
--header 'x-secret-key: <YOUR_SECRET_KEY>' \\
287346
--data '{
288347
"contractAddress": "{{contract_address}}",
289348
"eventName": "{{function}}",
@@ -612,7 +671,7 @@ export function CodeOverview(props: {
612671
| undefined;
613672

614673
const [environment, setEnvironment] = useState<CodeEnvironment>(
615-
defaultEnvironment || "javascript",
674+
defaultEnvironment || "api",
616675
);
617676

618677
const [tab, setTab] = useState("write");

apps/dashboard/src/@/components/contracts/functions/contract-function.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,24 @@ function ContractFunctionInner(props: ContractFunctionProps) {
134134
)
135135
: {};
136136

137+
// Safety check: Only include cURL snippet if the command exists in COMMANDS.curl
138+
const curlSnippet = COMMANDS.curl[commandsKey]
139+
? formatSnippet(
140+
{ curl: COMMANDS.curl[commandsKey] },
141+
{
142+
args: fn.inputs?.map((i) => i.name || ""),
143+
chainId: contract.chain.id,
144+
contractAddress: contract.address,
145+
extensionNamespace,
146+
fn,
147+
},
148+
)
149+
: {};
150+
137151
const codeSnippet = {
138152
...baseSnippet,
139153
...apiSnippet,
154+
...curlSnippet,
140155
};
141156

142157
return (

0 commit comments

Comments
 (0)