Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
dhirajs0 authored Feb 23, 2024
2 parents cd78177 + 8139b45 commit daac48e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 30 deletions.
52 changes: 24 additions & 28 deletions src/api/rpcspec/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ const block_id = {
],
};


const contract_address = {
const contract_address = {
placeholder:
"0x124aeb495b947201f5fac96fd1138e326ad86195b98df6dec9009158a533b49",
description: "The address of the contract",
Expand Down Expand Up @@ -195,7 +194,6 @@ const DEPLOY_ACCOUNT_TXN_V1 = {

const BROADCASTED_DEPLOY_ACCOUNT_TXN = DEPLOY_ACCOUNT_TXN_V1;


const ReadMethods = [
// Returns the version of the Starknet JSON-RPC specification being used
{
Expand All @@ -206,31 +204,30 @@ const ReadMethods = [
});
`,
starknetGo: `package main
import (
"context"
"fmt"
"log"
import (
"context"
"fmt"
"log"
"github.com/NethermindEth/starknet.go/rpc"
)
func main() {
rpcUrl := "https://free-rpc.nethermind.io/mainnet-juno/"
client, err := rpc.NewClient(rpcUrl)
if err != nil {
log.Fatal(err)
}
provider := rpc.NewProvider(client)
specVersion, err := provider.SpecVersion(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Println("SpecVersion:", specVersion)
}`,
"github.com/NethermindEth/starknet.go/rpc"
)
func main() {
rpcUrl := "https://free-rpc.nethermind.io/mainnet-juno/"
client, err := rpc.NewClient(rpcUrl)
if err != nil {
log.Fatal(err)
}
provider := rpc.NewProvider(client)
specVersion, err := provider.SpecVersion(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Println("SpecVersion:", specVersion)
}`,
starknetRs: `use starknet::{
providers::{
jsonrpc::{HttpTransport, JsonRpcClient},
Expand Down Expand Up @@ -933,7 +930,6 @@ const TraceMethods = [
},
];


export const Methods = [...ReadMethods, ...TraceMethods, ...WriteMethods];
export const comingSoon = [
"starknet_addDeclareTransaction",
Expand Down
63 changes: 61 additions & 2 deletions src/components/Builder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
extractRpcUrl,
extractNodeUrl,
capitalize,
toCamelCase,
} from "./utils";

const formatName = (name: string) => {
Expand Down Expand Up @@ -318,7 +319,65 @@ const Builder = () => {
const updateStarknetGoParams = (currentParamsObj: {
[key: string]: any;
}) => {
return method.starknetGo; // TODO: Implement this
const regexPattern =
/provider := rpc\.NewProvider\(client\)[\s\S]*?result, err := provider\.(\w+)\(context\.Background\(\), ([^)]*)\)/;
const codeSnippet = method.starknetGo;

const updatedCode = codeSnippet.replace(
regexPattern,
(match, methodName, params) => {
let variableDefinitions = "";
const values = Object.entries(currentParamsObj).flatMap(
([key, value]) => {
if (key === "block_id") {
if (
typeof value === "string" &&
["latest", "pending"].includes(value)
) {
return `rpc.BlockID{Tag: "${value}"}`;
} else if (typeof value === "object") {
if (value.block_hash !== undefined) {
const blockHash = value.block_hash as string;
variableDefinitions += ` blockHash, _ := utils.HexToFelt("${blockHash}")\n`;
return `rpc.BlockID{Hash: blockHash}`;
} else if (value.block_number !== undefined) {
const blockNumber = value.block_number as number;
variableDefinitions += ` blockNumber, _ := utils.HexToFelt(${blockNumber})\n`;
return `rpc.BlockID{Number: &blockNumber}`;
}
}
} else if (typeof value === "object" && !Array.isArray(value)) {
// If value is an object, return its stringified values
return Object.values(value).map((val) => {
if (typeof val === "string") {
if (val.startsWith("0x")) {
const camelKey = toCamelCase(key);
variableDefinitions += ` ${camelKey}, _ := utils.HexToFelt("${val}")\n`;
return camelKey;
}
return `"${val}"`;
}
return val;
});
} else if (typeof value === "string") {
if (value.startsWith("0x")) {
const camelKey = toCamelCase(key);
variableDefinitions += ` ${camelKey}, _ := utils.HexToFelt("${value}")\n`;
return camelKey;
}
// If value is a string, return it with quotes
return `"${value}"`;
}
return value; // Return other types (like numbers) as is
}
);

let stringifiedParams = values.join(", ");
return `provider := rpc.NewProvider(client)\n${variableDefinitions} result, err := provider.${methodName}(context.Background(), ${stringifiedParams})`;
}
);

return updatedCode;
};

const updateStarknetRsParams = (currentParamsObj: {
Expand Down Expand Up @@ -996,7 +1055,7 @@ const Builder = () => {
}}
/>
)}

{requestTab == "starknetRs" && (
<Editor
height="50vh"
Expand Down
7 changes: 7 additions & 0 deletions src/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@ export const extractNodeUrl = (starknetJsCode: string) => {
export const capitalize = (str: string) =>
`${str.charAt(0).toUpperCase()}${str.slice(1)}`;


export const toCamelCase = (str: string) => {
return str.replace(/_([a-z])/g, function (match, letter) {
return letter.toUpperCase();
});
};

0 comments on commit daac48e

Please sign in to comment.