ryt-sdk 1.0.9 → 1.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,38 +1,81 @@
1
+ import { getRYT, hasRYT, requestAccounts } from "./utils/ryt";
2
+ export { parseTokenUnits, formatTokenUnits } from "./utils/units";
1
3
  /**
2
4
  * ------------------------------------------------------
3
5
  * RYT SDK - Public Entry Point
4
6
  * ------------------------------------------------------
5
7
  *
6
- * This file exposes the main SDK modules:
7
- * - Provider (RPC layer)
8
- * - Wallet (signing layer)
9
- * - Contract (smart contract interaction layer)
8
+ * Exposes core SDK modules for:
9
+ * - Blockchain interaction (Provider)
10
+ * - Wallet management (Wallet)
11
+ * - Smart contracts (Contract layer)
10
12
  *
11
- * This is the ONLY file users should import from:
12
- *
13
- * @example
14
- * ```ts
15
- * import { RYTProvider, RYTWallet, RYTContract } from "ryt-sdk";
16
- * ```
13
+ * Works in:
14
+ * - Browser (MetaMask / injected wallets)
15
+ * - Node.js (RPC-based flows)
17
16
  */
18
17
  /**
19
- * Provider module for RPC interactions
18
+ * ------------------------------------------------------
19
+ * Core Modules
20
+ * ------------------------------------------------------
20
21
  */
21
22
  export { default as RYTProvider } from "./provider";
23
+ export { default as RYTWallet } from "./wallet";
24
+ export { default as RYTContract } from "./contract";
22
25
  /**
23
- * Wallet module for signing transactions
26
+ * ------------------------------------------------------
27
+ * Browser / Wallet Utilities
28
+ * ------------------------------------------------------
24
29
  */
25
- export { default as RYTWallet } from "./wallet";
26
30
  /**
27
- * Contract module for smart contract interactions
31
+ * Gets injected wallet provider (MetaMask, Rabby, etc.)
32
+ *
33
+ * @returns EIP-1193 provider or undefined
34
+ *
35
+ * @example
36
+ * const provider = getRYT();
28
37
  */
29
- export { default as RYTContract } from "./contract";
38
+ export { getRYT };
39
+ /**
40
+ * Checks if injected wallet is available in browser.
41
+ *
42
+ * @returns true if wallet is detected
43
+ *
44
+ * @example
45
+ * if (hasRYT()) console.log("Wallet ready");
46
+ */
47
+ export { hasRYT };
48
+ /**
49
+ * Requests wallet connection (opens MetaMask popup).
50
+ *
51
+ * @returns list of connected addresses
52
+ *
53
+ * @example
54
+ * const accounts = await requestAccounts();
55
+ */
56
+ export { requestAccounts };
30
57
  /**
31
58
  * ------------------------------------------------------
32
- * Optional: Future unified SDK export (recommended)
59
+ * Convenience helpers (SDK-level safe checks)
33
60
  * ------------------------------------------------------
61
+ */
62
+ /**
63
+ * Checks if running in browser environment.
34
64
  *
35
- * Uncomment when you want a single entry API:
65
+ * @returns true if window is available
66
+ */
67
+ export const isBrowser = () => {
68
+ return typeof globalThis !== "undefined" && typeof globalThis.window !== "undefined";
69
+ };
70
+ /**
71
+ * Checks if wallet is ready for use.
72
+ *
73
+ * Combines:
74
+ * - browser check
75
+ * - injected wallet check
36
76
  *
37
- * export { default as RYTClient } from "./client";
38
- */
77
+ * @returns true if wallet can be used
78
+ */
79
+ export const isWalletAvailable = () => {
80
+ return isBrowser() && hasRYT();
81
+ };
@@ -1,49 +1,82 @@
1
1
  import { ethers } from "ethers";
2
2
  import type { SDKConfig } from "./config";
3
3
  /**
4
- * RYTProvider is a provider instance that connects to the blockchain and allows interaction with it.
4
+ * Unified blockchain provider instance used across RYT SDK.
5
+ *
6
+ * Provides a single interface for interacting with:
7
+ * - Browser wallet providers (if available)
8
+ * - RPC-based blockchain nodes (fallback)
9
+ *
10
+ * Automatically selects the most appropriate transport layer
11
+ * based on runtime environment and configuration.
5
12
  */
6
13
  export default class RYTProvider {
7
14
  private provider;
8
15
  /**
9
- * Create a new RYTProvider instance
16
+ * Creates a new provider instance.
10
17
  *
11
- * @param config - SDK configuration containing RPC URLs
12
- * @param rpcUrl - Optional override RPC URL. If not provided, uses first config RPC URL
18
+ * Selection priority:
19
+ * 1. Injected wallet provider (if enabled and available)
20
+ * 2. RPC provider from config or override
13
21
  *
14
- * @example
15
- * ```ts
16
- * const provider = new RYTProvider(config);
17
- * ```
22
+ * @param config - SDK configuration containing chain and provider settings
23
+ * @param rpcUrl - Optional override RPC endpoint (takes priority over config)
24
+ *
25
+ * @throws Error if no valid provider source is available
18
26
  */
19
27
  constructor(config: SDKConfig, rpcUrl?: string);
20
28
  /**
21
- * Returns the provider instance
29
+ * Returns the underlying provider instance.
30
+ *
31
+ * This may be either:
32
+ * - Browser-based provider (wallet-connected)
33
+ * - RPC-based provider (node connection)
22
34
  *
23
35
  * @returns provider instance
24
36
  */
25
- getProvider(): ethers.JsonRpcProvider;
37
+ getProvider(): ethers.BrowserProvider | ethers.JsonRpcProvider;
38
+ /**
39
+ * Returns a signer for transaction signing.
40
+ *
41
+ * Available only when using a browser wallet provider.
42
+ *
43
+ * @throws Error if provider is not a browser wallet instance
44
+ * @returns Signer instance for transaction signing
45
+ */
46
+ getSigner(): Promise<ethers.Signer>;
26
47
  /**
27
- * Get the latest block number from the blockchain
48
+ * Retrieves the connected wallet address.
28
49
  *
29
- * @returns Promise resolving to latest block number
50
+ * Requires an active browser wallet connection.
30
51
  *
31
- * @example
32
- * ```ts
33
- * const blockNumber = await provider.getBlockNumber();
34
- * ```
52
+ * @returns Wallet address string
53
+ */
54
+ getAddress(): Promise<string>;
55
+ /**
56
+ * Retrieves the current network information.
57
+ *
58
+ * @returns Network metadata including chainId and name
59
+ */
60
+ getNetwork(): Promise<ethers.Network>;
61
+ /**
62
+ * Retrieves the latest block number from the blockchain.
63
+ *
64
+ * @returns Latest block number
35
65
  */
36
66
  getBlockNumber(): Promise<number>;
37
67
  /**
38
- * Get balance of a given address in wei
68
+ * Retrieves the balance of an address.
39
69
  *
40
- * @param address - Ethereum-style wallet address
41
- * @returns Promise resolving to balance in wei (BigInt-compatible)
70
+ * @param address - Valid Ethereum-compatible address
71
+ * @returns Balance in wei
42
72
  *
43
- * @example
44
- * ```ts
45
- * const balance = await provider.getBalance("0x123...");
46
- * ```
73
+ * @throws Error if address is invalid
47
74
  */
48
75
  getBalance(address: string): Promise<bigint>;
76
+ /**
77
+ * Checks whether the provider is using a browser wallet.
78
+ *
79
+ * @returns true if injected wallet is active, false if RPC is used
80
+ */
81
+ isInjected(): boolean;
49
82
  }
package/dist/provider.js CHANGED
@@ -1,28 +1,46 @@
1
1
  import { ethers } from "ethers";
2
+ import { getRYT } from "./utils/ryt";
2
3
  /**
3
- * RYTProvider is a provider instance that connects to the blockchain and allows interaction with it.
4
+ * Unified blockchain provider instance used across RYT SDK.
5
+ *
6
+ * Provides a single interface for interacting with:
7
+ * - Browser wallet providers (if available)
8
+ * - RPC-based blockchain nodes (fallback)
9
+ *
10
+ * Automatically selects the most appropriate transport layer
11
+ * based on runtime environment and configuration.
4
12
  */
5
13
  export default class RYTProvider {
6
14
  /**
7
- * Create a new RYTProvider instance
15
+ * Creates a new provider instance.
8
16
  *
9
- * @param config - SDK configuration containing RPC URLs
10
- * @param rpcUrl - Optional override RPC URL. If not provided, uses first config RPC URL
17
+ * Selection priority:
18
+ * 1. Injected wallet provider (if enabled and available)
19
+ * 2. RPC provider from config or override
11
20
  *
12
- * @example
13
- * ```ts
14
- * const provider = new RYTProvider(config);
15
- * ```
21
+ * @param config - SDK configuration containing chain and provider settings
22
+ * @param rpcUrl - Optional override RPC endpoint (takes priority over config)
23
+ *
24
+ * @throws Error if no valid provider source is available
16
25
  */
17
26
  constructor(config, rpcUrl) {
18
- const rpc = rpcUrl ?? config.rpcUrls[0];
27
+ const rpc = rpcUrl ?? config.rpcUrls?.[0];
28
+ const injected = getRYT();
29
+ if (injected && config.enableBrowserWallet !== false) {
30
+ this.provider = new ethers.BrowserProvider(injected);
31
+ return;
32
+ }
19
33
  if (!rpc) {
20
- throw new Error("No RPC URL provided in config or constructor");
34
+ throw new Error("No provider available (no RPC or wallet)");
21
35
  }
22
36
  this.provider = new ethers.JsonRpcProvider(rpc);
23
37
  }
24
38
  /**
25
- * Returns the provider instance
39
+ * Returns the underlying provider instance.
40
+ *
41
+ * This may be either:
42
+ * - Browser-based provider (wallet-connected)
43
+ * - RPC-based provider (node connection)
26
44
  *
27
45
  * @returns provider instance
28
46
  */
@@ -30,33 +48,66 @@ export default class RYTProvider {
30
48
  return this.provider;
31
49
  }
32
50
  /**
33
- * Get the latest block number from the blockchain
51
+ * Returns a signer for transaction signing.
34
52
  *
35
- * @returns Promise resolving to latest block number
53
+ * Available only when using a browser wallet provider.
36
54
  *
37
- * @example
38
- * ```ts
39
- * const blockNumber = await provider.getBlockNumber();
40
- * ```
55
+ * @throws Error if provider is not a browser wallet instance
56
+ * @returns Signer instance for transaction signing
57
+ */
58
+ async getSigner() {
59
+ if (!(this.provider instanceof ethers.BrowserProvider)) {
60
+ throw new Error("Signer not available in RPC mode");
61
+ }
62
+ return await this.provider.getSigner();
63
+ }
64
+ /**
65
+ * Retrieves the connected wallet address.
66
+ *
67
+ * Requires an active browser wallet connection.
68
+ *
69
+ * @returns Wallet address string
70
+ */
71
+ async getAddress() {
72
+ const signer = await this.getSigner();
73
+ return await signer.getAddress();
74
+ }
75
+ /**
76
+ * Retrieves the current network information.
77
+ *
78
+ * @returns Network metadata including chainId and name
79
+ */
80
+ async getNetwork() {
81
+ return await this.provider.getNetwork();
82
+ }
83
+ /**
84
+ * Retrieves the latest block number from the blockchain.
85
+ *
86
+ * @returns Latest block number
41
87
  */
42
88
  async getBlockNumber() {
43
89
  return await this.provider.getBlockNumber();
44
90
  }
45
91
  /**
46
- * Get balance of a given address in wei
92
+ * Retrieves the balance of an address.
47
93
  *
48
- * @param address - Ethereum-style wallet address
49
- * @returns Promise resolving to balance in wei (BigInt-compatible)
94
+ * @param address - Valid Ethereum-compatible address
95
+ * @returns Balance in wei
50
96
  *
51
- * @example
52
- * ```ts
53
- * const balance = await provider.getBalance("0x123...");
54
- * ```
97
+ * @throws Error if address is invalid
55
98
  */
56
99
  async getBalance(address) {
57
100
  if (!ethers.isAddress(address)) {
58
- throw new Error(`Invalid address: ${address}`);
101
+ throw new Error("Invalid address");
59
102
  }
60
103
  return await this.provider.getBalance(address);
61
104
  }
105
+ /**
106
+ * Checks whether the provider is using a browser wallet.
107
+ *
108
+ * @returns true if injected wallet is active, false if RPC is used
109
+ */
110
+ isInjected() {
111
+ return this.provider instanceof ethers.BrowserProvider;
112
+ }
62
113
  }
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Minimal EIP-1193 provider interface.
3
+ *
4
+ * Represents a browser-injected wallet provider such as:
5
+ * - MetaMask
6
+ * - Rabby
7
+ * - Coinbase Wallet
8
+ *
9
+ * This interface standardizes communication with injected wallets
10
+ * using the EIP-1193 request/response pattern.
11
+ */
12
+ export interface RYTProvider {
13
+ request: (args: {
14
+ method: string;
15
+ params?: any[];
16
+ }) => Promise<any>;
17
+ on?: (event: string, handler: (...args: any[]) => void) => void;
18
+ removeListener?: (event: string, handler: (...args: any[]) => void) => void;
19
+ }
20
+ /**
21
+ * Safely retrieves the injected provider from the browser.
22
+ *
23
+ * This function:
24
+ * - Works in browser environments only
25
+ * - Is SSR-safe (Next.js / Vite / Node.js compatible)
26
+ * - Returns undefined if no wallet is installed
27
+ *
28
+ * @returns provider instance if available, otherwise undefined
29
+ *
30
+ * @example
31
+ * const ryt = getRYT();
32
+ * if (ryt) {
33
+ * console.log("Wallet is available");
34
+ * }
35
+ */
36
+ export declare function getRYT(): RYTProvider | undefined;
37
+ /**
38
+ * Checks whether an injected wallet is available.
39
+ *
40
+ * This is a convenience helper around `getRYT()`.
41
+ * Useful for conditionally enabling wallet-based features.
42
+ *
43
+ * @returns true if a wallet (MetaMask / similar) is detected, otherwise false
44
+ *
45
+ * @example
46
+ * if (!hasRYT()) {
47
+ * alert("Please install MetaMask");
48
+ * }
49
+ */
50
+ export declare function hasRYT(): boolean;
51
+ /**
52
+ * Requests account access from the user's wallet.
53
+ *
54
+ * This triggers the wallet popup (e.g. MetaMask approval dialog)
55
+ * using the EIP-1193 `eth_requestAccounts` method.
56
+ *
57
+ * Requirements:
58
+ * - Must be called in a browser environment
59
+ * - Requires an injected wallet (MetaMask, etc.)
60
+ *
61
+ * @returns Promise resolving to an array of connected wallet addresses
62
+ *
63
+ * @throws Error if no injected wallet is available
64
+ *
65
+ * @example
66
+ * const accounts = await requestAccounts();
67
+ * console.log(accounts[0]);
68
+ */
69
+ export declare function requestAccounts(): Promise<string[]>;
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Safely retrieves the injected provider from the browser.
3
+ *
4
+ * This function:
5
+ * - Works in browser environments only
6
+ * - Is SSR-safe (Next.js / Vite / Node.js compatible)
7
+ * - Returns undefined if no wallet is installed
8
+ *
9
+ * @returns provider instance if available, otherwise undefined
10
+ *
11
+ * @example
12
+ * const ryt = getRYT();
13
+ * if (ryt) {
14
+ * console.log("Wallet is available");
15
+ * }
16
+ */
17
+ export function getRYT() {
18
+ if (typeof globalThis === "undefined")
19
+ return undefined;
20
+ const eth = globalThis.ethereum;
21
+ if (!eth)
22
+ return undefined;
23
+ return eth;
24
+ }
25
+ /**
26
+ * Checks whether an injected wallet is available.
27
+ *
28
+ * This is a convenience helper around `getRYT()`.
29
+ * Useful for conditionally enabling wallet-based features.
30
+ *
31
+ * @returns true if a wallet (MetaMask / similar) is detected, otherwise false
32
+ *
33
+ * @example
34
+ * if (!hasRYT()) {
35
+ * alert("Please install MetaMask");
36
+ * }
37
+ */
38
+ export function hasRYT() {
39
+ return !!getRYT();
40
+ }
41
+ /**
42
+ * Requests account access from the user's wallet.
43
+ *
44
+ * This triggers the wallet popup (e.g. MetaMask approval dialog)
45
+ * using the EIP-1193 `eth_requestAccounts` method.
46
+ *
47
+ * Requirements:
48
+ * - Must be called in a browser environment
49
+ * - Requires an injected wallet (MetaMask, etc.)
50
+ *
51
+ * @returns Promise resolving to an array of connected wallet addresses
52
+ *
53
+ * @throws Error if no injected wallet is available
54
+ *
55
+ * @example
56
+ * const accounts = await requestAccounts();
57
+ * console.log(accounts[0]);
58
+ */
59
+ export async function requestAccounts() {
60
+ const eth = getRYT();
61
+ if (!eth)
62
+ throw new Error("No injected wallet found");
63
+ return eth.request({ method: "eth_requestAccounts" });
64
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Converts human-readable token amount into smallest unit (wei-like format).
3
+ *
4
+ *
5
+ * @param amount - human readable value (e.g. "1.5")
6
+ * @param decimals - token decimals (e.g. 18)
7
+ * @returns bigint in smallest unit
8
+ *
9
+ * @example
10
+ * parseTokenUnits("1.5", 18)
11
+ */
12
+ export declare function parseTokenUnits(amount: string, decimals: number): bigint;
13
+ /**
14
+ * Converts smallest unit back to human-readable value.
15
+ *
16
+ *
17
+ * @param value - bigint or string
18
+ * @param decimals - token decimals
19
+ */
20
+ export declare function formatTokenUnits(value: bigint | string, decimals: number): string;
@@ -0,0 +1,25 @@
1
+ import { ethers } from "ethers";
2
+ /**
3
+ * Converts human-readable token amount into smallest unit (wei-like format).
4
+ *
5
+ *
6
+ * @param amount - human readable value (e.g. "1.5")
7
+ * @param decimals - token decimals (e.g. 18)
8
+ * @returns bigint in smallest unit
9
+ *
10
+ * @example
11
+ * parseTokenUnits("1.5", 18)
12
+ */
13
+ export function parseTokenUnits(amount, decimals) {
14
+ return ethers.parseUnits(amount, decimals);
15
+ }
16
+ /**
17
+ * Converts smallest unit back to human-readable value.
18
+ *
19
+ *
20
+ * @param value - bigint or string
21
+ * @param decimals - token decimals
22
+ */
23
+ export function formatTokenUnits(value, decimals) {
24
+ return ethers.formatUnits(value, decimals);
25
+ }
package/dist/wallet.d.ts CHANGED
@@ -1,63 +1,94 @@
1
1
  import { ethers } from "ethers";
2
2
  import type RYTProvider from "./provider";
3
3
  /**
4
- * Transaction request type
4
+ * Transaction request structure for blockchain operations.
5
5
  */
6
6
  export type TxRequest = ethers.TransactionRequest;
7
7
  /**
8
- * RYTWallet is a wallet instance that supports signing transactions and interacting with the blockchain.
8
+ * Unified wallet abstraction for RYT SDK.
9
+ *
10
+ * Supports:
11
+ * - Private key wallets (Node.js / backend)
12
+ * - Browser injected wallets (MetaMask, Rabby, etc.)
13
+ *
14
+ * Automatically adapts behavior based on runtime environment.
9
15
  */
10
16
  export default class RYTWallet {
11
- private wallet;
17
+ private signer;
12
18
  /**
13
- * Create a new RYTWallet instance
19
+ * Create a new wallet instance.
14
20
  *
15
- * @param privateKey - Ethereum private key (0x...)
16
- * @param provider - Optional RYTProvider instance for network access
21
+ * The wallet can operate in two modes:
17
22
  *
18
- * @example
19
- * ```ts
23
+ * 1. Backend mode (private key provided)
24
+ * - Full signing capability
25
+ * - Used in Node.js scripts, servers, bots
26
+ *
27
+ * 2. Browser mode (injected wallet)
28
+ * - Uses connected wallet (MetaMask, etc.)
29
+ * - No private key required or allowed
30
+ *
31
+ * @param signerOrPrivateKey
32
+ * - string → private key (backend mode)
33
+ * - signer → injected browser signer
34
+ *
35
+ * @param provider Optional provider instance for network access
36
+ *
37
+ * @throws Error if invalid input is provided
38
+ *
39
+ * @example Backend:
20
40
  * const wallet = new RYTWallet(PRIVATE_KEY, provider);
21
- * ```
41
+ *
42
+ * @example Browser:
43
+ * const wallet = new RYTWallet(signer);
22
44
  */
23
- constructor(privateKey: string, provider?: RYTProvider);
45
+ constructor(signerOrPrivateKey: string | ethers.Signer, provider?: RYTProvider);
24
46
  /**
25
- * Returns the wallet address
47
+ * Get wallet address.
26
48
  *
27
49
  * @returns Wallet public address
28
50
  *
29
51
  * @example
30
- * ```ts
31
- * const address = wallet.getAddress();
32
- * ```
52
+ * const address = await wallet.getAddress();
33
53
  */
34
- getAddress(): string;
54
+ getAddress(): Promise<string>;
35
55
  /**
36
- * Get wallet balance in wei
56
+ * Get wallet balance in wei.
37
57
  *
38
- * @returns Promise resolving to balance (bigint)
58
+ * Requires connected provider (RPC or injected).
39
59
  *
40
- * @throws Error if provider is not connected
60
+ * @returns Balance in wei
41
61
  *
42
- * @example
43
- * ```ts
44
- * const balance = await wallet.getBalance();
45
- * ```
62
+ * @throws Error if provider is not available
46
63
  */
47
64
  getBalance(): Promise<bigint>;
48
65
  /**
49
- * Send a signed transaction
66
+ * Send a blockchain transaction.
50
67
  *
51
- * @param tx - Transaction object (to, value, data, etc.)
52
- * @returns Promise resolving to transaction response
68
+ * Works in both:
69
+ * - Backend (signed via private key)
70
+ * - Browser (signed via MetaMask / injected wallet)
71
+ *
72
+ * @param tx Transaction request object
73
+ * @returns Transaction response
53
74
  *
54
75
  * @example
55
- * ```ts
56
76
  * await wallet.sendTransaction({
57
- * to: "0xabc...",
58
- * value: ethers.parseEther("0.01")
77
+ * to: "0x...",
78
+ * value: "0.01"
59
79
  * });
60
- * ```
61
80
  */
62
81
  sendTransaction(tx: TxRequest): Promise<ethers.TransactionResponse>;
82
+ /**
83
+ * Check if wallet is connected to a provider.
84
+ *
85
+ * @returns true if provider is available
86
+ */
87
+ isConnected(): boolean;
88
+ /**
89
+ * Get underlying signer instance.
90
+ *
91
+ * Useful for advanced contract interactions.
92
+ */
93
+ getSigner(): ethers.Signer;
63
94
  }