DeFi Module Integration Guide
This skill provides interactive guidance for integrating new DeFi modules or protocols into OneKey.
Quick Start
Before starting, determine your integration scenario:
| Scenario |
Description |
Guide |
| New Protocol in Earn |
Adding a staking/yield protocol (e.g., Lido, Rocket Pool) |
earn-module-guide.md |
| New Protocol in Borrow |
Adding a lending market (e.g., Aave, Compound) |
borrow-module-guide.md |
| Entirely New Module |
Creating a new DeFi category (e.g., Pendle, GMX) |
new-module-guide.md |
Integration Layers
Each DeFi module consists of 4 layers. Home and Modal are required; Details and List are optional.
┌─────────────────────────────────────────────────────────────┐
│ Layer 1: HOME PAGE (Required) │
│ - Overview data (total value, APY, health factor) │
│ - Asset cards/tables │
│ - Pending transaction tracking │
├─────────────────────────────────────────────────────────────┤
│ Layer 2: OPERATION MODALS (Required) │
│ - Earn: Stake, Unstake, Claim (2-3 types) │
│ - Borrow: Supply, Withdraw, Borrow, Repay (4 types) │
│ - Amount input, validation, risk warnings │
├─────────────────────────────────────────────────────────────┤
│ Layer 3: DETAILS PAGE (Optional) │
│ - Charts (APY history, interest rate model) │
│ - Detailed protocol information │
│ - Share functionality │
├─────────────────────────────────────────────────────────────┤
│ Layer 4: PROTOCOL LIST (Optional, for multi-token) │
│ - List of available protocols/assets │
│ - Filtering and sorting │
│ - Navigation to details │
└─────────────────────────────────────────────────────────────┘
Scenario Identification
When to ask the user
Ask for clarification when:
- The protocol type is ambiguous (could be Earn or Borrow)
- The operation types are not standard
- Special UI/UX requirements are mentioned
- The protocol has unique features not covered by existing patterns
Scenario characteristics
Earn Protocol (Staking/Yield):
- Operations: Stake, Unstake, Claim
- Data: APY, staked amount, rewards
- Examples: Lido, Rocket Pool, Babylon
Borrow Protocol (Lending):
- Operations: Supply, Withdraw, Borrow, Repay
- Data: Health factor, collateral, debt, APY
- Examples: Aave, Compound, Morpho
- Advanced features: Repay with Collateral (swap-based repayment)
- See: borrow-module-guide.md - Repay with Collateral
New Module:
- Operations differ significantly from Earn/Borrow
- Requires independent Tab or unique UI
- Examples: Pendle (PT/YT), GMX (perpetuals)
Time-Based Protocol (Fixed-rate yield):
- Operations: Buy, Sell, Redeem (conditional on maturity)
- Data: Maturity date, implied APY, underlying asset, discount rate
- Special features: Maturity status, conditional operations, multi-variant assets
- Examples: Pendle PT
- Integration: As sub-module of Earn Tab (Fixed-rate category)
- See: earn-module-guide.md - Time-Based Protocols
Key Decision Points
| Decision |
Options |
Default |
| Integration type |
Earn / Borrow / New Module |
Ask user |
| Tab placement |
Existing Earn Tab / New Tab |
Earn Tab |
| Operation count |
2-3 (Earn) / 4 (Borrow) / Custom |
Based on type |
| Risk warnings |
Liquidation / Slashing / None |
Based on type |
| Token selection |
Single / Multiple |
Single |
| Charts |
APY history / Interest model / None |
Based on type |
| Share feature |
Yes / No |
Yes |
| Multi-token list |
Yes / No |
Based on token count |
| Time-based features |
Maturity date / Conditional ops / None |
Based on protocol |
| Multi-variant assets |
Group by underlying / Flat list |
Based on protocol |
| Operation tabs |
Single op / Tab switching (Buy/Sell/Redeem) |
Based on protocol |
| Repay with collateral |
Wallet balance only / With collateral option |
Based on protocol |
| Dual amount input |
Single input / Bidirectional sync |
Based on operation |
| Slippage settings |
Not needed / Auto / Custom |
Based on swap involvement |
State Management Decision
When integrating a new DeFi module, analyze the state requirements and ask the user:
| State Type |
Recommendation |
Examples |
| Needs Persistence (across page navigation) |
Use Jotai atoms |
Portfolio data, user preferences, cached investments |
| Page-scoped (no persistence needed) |
Use React Context |
Current operation state, form data, temporary UI state |
When using this skill, you should:
- Analyze the state requirements of the new module
- Ask the user whether the state needs to persist across page navigation
- Recommend Jotai for persistent state, Context for page-scoped state
Example question to ask:
"Does this module's data need to persist when the user navigates away and returns? For example:
- If yes (like portfolio data that should be cached): Use Jotai atoms
- If no (like form state that resets on page exit): Use React Context"
Quick Reference
Key File Paths
| Module |
Path |
| Earn |
packages/kit/src/views/Earn/ |
| Borrow |
packages/kit/src/views/Borrow/ |
| Staking (shared) |
packages/kit/src/views/Staking/ |
| Routes |
packages/shared/src/routes/ |
| Modal Router |
packages/kit/src/routes/Modal/router.tsx |
| Tab Router |
packages/kit/src/routes/Tab/router.ts |
Common Components
| Component |
Location |
Usage |
StakingAmountInput |
Staking/components/ |
Amount input with validation |
StakingFormWrapper |
Staking/components/ |
Form layout wrapper |
EarnText |
Staking/components/ProtocolDetails/ |
Styled text with color support |
EarnTooltip |
Staking/components/ProtocolDetails/ |
Info tooltips |
PendingIndicator |
Staking/components/StakingActivityIndicator/ |
Pending tx indicator |
ManagePositionContent |
Staking/pages/ManagePosition/ |
Shared manage position UI |
ManagePosition |
Borrow/components/ManagePosition/ |
Unified Borrow operation component (Supply/Withdraw/Borrow/Repay) |
State Management Patterns
| Pattern |
Use Case |
Reference |
IAsyncData<T> |
Unified async data format |
state-management-guide.md |
DataGate |
Data orchestration |
state-management-guide.md |
PendingBridge |
External pending state |
state-management-guide.md |
| Tag System |
Pending tx identification |
state-management-guide.md |
Workflow
Step 1: Identify Scenario
- Determine if it's Earn, Borrow, or New Module
- Read the corresponding guide
Step 2: Plan Layers
- Decide which layers to implement (Home + Modal required)
- Identify optional layers needed
Step 3: Implement Layer by Layer
- Follow the guide for each layer
- Use the checklist to verify completion
Step 4: Test and Verify
- Test all operation types
- Verify pending state handling
- Check responsive layout
Related Skills
This skill works best when combined with these other OneKey skills:
| Skill |
Use For |
1k-i18n |
Adding translations, using ETranslations, useIntl() |
1k-coding-patterns |
React patterns, error handling, TypeScript best practices |
1k-cross-platform |
Platform-specific code, platformEnv checks |
page-and-route |
Route configuration, deep links, navigation |
i18n Quick Reference
All user-facing strings must use internationalization. See 1k-i18n skill for full details.
Basic Usage:
import { useIntl } from 'react-intl';
import { ETranslations } from '@onekeyhq/shared/src/locale';
function MyComponent() {
const intl = useIntl();
return (
<Text>
{intl.formatMessage({ id: ETranslations.defi_net_worth })}
</Text>
);
}
Common DeFi Translation Keys:
ETranslations.defi_net_worth - "Net Worth"
ETranslations.defi_net_apy - "Net APY"
ETranslations.defi_health_factor - "Health Factor"
ETranslations.defi_platform_bonus - "Platform Bonus"
ETranslations.defi_claimable_rewards - "Claimable Rewards"
ETranslations.global_history - "History"
Adding New Keys:
- Add key to
ETranslations enum in packages/shared/src/locale/enum.ts
- Add translations in locale JSON files
- Never hardcode user-facing strings
Reference Documents
| Document |
Content |
| earn-module-guide.md |
Earn module architecture, files, operations |
| borrow-module-guide.md |
Borrow module architecture, 4 operations, health factor |
| new-module-guide.md |
Creating new modules, Provider design |
| routing-guide.md |
Modal routes, Tab routes, navigation utilities, deep links |
| state-management-guide.md |
IAsyncData, Pending, Tag system, refresh strategies |
| checklist.md |
Integration checklist with required/optional markers |
1---2name: 1k-defi-module-integration3description: Guide for integrating new DeFi modules or protocols (staking, lending, Earn, Borrow) into OneKey.4---5
6# DeFi Module Integration Guide
7
8This skill provides interactive guidance for integrating new DeFi modules or protocols into OneKey.
9
10## Quick Start
11
12Before starting, determine your integration scenario:
13
14| Scenario | Description | Guide |
15|----------|-------------|-------|
16| **New Protocol in Earn** | Adding a staking/yield protocol (e.g., Lido, Rocket Pool) | [earn-module-guide.md](reference/earn-module-guide.md) |
17| **New Protocol in Borrow** | Adding a lending market (e.g., Aave, Compound) | [borrow-module-guide.md](reference/borrow-module-guide.md) |
18| **Entirely New Module** | Creating a new DeFi category (e.g., Pendle, GMX) | [new-module-guide.md](reference/new-module-guide.md) |
19
20---
21
22## Integration Layers
23
24Each DeFi module consists of 4 layers. **Home** and **Modal** are required; **Details** and **List** are optional.
25
26```
27┌─────────────────────────────────────────────────────────────┐
28│ Layer 1: HOME PAGE (Required) │
29│ - Overview data (total value, APY, health factor) │
30│ - Asset cards/tables │
31│ - Pending transaction tracking │
32├─────────────────────────────────────────────────────────────┤
33│ Layer 2: OPERATION MODALS (Required) │
34│ - Earn: Stake, Unstake, Claim (2-3 types) │
35│ - Borrow: Supply, Withdraw, Borrow, Repay (4 types) │
36│ - Amount input, validation, risk warnings │
37├─────────────────────────────────────────────────────────────┤
38│ Layer 3: DETAILS PAGE (Optional) │
39│ - Charts (APY history, interest rate model) │
40│ - Detailed protocol information │
41│ - Share functionality │
42├─────────────────────────────────────────────────────────────┤
43│ Layer 4: PROTOCOL LIST (Optional, for multi-token) │
44│ - List of available protocols/assets │
45│ - Filtering and sorting │
46│ - Navigation to details │
47└─────────────────────────────────────────────────────────────┘
48```
49
50---
51
52## Scenario Identification
53
54### When to ask the user
55
56Ask for clarification when:
571. The protocol type is ambiguous (could be Earn or Borrow)
582. The operation types are not standard
593. Special UI/UX requirements are mentioned
604. The protocol has unique features not covered by existing patterns
61
62### Scenario characteristics
63
64**Earn Protocol** (Staking/Yield):
65- Operations: Stake, Unstake, Claim
66- Data: APY, staked amount, rewards
67- Examples: Lido, Rocket Pool, Babylon
68
69**Borrow Protocol** (Lending):
70- Operations: Supply, Withdraw, Borrow, Repay
71- Data: Health factor, collateral, debt, APY
72- Examples: Aave, Compound, Morpho
73- Advanced features: Repay with Collateral (swap-based repayment)
74- See: [borrow-module-guide.md - Repay with Collateral](reference/borrow-module-guide.md#repay-with-collateral-pattern)
75
76**New Module**:
77- Operations differ significantly from Earn/Borrow
78- Requires independent Tab or unique UI
79- Examples: Pendle (PT/YT), GMX (perpetuals)
80
81**Time-Based Protocol** (Fixed-rate yield):
82- Operations: Buy, Sell, Redeem (conditional on maturity)
83- Data: Maturity date, implied APY, underlying asset, discount rate
84- Special features: Maturity status, conditional operations, multi-variant assets
85- Examples: Pendle PT
86- Integration: As sub-module of Earn Tab (Fixed-rate category)
87- See: [earn-module-guide.md - Time-Based Protocols](reference/earn-module-guide.md#time-based-protocols-eg-pendle-pt)
88
89---
90
91## Key Decision Points
92
93| Decision | Options | Default |
94|----------|---------|---------|
95| Integration type | Earn / Borrow / New Module | Ask user |
96| Tab placement | Existing Earn Tab / New Tab | Earn Tab |
97| Operation count | 2-3 (Earn) / 4 (Borrow) / Custom | Based on type |
98| Risk warnings | Liquidation / Slashing / None | Based on type |
99| Token selection | Single / Multiple | Single |
100| Charts | APY history / Interest model / None | Based on type |
101| Share feature | Yes / No | Yes |
102| Multi-token list | Yes / No | Based on token count |
103| **Time-based features** | Maturity date / Conditional ops / None | Based on protocol |
104| **Multi-variant assets** | Group by underlying / Flat list | Based on protocol |
105| **Operation tabs** | Single op / Tab switching (Buy/Sell/Redeem) | Based on protocol |
106| **Repay with collateral** | Wallet balance only / With collateral option | Based on protocol |
107| **Dual amount input** | Single input / Bidirectional sync | Based on operation |
108| **Slippage settings** | Not needed / Auto / Custom | Based on swap involvement |
109
110### State Management Decision
111
112When integrating a new DeFi module, analyze the state requirements and ask the user:
113
114| State Type | Recommendation | Examples |
115|------------|----------------|----------|
116| **Needs Persistence** (across page navigation) | Use Jotai atoms | Portfolio data, user preferences, cached investments |
117| **Page-scoped** (no persistence needed) | Use React Context | Current operation state, form data, temporary UI state |
118
119**When using this skill, you should:**
1201. Analyze the state requirements of the new module
1212. Ask the user whether the state needs to persist across page navigation
1223. Recommend Jotai for persistent state, Context for page-scoped state
123
124**Example question to ask:**
125> "Does this module's data need to persist when the user navigates away and returns? For example:
126> - If yes (like portfolio data that should be cached): Use Jotai atoms
127> - If no (like form state that resets on page exit): Use React Context"
128
129---
130
131## Quick Reference
132
133### Key File Paths
134
135| Module | Path |
136|--------|------|
137| Earn | `packages/kit/src/views/Earn/` |
138| Borrow | `packages/kit/src/views/Borrow/` |
139| Staking (shared) | `packages/kit/src/views/Staking/` |
140| Routes | `packages/shared/src/routes/` |
141| Modal Router | `packages/kit/src/routes/Modal/router.tsx` |
142| Tab Router | `packages/kit/src/routes/Tab/router.ts` |
143
144### Common Components
145
146| Component | Location | Usage |
147|-----------|----------|-------|
148| `StakingAmountInput` | `Staking/components/` | Amount input with validation |
149| `StakingFormWrapper` | `Staking/components/` | Form layout wrapper |
150| `EarnText` | `Staking/components/ProtocolDetails/` | Styled text with color support |
151| `EarnTooltip` | `Staking/components/ProtocolDetails/` | Info tooltips |
152| `PendingIndicator` | `Staking/components/StakingActivityIndicator/` | Pending tx indicator |
153| `ManagePositionContent` | `Staking/pages/ManagePosition/` | Shared manage position UI |
154| `ManagePosition` | `Borrow/components/ManagePosition/` | **Unified Borrow operation component (Supply/Withdraw/Borrow/Repay)** |
155
156### State Management Patterns
157
158| Pattern | Use Case | Reference |
159|---------|----------|-----------|
160| `IAsyncData<T>` | Unified async data format | [state-management-guide.md](reference/state-management-guide.md) |
161| `DataGate` | Data orchestration | [state-management-guide.md](reference/state-management-guide.md) |
162| `PendingBridge` | External pending state | [state-management-guide.md](reference/state-management-guide.md) |
163| Tag System | Pending tx identification | [state-management-guide.md](reference/state-management-guide.md) |
164
165---
166
167## Workflow
168
169### Step 1: Identify Scenario
170- Determine if it's Earn, Borrow, or New Module
171- Read the corresponding guide
172
173### Step 2: Plan Layers
174- Decide which layers to implement (Home + Modal required)
175- Identify optional layers needed
176
177### Step 3: Implement Layer by Layer
178- Follow the guide for each layer
179- Use the checklist to verify completion
180
181### Step 4: Test and Verify
182- Test all operation types
183- Verify pending state handling
184- Check responsive layout
185
186---
187
188## Related Skills
189
190This skill works best when combined with these other OneKey skills:
191
192| Skill | Use For |
193|-------|---------|
194| `1k-i18n` | Adding translations, using `ETranslations`, `useIntl()` |
195| `1k-coding-patterns` | React patterns, error handling, TypeScript best practices |
196| `1k-cross-platform` | Platform-specific code, `platformEnv` checks |
197| `page-and-route` | Route configuration, deep links, navigation |
198
199---
200
201## i18n Quick Reference
202
203All user-facing strings must use internationalization. See `1k-i18n` skill for full details.
204
205**Basic Usage:**
206```typescript
207import { useIntl } from 'react-intl';
208import { ETranslations } from '@onekeyhq/shared/src/locale';
209
210function MyComponent() {
211 const intl = useIntl();
212
213 return (
214 <Text>
215 {intl.formatMessage({ id: ETranslations.defi_net_worth })}
216 </Text>
217 );
218}
219```
220
221**Common DeFi Translation Keys:**
222- `ETranslations.defi_net_worth` - "Net Worth"
223- `ETranslations.defi_net_apy` - "Net APY"
224- `ETranslations.defi_health_factor` - "Health Factor"
225- `ETranslations.defi_platform_bonus` - "Platform Bonus"
226- `ETranslations.defi_claimable_rewards` - "Claimable Rewards"
227- `ETranslations.global_history` - "History"
228
229**Adding New Keys:**
2301. Add key to `ETranslations` enum in `packages/shared/src/locale/enum.ts`
2312. Add translations in locale JSON files
2323. Never hardcode user-facing strings
233
234---
235
236## Reference Documents
237
238| Document | Content |
239|----------|---------|
240| [earn-module-guide.md](reference/earn-module-guide.md) | Earn module architecture, files, operations |
241| [borrow-module-guide.md](reference/borrow-module-guide.md) | Borrow module architecture, 4 operations, health factor |
242| [new-module-guide.md](reference/new-module-guide.md) | Creating new modules, Provider design |
243| [routing-guide.md](reference/routing-guide.md) | Modal routes, Tab routes, navigation utilities, deep links |
244| [state-management-guide.md](reference/state-management-guide.md) | IAsyncData, Pending, Tag system, refresh strategies |
245| [checklist.md](reference/checklist.md) | Integration checklist with required/optional markers |