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

Add cluster API #65

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions .changeset/yellow-sloths-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@solana/wallet-standard-chains': minor
'@solana/wallet-standard-features': minor
---

Add `getCluster` and `setCluster` features
33 changes: 2 additions & 31 deletions packages/core/chains/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,2 @@
import type { IdentifierString } from '@wallet-standard/base';

/** Solana Mainnet (beta) cluster, e.g. https://api.mainnet-beta.solana.com */
export const SOLANA_MAINNET_CHAIN = 'solana:mainnet';

/** Solana Devnet cluster, e.g. https://api.devnet.solana.com */
export const SOLANA_DEVNET_CHAIN = 'solana:devnet';

/** Solana Testnet cluster, e.g. https://api.testnet.solana.com */
export const SOLANA_TESTNET_CHAIN = 'solana:testnet';

/** Solana Localnet cluster, e.g. http://localhost:8899 */
export const SOLANA_LOCALNET_CHAIN = 'solana:localnet';

/** Array of all Solana clusters */
export const SOLANA_CHAINS = [
SOLANA_MAINNET_CHAIN,
SOLANA_DEVNET_CHAIN,
SOLANA_TESTNET_CHAIN,
SOLANA_LOCALNET_CHAIN,
] as const;

/** Type of all Solana clusters */
export type SolanaChain = (typeof SOLANA_CHAINS)[number];

/**
* Check if a chain corresponds with one of the Solana clusters.
*/
export function isSolanaChain(chain: IdentifierString): chain is SolanaChain {
return SOLANA_CHAINS.includes(chain as SolanaChain);
}
export * from './solana.js';
export * from './sonic.js';
31 changes: 31 additions & 0 deletions packages/core/chains/src/solana.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { IdentifierString } from '@wallet-standard/base';

/** Solana Mainnet (beta) cluster, e.g. https://api.mainnet-beta.solana.com */
export const SOLANA_MAINNET_CHAIN = 'solana:mainnet';

/** Solana Devnet cluster, e.g. https://api.devnet.solana.com */
export const SOLANA_DEVNET_CHAIN = 'solana:devnet';

/** Solana Testnet cluster, e.g. https://api.testnet.solana.com */
export const SOLANA_TESTNET_CHAIN = 'solana:testnet';

/** Solana Localnet cluster, e.g. http://localhost:8899 */
export const SOLANA_LOCALNET_CHAIN = 'solana:localnet';

/** Array of all Solana clusters */
export const SOLANA_CHAINS = [
SOLANA_MAINNET_CHAIN,
SOLANA_DEVNET_CHAIN,
SOLANA_TESTNET_CHAIN,
SOLANA_LOCALNET_CHAIN,
] as const;

/** Type of all Solana clusters */
export type SolanaChain = (typeof SOLANA_CHAINS)[number];

/**
* Check if a chain corresponds with one of the Solana clusters.
*/
export function isSolanaChain(chain: IdentifierString): chain is SolanaChain {
return SOLANA_CHAINS.includes(chain as SolanaChain);
}
31 changes: 31 additions & 0 deletions packages/core/chains/src/sonic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { IdentifierString } from '@wallet-standard/base';

/** Sonic Mainnet (beta) cluster, e.g. https://api.mainnet-beta.sonic.com */
export const SONIC_MAINNET_CHAIN = 'sonic:mainnet';

/** Sonic Devnet cluster, e.g. https://api.devnet.sonic.com */
export const SONIC_DEVNET_CHAIN = 'sonic:devnet';

/** Sonic Testnet cluster, e.g. https://api.testnet.sonic.com */
export const SONIC_TESTNET_CHAIN = 'sonic:testnet';

/** Sonic Localnet cluster, e.g. http://localhost:8899 */
export const SONIC_LOCALNET_CHAIN = 'sonic:localnet';

/** Array of all Sonic clusters */
export const SONIC_CHAINS = [
SONIC_MAINNET_CHAIN,
SONIC_DEVNET_CHAIN,
SONIC_TESTNET_CHAIN,
SONIC_LOCALNET_CHAIN,
] as const;

/** Type of all Sonic clusters */
export type SonicChain = (typeof SONIC_CHAINS)[number];

/**
* Check if a chain corresponds with one of the Sonic clusters.
*/
export function isSonicChain(chain: IdentifierString): chain is SonicChain {
return SONIC_CHAINS.includes(chain as SonicChain);
}
Comment on lines +1 to +31
Copy link

@kespinola kespinola Jun 21, 2024

Choose a reason for hiding this comment

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

The data should be stored in a json file IMO.

packages/core/data/chains.json

identifier: {network}:{environment}:{endpoint}

[
  {
    "name": "sonic",
    "environments": [
      {
        "name": "devnet",
        "proofOfHistorySha": "abac...cerr3",
        "endpoints": [
          {
            "name": "sonic foundation",
            "url": "https://api.devnet.sonic.com"
          }
        ]
      }
    ]
  }
]

Copy link
Author

Choose a reason for hiding this comment

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

I am fine with that solution.

Choose a reason for hiding this comment

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

Should maybe be flatter where network is combined with environment but I can imagine wallets wanting to let users drill down to an rpc endpoint.

35 changes: 35 additions & 0 deletions packages/core/features/src/getCluster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { IdentifierString } from '@wallet-standard/base';

/** Name of the feature. */
export const SolanaGetCluster = 'solana:getCluster';

/** Represents the feature for getting the current Solana cluster name. */
export type SolanaGetClusterFeature = {
/** Name of the feature. */
readonly [SolanaGetCluster]: {
/** Version of the feature API. */
readonly version: SolanaGetClusterVersion;

/**
* Method to get the current Solana cluster name.
* This method interacts with wallet to get currently enabled Solana cluster.
*
* @returns {Promise<readonly SolanaGetClusterOutput>} A promise that resolves to Solana cluster name.
*/
readonly getCluster: SolanaGetClusterMethod;
};
};

/** Version of the feature API. */
export type SolanaGetClusterVersion = '1.0.0';

/**
* Method to get the current Solana cluster name.
* This method returns a promise that resolves to Solana cluster name.
*
* @returns {Promise<readonly SolanaGetClusterOutput>} A promise that resolves to Solana cluster name.
*/
export type SolanaGetClusterMethod = () => Promise<SolanaGetClusterOutput>;

/** Output type representing the Solana cluster information. */
export type SolanaGetClusterOutput = IdentifierString;
8 changes: 7 additions & 1 deletion packages/core/features/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import type { SolanaSignAndSendTransactionFeature } from './signAndSendTransacti
import type { SolanaSignInFeature } from './signIn.js';
import type { SolanaSignMessageFeature } from './signMessage.js';
import type { SolanaSignTransactionFeature } from './signTransaction.js';
import type { SolanaGetClusterFeature } from './getCluster.js';
import type { SolanaSetClusterFeature } from './setCluster.js';

/** TODO: docs */
export type SolanaFeatures =
| SolanaSignAndSendTransactionFeature
| SolanaSignInFeature
| SolanaSignMessageFeature
| SolanaSignTransactionFeature;
| SolanaSignTransactionFeature
| SolanaGetClusterFeature
| SolanaSetClusterFeature;

/** TODO: docs */
export type WalletWithSolanaFeatures = WalletWithFeatures<SolanaFeatures>;
Expand All @@ -18,3 +22,5 @@ export * from './signAndSendTransaction.js';
export * from './signIn.js';
export * from './signMessage.js';
export * from './signTransaction.js';
export * from './getCluster.js';
export * from './setCluster.js';
37 changes: 37 additions & 0 deletions packages/core/features/src/setCluster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { IdentifierString } from '@wallet-standard/base';

/** Name of the setCluster feature. */
export const SolanaSetCluster = 'solana:setCluster';

/** Represents the feature for setting the current Solana cluster. */
export type SolanaSetClusterFeature = {
/** Name of the setCluster feature. */
readonly [SolanaSetCluster]: {
/** Version of the setCluster feature API. */
readonly version: SolanaSetClusterVersion;

/**
* Method to set the current Solana cluster.
* This method interacts with the wallet to set the currently enabled Solana cluster.
*
* @param clusterName - The name of the Solana cluster to set.
* @returns {Promise<void>} A promise that resolves when the cluster has been set.
*/
readonly setCluster: SolanaSetClusterMethod;
};
};

/** Version of the setCluster feature API. */
export type SolanaSetClusterVersion = '1.0.0';

/**
* Method to set the current Solana cluster.
* This method returns a promise that resolves when the cluster has been set.
*
* @param clusterName - The name of the Solana cluster to set.
* @returns {Promise<void>} A promise that resolves when the cluster has been set.
*/
export type SolanaSetClusterMethod = (clusterName: SolanaClusterInput) => Promise<void>;

/** Input type representing the Solana cluster to be set. */
export type SolanaClusterInput = IdentifierString;