Initial commit
This commit is contained in:
46
packages/wallets/README.md
Normal file
46
packages/wallets/README.md
Normal file
@@ -0,0 +1,46 @@
|
||||
# @dbis-thirdweb/wallets
|
||||
|
||||
Wallet connectors and configuration for Chain 138.
|
||||
|
||||
## Usage
|
||||
|
||||
### Wallet Configuration
|
||||
|
||||
```typescript
|
||||
import { getWalletConfig } from '@dbis-thirdweb/wallets';
|
||||
|
||||
const config = getWalletConfig({
|
||||
confirmationBlocks: 2,
|
||||
gasStrategy: {
|
||||
multiplier: 1.5,
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
### Chain Switching
|
||||
|
||||
```typescript
|
||||
import { switchToChain138, ensureChain138 } from '@dbis-thirdweb/wallets';
|
||||
import { useWallet } from '@thirdweb-dev/react';
|
||||
|
||||
function MyComponent() {
|
||||
const wallet = useWallet();
|
||||
|
||||
const handleSwitch = async () => {
|
||||
await switchToChain138(wallet);
|
||||
};
|
||||
|
||||
// Or use ensure (switches only if not already on Chain 138)
|
||||
const handleEnsure = async () => {
|
||||
await ensureChain138(wallet);
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
## Features
|
||||
|
||||
- Chain 138 wallet configuration defaults
|
||||
- Gas strategy configuration
|
||||
- RPC failover support
|
||||
- Chain switching utilities
|
||||
- Automatic chain addition if missing
|
||||
40
packages/wallets/package.json
Normal file
40
packages/wallets/package.json
Normal file
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"name": "@dbis-thirdweb/wallets",
|
||||
"version": "0.1.0",
|
||||
"description": "Wallet connectors and configuration for Chain 138",
|
||||
"type": "module",
|
||||
"main": "./dist/index.js",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/index.js"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsup src/index.ts --format cjs,esm --dts",
|
||||
"lint": "eslint src",
|
||||
"test": "echo \"No tests yet\""
|
||||
},
|
||||
"keywords": [
|
||||
"thirdweb",
|
||||
"wallet",
|
||||
"chain-138",
|
||||
"connector"
|
||||
],
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@dbis-thirdweb/chain": "workspace:*",
|
||||
"@thirdweb-dev/chains": "^0.1.0",
|
||||
"@thirdweb-dev/react": "^4.0.0",
|
||||
"@thirdweb-dev/sdk": "^4.0.0",
|
||||
"ethers": "^5.7.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.0.0",
|
||||
"tsup": "^8.0.0",
|
||||
"typescript": "^5.0.0"
|
||||
}
|
||||
}
|
||||
71
packages/wallets/src/chainSwitching.ts
Normal file
71
packages/wallets/src/chainSwitching.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { chain138 } from '@dbis-thirdweb/chain';
|
||||
import type { Chain } from '@thirdweb-dev/chains';
|
||||
|
||||
/**
|
||||
* Wallet interface for chain switching operations
|
||||
* Compatible with thirdweb wallet connectors
|
||||
*/
|
||||
export interface WalletLike {
|
||||
getChainId(): number;
|
||||
switchChain(chainId: number): Promise<void>;
|
||||
addChain(chain: Chain): Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Switch wallet to Chain 138
|
||||
* @param wallet - Connected wallet instance
|
||||
* @returns Promise that resolves when chain switch is complete
|
||||
*/
|
||||
export async function switchToChain138(wallet: WalletLike): Promise<void> {
|
||||
try {
|
||||
// Check if wallet is already on Chain 138
|
||||
if (wallet.getChainId() === chain138.chainId) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Attempt to switch chain
|
||||
await wallet.switchChain(chain138.chainId);
|
||||
} catch (error: unknown) {
|
||||
// If chain doesn't exist in wallet, try to add it
|
||||
if (
|
||||
error &&
|
||||
typeof error === 'object' &&
|
||||
'code' in error &&
|
||||
(error.code === 4902 || error.code === -32603)
|
||||
) {
|
||||
await addChain138(wallet);
|
||||
await wallet.switchChain(chain138.chainId);
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Chain 138 to wallet if it doesn't exist
|
||||
* @param wallet - Connected wallet instance
|
||||
*/
|
||||
export async function addChain138(wallet: WalletLike): Promise<void> {
|
||||
const chain: Chain = chain138;
|
||||
await wallet.addChain(chain);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if wallet is connected to Chain 138
|
||||
* @param wallet - Connected wallet instance
|
||||
* @returns true if wallet is on Chain 138
|
||||
*/
|
||||
export function isOnChain138(wallet: WalletLike): boolean {
|
||||
return wallet.getChainId() === chain138.chainId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure wallet is on Chain 138, switching if necessary
|
||||
* @param wallet - Connected wallet instance
|
||||
* @returns Promise that resolves when wallet is on Chain 138
|
||||
*/
|
||||
export async function ensureChain138(wallet: WalletLike): Promise<void> {
|
||||
if (!isOnChain138(wallet)) {
|
||||
await switchToChain138(wallet);
|
||||
}
|
||||
}
|
||||
3
packages/wallets/src/index.ts
Normal file
3
packages/wallets/src/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from './walletConfig';
|
||||
export * from './chainSwitching';
|
||||
export { chain138 } from '@dbis-thirdweb/chain';
|
||||
92
packages/wallets/src/walletConfig.ts
Normal file
92
packages/wallets/src/walletConfig.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import { chain138 } from '@dbis-thirdweb/chain';
|
||||
|
||||
/**
|
||||
* Wallet configuration defaults for Chain 138
|
||||
*/
|
||||
export interface WalletConfig {
|
||||
/**
|
||||
* Number of confirmation blocks required before considering a transaction final
|
||||
* @default 1
|
||||
*/
|
||||
confirmationBlocks: number;
|
||||
|
||||
/**
|
||||
* Gas strategy configuration
|
||||
*/
|
||||
gasStrategy: {
|
||||
/**
|
||||
* Gas price multiplier (1.0 = current gas price, 1.2 = 20% more)
|
||||
* @default 1.2
|
||||
*/
|
||||
multiplier: number;
|
||||
|
||||
/**
|
||||
* Maximum gas price in gwei (0 = no limit)
|
||||
* @default 0
|
||||
*/
|
||||
maxGasPriceGwei: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* RPC failover configuration
|
||||
*/
|
||||
rpcFailover: {
|
||||
/**
|
||||
* Primary RPC endpoint
|
||||
*/
|
||||
primary: string;
|
||||
|
||||
/**
|
||||
* Fallback RPC endpoints (used if primary fails)
|
||||
*/
|
||||
fallbacks: string[];
|
||||
|
||||
/**
|
||||
* Timeout in milliseconds for RPC requests
|
||||
* @default 10000
|
||||
*/
|
||||
timeout: number;
|
||||
|
||||
/**
|
||||
* Number of retries before switching to fallback
|
||||
* @default 2
|
||||
*/
|
||||
retries: number;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Default wallet configuration for Chain 138
|
||||
*/
|
||||
export const defaultWalletConfig: WalletConfig = {
|
||||
confirmationBlocks: 1,
|
||||
gasStrategy: {
|
||||
multiplier: 1.2,
|
||||
maxGasPriceGwei: 0,
|
||||
},
|
||||
rpcFailover: {
|
||||
primary: chain138.rpc[0] || 'https://138.rpc.thirdweb.com',
|
||||
fallbacks: [],
|
||||
timeout: 10000,
|
||||
retries: 2,
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Get wallet configuration for Chain 138 with optional overrides
|
||||
*/
|
||||
export function getWalletConfig(overrides?: Partial<WalletConfig>): WalletConfig {
|
||||
return {
|
||||
...defaultWalletConfig,
|
||||
...overrides,
|
||||
gasStrategy: {
|
||||
...defaultWalletConfig.gasStrategy,
|
||||
...overrides?.gasStrategy,
|
||||
},
|
||||
rpcFailover: {
|
||||
...defaultWalletConfig.rpcFailover,
|
||||
...overrides?.rpcFailover,
|
||||
fallbacks: overrides?.rpcFailover?.fallbacks ?? defaultWalletConfig.rpcFailover.fallbacks,
|
||||
},
|
||||
};
|
||||
}
|
||||
12
packages/wallets/tsconfig.json
Normal file
12
packages/wallets/tsconfig.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"composite": false
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"references": [
|
||||
{ "path": "../chain" }
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user