Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement query field resolvers to list the address's assets/balances #168

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add rgbpp address balances field
ahonn committed Sep 19, 2024
commit f067117795f1036574f34b207aa30006bdd9acb2
3 changes: 2 additions & 1 deletion backend/src/modules/rgbpp/address/address.module.ts
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import { CkbRpcModule } from 'src/core/ckb-rpc/ckb-rpc.module';
import { RgbppModule } from '../rgbpp.module';
import { CkbTransactionModule } from 'src/modules/ckb/transaction/transaction.module';
import { BullModule } from '@nestjs/bullmq';
import { RgbppAddressService } from './address.service';

@Module({
imports: [
@@ -22,6 +23,6 @@ import { BullModule } from '@nestjs/bullmq';
},
}),
],
providers: [RgbppAddressResolver],
providers: [RgbppAddressResolver, RgbppAddressService],
})
export class RgbppAddressModule {}
21 changes: 8 additions & 13 deletions backend/src/modules/rgbpp/address/address.resolver.ts
Original file line number Diff line number Diff line change
@@ -4,15 +4,14 @@ import { RgbppAddress } from './address.model';
import { ParentField } from 'src/decorators/parent-field.decorator';
import { RgbppAsset } from '../asset/asset.model';
import { CkbXUDTInfo } from 'src/modules/ckb/cell/cell.model';
import { Loader } from 'src/common/dataloader';
import {
CkbExplorerTransactionLoader,
CkbExplorerTransactionLoaderType,
} from 'src/modules/ckb/transaction/transaction.dataloader';
import { RgbppAddressService } from './address.service';

@Resolver(() => RgbppAddress)
export class RgbppAddressResolver {
constructor(private ckbExplorerService: CkbExplorerService) { }
constructor(
private ckbExplorerService: CkbExplorerService,
private rgbppAddressService: RgbppAddressService,
) {}

@Query(() => RgbppAddress, { name: 'rgbppAddress', nullable: true })
public async getBtcAddress(@Args('address') address: string): Promise<RgbppAddress> {
@@ -35,16 +34,12 @@ export class RgbppAddressResolver {

@ResolveField(() => [RgbppAsset])
public async assets(@ParentField('address') address: string): Promise<RgbppAsset[]> {
// TODO: implement this
return [];
}

@ResolveField(() => [CkbXUDTInfo])
public async balances(
@ParentField('address') address: string,
@Loader(CkbExplorerTransactionLoader) explorerTxLoader: CkbExplorerTransactionLoaderType,
): Promise<(CkbXUDTInfo | null)[]> {
// TODO: implement this
return [];
public async balances(@ParentField('address') address: string): Promise<(CkbXUDTInfo | null)[]> {
const balances = await this.rgbppAddressService.getAddressBalances(address);
return balances;
}
}
34 changes: 34 additions & 0 deletions backend/src/modules/rgbpp/address/address.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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 { CkbXUDTInfo } from 'src/modules/ckb/cell/cell.model';

@Injectable()
export class RgbppAddressService {
constructor(
private prismaService: PrismaService,
private ckbExplorerService: CkbExplorerService,
) { }

public async getAddressBalances(address: string): Promise<CkbXUDTInfo[]> {
const result = await this.prismaService.holder.findMany({
where: {
address,
},
});
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;
}
}