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 /rgbpp/v1/address/{btc_address}/activity #250

Merged
merged 3 commits into from
Aug 28, 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/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
98 changes: 85 additions & 13 deletions packages/service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ interface BaseApiRequestOptions extends RequestInit {
params?: Record<string, any>;
method?: 'GET' | 'POST';
requireToken?: boolean;
allow404?: boolean;
}

interface BtcAssetsApiToken {
Expand Down Expand Up @@ -188,7 +189,6 @@ interface BtcApis {
interface BtcApiBlockchainInfo {
chain: string;
blocks: number;
headers: number;
bestblockhash: number;
ShookLyngs marked this conversation as resolved.
Show resolved Hide resolved
difficulty: number;
mediantime: number;
Expand Down Expand Up @@ -222,11 +222,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 @@ -245,7 +252,6 @@ interface BtcApiUtxoParams {
min_satoshi?: number;
no_cache?: boolean;
}

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

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

Expand Down Expand Up @@ -312,9 +318,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 @@ -331,15 +339,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?: {
ckbVirtualTx?: CKBComponents.RawTransaction;
ckbTx?: CKBComponents.Transaction;
inputs?: CKBComponents.CellOutput[];
outputs?: CKBComponents.CellOutput[];
ShookLyngs marked this conversation as resolved.
Show resolved Hide resolved
status: {
confirmed: boolean;
};
};
}[];
}

interface RgbppApiSpvProof {
proof: string;
spv_client: {
Expand All @@ -350,12 +419,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 @@ -366,4 +437,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
2 changes: 1 addition & 1 deletion packages/service/src/types/btc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface BtcApis {
export interface BtcApiBlockchainInfo {
chain: string;
blocks: number;
bestblockhash: number;
bestblockhash: string;
difficulty: number;
mediantime: number;
}
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?: {
ckbVirtualTx?: 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
25 changes: 24 additions & 1 deletion packages/service/tests/Service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ describe(
});
it('getBtcTransactions()', async () => {
const res = await service.getBtcTransactions(btcAddress);
console.log(res.map((tx) => tx.txid));
expect(Array.isArray(res)).toBe(true);
expect(res.length).toBeGreaterThan(0);
res.forEach((transaction) => {
Expand Down Expand Up @@ -277,6 +276,30 @@ 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');
const hasTxOrVirtualTx = tx.isomorphicTx.ckbVirtualTx ?? tx.isomorphicTx.ckbTx;
if (hasTxOrVirtualTx) {
expect(tx.isomorphicTx.inputs).toBeDefined();
expect(tx.isomorphicTx.outputs).toBeDefined();
}
}
}
}
});
it('getRgbppSpvProof()', async () => {
const res = await service.getRgbppSpvProof(rgbppBtcTxId, 6);
expect(res).toBeDefined();
Expand Down