Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OK-33669: Add order data to staking #6159

Merged
merged 12 commits into from
Nov 12, 2024
Merged

OK-33669: Add order data to staking #6159

merged 12 commits into from
Nov 12, 2024

Conversation

originalix
Copy link
Contributor

@originalix originalix commented Nov 9, 2024

Summary by CodeRabbit

  • 新功能

    • 引入了新的 earnOrders 属性,增强了数据库和代理服务的功能。
    • 新增 SimpleDbEntityEarnOrders 类,提供管理赚取订单的接口和方法。
    • 增加了与赚取订单相关的异步方法,提升了订单管理能力。
    • 在多个钩子中引入订单 ID,确保在各种操作中一致处理。
    • 新增 dappRiskDetect 方法,增强风险检测功能。
    • StakingScope 中添加了 order 属性,扩展了场景管理能力。
    • 新增 OrderScene 类,提供订单管理的公共方法。
  • 文档

    • 更新了类型定义以支持新的交易状态和结构。

Copy link

codesandbox bot commented Nov 9, 2024

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link
Contributor

coderabbitai bot commented Nov 9, 2024

Walkthrough

此次更改主要涉及多个文件,新增了与“赚取订单”相关的功能。SimpleDbSimpleDbProxy类中分别添加了earnOrders属性,类型为SimpleDbEntityEarnOrders。新文件SimpleDbEntityEarnOrders.ts定义了管理赚取订单的类及其接口。ServiceHistoryServiceStaking类中新增了处理订单的相关方法,并更新了API路径。VaultBaseuseUniversalHooks文件也进行了相应的类型和方法签名更新,以适应新的结构。

Changes

文件路径 更改摘要
packages/kit-bg/src/dbs/simple/base/SimpleDb.ts 新增属性earnOrders,类型为SimpleDbEntityEarnOrders
packages/kit-bg/src/dbs/simple/base/SimpleDbProxy.ts 新增属性earnOrders,类型为SimpleDbEntityEarnOrders;新增类型导入。
packages/kit-bg/src/dbs/simple/entity/SimpleDbEntityEarnOrders.ts 新增类SimpleDbEntityEarnOrders及其接口和方法,用于管理赚取订单。
packages/kit-bg/src/services/ServiceHistory.ts 新增类型IChangedPendingTxInfo;更新fetchAccountHistory方法以跟踪待处理事务的变化。
packages/kit-bg/src/services/ServiceStaking.ts 新增方法addEarnOrderupdateEarnOrderupdateEarnOrderStatusToServer;更新API路径和方法签名。
packages/kit-bg/src/vaults/base/VaultBase.ts 更新方法签名,类型从IStakeTxResponse改为IStakeTx
packages/kit/src/views/Staking/hooks/useUniversalHooks.ts 新增函数createStakeInfoWithOrderIdhandleStakeSuccess,更新成功处理逻辑。
packages/shared/types/history.ts 新增类型EDecodedTxStatus和接口IChangedPendingTxInfo
packages/shared/types/staking.ts 新增属性orderIdIStakingInfo;新增类型IStakeTxResponse,修改IStakeTx类型。

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary or Summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@originalix originalix marked this pull request as draft November 9, 2024 09:34
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 16

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Reviewing files that changed from the base of the PR and between da11b87 and 050d0c4.

📒 Files selected for processing (9)
  • packages/kit-bg/src/dbs/simple/base/SimpleDb.ts (2 hunks)
  • packages/kit-bg/src/dbs/simple/base/SimpleDbProxy.ts (2 hunks)
  • packages/kit-bg/src/dbs/simple/entity/SimpleDbEntityEarnOrders.ts (1 hunks)
  • packages/kit-bg/src/services/ServiceHistory.ts (3 hunks)
  • packages/kit-bg/src/services/ServiceStaking.ts (7 hunks)
  • packages/kit-bg/src/vaults/base/VaultBase.ts (2 hunks)
  • packages/kit/src/views/Staking/hooks/useUniversalHooks.ts (8 hunks)
  • packages/shared/types/history.ts (2 hunks)
  • packages/shared/types/staking.ts (2 hunks)
🔇 Additional comments (15)
packages/kit-bg/src/dbs/simple/base/SimpleDb.ts (2)

16-16: 导入语句格式正确

新增的 SimpleDbEntityEarnOrders 导入语句遵循了文件现有的导入规范,位置排序恰当。


77-78: earnOrders 属性初始化符合规范

earnOrders 属性的声明和初始化与文件中其他数据库实体保持一致,维护了良好的代码风格。

packages/shared/types/history.ts (2)

6-6: 导入声明清晰明确!

新增的 EDecodedTxStatus 类型导入与现有的事务相关类型放在一起,结构合理。


230-235: 接口定义结构完整!

IChangedPendingTxInfo 接口设计合理,包含了追踪待处理交易状态变化所需的必要字段:

  • accountId 和 networkId 用于定位具体账户
  • txId 用于标识具体交易
  • status 用于表示交易状态

这些字段能够有效支持赚取订单功能中的交易状态跟踪。

packages/kit-bg/src/dbs/simple/base/SimpleDbProxy.ts (2)

20-20: 导入语句格式正确

新增的 SimpleDbEntityEarnOrders 类型导入遵循了现有的导入模式,并保持了正确的字母顺序排序。


164-166: earnOrders 属性实现规范

新增的 earnOrders 属性:

  • 使用了标准的 _createProxyService 方法初始化
  • 类型声明正确
  • 与其他属性保持一致的实现方式
  • 在 earn 属性后保持了正确的字母顺序
packages/shared/types/staking.ts (3)

26-26: 新增 orderId 字段符合预期!

IStakingInfo 接口中添加可选的 orderId 字段,与 PR 目标保持一致。


150-153: 类型定义清晰简洁!

IStakeTx 联合类型的定义清晰明了,包含了所有必要的交易类型。


156-159: 响应类型结构合理!

新增的 IStakeTxResponse 类型设计合理:

  • 包含交易信息 tx
  • 包含订单ID orderId
  • 结构简单清晰

这个设计很好地支持了订单相关功能的集成。

packages/kit/src/views/Staking/hooks/useUniversalHooks.ts (2)

19-28: 新增的 createStakeInfoWithOrderId 函数实现合理

函数实现简洁明了,通过扩展运算符保留了原有属性,同时添加了 orderId。这种实现方式既保证了类型安全,又避免了直接修改原对象。


99-99: useUniversalStake 中的改动需要验证

新增的 orderId 处理逻辑看起来合理,但需要确保:

  1. stakeTx.orderId 是否始终存在
  2. 上游组件是否正确处理了包含 orderId 的 stakingInfo

Also applies to: 112-127

✅ Verification successful

Let me search for more specific patterns to understand the stakeTx and orderId handling.


需要进一步验证 orderId 的处理逻辑

根据现有代码分析,我们看到:

  1. stakeTx.orderId 被用于多个场景(质押、解质押、领取收益)
  2. 上游组件通过 navigationToSendConfirm 接收 stakingInfo

但我们仍需要验证关键接口定义。


orderId 处理逻辑已得到验证

基于代码分析,我们可以确认:

  1. stakeTx.orderId 在质押、解质押和领取收益的所有场景中都被正确传递
  2. navigationToSendConfirm 正确接收并处理包含 orderId 的 stakingInfo
  3. 整个流程保持了数据一致性,从交易构建到确认页面
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 检查 stakeTx.orderId 的生成逻辑
ast-grep --pattern 'buildStakeTransaction($$$) {
  $$$
  return {
    $$$
    orderId: $_,
    $$$
  }
}'

Length of output: 107


Script:

#!/bin/bash
# Search for stakeTx related code patterns
rg -A 5 "stakeTx" packages/kit/src/views/Staking/hooks/useUniversalHooks.ts

# Search for orderId usage in the staking related files
rg "orderId" packages/kit/src/views/Staking/

Length of output: 2661


Script:

#!/bin/bash
# Search for stake transaction building logic
ast-grep --pattern 'const stakeTx = $_($$$)'

# Search for staking info type definitions
rg -A 5 "interface.*StakingInfo|type.*StakingInfo" packages/kit/src/views/Staking/

Length of output: 131


Script:

#!/bin/bash
# Search for IStakeTxResponse interface definition
rg -A 10 "interface IStakeTxResponse" packages/kit/src/

# Search for buildStakeTransaction implementation
rg -A 10 "buildStakeTransaction.*\{" packages/kit/src/

# Search for stakingInfo type usage in navigation
rg -A 5 "navigationToSendConfirm.*stakingInfo" packages/kit/src/

Length of output: 1115


Script:

#!/bin/bash
# 搜索 serviceStaking 相关接口定义
rg -A 10 "class.*ServiceStaking" packages/kit/src/

# 搜索 stakingInfo 相关类型定义
rg -A 10 "type.*StakingInfo|interface.*StakingInfo" packages/kit/src/background/services/

# 搜索 navigationToSendConfirm 的实现
rg -A 10 "navigationToSendConfirm.*=.*" packages/kit/src/

Length of output: 7085

packages/kit-bg/src/services/ServiceHistory.ts (1)

290-305: 代码实现清晰简洁!

新增的状态变更跟踪逻辑实现得很好,变量命名清晰,代码结构合理。

packages/kit-bg/src/vaults/base/VaultBase.ts (1)

79-79: 导入语句更新已确认

导入语句已正确更新为使用新的 IStakeTx 类型。

packages/kit-bg/src/services/ServiceStaking.ts (2)

17-20: 新增类型导入

添加了必要的类型导入,保持了代码的类型安全性。

Also applies to: 42-47, 54-57


828-828: 方法 buildEarnTx 参数类型更改

buildEarnTx 方法的参数 tx 类型从 IStakeTxResponse 更改为 IStakeTx。请确保所有调用此方法的代码已更新,以匹配新的参数类型,防止类型不匹配导致的错误。

运行以下脚本以验证方法的使用情况:

✅ Verification successful

Let me gather more information about the types and their usage.


参数类型变更是安全的

从代码分析结果来看,所有调用 buildEarnTx 的地方都是通过 stakeTx.tx 传参,而 stakeTx 的类型 IStakeTxResponse 中的 tx 属性就是 IStakeTx 类型。因此这个类型变更是向后兼容的,不会导致类型不匹配问题。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 描述:检查所有对 `buildEarnTx` 方法的调用,确保参数类型已更新。

# 使用 ripgrep 搜索 `buildEarnTx` 的调用,并显示上下文
rg -A 3 -B 1 'buildEarnTx\('

Length of output: 1677


Script:

#!/bin/bash
# 检查 IStakeTx 和 IStakeTxResponse 类型的定义和使用
ast-grep --pattern 'interface IStakeTx {
  $$$
}'

ast-grep --pattern 'interface IStakeTxResponse {
  $$$
}'

# 检查 stakeTx.tx 的类型和使用
rg -A 5 'stakeTx.*tx' 

Length of output: 1576


Script:

#!/bin/bash
# 检查 stakeTx 变量的类型定义
rg -B 5 -A 1 'const stakeTx'

# 检查 tx 属性的类型定义
ast-grep --pattern 'type $_ = {
  tx: $_
  $$$
}'

# 搜索可能的类型导入
rg 'import.*IStakeTx|import.*IStakeTxResponse'

Length of output: 2760

packages/kit-bg/src/services/ServiceHistory.ts Outdated Show resolved Hide resolved
packages/kit-bg/src/services/ServiceStaking.ts Outdated Show resolved Hide resolved
packages/kit-bg/src/services/ServiceStaking.ts Outdated Show resolved Hide resolved
@originalix originalix marked this pull request as ready for review November 11, 2024 08:15
@originalix originalix enabled auto-merge (squash) November 11, 2024 08:15
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Reviewing files that changed from the base of the PR and between 050d0c4 and 5d6e94a.

📒 Files selected for processing (6)
  • packages/kit-bg/src/services/ServiceHistory.ts (3 hunks)
  • packages/kit-bg/src/services/ServiceStaking.ts (8 hunks)
  • packages/kit/src/views/DAppConnection/hooks/useRiskDetection.ts (2 hunks)
  • packages/shared/src/logger/scopes/discovery/scenes/dapp.ts (2 hunks)
  • packages/shared/src/logger/scopes/staking/index.ts (1 hunks)
  • packages/shared/src/logger/scopes/staking/scenes/order.ts (1 hunks)
🔇 Additional comments (10)
packages/shared/src/logger/scopes/staking/index.ts (2)

4-4: 导入语句看起来不错!

导入语句遵循了与 PageScene 相同的模式,保持了代码的一致性。


12-12: 很好的实现!

order 场景的添加方式与现有的 page 场景保持一致,使用了相同的 createScene 工厂方法。这种一致性有助于代码的可维护性。

packages/shared/src/logger/scopes/staking/scenes/order.ts (1)

1-7: 导入路径验证

导入看起来结构清晰,但建议验证这些导入路径的正确性。

✅ Verification successful

导入路径验证完成,一切正常

导入路径已确认存在且正确:

  • @onekeyhq/kit-bg/src/dbs/simple/entity/SimpleDbEntityEarnOrders
  • @onekeyhq/shared/types/tx
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 验证导入路径是否存在
fd --type f "SimpleDbEntityEarnOrders.ts$" packages/kit-bg/src/dbs/simple/entity/
fd --type f "tx.ts$" packages/shared/types/

Length of output: 223

packages/shared/src/logger/scopes/discovery/scenes/dapp.ts (1)

1-1: 导入声明看起来不错!

导入语句清晰明确,并且仅引入所需的类型。

packages/kit/src/views/DAppConnection/hooks/useRiskDetection.ts (1)

6-6: 导入 defaultLogger 符合预期!

从共享模块导入 logger 是正确的做法,有助于保持日志记录的一致性。

packages/kit-bg/src/services/ServiceStaking.ts (1)

914-973: 代码逻辑清晰,方法实现正确

新增的 addEarnOrderupdateEarnOrderupdateEarnOrderStatusToServer 方法逻辑正确,实现合理。

packages/kit-bg/src/services/ServiceHistory.ts (4)

18-18: 新类型 IChangedPendingTxInfo 导入正确

引入了新的类型 IChangedPendingTxInfo,用于跟踪状态已改变的待处理交易。


290-290: 初始化 changedPendingTxInfos 数组

成功初始化了用于收集状态变化的待处理交易信息的数组。


297-305: 正确收集已确认的交易信息

在找到已确认的交易后,正确地将其信息添加到 changedPendingTxInfos 数组中,逻辑清晰。


309-314: 重复的建议:添加错误处理逻辑

之前的评论仍然适用,建议为异步操作添加错误处理,以捕获和处理可能的异常。

@originalix originalix merged commit ac67099 into x Nov 12, 2024
7 of 8 checks passed
@originalix originalix deleted the feat/earn-order branch November 12, 2024 03:05
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Reviewing files that changed from the base of the PR and between 5d6e94a and c63f1ec.

📒 Files selected for processing (1)
  • packages/kit-bg/src/vaults/base/VaultBase.ts (2 hunks)
🔇 Additional comments (1)
packages/kit-bg/src/vaults/base/VaultBase.ts (1)

79-79: 验证类型更改在代码库中的影响

需要确保 IStakeTx 类型的变更与其他文件保持一致。

✅ Verification successful

类型变更已经得到正确处理

通过代码分析可以确认:

  • IStakeTx 是一个联合类型,在 packages/shared/types/staking.ts 中定义
  • 所有使用 IStakeTx 的实现都已经正确处理了类型关系:
    • BTC Vault 使用 IStakeTxBtcBabylon
    • Cosmos Vault 使用 IStakeTxCosmosAmino
    • 基础 VaultBase 提供了默认实现

类型系统保持一致,无需额外修改。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 检查 IStakeTx 的使用情况
rg "IStakeTx" -A 3

# 检查可能受影响的质押相关文件
rg "buildStakeEncodedTx" -A 3

Length of output: 8523

Comment on lines +1132 to 1134
buildStakeEncodedTx(params: IStakeTx): Promise<IEncodedTx> {
return Promise.resolve(params as IEncodedTx);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

建议改进 buildStakeEncodedTx 方法的实现

当前实现存在以下问题:

  • 直接将输入参数转换为 IEncodedTx 类型,缺少必要的验证
  • 没有错误处理机制

建议按如下方式重构:

-  buildStakeEncodedTx(params: IStakeTx): Promise<IEncodedTx> {
-    return Promise.resolve(params as IEncodedTx);
-  }
+  buildStakeEncodedTx(params: IStakeTx): Promise<IEncodedTx> {
+    if (!params.amount || !params.contractAddress) {
+      throw new Error('无效的质押交易参数');
+    }
+    const encodedTx: IEncodedTx = {
+      ...params,
+      type: 'stake',
+    };
+    return Promise.resolve(encodedTx);
+  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
buildStakeEncodedTx(params: IStakeTx): Promise<IEncodedTx> {
return Promise.resolve(params as IEncodedTx);
}
buildStakeEncodedTx(params: IStakeTx): Promise<IEncodedTx> {
if (!params.amount || !params.contractAddress) {
throw new Error('无效的质押交易参数');
}
const encodedTx: IEncodedTx = {
...params,
type: 'stake',
};
return Promise.resolve(encodedTx);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants