-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support network and api urls on AptosConfig
- Loading branch information
Showing
5 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ?? {}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Network } from "./api-endpoints"; | ||
|
||
export const DEFAULT_NETWORK = Network.TESTNET; |
59 changes: 59 additions & 0 deletions
59
ecosystem/typescript/sdk_v2/tests/unit/aptos_config.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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"); | ||
}); | ||
}); |