-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.DEVNET; |
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 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"); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 afromString()
kind of stuff?I found myself having to do `toString() to compare sometimes to inputs from the wallet adapter wallets.
There was a problem hiding this comment.
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 outputtrue
.There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.