Skip to content

Commit

Permalink
feat: support rgbpp activity API in the service lib, adapting btc-ass…
Browse files Browse the repository at this point in the history
…ets-api#182
  • Loading branch information
ShookLyngs committed Jul 11, 2024
1 parent e23abaa commit 599fd8a
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-cheetahs-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rgbpp-sdk/service": minor
---

Add support of /rgbpp/v1/address/{btc_address}/activity API for querying RGBPP asset activities by an BTC address, adapting btc-assets-api#182
98 changes: 85 additions & 13 deletions packages/service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ interface BaseApiRequestOptions extends RequestInit {
params?: Record<string, any>;
method?: 'GET' | 'POST';
requireToken?: boolean;
allow404?: boolean;
}

interface BtcAssetsApiToken {
Expand Down Expand Up @@ -191,7 +192,6 @@ interface BtcApis {
interface BtcApiBlockchainInfo {
chain: string;
blocks: number;
headers: number;
bestblockhash: number;
difficulty: number;
mediantime: number;
Expand Down Expand Up @@ -225,11 +225,18 @@ interface BtcApiBlockTransactionIds {
txids: string[];
}

interface BtcApiRecommendedFeeRates {
fastestFee: number;
halfHourFee: number;
hourFee: number;
economyFee: number;
minimumFee: number;
}

interface BtcApiBalanceParams {
min_satoshi?: number;
no_cache?: boolean;
}

interface BtcApiBalance {
address: string;
// @deprecated Use available_satoshi instead
Expand All @@ -248,7 +255,6 @@ interface BtcApiUtxoParams {
min_satoshi?: number;
no_cache?: boolean;
}

interface BtcApiUtxo {
txid: string;
vout: number;
Expand All @@ -265,7 +271,7 @@ interface BtcApiSentTransaction {
txid: string;
}

export interface BtcApiTransactionParams {
interface BtcApiTransactionParams {
after_txid?: string;
}

Expand Down Expand Up @@ -315,9 +321,11 @@ interface RgbppApis {
getRgbppPaymasterInfo(): Promise<RgbppApiPaymasterInfo>;
getRgbppTransactionHash(btcTxId: string): Promise<RgbppApiCkbTransactionHash>;
getRgbppTransactionState(btcTxId: string): Promise<RgbppApiTransactionState>;
getRgbppAssetsByBtcTxId(btcTxId: string): Promise<Cell[]>;
getRgbppAssetsByBtcUtxo(btcTxId: string, vout: number): Promise<Cell[]>;
getRgbppAssetsByBtcAddress(btcAddress: string, params?: RgbppApiAssetsByAddressParams): Promise<Cell[]>;
getRgbppAssetsByBtcTxId(btcTxId: string): Promise<RgbppCell[]>;
getRgbppAssetsByBtcUtxo(btcTxId: string, vout: number): Promise<RgbppCell[]>;
getRgbppAssetsByBtcAddress(btcAddress: string, params?: RgbppApiAssetsByAddressParams): Promise<RgbppCell[]>;
getRgbppBalanceByBtcAddress(btcAddress: string, params?: RgbppApiBalanceByAddressParams): Promise<RgbppApiBalance>;
getRgbppActivityByBtcAddress(btcAddress: string, params?: RgbppApiActivityByAddressParams): Promise<RgbppApiActivity>;
getRgbppSpvProof(btcTxId: string, confirmations: number): Promise<RgbppApiSpvProof>;
sendRgbppCkbTransaction(payload: RgbppApiSendCkbTransactionPayload): Promise<RgbppApiTransactionState>;
retryRgbppCkbTransaction(payload: RgbppApiRetryCkbTransactionPayload): Promise<RgbppApiTransactionRetry>;
Expand All @@ -334,15 +342,76 @@ interface RgbppApiCkbTransactionHash {
txhash: string;
}

interface RgbppApiTransactionStateParams {
with_data?: boolean;
}

interface RgbppApiTransactionState {
state: RgbppTransactionState;
attempts: number;
failedReason?: string;
data?: {
txid: string;
ckbVirtualResult: {
ckbRawTx: CKBComponents.RawTransaction;
needPaymasterCell: boolean;
sumInputsCapacity: string;
commitment: string;
};
};
}

interface RgbppCell extends Cell {
typeHash?: Hash;
}

interface RgbppApiAssetsByAddressParams {
type_script?: string;
no_cache?: boolean;
}

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

interface RgbppApiActivityByAddressParams {
rgbpp_only?: boolean;
type_script?: string;
after_btc_txid?: string;
}
interface RgbppApiActivity {
address: string;
cursor: string;
txs: {
btcTx: BtcApiTransaction;
isRgbpp: boolean;
isomorphicTx?: {
ckbRawTx?: CKBComponents.RawTransaction;
ckbTx?: CKBComponents.Transaction;
inputs?: CKBComponents.CellOutput[];
outputs?: CKBComponents.CellOutput[];
status: {
confirmed: boolean;
};
};
}[];
}

interface RgbppApiSpvProof {
proof: string;
spv_client: {
Expand All @@ -353,12 +422,14 @@ interface RgbppApiSpvProof {

interface RgbppApiSendCkbTransactionPayload {
btc_txid: string;
ckb_virtual_result: {
ckbRawTx: CKBComponents.RawTransaction;
needPaymasterCell: boolean;
sumInputsCapacity: string;
commitment: string;
};
// Support ckbVirtualTxResult and it's JSON string as request parameter
ckb_virtual_result: RgbppApiSendCkbVirtualResult | string;
}
interface RgbppApiSendCkbVirtualResult {
ckbRawTx: CKBComponents.RawTransaction;
needPaymasterCell: boolean;
sumInputsCapacity: string;
commitment: string;
}

interface RgbppApiRetryCkbTransactionPayload {
Expand All @@ -369,4 +440,5 @@ interface RgbppApiTransactionRetry {
success: boolean;
state: RgbppTransactionState;
}

```
8 changes: 8 additions & 0 deletions packages/service/src/service/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
BtcApiUtxoParams,
BtcApiTransactionParams,
BtcApiRecommendedFeeRates,
RgbppApiActivityByAddressParams,
RgbppApiActivity,
} from '../types';
import {
RgbppApis,
Expand Down Expand Up @@ -136,6 +138,12 @@ export class BtcAssetsApi extends BtcAssetsApiBase implements BtcApis, RgbppApis
});
}

getRgbppActivityByBtcAddress(btcAddress: string, params?: RgbppApiActivityByAddressParams) {
return this.request<RgbppApiActivity>(`/rgbpp/v1/address/${btcAddress}/activity`, {
params,
});
}

getRgbppSpvProof(btcTxId: string, confirmations: number) {
return this.request<RgbppApiSpvProof>('/rgbpp/v1/btc-spv/proof', {
params: {
Expand Down
42 changes: 33 additions & 9 deletions packages/service/src/types/rgbpp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Cell, Hash, Script } from '@ckb-lumos/base';
import { BtcApiTransaction } from './btc';

export interface RgbppApis {
getRgbppPaymasterInfo(): Promise<RgbppApiPaymasterInfo>;
Expand All @@ -8,6 +9,7 @@ export interface RgbppApis {
getRgbppAssetsByBtcUtxo(btcTxId: string, vout: number): Promise<RgbppCell[]>;
getRgbppAssetsByBtcAddress(btcAddress: string, params?: RgbppApiAssetsByAddressParams): Promise<RgbppCell[]>;
getRgbppBalanceByBtcAddress(btcAddress: string, params?: RgbppApiBalanceByAddressParams): Promise<RgbppApiBalance>;
getRgbppActivityByBtcAddress(btcAddress: string, params?: RgbppApiActivityByAddressParams): Promise<RgbppApiActivity>;
getRgbppSpvProof(btcTxId: string, confirmations: number): Promise<RgbppApiSpvProof>;
sendRgbppCkbTransaction(payload: RgbppApiSendCkbTransactionPayload): Promise<RgbppApiTransactionState>;
retryRgbppCkbTransaction(payload: RgbppApiRetryCkbTransactionPayload): Promise<RgbppApiTransactionRetry>;
Expand Down Expand Up @@ -71,6 +73,29 @@ export interface RgbppApiXudtBalance {
type_script: Script;
}

export interface RgbppApiActivityByAddressParams {
rgbpp_only?: boolean;
type_script?: string;
after_btc_txid?: string;
}
export interface RgbppApiActivity {
address: string;
cursor: string;
txs: {
btcTx: BtcApiTransaction;
isRgbpp: boolean;
isomorphicTx?: {
ckbRawTx?: CKBComponents.RawTransaction;
ckbTx?: CKBComponents.Transaction;
inputs?: CKBComponents.CellOutput[];
outputs?: CKBComponents.CellOutput[];
status: {
confirmed: boolean;
};
};
}[];
}

export interface RgbppApiSpvProof {
proof: string;
spv_client: {
Expand All @@ -81,15 +106,14 @@ export interface RgbppApiSpvProof {

export interface RgbppApiSendCkbTransactionPayload {
btc_txid: string;
// Support ckbVirtaulTxResult and it's JSON string as request parameter
ckb_virtual_result:
| {
ckbRawTx: CKBComponents.RawTransaction;
needPaymasterCell: boolean;
sumInputsCapacity: string;
commitment: string;
}
| string;
// Support ckbVirtualTxResult and it's JSON string as request parameter
ckb_virtual_result: RgbppApiSendCkbVirtualResult | string;
}
export interface RgbppApiSendCkbVirtualResult {
ckbRawTx: CKBComponents.RawTransaction;
needPaymasterCell: boolean;
sumInputsCapacity: string;
commitment: string;
}

export interface RgbppApiRetryCkbTransactionPayload {
Expand Down
21 changes: 21 additions & 0 deletions packages/service/tests/Service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ describe(
for (const cell of res) {
expectRgbppCell(cell);
}
console.log(res);
});
it('getRgbppBalanceByBtcAddress()', async () => {
const res = await service.getRgbppBalanceByBtcAddress(rgbppBtcAddress);
Expand All @@ -277,6 +278,26 @@ describe(
expectScript(xudt.type_script);
}
});
it('getRgbppActivityByBtcAddress()', async () => {
const res = await service.getRgbppActivityByBtcAddress(rgbppBtcAddress, {
type_script: rgbppCellType,
});
expect(res).toBeDefined();
expect(res.address).toBeTypeOf('string');
expect(res.cursor).toBeTypeOf('string');
expect(res.txs).toHaveProperty('length');
if (res.txs.length > 0) {
for (const tx of res.txs) {
expect(tx.btcTx).toBeDefined();
expect(tx.isRgbpp).toBeTypeOf('boolean');
if (tx.isRgbpp) {
expect(tx.isomorphicTx).toBeDefined();
expect(tx.isomorphicTx.status.confirmed).toBeTypeOf('boolean');
}
}
}
console.log(res);
});
it('getRgbppSpvProof()', async () => {
const res = await service.getRgbppSpvProof(rgbppBtcTxId, 6);
expect(res).toBeDefined();
Expand Down

0 comments on commit 599fd8a

Please sign in to comment.