Skip to content

Commit

Permalink
[TS SDK] Support network and API endpoints on AptosConfig (#9549)
Browse files Browse the repository at this point in the history
* support network and api urls on AptosConfig

* devnet as default network
  • Loading branch information
0xmaayan authored and msmouse committed Aug 9, 2023
1 parent e1cb17f commit 2ec41b1
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ecosystem/typescript/sdk_v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions ecosystem/typescript/sdk_v2/src/api/aptos_config.ts
Original file line number Diff line number Diff line change
@@ -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 ?? {};
}
}
27 changes: 27 additions & 0 deletions ecosystem/typescript/sdk_v2/src/utils/api-endpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export const NetworkToIndexerAPI: Record<string, string> = {
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<string, string> = {
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<string, string> = {
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",
}
3 changes: 3 additions & 0 deletions ecosystem/typescript/sdk_v2/src/utils/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Network } from "./api-endpoints";

export const DEFAULT_NETWORK = Network.DEVNET;
59 changes: 59 additions & 0 deletions ecosystem/typescript/sdk_v2/tests/unit/aptos_config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Aptos, AptosConfig } from "../../src";
import { Network } from "../../src/utils/api-endpoints";

describe("aptos config", () => {
test("it should set DEVNET network if network is not provided", async () => {
const aptos = new Aptos();
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 the provided network", async () => {
const settings: AptosConfig = {
network: Network.TESTNET,
};
const aptos = new Aptos(settings);
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 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");
});
});

0 comments on commit 2ec41b1

Please sign in to comment.