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(service): support BtcAssetsApi.getRgbppApiBalanceByAddress() #222

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/silly-rules-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rgbpp-sdk/service": minor
---

Add BtcAssetsApi.getRgbppApiBalanceByAddress() API for querying RGBPP XUDT balances by a BTC address
8 changes: 8 additions & 0 deletions packages/service/src/service/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
RgbppApiSendCkbTransactionPayload,
RgbppApiCkbTransactionHash,
RgbppApiAssetsByAddressParams,
RgbppApiBalanceByAddressParams,
RgbppApiBalance,
RgbppApiRetryCkbTransactionPayload,
RgbppApiTransactionStateParams,
RgbppApiTransactionRetry,
Expand Down Expand Up @@ -128,6 +130,12 @@ export class BtcAssetsApi extends BtcAssetsApiBase implements BtcApis, RgbppApis
});
}

getRgbppBalanceByBtcAddress(btcAddress: string, params?: RgbppApiBalanceByAddressParams) {
return this.request<RgbppApiBalance>(`/rgbpp/v1/address/${btcAddress}/balance`, {
params,
});
}

getRgbppSpvProof(btcTxId: string, confirmations: number) {
return this.request<RgbppApiSpvProof>('/rgbpp/v1/btc-spv/proof', {
params: {
Expand Down
19 changes: 19 additions & 0 deletions packages/service/src/types/rgbpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface RgbppApis {
getRgbppAssetsByBtcTxId(btcTxId: string): Promise<Cell[]>;
getRgbppAssetsByBtcUtxo(btcTxId: string, vout: number): Promise<Cell[]>;
getRgbppAssetsByBtcAddress(btcAddress: string, params?: RgbppApiAssetsByAddressParams): Promise<Cell[]>;
getRgbppBalanceByBtcAddress(btcAddress: string, params?: RgbppApiBalanceByAddressParams): Promise<RgbppApiBalance>;
getRgbppSpvProof(btcTxId: string, confirmations: number): Promise<RgbppApiSpvProof>;
sendRgbppCkbTransaction(payload: RgbppApiSendCkbTransactionPayload): Promise<RgbppApiTransactionState>;
retryRgbppCkbTransaction(payload: RgbppApiRetryCkbTransactionPayload): Promise<RgbppApiTransactionRetry>;
Expand Down Expand Up @@ -47,6 +48,24 @@ export interface RgbppApiAssetsByAddressParams {
no_cache?: boolean;
}

export interface RgbppApiBalanceByAddressParams {
type_script?: string;
no_cache?: boolean;
}
export interface RgbppApiBalance {
address: string;
xudt: RgbppApiXudtBalance[];
}
export interface RgbppApiXudtBalance {
name: string;
decimal: number;
symbol: string;
total_amount: string;
available_amount: string;
pending_amount: string;
type_hash: string;
}

export interface RgbppApiSpvProof {
proof: string;
spv_client: {
Expand Down
18 changes: 13 additions & 5 deletions packages/service/tests/Service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,20 @@ describe(
expectCell(cell);
}
});
it('getRgbppAssetsByBtcAddress() with no_cache', async () => {
const res = await service.getRgbppAssetsByBtcAddress(rgbppBtcAddress, {
no_cache: true,
});
it('getRgbppBalanceByBtcAddress()', async () => {
const res = await service.getRgbppBalanceByBtcAddress(rgbppBtcAddress);
expect(res).toBeDefined();
expect(res.length).toBeGreaterThan(0);
expect(res.address).toBeTypeOf('string');
expect(res.xudt.length).toBeGreaterThan(0);
for (const xudt of res.xudt) {
expect(xudt.name).toBeTypeOf('string');
expect(xudt.decimal).toBeTypeOf('number');
expect(xudt.symbol).toBeTypeOf('string');
expect(xudt.total_amount).toBeTypeOf('string');
expect(xudt.available_amount).toBeTypeOf('string');
expect(xudt.pending_amount).toBeTypeOf('string');
expect(xudt.type_hash).toBeTypeOf('string');
}
});
it('getRgbppSpvProof()', async () => {
const res = await service.getRgbppSpvProof(rgbppBtcTxId, 6);
Expand Down