diff --git a/ecosystem/typescript/sdk_v2/package.json b/ecosystem/typescript/sdk_v2/package.json index 974a0be0871518..59b9a1144066c1 100644 --- a/ecosystem/typescript/sdk_v2/package.json +++ b/ecosystem/typescript/sdk_v2/package.json @@ -25,7 +25,8 @@ "_build:cjs": "tsup src/index.ts --format cjs --dts --out-dir dist/cjs", "_build:types": "tsup src/types/index.ts --dts --out-dir dist/types", "generate-openapi-response-types": "openapi -i ../../../../api/doc/spec.yaml -o ./src/types/generated --exportCore=false --exportServices=false", - "lint": "eslint \"**/*.ts\"" + "lint": "eslint \"**/*.ts\"", + "test": "pnpm jest" }, "dependencies": { "@aptos-labs/aptos-client": "^0.0.2", diff --git a/ecosystem/typescript/sdk_v2/src/api/aptos_config.ts b/ecosystem/typescript/sdk_v2/src/api/aptos_config.ts index cd2c26a9e857c2..2bfd56fd929b55 100644 --- a/ecosystem/typescript/sdk_v2/src/api/aptos_config.ts +++ b/ecosystem/typescript/sdk_v2/src/api/aptos_config.ts @@ -1,9 +1,23 @@ import { ClientConfig } from "../client/types"; +import { NetworkToNodeAPI, NetworkToFaucetAPI, NetworkToIndexerAPI, Network } from "../utils/api-endpoints"; +import { DEFAULT_NETWORK } from "../utils/const"; export class AptosConfig { + readonly network?: Network; + + readonly fullnode?: string; + + readonly faucet?: string; + + readonly indexer?: string; + readonly clientConfig?: ClientConfig; constructor(config?: AptosConfig) { + this.network = config?.network ?? DEFAULT_NETWORK; + this.fullnode = config?.fullnode ?? NetworkToNodeAPI[this.network]; + this.faucet = config?.faucet ?? NetworkToFaucetAPI[this.network]; + this.indexer = config?.indexer ?? NetworkToIndexerAPI[this.network]; this.clientConfig = config?.clientConfig ?? {}; } } diff --git a/ecosystem/typescript/sdk_v2/src/utils/api-endpoints.ts b/ecosystem/typescript/sdk_v2/src/utils/api-endpoints.ts new file mode 100644 index 00000000000000..80f13c29edff40 --- /dev/null +++ b/ecosystem/typescript/sdk_v2/src/utils/api-endpoints.ts @@ -0,0 +1,27 @@ +export const NetworkToIndexerAPI: Record = { + mainnet: "https://indexer.mainnet.aptoslabs.com/v1/graphql", + testnet: "https://indexer-testnet.staging.gcp.aptosdev.com/v1/graphql", + devnet: "https://indexer-devnet.staging.gcp.aptosdev.com/v1/graphql", +}; + +export const NetworkToNodeAPI: Record = { + mainnet: "https://fullnode.mainnet.aptoslabs.com/v1", + testnet: "https://fullnode.testnet.aptoslabs.com/v1", + devnet: "https://fullnode.devnet.aptoslabs.com/v1", + local: "http://localhost:8080/v1", +}; + +export const NetworkToFaucetAPI: Record = { + mainnet: "https://faucet.mainnet.aptoslabs.com", + testnet: "https://faucet.testnet.aptoslabs.com", + devnet: "https://faucet.devnet.aptoslabs.com", + local: "http://localhost:8081", +}; + +export enum Network { + MAINNET = "mainnet", + TESTNET = "testnet", + DEVNET = "devnet", + LOCAL = "local", + CUSTOM = "custom", +} diff --git a/ecosystem/typescript/sdk_v2/src/utils/const.ts b/ecosystem/typescript/sdk_v2/src/utils/const.ts new file mode 100644 index 00000000000000..7fc138d12cd7b1 --- /dev/null +++ b/ecosystem/typescript/sdk_v2/src/utils/const.ts @@ -0,0 +1,3 @@ +import { Network } from "./api-endpoints"; + +export const DEFAULT_NETWORK = Network.TESTNET; diff --git a/ecosystem/typescript/sdk_v2/tests/unit/aptos_config.test.ts b/ecosystem/typescript/sdk_v2/tests/unit/aptos_config.test.ts new file mode 100644 index 00000000000000..97c17eb93ae3e6 --- /dev/null +++ b/ecosystem/typescript/sdk_v2/tests/unit/aptos_config.test.ts @@ -0,0 +1,59 @@ +import { Aptos, AptosConfig } from "../../src"; +import { Network } from "../../src/utils/api-endpoints"; + +describe("aptos config", () => { + test("it should set TESTNET network if network is not provided", async () => { + const aptos = new Aptos(); + expect(aptos.config.network).toEqual("testnet"); + expect(aptos.config.fullnode).toEqual("https://fullnode.testnet.aptoslabs.com/v1"); + expect(aptos.config.faucet).toEqual("https://faucet.testnet.aptoslabs.com"); + expect(aptos.config.indexer).toEqual("https://indexer-testnet.staging.gcp.aptosdev.com/v1/graphql"); + }); + + test("it should set urls based on the provided network", async () => { + const settings: AptosConfig = { + network: Network.DEVNET, + }; + const aptos = new Aptos(settings); + expect(aptos.config.network).toEqual("devnet"); + expect(aptos.config.fullnode).toEqual("https://fullnode.devnet.aptoslabs.com/v1"); + expect(aptos.config.faucet).toEqual("https://faucet.devnet.aptoslabs.com"); + expect(aptos.config.indexer).toEqual("https://indexer-devnet.staging.gcp.aptosdev.com/v1/graphql"); + }); + + test("it should set urls based on a local network", async () => { + const settings: AptosConfig = { + network: Network.LOCAL, + }; + const aptos = new Aptos(settings); + expect(aptos.config.network).toEqual("local"); + expect(aptos.config.fullnode).toEqual("http://localhost:8080/v1"); + expect(aptos.config.faucet).toEqual("http://localhost:8081"); + expect(aptos.config.indexer).toBeUndefined(); + }); + + test("it should have undefined urls when network is custom and no urls provided", async () => { + const settings: AptosConfig = { + network: Network.CUSTOM, + }; + const aptos = new Aptos(settings); + expect(aptos.config.network).toEqual("custom"); + expect(aptos.config.fullnode).toBeUndefined(); + expect(aptos.config.faucet).toBeUndefined(); + expect(aptos.config.indexer).toBeUndefined(); + }); + + test("it should set urls when network is custom and urls provided", async () => { + const settings: AptosConfig = { + network: Network.CUSTOM, + fullnode: "my-fullnode-url", + faucet: "my-faucet-url", + indexer: "my-indexer-url", + }; + const aptos = new Aptos(settings); + expect(aptos.config.network).toEqual("custom"); + expect(aptos.config.fullnode).toEqual("my-fullnode-url"); + expect(aptos.config.faucet).toEqual("my-faucet-url"); + expect(aptos.config.indexer).toEqual("my-indexer-url"); + }); +});