Skip to content

Commit

Permalink
clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
killroy192 committed Jul 23, 2024
1 parent 3ed0911 commit df32837
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 90 deletions.
1 change: 1 addition & 0 deletions .apprc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cli": {
"cluster_id": "main",
"funding": {
"chain": "eth",
"filters": [
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ reports/
master_key
.apprc
.secretsrc
.secrets
.secrets
clusters/
3 changes: 1 addition & 2 deletions docs/profile.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,5 @@ make recover
Decrypt

```sh
make decrypt in=.profiles out=.profiles.json
make decrypt in=clusters/main/.profiles out=clusters/main/.profiles.json
```

4 changes: 1 addition & 3 deletions src/cli/profiles.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { createProfiles } from "src/core/profiles";
import { getAppConf } from "src/libs/configs";
import { getMasterKey } from "src/libs/shared";
import { logger } from "src/logger";

(async function main() {
logger.info("initiated", { label: "CLI::profiles" });
const conf = await getAppConf();
const masterKey = await getMasterKey();
await createProfiles(conf.cli.profiles.amount, conf.cli.profiles.chains, masterKey);
await createProfiles(conf.cli.profiles.amount, conf.cli.profiles.chains);
logger.info("finished", { label: "CLI::profiles" });
})();
4 changes: 1 addition & 3 deletions src/cli/recover.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { recoverProfiles } from "src/core/profiles";
import { getAppConf } from "src/libs/configs";
import { getMasterKey } from "src/libs/shared";
import { logger } from "src/logger";

(async function main() {
logger.info("initiated", { label: "CLI::recover" });
const conf = await getAppConf();
const masterKey = await getMasterKey();
await recoverProfiles(conf.cli.profiles.chains, masterKey);
await recoverProfiles(conf.cli.profiles.chains);
logger.info("finished", { label: "CLI::recover" });
})();
37 changes: 13 additions & 24 deletions src/core/profiles/profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { genBtc } from "./btc";
import { genTia, genAtom } from "./cosmos";
import { genEVM } from "./evm";
import { genSol } from "./sol";
import { getProfiles } from "src/libs/configs";
import { encrypt } from "src/libs/crypt";
import { getProfilesSafe, saveProfiles } from "src/libs/configs";
import { Networks, Profile, NetworkWallet } from "src/types/configs";

const networksMap = {
Expand All @@ -27,10 +26,9 @@ const genAll = (seed: Buffer, networks: Networks[]) =>
{} as Record<Networks, NetworkWallet>,
);

const generateAndSave = async (mnemonics: string[], networks: Networks[], masterKey: string) => {
const addProfiles = fs.existsSync(".profiles.json");
const profiles = addProfiles ? await getProfiles() : {};
const offsetIndex = addProfiles ? Number(Object.keys(profiles).sort((a, b) => Number(b) - Number(a))[0]) + 1 : 0;
const generateAndSave = async (mnemonics: string[], networks: Networks[]) => {
const profiles = await getProfilesSafe();
const offsetIndex = profiles.length ? Number(Object.keys(profiles).sort((a, b) => Number(b) - Number(a))[0]) + 1 : 0;
const data = mnemonics.reduce((acc, mnemonic, i) => {
const seed = bip39.mnemonicToSeedSync(mnemonic);
acc[i + offsetIndex] = {
Expand All @@ -39,24 +37,15 @@ const generateAndSave = async (mnemonics: string[], networks: Networks[], master
};
return acc;
}, {} as Profile);
fs.writeFileSync(
".profiles",
encrypt(
JSON.stringify({
...profiles,
...data,
}),
masterKey,
),
);
saveProfiles({
...profiles,
...data,
});
};

export const createProfiles = (amount: number, networks: Networks[], masterKey: string) =>
generateAndSave(
Array.from({ length: amount }).map(() => bip39.generateMnemonic()),
networks,
masterKey,
);
const genMnemonics = (amount: number) => Array.from({ length: amount }).map(() => bip39.generateMnemonic());

export const createProfiles = (amount: number, networks: Networks[]) => generateAndSave(genMnemonics(amount), networks);

export const recoverProfiles = (networks: Networks[], masterKey: string) =>
generateAndSave(JSON.parse(fs.readFileSync(".mnemonics.json", "utf-8")) as string[], networks, masterKey);
export const recoverProfiles = (networks: Networks[]) =>
generateAndSave(JSON.parse(fs.readFileSync(".mnemonics.json", "utf-8")) as string[], networks);
5 changes: 3 additions & 2 deletions src/core/reports/mainnet/mainnetReport.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axiosRetry from "axios-retry";
import * as chains from "viem/chains";
import { getFuelReport } from "./fuel";
import { getProfiles } from "src/libs/configs";
import { getProfiles, operationFolder } from "src/libs/configs";
import { getFormattedPortfolio } from "src/libs/portfolio";
import { refreshProxy } from "src/libs/proxify";
import { saveInFolder, getRandomArbitrary } from "src/libs/shared";
Expand Down Expand Up @@ -53,6 +53,7 @@ export async function mainnetReport({ save, params }: { save: boolean; params: s
}

if (save) {
saveInFolder("./reports/mainnet.report.json", JSON.stringify(report, null, 2));
const folder = await operationFolder();
saveInFolder(`${folder}/reports/mainnet.report.json`, JSON.stringify(report, null, 2));
}
}
5 changes: 3 additions & 2 deletions src/core/reports/scroll/scrollReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { scroll } from "viem/chains";
import { isProfileMinted } from "./canvas";
import { getLpStats } from "./nuri";
import { accountPoints } from "./points";
import { getProfiles } from "src/libs/configs";
import { getProfiles, operationFolder } from "src/libs/configs";
import { getFormattedPortfolio } from "src/libs/portfolio";
import { refreshProxy } from "src/libs/proxify";
import { saveInFolder, getRandomArbitrary } from "src/libs/shared";
Expand Down Expand Up @@ -45,6 +45,7 @@ export async function scrollReport({ save }: { save: boolean }) {
Promise.resolve({}),
);
if (save) {
saveInFolder("./reports/scroll.report.json", JSON.stringify(data, null, 2));
const folder = await operationFolder();
saveInFolder(`${folder}/reports/scroll.report.json`, JSON.stringify(data, null, 2));
}
}
47 changes: 39 additions & 8 deletions src/libs/configs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { readFileSync } from "node:fs";
import { readFileSync, existsSync } from "node:fs";
import { resolve } from "node:path";
import { decryptJson } from "./crypt";
import { getMasterKey } from "./shared";
import { decryptJson, encrypt } from "./crypt";
import { getMasterKey, saveInFolder } from "./shared";
import { logger } from "src/logger";
import { JsonObj } from "src/types/common";
import { AppConfig, Profile, EVMWallet } from "src/types/configs";

Expand All @@ -22,13 +23,43 @@ let profiles: Profile;

export const getProfiles = async () => {
if (!profiles) {
const folder = await operationFolder();
const masterKey = await getMasterKey();
profiles = decryptJson(readFileSync(resolve(".", ".profiles"), "utf-8").trimEnd(), masterKey) as Profile;
profiles = decryptJson(readFileSync(`${folder}/.profiles`, "utf-8").trimEnd(), masterKey) as Profile;
}
return profiles;
};

export const getEVMWallets = async () =>
Object.values(await getProfiles()).map(({ wallets }) => ({
...(wallets.evm as EVMWallet),
}));
export const getProfilesSafe = async () => {
const folder = await operationFolder();
const profilesExist = existsSync(`${folder}/.profiles`);
if (profilesExist) {
return getProfiles();
}
logger.warn(`.profiles file is not exist for path ${folder}/.profiles, return empty`, {
label: "utils/getProfilesSafe",
});
return {};
};

export const saveProfiles = async (profiles: Profile) => {
const masterKey = await getMasterKey();
const folder = await operationFolder();
return saveInFolder(`${folder}/.profiles`, encrypt(JSON.stringify(profiles), masterKey));
};

let wallets: EVMWallet[];

export const getEVMWallets = async () => {
if (!wallets) {
wallets = Object.values(await getProfiles()).map(({ wallets }) => ({
...(wallets.evm as EVMWallet),
}));
}
return wallets;
};

export const operationFolder = async () => {
const appConfig = await getAppConf();
return `clusters/${appConfig.cli.cluster_id}`;
};
10 changes: 4 additions & 6 deletions src/libs/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ export const getRandomArbitrary = (min: number, max: number) => {
};

export const saveInFolder = (savePath: string, data: string) => {
const parsedPath = savePath.split("/");
const clearPath = parsedPath
.slice(0, -1)
.filter((folderName) => !!folderName)
.join("/");
const parsedPath = savePath.split("/").filter((folderName) => !!folderName);
const clearPath = parsedPath.slice(0, -1).join("/");
if (!existsSync(clearPath)) {
mkdirSync(clearPath);
logger.debug(`clearPath : ${clearPath}`, { label: "saveInFolder" });
mkdirSync(clearPath, { recursive: true });
}
writeFileSync(savePath, data);
};
Expand Down
79 changes: 40 additions & 39 deletions src/types/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import { OKX_WITHDRAW_CHAINS } from "src/constants/okx";
export type Networks = "btc" | "evm" | "sol" | "tia" | "atom";

export type NetworkWallet = {
address?: string;
pkᵻ: string;
readonly address?: string;
readonly pkᵻ: string;
};

export type EVMWallet = { address: `0x${string}`; pkᵻ: `0x${string}` };
export type EVMWallet = { readonly address: `0x${string}`; readonly pkᵻ: `0x${string}` };

export type Wallet = {
[key in Networks]: NetworkWallet;
readonly [key in Networks]: NetworkWallet;
};

export type Profile = {
[id: string]: {
wallets: Wallet;
mnemonicᵻ: string;
readonly wallets: Wallet;
readonly mnemonicᵻ: string;
};
};

Expand All @@ -26,49 +26,50 @@ export type ReportConf = {
};

export type AppConfig = {
proxy: {
userᵻ: string;
passᵻ: string;
hostᵻ: string;
portᵻ: string;
"reboot-linkᵻ": string;
readonly proxy: {
readonly userᵻ: string;
readonly passᵻ: string;
readonly hostᵻ: string;
readonly portᵻ: string;
readonly "reboot-linkᵻ": string;
};
rpc: {
alchemy: {
keyᵻ: string;
readonly rpc: {
readonly alchemy: {
readonly keyᵻ: string;
};
qnode: {
keyᵻ: string;
readonly qnode: {
readonly keyᵻ: string;
};
};
okx: {
keyᵻ: string;
secretᵻ: string;
passwordᵻ: string;
readonly okx: {
readonly keyᵻ: string;
readonly secretᵻ: string;
readonly passwordᵻ: string;
};
randommer: {
keyᵻ: string;
readonly randommer: {
readonly keyᵻ: string;
};
cli: {
funding: {
chain: keyof typeof OKX_WITHDRAW_CHAINS;
filters: ("noFuel" | "onlyZero" | "lteBalance")[];
depositRange: [number, number];
lteBalance?: number;
maxFee: number;
readonly cli: {
readonly cluster_id: string;
readonly funding: {
readonly chain: keyof typeof OKX_WITHDRAW_CHAINS;
readonly filters: ("noFuel" | "onlyZero" | "lteBalance")[];
readonly depositRange: [number, number];
readonly lteBalance?: number;
readonly maxFee: number;
};
fuel: {
minDeposit: number;
readonly fuel: {
readonly minDeposit: number;
};
scroll: {
kelp: {
minDeposit: number;
readonly scroll: {
readonly kelp: {
readonly minDeposit: number;
};
};
report: ReportConf;
profiles: {
amount: number;
chains: Networks[];
readonly report: ReportConf;
readonly profiles: {
readonly amount: number;
readonly chains: Networks[];
};
};
};

0 comments on commit df32837

Please sign in to comment.