Skip to content

Commit dbd3415

Browse files
feat: support mint and borrow flow (#301)
1 parent a6dbfa1 commit dbd3415

File tree

25 files changed

+956
-152
lines changed

25 files changed

+956
-152
lines changed

package-lock.json

Lines changed: 333 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"tsx": "^4.19.4",
3737
"typescript": "^5.8.3",
3838
"typescript-eslint": "^8.44.1",
39-
"uuid": "11.1.0"
39+
"uuid": "11.1.0",
40+
"vite-plugin-top-level-await": "^1.6.0"
4041
},
4142
"workspaces": [
4243
"packages/*",

packages/babylon-config/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# @babylonlabs-io/config
2+
3+
Shared configuration package for Babylon Labs applications.
4+
5+
## Purpose
6+
7+
Centralized network configurations for ETH, BTC, and BBN chains used across all Babylon services and routes.
8+
9+
## Installation
10+
11+
```bash
12+
npm install @babylonlabs-io/config
13+
```
14+
15+
## Usage
16+
17+
### ETH Configuration
18+
19+
```typescript
20+
import { getNetworkConfigETH, getETHChain, network } from '@babylonlabs-io/config';
21+
22+
// Get current network configuration
23+
const config = getNetworkConfigETH();
24+
console.log(config.chainId); // 1, 11155111, or 31337
25+
26+
// Get viem Chain object
27+
const chain = getETHChain();
28+
29+
// Get current network name
30+
console.log(network); // 'mainnet', 'testnet', or 'localhost'
31+
```
32+
33+
### Environment Variables
34+
35+
- `NEXT_PUBLIC_NETWORK` - Network selection: `mainnet`, `testnet`, `localhost`, etc.
36+
- `NEXT_PUBLIC_ETH_CHAIN_ID` - Override chain ID
37+
- `NEXT_PUBLIC_ETH_RPC_URL` - Override RPC URL
38+
39+
## Supported Networks
40+
41+
### ETH
42+
- **Mainnet** (chainId: 1)
43+
- **Sepolia Testnet** (chainId: 11155111)
44+
- **Localhost/Anvil** (chainId: 31337)
45+
46+
## Architecture
47+
48+
This package provides the base configuration that can be extended by services:
49+
50+
```
51+
packages/babylon-config (base)
52+
└── services/simple-staking (extends with UI-specific properties like icons)
53+
└── routes/vault (uses base directly)
54+
```
55+
56+
## Development
57+
58+
```bash
59+
# Type check
60+
npm run typecheck
61+
62+
# Lint
63+
npm run lint
64+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "@babylonlabs-io/config",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module",
6+
"main": "src/index.ts",
7+
"types": "src/index.ts",
8+
"exports": {
9+
".": {
10+
"types": "./src/index.ts",
11+
"default": "./src/index.ts"
12+
},
13+
"./network/*": "./src/network/*"
14+
},
15+
"scripts": {
16+
"lint": "eslint",
17+
"typecheck": "tsc --noEmit"
18+
},
19+
"dependencies": {
20+
"@babylonlabs-io/wallet-connector": "*",
21+
"viem": "^2.37.9"
22+
},
23+
"devDependencies": {
24+
"@internal/eslint-config": "*",
25+
"typescript": "^5.8.3"
26+
}
27+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Network configurations
2+
export * from './network/eth';
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import type { ETHConfig } from "@babylonlabs-io/wallet-connector";
2+
import { mainnet, sepolia } from "viem/chains";
3+
import type { Chain } from "viem";
4+
import { defineChain } from "viem";
5+
6+
// Define localhost/Anvil chain (31337) since viem's localhost is 1337
7+
export const localhost = defineChain({
8+
id: 31337,
9+
name: 'Localhost',
10+
nativeCurrency: {
11+
decimals: 18,
12+
name: 'Ether',
13+
symbol: 'ETH',
14+
},
15+
rpcUrls: {
16+
default: {
17+
http: ['http://127.0.0.1:8545'],
18+
},
19+
},
20+
});
21+
22+
const defaultNetwork = "testnet";
23+
// Export network for modules that need to know which network is active
24+
export const network = process.env.NEXT_PUBLIC_NETWORK ?? defaultNetwork;
25+
26+
// Extended config type for UI-specific properties
27+
export type ExtendedETHConfig = ETHConfig & {
28+
name: string;
29+
displayUSD: boolean;
30+
};
31+
32+
type Config = ExtendedETHConfig;
33+
34+
const config: Record<string, Config> = {
35+
mainnet: {
36+
name: "Ethereum",
37+
chainId: 1,
38+
chainName: "Ethereum Mainnet",
39+
rpcUrl: process.env.NEXT_PUBLIC_ETH_RPC_URL || "https://eth.llamarpc.com",
40+
explorerUrl: "https://etherscan.io",
41+
nativeCurrency: {
42+
name: "Ether",
43+
symbol: "ETH",
44+
decimals: 18,
45+
},
46+
displayUSD: true,
47+
},
48+
canary: {
49+
// Using Sepolia for canary
50+
name: "Ethereum Sepolia",
51+
chainId: 11155111,
52+
chainName: "Sepolia Testnet",
53+
rpcUrl: process.env.NEXT_PUBLIC_ETH_RPC_URL || "https://rpc.sepolia.org",
54+
explorerUrl: "https://sepolia.etherscan.io",
55+
nativeCurrency: {
56+
name: "Sepolia ETH",
57+
symbol: "ETH",
58+
decimals: 18,
59+
},
60+
displayUSD: false,
61+
},
62+
testnet: {
63+
name: "Ethereum Sepolia",
64+
chainId: 11155111,
65+
chainName: "Sepolia Testnet",
66+
rpcUrl: process.env.NEXT_PUBLIC_ETH_RPC_URL || "https://rpc.sepolia.org",
67+
explorerUrl: "https://sepolia.etherscan.io",
68+
nativeCurrency: {
69+
name: "Sepolia ETH",
70+
symbol: "ETH",
71+
decimals: 18,
72+
},
73+
displayUSD: false,
74+
},
75+
devnet: {
76+
name: "Ethereum Sepolia",
77+
chainId: 11155111,
78+
chainName: "Sepolia Testnet",
79+
rpcUrl: process.env.NEXT_PUBLIC_ETH_RPC_URL || "https://rpc.sepolia.org",
80+
explorerUrl: "https://sepolia.etherscan.io",
81+
nativeCurrency: {
82+
name: "Sepolia ETH",
83+
symbol: "ETH",
84+
decimals: 18,
85+
},
86+
displayUSD: false,
87+
},
88+
localhost: {
89+
name: "Local Anvil",
90+
chainId: 31337,
91+
chainName: "Localhost",
92+
rpcUrl: process.env.NEXT_PUBLIC_ETH_RPC_URL || "http://localhost:8545",
93+
explorerUrl: "",
94+
nativeCurrency: {
95+
name: "Ether",
96+
symbol: "ETH",
97+
decimals: 18,
98+
},
99+
displayUSD: false,
100+
},
101+
};
102+
103+
export function getNetworkConfigETH(): Config {
104+
// Use the chain ID from environment if available
105+
const chainId = parseInt(process.env.NEXT_PUBLIC_ETH_CHAIN_ID || "0");
106+
107+
// If chain ID is set, use chain ID-based config
108+
if (chainId === 1) {
109+
return config.mainnet;
110+
} else if (chainId === 31337) {
111+
return config.localhost;
112+
} else if (chainId === 11155111) {
113+
return config.testnet;
114+
}
115+
116+
// Otherwise use the network-based config
117+
if (network === "localhost") {
118+
return config.localhost;
119+
}
120+
121+
return config[network] ?? config[defaultNetwork];
122+
}
123+
124+
/**
125+
* Get viem Chain object for the current network configuration
126+
* Used by contract clients that need Chain object
127+
*/
128+
export function getETHChain(): Chain {
129+
const config = getNetworkConfigETH();
130+
131+
// Map chainId to viem chain object
132+
switch (config.chainId) {
133+
case 1:
134+
return mainnet;
135+
case 11155111:
136+
return sepolia;
137+
case 31337:
138+
return localhost;
139+
default:
140+
// Default to testnet (Sepolia)
141+
return sepolia;
142+
}
143+
}
144+
145+
/**
146+
* Validate Ethereum address format
147+
* @param address - Address to validate
148+
* @throws Error if address format is invalid
149+
*/
150+
export function validateETHAddress(address: string): void {
151+
// Basic ETH address validation
152+
if (!address.match(/^0x[a-fA-F0-9]{40}$/)) {
153+
throw new Error(
154+
"Invalid Ethereum address format. Expected address to start with '0x' followed by 40 hexadecimal characters."
155+
);
156+
}
157+
158+
// TODO: Add checksum validation when needed
159+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"lib": ["ES2022"],
5+
"target": "ES2022",
6+
"module": "ESNext",
7+
"moduleResolution": "bundler",
8+
"allowImportingTsExtensions": true,
9+
"noEmit": true,
10+
"strict": true,
11+
"skipLibCheck": true,
12+
"resolveJsonModule": true,
13+
"isolatedModules": true
14+
},
15+
"include": ["src/**/*"],
16+
"exclude": ["node_modules", "dist"]
17+
}

packages/babylon-wallet-connector/src/core/wallets/eth/appkit/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ETHConfig, IETHProvider, WalletMetadata } from "@/core/types";
22
import { Network } from "@/core/types";
3+
34
import { AppKitProvider } from "./provider";
45

56
const WALLET_PROVIDER_NAME = "AppKit";
@@ -23,6 +24,7 @@ const metadata: WalletMetadata<IETHProvider, ETHConfig> = {
2324
networks: [
2425
Network.MAINNET, // ETH mainnet (chainId: 1)
2526
Network.TESTNET, // ETH testnet (chainId: 11155111 - Sepolia)
27+
Network.SIGNET, // Also allow SIGNET/devnet/localhost environments
2628
],
2729
label: "Connect ETH Wallet",
2830
};

routes/vault/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"tailwind-merge": "2.5.4"
2525
},
2626
"dependencies": {
27+
"@babylonlabs-io/config": "*",
2728
"@babylonlabs-io/core-ui": "*",
2829
"@babylonlabs-io/wallet-connector": "*",
2930
"@wagmi/core": "^2.16.3",

routes/vault/src/clients/eth-contract/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Shared ETH client singleton for all contract interactions
22

33
import { createPublicClient, http, type PublicClient } from 'viem';
4-
import { getNetworkConfigETH } from '../../config/network';
4+
import { getNetworkConfigETH, getETHChain } from '@babylonlabs-io/config';
55

66
/**
77
* ETHClient - Singleton client for Ethereum interactions
@@ -15,7 +15,7 @@ class ETHClient {
1515
private constructor() {
1616
// Create public client with config from environment
1717
this.publicClient = createPublicClient({
18-
chain: this.config.chain,
18+
chain: getETHChain(),
1919
transport: http(this.config.rpcUrl),
2020
});
2121
}

0 commit comments

Comments
 (0)