Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS SDK] Support network and API endpoints on AptosConfig #9549

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
}
Comment on lines +21 to +27
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this include a toString() and a fromString() kind of stuff?

I found myself having to do `toString() to compare sometimes to inputs from the wallet adapter wallets.

Copy link
Contributor Author

@0xmaayan 0xmaayan Aug 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not completely sure what you mean.. you can do Network.DEVNET === "devnet" that would output true.

The toString() method returns a string representing the object

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, now I remember, it's because one side was "Devnet" and the other was "Network.DEVNET", so had to lowercase one of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh yes, wallet standard and co.

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");
});
});