-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement query field resolvers to list the address's assets/ba…
…lances (#168) Merge pull request #168 from utxostack/145-feat-a-query-interface-to-list-the-coins-held-by-an-address
- Loading branch information
Showing
5 changed files
with
107 additions
and
23 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
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,83 @@ | ||
import { BI } from '@ckb-lumos/bi'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { CkbExplorerService } from 'src/core/ckb-explorer/ckb-explorer.service'; | ||
import { PrismaService } from 'src/core/database/prisma/prisma.service'; | ||
import { CkbCell, CkbXUDTInfo } from 'src/modules/ckb/cell/cell.model'; | ||
import { RgbppAsset } from '../asset/asset.model'; | ||
import { CkbRpcWebsocketService } from 'src/core/ckb-rpc/ckb-rpc-websocket.service'; | ||
import { CKB_CHAIN_ID } from 'src/constants'; | ||
|
||
@Injectable() | ||
export class RgbppAddressService { | ||
constructor( | ||
private prismaService: PrismaService, | ||
private ckbExplorerService: CkbExplorerService, | ||
private ckbRpcService: CkbRpcWebsocketService, | ||
) { } | ||
|
||
public async getAddressAssets(address: string) { | ||
const lockScript = await this.prismaService.lockScript.findMany({ | ||
where: { | ||
ownerAddress: address, | ||
chainId: CKB_CHAIN_ID, | ||
}, | ||
}); | ||
if (lockScript.length === 0) { | ||
return []; | ||
} | ||
const results = await this.prismaService.asset.findMany({ | ||
where: { | ||
lockScriptHash: { | ||
in: lockScript.map((script) => script.scriptHash), | ||
}, | ||
isLive: true, | ||
}, | ||
}); | ||
if (results.length === 0) { | ||
return []; | ||
} | ||
const outputMap = results.reduce( | ||
(map, { txHash, index }) => { | ||
if (map[txHash] === undefined) { | ||
map[txHash] = []; | ||
} | ||
map[txHash].push(index); | ||
return map; | ||
}, | ||
{} as Record<string, string[]>, | ||
); | ||
const assets = await Promise.all( | ||
Object.keys(outputMap).map(async (txHash) => { | ||
const tx = await this.ckbRpcService.getTransaction(txHash); | ||
return outputMap[txHash].map((index) => | ||
RgbppAsset.fromTransaction(address, tx.transaction, BI.from(index).toNumber()), | ||
); | ||
}), | ||
); | ||
return assets.flat(); | ||
} | ||
|
||
public async getAddressBalances(address: string): Promise<CkbXUDTInfo[]> { | ||
const result = await this.prismaService.holder.findMany({ | ||
where: { | ||
address, | ||
}, | ||
orderBy: { | ||
assetAmount: 'desc', | ||
}, | ||
}); | ||
const balances = await Promise.all( | ||
result.map(async ({ typeScriptHash, assetAmount }) => { | ||
const info = await this.ckbExplorerService.getXUDT(typeScriptHash); | ||
const xudtInfo: CkbXUDTInfo = { | ||
symbol: info.data.attributes.symbol, | ||
decimal: BI.from(info.data.attributes.decimal).toNumber(), | ||
typeHash: typeScriptHash, | ||
amount: assetAmount.toHex(), | ||
}; | ||
return xudtInfo; | ||
}), | ||
); | ||
return balances; | ||
} | ||
} |
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