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: Interactive guide for integrating new DeFi modules or protocols into OneKey. Use when adding new DeFi features like staking protocols, lending markets, or entirely new DeFi modules. Triggers on DeFi, protocol, integration, Earn, Borrow, staking, lending, supply, borrow, withdraw, repay, claim, new module, Pendle, Aave, Compound.4---56# DeFi Module Integration Guide78This skill provides interactive guidance for integrating new DeFi modules or protocols into OneKey.910## Quick Start1112Before starting, determine your integration scenario:1314| 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) |1920---2122## Integration Layers2324Each DeFi module consists of 4 layers. **Home** and **Modal** are required; **Details** and **List** are optional.2526```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```4950---5152## Scenario Identification5354### When to ask the user5556Ask for clarification when:571. The protocol type is ambiguous (could be Earn or Borrow)582. The operation types are not standard593. Special UI/UX requirements are mentioned604. The protocol has unique features not covered by existing patterns6162### Scenario characteristics6364**Earn Protocol** (Staking/Yield):65- Operations: Stake, Unstake, Claim66- Data: APY, staked amount, rewards67- Examples: Lido, Rocket Pool, Babylon6869**Borrow Protocol** (Lending):70- Operations: Supply, Withdraw, Borrow, Repay71- Data: Health factor, collateral, debt, APY72- Examples: Aave, Compound, Morpho73- 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)7576**New Module**:77- Operations differ significantly from Earn/Borrow78- Requires independent Tab or unique UI79- Examples: Pendle (PT/YT), GMX (perpetuals)8081**Time-Based Protocol** (Fixed-rate yield):82- Operations: Buy, Sell, Redeem (conditional on maturity)83- Data: Maturity date, implied APY, underlying asset, discount rate84- Special features: Maturity status, conditional operations, multi-variant assets85- Examples: Pendle PT86- 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)8889---9091## Key Decision Points9293| 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 |109110### State Management Decision111112When integrating a new DeFi module, analyze the state requirements and ask the user:113114| 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 |118119**When using this skill, you should:**1201. Analyze the state requirements of the new module1212. Ask the user whether the state needs to persist across page navigation1223. Recommend Jotai for persistent state, Context for page-scoped state123124**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 atoms127> - If no (like form state that resets on page exit): Use React Context"128129---130131## Quick Reference132133### Key File Paths134135| 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` |143144### Common Components145146| 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)** |155156### State Management Patterns157158| 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) |164165---166167## Workflow168169### Step 1: Identify Scenario170- Determine if it's Earn, Borrow, or New Module171- Read the corresponding guide172173### Step 2: Plan Layers174- Decide which layers to implement (Home + Modal required)175- Identify optional layers needed176177### Step 3: Implement Layer by Layer178- Follow the guide for each layer179- Use the checklist to verify completion180181### Step 4: Test and Verify182- Test all operation types183- Verify pending state handling184- Check responsive layout185186---187188## Related Skills189190This skill works best when combined with these other OneKey skills:191192| 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 |198199---200201## i18n Quick Reference202203All user-facing strings must use internationalization. See `1k-i18n` skill for full details.204205**Basic Usage:**206```typescript207import { useIntl } from 'react-intl';208import { ETranslations } from '@onekeyhq/shared/src/locale';209210function MyComponent() {211 const intl = useIntl();212213 return (214 <Text>215 {intl.formatMessage({ id: ETranslations.defi_net_worth })}216 </Text>217 );218}219```220221**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"228229**Adding New Keys:**2301. Add key to `ETranslations` enum in `packages/shared/src/locale/enum.ts`2312. Add translations in locale JSON files2323. Never hardcode user-facing strings233234---235236## Reference Documents237238| 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 |