@mapprotocol/common-contracts 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,81 +0,0 @@
1
- let fs = require("fs");
2
- let path = require("path");
3
-
4
- type Deployment = {
5
- [network: string]: {
6
- [key: string]: any;
7
- };
8
- };
9
-
10
- export interface DeploymentOptions {
11
- basePath?: string; // defaults to <cwd>/deployments/
12
- suffix?: string; // defaults to "prod"
13
- }
14
-
15
- // Default deployments path: <project_root>/deployments/
16
- function defaultDeployPath(): string {
17
- return path.join(process.cwd(), "deployments");
18
- }
19
-
20
- // Resolve deployment environment key from network name
21
- // e.g., "Bsc" + "prod" -> "Bsc_prod", "Bsc_test" -> "Bsc_test"
22
- export function resolveDeploymentEnv(network: string, suffix: string = "prod"): string {
23
- if (network.includes("test")) return network;
24
- return `${network}_${suffix}`;
25
- }
26
-
27
- export async function getDeploymentByKey(network: string, key: string, opts?: DeploymentOptions): Promise<string> {
28
- const deployPath = opts?.basePath || defaultDeployPath();
29
- const env = resolveDeploymentEnv(network, opts?.suffix);
30
- const deployment = await readDeploymentFromFile(deployPath, env);
31
- const addr = deployment[env]?.[key];
32
- if (!addr) throw `no ${key} deployment in ${env}`;
33
- return addr;
34
- }
35
-
36
- export async function hasDeployment(network: string, key: string, opts?: DeploymentOptions): Promise<boolean> {
37
- try {
38
- const deployPath = opts?.basePath || defaultDeployPath();
39
- const env = resolveDeploymentEnv(network, opts?.suffix);
40
- const deployment = await readDeploymentFromFile(deployPath, env);
41
- const addr = deployment[env]?.[key];
42
- return !!addr && addr.length > 2 && addr !== "0x";
43
- } catch {
44
- return false;
45
- }
46
- }
47
-
48
- export async function saveDeployment(network: string, key: string, addr: string, opts?: DeploymentOptions): Promise<void> {
49
- const deployPath = opts?.basePath || defaultDeployPath();
50
- const env = resolveDeploymentEnv(network, opts?.suffix);
51
- const deployment = await readDeploymentFromFile(deployPath, env);
52
- deployment[env][key] = addr;
53
- const p = path.resolve(deployPath, "deploy.json");
54
- await ensureDir(deployPath);
55
- fs.writeFileSync(p, JSON.stringify(deployment, null, "\t"));
56
- }
57
-
58
- async function readDeploymentFromFile(basePath: string, env: string): Promise<Deployment> {
59
- const p = path.resolve(basePath, "deploy.json");
60
- let deploy: Deployment;
61
- if (!fs.existsSync(p)) {
62
- deploy = {};
63
- deploy[env] = {};
64
- } else {
65
- const rawdata = fs.readFileSync(p, "utf-8");
66
- deploy = JSON.parse(rawdata);
67
- if (!deploy[env]) {
68
- deploy[env] = {};
69
- }
70
- }
71
- return deploy;
72
- }
73
-
74
- async function ensureDir(dirPath: string): Promise<void> {
75
- const absPath = path.resolve(dirPath);
76
- try {
77
- await fs.promises.stat(absPath);
78
- } catch (e) {
79
- await fs.promises.mkdir(absPath, { recursive: true });
80
- }
81
- }