Skip to content

Commit 3ee71df

Browse files
authored
feat(service): support /rgbpp/v1/address/{btc_address}/activity (#250)
* feat: support rgbpp activity API in the service lib * fix: type of BtcApiBlockchainInfo.bestblockhash should be string * fix: type of RgbppApiActivity in the README of the service lib
1 parent db31727 commit 3ee71df

File tree

6 files changed

+156
-24
lines changed

6 files changed

+156
-24
lines changed

.changeset/odd-cheetahs-shake.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@rgbpp-sdk/service": minor
3+
---
4+
5+
Add support of /rgbpp/v1/address/{btc_address}/activity API for querying RGBPP asset activities by an BTC address

packages/service/README.md

+85-13
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ interface BaseApiRequestOptions extends RequestInit {
161161
params?: Record<string, any>;
162162
method?: 'GET' | 'POST';
163163
requireToken?: boolean;
164+
allow404?: boolean;
164165
}
165166

166167
interface BtcAssetsApiToken {
@@ -188,7 +189,6 @@ interface BtcApis {
188189
interface BtcApiBlockchainInfo {
189190
chain: string;
190191
blocks: number;
191-
headers: number;
192192
bestblockhash: number;
193193
difficulty: number;
194194
mediantime: number;
@@ -222,11 +222,18 @@ interface BtcApiBlockTransactionIds {
222222
txids: string[];
223223
}
224224

225+
interface BtcApiRecommendedFeeRates {
226+
fastestFee: number;
227+
halfHourFee: number;
228+
hourFee: number;
229+
economyFee: number;
230+
minimumFee: number;
231+
}
232+
225233
interface BtcApiBalanceParams {
226234
min_satoshi?: number;
227235
no_cache?: boolean;
228236
}
229-
230237
interface BtcApiBalance {
231238
address: string;
232239
// @deprecated Use available_satoshi instead
@@ -245,7 +252,6 @@ interface BtcApiUtxoParams {
245252
min_satoshi?: number;
246253
no_cache?: boolean;
247254
}
248-
249255
interface BtcApiUtxo {
250256
txid: string;
251257
vout: number;
@@ -262,7 +268,7 @@ interface BtcApiSentTransaction {
262268
txid: string;
263269
}
264270

265-
export interface BtcApiTransactionParams {
271+
interface BtcApiTransactionParams {
266272
after_txid?: string;
267273
}
268274

@@ -312,9 +318,11 @@ interface RgbppApis {
312318
getRgbppPaymasterInfo(): Promise<RgbppApiPaymasterInfo>;
313319
getRgbppTransactionHash(btcTxId: string): Promise<RgbppApiCkbTransactionHash>;
314320
getRgbppTransactionState(btcTxId: string): Promise<RgbppApiTransactionState>;
315-
getRgbppAssetsByBtcTxId(btcTxId: string): Promise<Cell[]>;
316-
getRgbppAssetsByBtcUtxo(btcTxId: string, vout: number): Promise<Cell[]>;
317-
getRgbppAssetsByBtcAddress(btcAddress: string, params?: RgbppApiAssetsByAddressParams): Promise<Cell[]>;
321+
getRgbppAssetsByBtcTxId(btcTxId: string): Promise<RgbppCell[]>;
322+
getRgbppAssetsByBtcUtxo(btcTxId: string, vout: number): Promise<RgbppCell[]>;
323+
getRgbppAssetsByBtcAddress(btcAddress: string, params?: RgbppApiAssetsByAddressParams): Promise<RgbppCell[]>;
324+
getRgbppBalanceByBtcAddress(btcAddress: string, params?: RgbppApiBalanceByAddressParams): Promise<RgbppApiBalance>;
325+
getRgbppActivityByBtcAddress(btcAddress: string, params?: RgbppApiActivityByAddressParams): Promise<RgbppApiActivity>;
318326
getRgbppSpvProof(btcTxId: string, confirmations: number): Promise<RgbppApiSpvProof>;
319327
sendRgbppCkbTransaction(payload: RgbppApiSendCkbTransactionPayload): Promise<RgbppApiTransactionState>;
320328
retryRgbppCkbTransaction(payload: RgbppApiRetryCkbTransactionPayload): Promise<RgbppApiTransactionRetry>;
@@ -331,15 +339,76 @@ interface RgbppApiCkbTransactionHash {
331339
txhash: string;
332340
}
333341

342+
interface RgbppApiTransactionStateParams {
343+
with_data?: boolean;
344+
}
345+
334346
interface RgbppApiTransactionState {
335347
state: RgbppTransactionState;
348+
attempts: number;
349+
failedReason?: string;
350+
data?: {
351+
txid: string;
352+
ckbVirtualResult: {
353+
ckbRawTx: CKBComponents.RawTransaction;
354+
needPaymasterCell: boolean;
355+
sumInputsCapacity: string;
356+
commitment: string;
357+
};
358+
};
359+
}
360+
361+
interface RgbppCell extends Cell {
362+
typeHash?: Hash;
336363
}
337364

338365
interface RgbppApiAssetsByAddressParams {
339366
type_script?: string;
340367
no_cache?: boolean;
341368
}
342369

370+
interface RgbppApiBalanceByAddressParams {
371+
type_script?: string;
372+
no_cache?: boolean;
373+
}
374+
interface RgbppApiBalance {
375+
address: string;
376+
xudt: RgbppApiXudtBalance[];
377+
}
378+
interface RgbppApiXudtBalance {
379+
name: string;
380+
decimal: number;
381+
symbol: string;
382+
total_amount: string;
383+
available_amount: string;
384+
pending_amount: string;
385+
type_hash: string;
386+
type_script: Script;
387+
}
388+
389+
interface RgbppApiActivityByAddressParams {
390+
rgbpp_only?: boolean;
391+
type_script?: string;
392+
after_btc_txid?: string;
393+
}
394+
interface RgbppApiActivity {
395+
address: string;
396+
cursor: string;
397+
txs: {
398+
btcTx: BtcApiTransaction;
399+
isRgbpp: boolean;
400+
isomorphicTx?: {
401+
ckbVirtualTx?: CKBComponents.RawTransaction;
402+
ckbTx?: CKBComponents.Transaction;
403+
inputs?: CKBComponents.CellOutput[];
404+
outputs?: CKBComponents.CellOutput[];
405+
status: {
406+
confirmed: boolean;
407+
};
408+
};
409+
}[];
410+
}
411+
343412
interface RgbppApiSpvProof {
344413
proof: string;
345414
spv_client: {
@@ -350,12 +419,14 @@ interface RgbppApiSpvProof {
350419

351420
interface RgbppApiSendCkbTransactionPayload {
352421
btc_txid: string;
353-
ckb_virtual_result: {
354-
ckbRawTx: CKBComponents.RawTransaction;
355-
needPaymasterCell: boolean;
356-
sumInputsCapacity: string;
357-
commitment: string;
358-
};
422+
// Support ckbVirtualTxResult and it's JSON string as request parameter
423+
ckb_virtual_result: RgbppApiSendCkbVirtualResult | string;
424+
}
425+
interface RgbppApiSendCkbVirtualResult {
426+
ckbRawTx: CKBComponents.RawTransaction;
427+
needPaymasterCell: boolean;
428+
sumInputsCapacity: string;
429+
commitment: string;
359430
}
360431

361432
interface RgbppApiRetryCkbTransactionPayload {
@@ -366,4 +437,5 @@ interface RgbppApiTransactionRetry {
366437
success: boolean;
367438
state: RgbppTransactionState;
368439
}
440+
369441
```

packages/service/src/service/service.ts

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
BtcApiUtxoParams,
1515
BtcApiTransactionParams,
1616
BtcApiRecommendedFeeRates,
17+
RgbppApiActivityByAddressParams,
18+
RgbppApiActivity,
1719
} from '../types';
1820
import {
1921
RgbppApis,
@@ -136,6 +138,12 @@ export class BtcAssetsApi extends BtcAssetsApiBase implements BtcApis, RgbppApis
136138
});
137139
}
138140

141+
getRgbppActivityByBtcAddress(btcAddress: string, params?: RgbppApiActivityByAddressParams) {
142+
return this.request<RgbppApiActivity>(`/rgbpp/v1/address/${btcAddress}/activity`, {
143+
params,
144+
});
145+
}
146+
139147
getRgbppSpvProof(btcTxId: string, confirmations: number) {
140148
return this.request<RgbppApiSpvProof>('/rgbpp/v1/btc-spv/proof', {
141149
params: {

packages/service/src/types/btc.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface BtcApis {
1515
export interface BtcApiBlockchainInfo {
1616
chain: string;
1717
blocks: number;
18-
bestblockhash: number;
18+
bestblockhash: string;
1919
difficulty: number;
2020
mediantime: number;
2121
}

packages/service/src/types/rgbpp.ts

+33-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Cell, Hash, Script } from '@ckb-lumos/base';
2+
import { BtcApiTransaction } from './btc';
23

34
export interface RgbppApis {
45
getRgbppPaymasterInfo(): Promise<RgbppApiPaymasterInfo>;
@@ -8,6 +9,7 @@ export interface RgbppApis {
89
getRgbppAssetsByBtcUtxo(btcTxId: string, vout: number): Promise<RgbppCell[]>;
910
getRgbppAssetsByBtcAddress(btcAddress: string, params?: RgbppApiAssetsByAddressParams): Promise<RgbppCell[]>;
1011
getRgbppBalanceByBtcAddress(btcAddress: string, params?: RgbppApiBalanceByAddressParams): Promise<RgbppApiBalance>;
12+
getRgbppActivityByBtcAddress(btcAddress: string, params?: RgbppApiActivityByAddressParams): Promise<RgbppApiActivity>;
1113
getRgbppSpvProof(btcTxId: string, confirmations: number): Promise<RgbppApiSpvProof>;
1214
sendRgbppCkbTransaction(payload: RgbppApiSendCkbTransactionPayload): Promise<RgbppApiTransactionState>;
1315
retryRgbppCkbTransaction(payload: RgbppApiRetryCkbTransactionPayload): Promise<RgbppApiTransactionRetry>;
@@ -71,6 +73,29 @@ export interface RgbppApiXudtBalance {
7173
type_script: Script;
7274
}
7375

76+
export interface RgbppApiActivityByAddressParams {
77+
rgbpp_only?: boolean;
78+
type_script?: string;
79+
after_btc_txid?: string;
80+
}
81+
export interface RgbppApiActivity {
82+
address: string;
83+
cursor: string;
84+
txs: {
85+
btcTx: BtcApiTransaction;
86+
isRgbpp: boolean;
87+
isomorphicTx?: {
88+
ckbVirtualTx?: CKBComponents.RawTransaction;
89+
ckbTx?: CKBComponents.Transaction;
90+
inputs?: CKBComponents.CellOutput[];
91+
outputs?: CKBComponents.CellOutput[];
92+
status: {
93+
confirmed: boolean;
94+
};
95+
};
96+
}[];
97+
}
98+
7499
export interface RgbppApiSpvProof {
75100
proof: string;
76101
spv_client: {
@@ -81,15 +106,14 @@ export interface RgbppApiSpvProof {
81106

82107
export interface RgbppApiSendCkbTransactionPayload {
83108
btc_txid: string;
84-
// Support ckbVirtaulTxResult and it's JSON string as request parameter
85-
ckb_virtual_result:
86-
| {
87-
ckbRawTx: CKBComponents.RawTransaction;
88-
needPaymasterCell: boolean;
89-
sumInputsCapacity: string;
90-
commitment: string;
91-
}
92-
| string;
109+
// Support ckbVirtualTxResult and it's JSON string as request parameter
110+
ckb_virtual_result: RgbppApiSendCkbVirtualResult | string;
111+
}
112+
export interface RgbppApiSendCkbVirtualResult {
113+
ckbRawTx: CKBComponents.RawTransaction;
114+
needPaymasterCell: boolean;
115+
sumInputsCapacity: string;
116+
commitment: string;
93117
}
94118

95119
export interface RgbppApiRetryCkbTransactionPayload {

packages/service/tests/Service.test.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ describe(
146146
});
147147
it('getBtcTransactions()', async () => {
148148
const res = await service.getBtcTransactions(btcAddress);
149-
console.log(res.map((tx) => tx.txid));
150149
expect(Array.isArray(res)).toBe(true);
151150
expect(res.length).toBeGreaterThan(0);
152151
res.forEach((transaction) => {
@@ -277,6 +276,30 @@ describe(
277276
expectScript(xudt.type_script);
278277
}
279278
});
279+
it('getRgbppActivityByBtcAddress()', async () => {
280+
const res = await service.getRgbppActivityByBtcAddress(rgbppBtcAddress, {
281+
type_script: rgbppCellType,
282+
});
283+
expect(res).toBeDefined();
284+
expect(res.address).toBeTypeOf('string');
285+
expect(res.cursor).toBeTypeOf('string');
286+
expect(res.txs).toHaveProperty('length');
287+
if (res.txs.length > 0) {
288+
for (const tx of res.txs) {
289+
expect(tx.btcTx).toBeDefined();
290+
expect(tx.isRgbpp).toBeTypeOf('boolean');
291+
if (tx.isRgbpp) {
292+
expect(tx.isomorphicTx).toBeDefined();
293+
expect(tx.isomorphicTx.status.confirmed).toBeTypeOf('boolean');
294+
const hasTxOrVirtualTx = tx.isomorphicTx.ckbVirtualTx ?? tx.isomorphicTx.ckbTx;
295+
if (hasTxOrVirtualTx) {
296+
expect(tx.isomorphicTx.inputs).toBeDefined();
297+
expect(tx.isomorphicTx.outputs).toBeDefined();
298+
}
299+
}
300+
}
301+
}
302+
});
280303
it('getRgbppSpvProof()', async () => {
281304
const res = await service.getRgbppSpvProof(rgbppBtcTxId, 6);
282305
expect(res).toBeDefined();

0 commit comments

Comments
 (0)