Skip to content

Commit

Permalink
fix: history api amount bug & history tx duplicated bug
Browse files Browse the repository at this point in the history
  • Loading branch information
PainterPuppets committed Dec 31, 2023
1 parent 86dfdf2 commit d2529c8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
21 changes: 8 additions & 13 deletions packages/samples/sudt/src/controllers/account.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,22 @@ export class AccountController extends BaseController {
async accountTransaction(
@Param('address') address: string,
@Query('size') size: number,
@Query('typeId') typeId: string,
@Query('lastCursor') lastCursor?: string,
) {
const tokens = await this._dataSource.getRepository(Token).find()

if (tokens.length === 0) {
return { lastCursor: '', history: [] }
}

const tokenMap = tokens.reduce((acc, cur) => {
acc.set(cur.typeId, cur)
return acc
}, new Map<string, Token>())

const history = await this._nervosService.fetchTransferHistory(
getLock(address),
tokens.map((token) => token.typeId),
size,
const history = await this._nervosService.fetchTransferHistory({
lockScript: getLock(address),
typeIds: tokens.map((token) => token.typeId),
sizeLimit: size,
lastCursor,
)

console.log(history)
})

return {
...history,
Expand All @@ -77,11 +72,11 @@ export class AccountController extends BaseController {
...{
from: tx.from.map((from) => ({
...from,
...{ typeId: from.token, token: tokenMap.get(from.token) ?? '' },
...{ typeId: from.typeId, token: from.typeId ? tokenMap.get(from.typeId) ?? undefined : undefined },
})),
to: tx.to.map((to) => ({
...to,
...{ typeId: to.token, token: tokenMap.get(to.token) ?? '' },
...{ typeId: to.typeId, token: to.typeId ? tokenMap.get(to.typeId) ?? undefined : undefined },
})),
},
})),
Expand Down
39 changes: 24 additions & 15 deletions packages/samples/sudt/src/services/nervos.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { BI, Indexer, RPC, Script, Transaction, utils } from '@ckb-lumos/lumos'
import { encodeToAddress } from '@ckb-lumos/helpers'
import { number } from '@ckb-lumos/codec'

export interface Transfer {
address: string
token: string
typeId?: string
ckb: string
amount: string
amount?: string
}

export class NervosService {
Expand All @@ -23,22 +24,20 @@ export class NervosService {
const txIndex = parseInt(input.previousOutput.index, 16)
const previousOutput = previousTransaction.transaction.outputs[txIndex]
if (previousOutput.type) {
const typeHash = utils.computeScriptHash(previousOutput.type)
if (typeIds.find((typeId) => typeHash === typeId)) {
const typeId = utils.computeScriptHash(previousOutput.type)
if (typeIds.find((i) => typeId === i)) {
const previousOutputData = previousTransaction.transaction.outputsData[txIndex]
from.push({
address: encodeToAddress(previousOutput.lock),
token: typeHash,
typeId,
ckb: previousOutput.capacity,
amount: BI.from(previousOutputData).toString(),
amount: BI.from(number.Uint128LE.unpack(previousOutputData.slice(0, 34))).toString(),
})
}
} else {
from.push({
address: encodeToAddress(previousOutput.lock),
token: '',
ckb: previousOutput.capacity,
amount: '0',
})
}
}
Expand All @@ -49,34 +48,44 @@ export class NervosService {
#filterTo = async (tx: Transaction, typeIds: string[]): Promise<Transfer[]> =>
tx.outputs.reduce<Transfer[]>((acc, cur, key) => {
if (cur.type) {
const typeHash = utils.computeScriptHash(cur.type)
if (typeIds.find((typeId) => typeHash === typeId)) {
const typeId = utils.computeScriptHash(cur.type)
if (typeIds.find((i) => typeId === i)) {
acc.push({
address: encodeToAddress(cur.lock),
token: typeHash,
typeId,
ckb: cur.capacity,
amount: tx.outputsData[key],
amount: BI.from(number.Uint128LE.unpack(tx.outputsData[key].slice(0, 34))).toString(),
})
}
} else {
acc.push({
address: encodeToAddress(cur.lock),
token: 'CKB',
ckb: cur.capacity,
amount: '0',
})
}
return acc
}, [])

fetchTransferHistory = async (lockScript: Script, typeIds: string[], sizeLimit: number, lastCursor?: string) => {
fetchTransferHistory = async ({
lockScript,
typeIds,
sizeLimit,
lastCursor,
}: {
lockScript: Script
typeIds: string[]
sizeLimit: number
lastCursor?: string
}) => {
const txs = await this.#indexer.getTransactions(
{
script: lockScript,
scriptType: 'lock',
groupByTransaction: true,
},
{ order: 'desc', sizeLimit, lastCursor },
)

const history = await Promise.all(
txs.objects.map(async ({ txHash }) => {
const { transaction } = await this.#rpc.getTransaction(txHash)
Expand Down

0 comments on commit d2529c8

Please sign in to comment.