diff --git a/packages/samples/sudt/src/controllers/account.controller.ts b/packages/samples/sudt/src/controllers/account.controller.ts index 28a71cda..b9bbb97c 100644 --- a/packages/samples/sudt/src/controllers/account.controller.ts +++ b/packages/samples/sudt/src/controllers/account.controller.ts @@ -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()) - 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, @@ -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 }, })), }, })), diff --git a/packages/samples/sudt/src/services/nervos.service.ts b/packages/samples/sudt/src/services/nervos.service.ts index 79f61500..918c0b9a 100644 --- a/packages/samples/sudt/src/services/nervos.service.ts +++ b/packages/samples/sudt/src/services/nervos.service.ts @@ -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 { @@ -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', }) } } @@ -49,34 +48,44 @@ export class NervosService { #filterTo = async (tx: Transaction, typeIds: string[]): Promise => tx.outputs.reduce((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)