Skip to content
This repository was archived by the owner on Feb 11, 2025. It is now read-only.

Commit

Permalink
fix: bundle and use dotenv in binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
simonheys committed Jan 24, 2024
1 parent a07b2e5 commit 0eed4ce
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 15 deletions.
7 changes: 7 additions & 0 deletions dotenv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import path from "path";

/** Explicit path.join ensures that .env will be detected by pkg @see https://www.npmjs.com/package/pkg#detecting-assets-in-source-code */
const dotenvPath = path.join(__dirname, ".env");
require("dotenv").config({
path: dotenvPath,
});
4 changes: 3 additions & 1 deletion getAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ async function getAccountByKeyPair(
};
}
} catch (e) {
console.error(e);
if ((e as Error).message !== "RPC does not implement getCode function") {
console.error(e);
}
}
}

Expand Down
36 changes: 23 additions & 13 deletions getProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { RpcProvider } from "starknet";
import { RpcProvider } from "starknet-4220";
import { NetworkId } from "./types";

export const argentCliHeaders = {
"argent-version": process.env.VERSION || "Unknown version",
"argent-client": "argent-cli",
};

export const defaultRpcNodeUrls: Record<NetworkId, string[]> = {
"mainnet-alpha": [
"https://starknet-mainnet.public.blastapi.io",
Expand All @@ -12,23 +17,28 @@ export const defaultRpcNodeUrls: Record<NetworkId, string[]> = {
],
};

export const getRandomDefaultRpcNodeUrlsForNetworkId = (
networkId: NetworkId
) => {
const nodeUrls = defaultRpcNodeUrls[networkId];
const randomIndex = Math.floor(Math.random() * nodeUrls.length);
return nodeUrls[randomIndex];
export const getRpcNodeUrlsForNetworkId = (networkId: NetworkId) => {
const fallbackNodeUrls = defaultRpcNodeUrls[networkId];
if (networkId === "mainnet-alpha") {
return process.env.MAINNET_RPC_URL
? [process.env.MAINNET_RPC_URL]
: fallbackNodeUrls;
}
return process.env.TESTNET_RPC_URL
? [process.env.TESTNET_RPC_URL]
: fallbackNodeUrls;
};

export const getRpcNodeUrlForNetworkId = (networkId: NetworkId) => {
const fallbackNodeUrl = getRandomDefaultRpcNodeUrlsForNetworkId(networkId);
if (networkId === "mainnet-alpha") {
return process.env.MAINNET_RPC_URL ?? fallbackNodeUrl;
}
return process.env.TESTNET_RPC_URL ?? fallbackNodeUrl;
const nodeUrls = getRpcNodeUrlsForNetworkId(networkId);
const randomIndex = Math.floor(Math.random() * nodeUrls.length);
return nodeUrls[randomIndex];
};

export const getProviderForNetworkId = (networkId: NetworkId) => {
const nodeUrl = getRpcNodeUrlForNetworkId(networkId);
return new RpcProvider({ nodeUrl });
return new RpcProvider({
nodeUrl,
headers: argentCliHeaders,
});
};
7 changes: 7 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env npx ts-node
import "@total-typescript/ts-reset";
import "./dotenv";
import "./fetchPolyfill";

import { program } from "commander";
Expand All @@ -14,6 +15,7 @@ import { askForExtraAccounts, extraAccount } from "./ui/extraAccount";
import { equalSigner, getDefaultSigners } from "./genSigners";
import { detectAccountIssues, fixAccountIssues } from "./issues";
import { ec } from "starknet";
import { getRpcNodeUrlsForNetworkId } from "./getProvider";

program
.name("Argent X CLI")
Expand All @@ -36,6 +38,11 @@ program.parse();
(async () => {
const spinner = ora();

const mainnetRpcNodeUrls = getRpcNodeUrlsForNetworkId("mainnet-alpha");
const testnetRpcNodeUrls = getRpcNodeUrlsForNetworkId("goerli-alpha");
spinner.info(`Mainnet RPC: ${mainnetRpcNodeUrls.join(", ")}`);
spinner.info(`Testnet RPC: ${testnetRpcNodeUrls.join(", ")}`);

let { accounts, networkId, privateKey, seed } = await getAccountsAndNetwork(
spinner
);
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"start": "ts-node index.ts",
"prepare": "patch-package && tsc",
"build": "tsc && pkg . -C GZip"
"build": "tsc && [ -f .env ] && cp .env dist/ || true && pkg . --compress GZip --options \"no-warnings\""
},
"pkg": {
"targets": [
Expand All @@ -19,13 +19,17 @@
"latest-win-arm64",
"latest-win-x64"
],
"assets": [
"/**/.env"
],
"outputPath": "bin"
},
"devDependencies": {
"@total-typescript/ts-reset": "^0.4.2",
"@types/lodash.chunk": "^4.2.7",
"@types/prompts": "^2.0.14",
"@types/semver": "^7.3.10",
"dotenv": "^16.3.2",
"patch-package": "^6.4.7",
"pkg": "^5.8.0",
"postinstall-postinstall": "^2.1.0",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,11 @@ dir-glob@^3.0.1:
dependencies:
path-type "^4.0.0"

dotenv@^16.3.2:
version "16.3.2"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.3.2.tgz#3cb611ce5a63002dbabf7c281bc331f69d28f03f"
integrity sha512-HTlk5nmhkm8F6JcdXvHIzaorzCoziNQT9mGxLPVXW8wJF1TiGSL60ZGB4gHWabHOaMmWmhvk2/lPHfnBiT78AQ==

[email protected], elliptic@^6.5.4:
version "6.5.4"
resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.5.4.tgz#da37cebd31e79a1367e941b592ed1fbebd58abbb"
Expand Down

0 comments on commit 0eed4ce

Please sign in to comment.