-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OK-33669: Add order data to staking (#6159)
* feat: Earn orders db * feat: Update earn orders * feat: Withdraw and Claim add to order * feat: Staking tx speed up * feat: Add log
- Loading branch information
1 parent
4073f60
commit ac67099
Showing
13 changed files
with
397 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
packages/kit-bg/src/dbs/simple/entity/SimpleDbEntityEarnOrders.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { backgroundMethod } from '@onekeyhq/shared/src/background/backgroundDecorators'; | ||
import type { EDecodedTxStatus } from '@onekeyhq/shared/types/tx'; | ||
|
||
import { SimpleDbEntityBase } from '../base/SimpleDbEntityBase'; | ||
|
||
export interface IEarnOrderItem { | ||
orderId: string; | ||
networkId: string; | ||
txId: string; | ||
previousTxIds: string[]; | ||
status: EDecodedTxStatus; | ||
updatedAt: number; | ||
createdAt: number; | ||
} | ||
|
||
export interface IEarnOrderDBStructure { | ||
data: Record<string, IEarnOrderItem>; | ||
txIdToOrderIdMap: Record<string, string>; | ||
} | ||
|
||
export type IAddEarnOrderParams = Omit< | ||
IEarnOrderItem, | ||
'updatedAt' | 'createdAt' | 'previousTxIds' | ||
>; | ||
|
||
export class SimpleDbEntityEarnOrders extends SimpleDbEntityBase<IEarnOrderDBStructure> { | ||
entityName = 'earnOrders'; | ||
|
||
override enableCache = false; | ||
|
||
@backgroundMethod() | ||
async addOrder(order: IAddEarnOrderParams) { | ||
await this.setRawData(({ rawData }) => { | ||
const data: IEarnOrderDBStructure = { | ||
data: { ...(rawData?.data || {}) }, | ||
txIdToOrderIdMap: { ...(rawData?.txIdToOrderIdMap || {}) }, | ||
}; | ||
const now = Date.now(); | ||
data.data[order.orderId] = { | ||
...order, | ||
previousTxIds: [], | ||
updatedAt: now, | ||
createdAt: now, | ||
}; | ||
data.txIdToOrderIdMap[order.txId] = order.orderId; | ||
return data; | ||
}); | ||
} | ||
|
||
@backgroundMethod() | ||
async updateOrderStatusByTxId(params: { | ||
currentTxId: string; | ||
newTxId?: string; | ||
status: EDecodedTxStatus; | ||
}): Promise<{ | ||
success: boolean; | ||
order?: IEarnOrderItem; | ||
}> { | ||
const { currentTxId, newTxId, status } = params; | ||
|
||
await this.setRawData(({ rawData }) => { | ||
const data: IEarnOrderDBStructure = { | ||
data: { ...(rawData?.data || {}) }, | ||
txIdToOrderIdMap: { ...(rawData?.txIdToOrderIdMap || {}) }, | ||
}; | ||
|
||
const orderId = data.txIdToOrderIdMap[currentTxId]; | ||
if (!orderId) { | ||
return data; | ||
} | ||
|
||
const order = data.data[orderId]; | ||
if (!order) { | ||
return data; | ||
} | ||
|
||
// Update txId related info if new txId exists | ||
if (newTxId && newTxId !== currentTxId) { | ||
// Store old txId in history | ||
const previousTxIds = [...(order.previousTxIds || [])]; | ||
previousTxIds.push(currentTxId); | ||
|
||
// Update txId mapping | ||
delete data.txIdToOrderIdMap[currentTxId]; | ||
data.txIdToOrderIdMap[newTxId] = orderId; | ||
|
||
// Update order details | ||
data.data[orderId] = { | ||
...order, | ||
txId: newTxId, | ||
previousTxIds, | ||
status, | ||
updatedAt: Date.now(), | ||
}; | ||
} else { | ||
// Update status only | ||
data.data[orderId] = { | ||
...order, | ||
status, | ||
updatedAt: Date.now(), | ||
}; | ||
} | ||
|
||
return data; | ||
}); | ||
|
||
// Get updated order info | ||
const updatedOrder = await this.getOrderByTxId(newTxId || currentTxId); | ||
return { | ||
success: updatedOrder?.status === status, | ||
order: updatedOrder, | ||
}; | ||
} | ||
|
||
@backgroundMethod() | ||
async getOrderByTxId(txId: string): Promise<IEarnOrderItem | undefined> { | ||
const rawData = await this.getRawData(); | ||
const orderId = rawData?.txIdToOrderIdMap?.[txId]; | ||
return orderId ? rawData?.data?.[orderId] : undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.