diff --git a/package.json b/package.json index f57bf71..63fabd0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "darchlabs", - "version": "3.6.0", + "version": "3.7.0", "description": "The DarchLabs client is a Node.js library for interacting with the Synchronizers, Nodes and Jobs APIs. It provides an easy-to-use interface for managing data.", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 05366ae..61db5b0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +export * from "./nodes"; export * from "./synchronizers"; export * from "./smartcontracts"; export * from "./util"; diff --git a/src/nodes/index.ts b/src/nodes/index.ts new file mode 100644 index 0000000..b8f8d27 --- /dev/null +++ b/src/nodes/index.ts @@ -0,0 +1,4 @@ +export * from "./nodes"; +export * from "./network"; +export * from "./responses"; +export * from "./types"; diff --git a/src/nodes/network.ts b/src/nodes/network.ts new file mode 100644 index 0000000..069155e --- /dev/null +++ b/src/nodes/network.ts @@ -0,0 +1,25 @@ +import { Subset, type Network } from "../util"; + +export type NetworkEnvironment = + // | ["ethereum", "mainnet"] + // | ["celo", "mainnet"] + | ["celo", "alfajores"] + // | ["chainlink", "mainnet"] + | ["chainlink", "sepolia"]; + +export type NetworkEnvironmentKey = Subset< + Network, + // "celo" | + | "alfajores" + // "chainlink" | + | "chainlink_sepolia" + // "ethereum" | "celo" | "alfajores" | "chainlink" | "chainlink_sepolia" +>; + +export const NetworksEnvironments: { [K in NetworkEnvironmentKey]: NetworkEnvironment } = { + // ethereum: ["ethereum", "mainnet"], + // celo: ["celo", "mainnet"], + alfajores: ["celo", "alfajores"], + // chainlink: ["chainlink", "mainnet"], + chainlink_sepolia: ["chainlink", "sepolia"], +}; diff --git a/src/nodes/nodes.ts b/src/nodes/nodes.ts new file mode 100644 index 0000000..0a3789f --- /dev/null +++ b/src/nodes/nodes.ts @@ -0,0 +1,33 @@ +import axios from "axios"; +import { NodesConfig, type Node, type NodesNetworkEnvironment, NodeInput } from "./types"; +import { DeleteNodeResponse, InsertNodeResponse, ListNodesResponse } from "./responses"; + +export class Nodes { + private baseUrl: string; + + constructor(baseUrl: string) { + this.baseUrl = baseUrl; + } + + public async insertNode( + input: NodeInput + ): Promise> { + const url = `${this.baseUrl}/api/v2/nodes`; + const { data } = await axios.post>(url, input); + return { data }; + } + + public async listNodes(): Promise { + const url = `${this.baseUrl}/api/v2/nodes`; + const { + data: { nodes }, + } = await axios.get<{ nodes: Node[] }>(url); + return { data: nodes } as ListNodesResponse; + } + + public async deleteNodeById(id: string): Promise { + const url = `${this.baseUrl}/api/v2/nodes`; + await axios.delete(url, { data: { id } }); + return {}; + } +} diff --git a/src/nodes/responses.ts b/src/nodes/responses.ts new file mode 100644 index 0000000..18fd549 --- /dev/null +++ b/src/nodes/responses.ts @@ -0,0 +1,11 @@ +import { Node, NodesNetworkEnvironment } from "./types"; + +export type InsertNodeResponse = { + data: Node; +}; + +export type ListNodesResponse = { + data: Node[]; +}; + +export type DeleteNodeResponse = {}; diff --git a/src/nodes/types.ts b/src/nodes/types.ts new file mode 100644 index 0000000..8e069d5 --- /dev/null +++ b/src/nodes/types.ts @@ -0,0 +1,66 @@ +import { Subset, type Network } from "../util"; +import { NetworkEnvironment } from "./network"; + +// define node statuses +export type NodeStatus = "running" | "error"; + +// define [networks,environment] for blockchains +export type NodesEthereumNE = Subset; +export type NodesCeloNE = Subset; +export type NodesChainlinkNE = Subset; +export type NodesNetworkEnvironment = NodesEthereumNE | NodesCeloNE | NodesChainlinkNE; + +// define celo blockchain implementation +export type NodesCeloConfig = { + ENVIRONMENT: NE[1]; + PASSWORD: string; +}; + +// define celo blockchain environment implementation +export type NodesEthereumConfig = { + ENVIRONMENT: NE[1]; + HOST: string; + NETWORK_URL: string; + BASE_CHAIN_DATA_PATH: string; + RPC_PORT: string; + FROM_BLOCK_NUMBER: string; +}; + +// define chainlink blockchain implemtation +export type NodesChainlinkConfig = { + ENVIRONMENT: NE[1]; + ETH_URL: string; + PASSWORD: string; + NODE_EMAIL: string; + NODE_EMAIL_PWD: string; +}; + +// define node config and input type +export type NodesConfig = NE extends NodesCeloNE + ? NodesCeloConfig + : NE extends NodesEthereumNE + ? NodesEthereumConfig + : NE extends NodesChainlinkNE + ? NodesChainlinkConfig + : never; + +export type NodeInput = { + network: NE[0]; + envVars: NodesConfig; +}; + +// define node type used in api responses +export type Node = { + id: string; + name: string; + network: NE[0]; + environment: NE[1]; + port: number; + status: NodeStatus; + artifacts: { + Deployments: string[]; + Pods: string[]; + Services: string[]; + }; + createdAt: string; +}; diff --git a/src/smartcontracts/smartcontracts.ts b/src/smartcontracts/smartcontracts.ts index c082cfa..44ccd0c 100644 --- a/src/smartcontracts/smartcontracts.ts +++ b/src/smartcontracts/smartcontracts.ts @@ -7,7 +7,7 @@ import { RestartSmartContractResponse, } from "./responses"; -import type { SmartContractInput } from "./types"; +import { type SmartContractInput } from "./types"; export class SmartContracts { private baseUrl: string; diff --git a/src/smartcontracts/types.ts b/src/smartcontracts/types.ts index 7502637..6b959e8 100644 --- a/src/smartcontracts/types.ts +++ b/src/smartcontracts/types.ts @@ -1,8 +1,11 @@ import { Event } from "../synchronizers/types"; -import { Abi, Network } from "../util"; +import { Abi, Network, type Subset } from "../util"; export type SmartContractStatus = "idle" | "running" | "stopping" | "synching" | "stopped" | "error" | "quota_exceeded"; +export type SmartContactNetwork = Subset; +export const SmartContractNetwoks: SmartContactNetwork[] = ["ethereum", "polygon"]; + export type SmartContractInput = { name: string; network: Network; diff --git a/src/util/index.ts b/src/util/index.ts index 8e21e7a..6862bf6 100644 --- a/src/util/index.ts +++ b/src/util/index.ts @@ -2,3 +2,4 @@ export * from "./abi"; export * from "./get-chain"; export * from "./network"; export * from "./pagination"; +export * from "./subset"; diff --git a/src/util/network.ts b/src/util/network.ts index ebf257e..8a16803 100644 --- a/src/util/network.ts +++ b/src/util/network.ts @@ -1,53 +1,115 @@ -export type Network = "ethereum" | "rinkeby" | "goerli" | "polygon" | "mumbai"; -export type NetworkType = "evm" | "solana"; +export const Networks = [ + "ethereum", + "rinkeby", + "goerli", + "sepolia", + "polygon", + "mumbai", + "celo", + "alfajores", + "chainlink", + "chainlink_sepolia", +] as const; +export type Network = (typeof Networks)[number]; + +export const NetworkType = ["evm", "solana"] as const; +export type NetworkType = (typeof NetworkType)[number]; export const ChainIds: { [key in Network]: number } = { ethereum: 1, rinkeby: 4, goerli: 5, + sepolia: 11155111, polygon: 137, mumbai: 80001, + celo: 42220, + alfajores: 44787, + chainlink: 1, + chainlink_sepolia: 11155111, }; -export type Token = "ETH" | "MATIC"; +export const Tokens = ["ETH", "MATIC", "CELO", "LINK"] as const; +export type Token = (typeof Tokens)[number]; + +export type Info = { + name: string; + mainnet: boolean; + baseNetwork: Network; + type: NetworkType; + token: Token; +}; -export const Networks: Network[] = ["ethereum", "rinkeby", "goerli", "polygon", "mumbai"]; export const NetworkInfo: { - [key in Network]: { - mainnet: boolean; - baseNetwork: Network; - type: NetworkType; - token: Token; - }; + [key in Network]: Info; } = { ethereum: { + name: "Ethereum", mainnet: true, baseNetwork: "ethereum", type: "evm", token: "ETH", }, rinkeby: { + name: "Ethereum Rinkeby", + mainnet: false, + baseNetwork: "ethereum", + type: "evm", + token: "ETH", + }, + goerli: { + name: "Goerli", + mainnet: false, + baseNetwork: "ethereum", + type: "evm", + token: "ETH", + }, + sepolia: { + name: "Ethereum Sepolia", mainnet: false, baseNetwork: "ethereum", type: "evm", token: "ETH", }, polygon: { + name: "Polygon", mainnet: true, baseNetwork: "polygon", type: "evm", token: "MATIC", }, mumbai: { + name: "Polygon Mumbai", mainnet: false, baseNetwork: "polygon", type: "evm", token: "MATIC", }, - goerli: { + celo: { + name: "Celo", + mainnet: true, + baseNetwork: "celo", + type: "evm", + token: "CELO", + }, + alfajores: { + name: "Celo Alfajores", mainnet: false, - baseNetwork: "ethereum", + baseNetwork: "celo", type: "evm", - token: "ETH", + token: "CELO", + }, + chainlink: { + name: "Chainlink", + mainnet: true, + baseNetwork: "chainlink", + type: "evm", + token: "LINK", + }, + chainlink_sepolia: { + name: "Chainlink Sepolia", + mainnet: false, + baseNetwork: "chainlink", + type: "evm", + token: "LINK", }, }; diff --git a/src/util/subset.ts b/src/util/subset.ts new file mode 100644 index 0000000..784c95d --- /dev/null +++ b/src/util/subset.ts @@ -0,0 +1,3 @@ +export type Subset = T extends U ? T : never; + +// export type NodesNetwork = Subset;