-
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: impl rgbpp assets holders resolve field
- Loading branch information
Showing
6 changed files
with
181 additions
and
7 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 |
---|---|---|
@@ -1,9 +1,27 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { Logger, Module } from '@nestjs/common'; | ||
import { CkbExplorerModule } from 'src/core/ckb-explorer/ckb-explorer.module'; | ||
import { RgbppStatisticResolver } from './statistic.resolver'; | ||
import { RgbppStatisticService } from './statistic.service'; | ||
import { CkbRpcModule } from 'src/core/ckb-rpc/ckb-rpc.module'; | ||
import { BitcoinApiModule } from 'src/core/bitcoin-api/bitcoin-api.module'; | ||
import { Cron, CronExpression } from '@nestjs/schedule'; | ||
|
||
@Module({ | ||
imports: [CkbExplorerModule], | ||
providers: [RgbppStatisticResolver], | ||
imports: [CkbExplorerModule, CkbRpcModule, BitcoinApiModule], | ||
providers: [RgbppStatisticResolver, RgbppStatisticService], | ||
exports: [RgbppStatisticService], | ||
}) | ||
export class RgbppStatisticModule {} | ||
export class RgbppStatisticModule { | ||
private readonly logger = new Logger(RgbppStatisticModule.name); | ||
|
||
constructor( | ||
private rgbppStatisticService: RgbppStatisticService, | ||
) {} | ||
|
||
@Cron(CronExpression.EVERY_10_MINUTES) | ||
public async collectRgbppAssetsHoldersCronTask() { | ||
this.logger.log('Collecting RGBPP assets holders...'); | ||
const holders = await this.rgbppStatisticService.collectRgbppAssetsHolders(); | ||
await this.rgbppStatisticService.setRgbppAssetsHolders(holders); | ||
} | ||
} |
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
104 changes: 104 additions & 0 deletions
104
backend/src/modules/rgbpp/statistic/statistic.service.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,104 @@ | ||
import { BI } from '@ckb-lumos/bi'; | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { NetworkType } from '@rgbpp-sdk/btc'; | ||
import { getRgbppLockScript, remove0x, RGBPPLock } from '@rgbpp-sdk/ckb'; | ||
import { BtcNetworkTypeMap } from 'src/constants'; | ||
import { BitcoinApiService } from 'src/core/bitcoin-api/bitcoin-api.service'; | ||
import { CkbExplorerService } from 'src/core/ckb-explorer/ckb-explorer.service'; | ||
import { CkbRpcWebsocketService } from 'src/core/ckb-rpc/ckb-rpc-websocket.service'; | ||
import { Env } from 'src/env'; | ||
import * as pLimit from 'p-limit'; | ||
import { bytes } from '@ckb-lumos/lumos/codec'; | ||
import { CACHE_MANAGER, Cache } from '@nestjs/cache-manager'; | ||
import { TEN_MINUTES_MS } from 'src/common/date'; | ||
|
||
const limit = pLimit(100); | ||
|
||
@Injectable() | ||
export class RgbppStatisticService { | ||
private holdersCacheKey = `RgbppStatisticService:holders`; | ||
|
||
constructor( | ||
private configService: ConfigService<Env>, | ||
private bitcoinApiService: BitcoinApiService, | ||
private ckbExplorerService: CkbExplorerService, | ||
private ckbRpcService: CkbRpcWebsocketService, | ||
@Inject(CACHE_MANAGER) protected cacheManager: Cache, | ||
) { } | ||
|
||
public async getRgbppAssetsHolders() { | ||
const cached = await this.cacheManager.get<string[]>(this.holdersCacheKey); | ||
if (cached) { | ||
return cached; | ||
} | ||
const holders = await this.collectRgbppAssetsHolders(); | ||
await this.setRgbppAssetsHolders(holders); | ||
return holders; | ||
} | ||
|
||
public async setRgbppAssetsHolders(holders: string[]) { | ||
await this.cacheManager.set(this.holdersCacheKey, holders, TEN_MINUTES_MS); | ||
}; | ||
|
||
public async collectRgbppAssetsHolders() { | ||
const network = this.configService.get('NETWORK'); | ||
const rgbppLock = getRgbppLockScript( | ||
network === NetworkType.MAINNET, | ||
BtcNetworkTypeMap[network], | ||
); | ||
const rgbppTxs = await this.ckbExplorerService.getRgbppTransactions(); | ||
const cells = await this.ckbRpcService.getCells( | ||
{ | ||
script: { | ||
code_hash: rgbppLock.codeHash, | ||
hash_type: rgbppLock.hashType, | ||
args: '0x', | ||
}, | ||
script_type: 'lock', | ||
}, | ||
'desc', | ||
BI.from(rgbppTxs.meta.total).toHexString(), | ||
); | ||
|
||
const utxos = cells.objects | ||
.map((cell) => { | ||
const { args } = cell.output.lock; | ||
try { | ||
const unpack = RGBPPLock.unpack(args); | ||
const btcTxid = bytes.hexify(bytes.bytify(unpack.btcTxid).reverse()); | ||
return { | ||
outIndex: unpack.outIndex, | ||
btcTxid: remove0x(btcTxid), | ||
}; | ||
} catch { | ||
return null; | ||
} | ||
}) | ||
.filter((utxo) => utxo !== null); | ||
|
||
const txidSet = new Set(utxos.map((utxo) => utxo.btcTxid)); | ||
const txs = await Promise.allSettled( | ||
Array.from(txidSet).map((txid) => | ||
limit(async () => { | ||
const tx = await this.bitcoinApiService.getTx({ txid }); | ||
return tx; | ||
}), | ||
), | ||
); | ||
|
||
const holdersSet = txs.reduce((set, tx) => { | ||
if (tx.status === 'fulfilled') { | ||
const { vout } = tx.value; | ||
vout.forEach((output) => { | ||
const { scriptpubkey } = output; | ||
if (scriptpubkey) { | ||
set.add(scriptpubkey); | ||
} | ||
}); | ||
} | ||
return set; | ||
}, new Set<string>()); | ||
return Array.from(holdersSet); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.