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|btc)!: adapt org mempool updates in btc-assets-api #148

Merged
merged 7 commits into from
Apr 30, 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
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
run: pnpm run build:packages

- name: Run tests for packages
run: pnpm run test:ckb
run: pnpm run test:packages
env:
VITE_SERVICE_URL: ${{ secrets.SERVICE_URL }}
VITE_SERVICE_TOKEN: ${{ secrets.SERVICE_TOKEN }}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
],
"scripts": {
"prepare": "husky",
"test:ckb": "turbo run test --filter=./packages/ckb",
"test:packages": "turbo run test --filter=./packages/*",
"build": "turbo run build",
"build:packages": "turbo run build --filter=./packages/*",
Expand Down
2 changes: 0 additions & 2 deletions packages/btc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,6 @@ interface DataSource {
satoshi: number;
exceedSatoshi: number;
}>;
getRecommendedFeeRates(): Promise<FeesRecommended>;
getAverageFeeRate(): Promise<number>;
}
```

Expand Down
1 change: 0 additions & 1 deletion packages/btc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"dependencies": {
"@bitcoinerlab/secp256k1": "^1.1.1",
"@ckb-lumos/codec": "0.22.2",
"@mempool/mempool.js": "^2.3.0",
"@nervosnetwork/ckb-types": "^0.109.1",
"@rgbpp-sdk/ckb": "workspace:^",
"@rgbpp-sdk/service": "workspace:^",
Expand Down
41 changes: 0 additions & 41 deletions packages/btc/src/query/mempool.ts

This file was deleted.

30 changes: 8 additions & 22 deletions packages/btc/src/query/source.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { FeesRecommended } from '@mempool/mempool.js/lib/interfaces/bitcoin/fees';
import { BtcApiUtxoParams, BtcAssetsApi, BtcAssetsApiError, ErrorCodes as ServiceErrorCodes } from '@rgbpp-sdk/service';
import {
BtcApiRecommendedFeeRates,
BtcApiUtxoParams,
BtcAssetsApi,
BtcAssetsApiError,
ErrorCodes as ServiceErrorCodes,
} from '@rgbpp-sdk/service';
import { Output, Utxo } from '../transaction/utxo';
import { NetworkType } from '../preset/types';
import { ErrorCodes, TxBuildError } from '../error';
import { TxAddressOutput } from '../transaction/build';
import { isOpReturnScriptPubkey } from '../transaction/embed';
import { addressToScriptPublicKeyHex, getAddressType } from '../address';
import { createMempool, MempoolInstance } from './mempool';
import { remove0x } from '../utils';
import { TxAddressOutput } from '../transaction/build';

export class DataSource {
public service: BtcAssetsApi;
public networkType: NetworkType;
public mempool: MempoolInstance;

constructor(service: BtcAssetsApi, networkType: NetworkType) {
this.service = service;
this.networkType = networkType;
this.mempool = createMempool(networkType);
}

// Query a UTXO from the service.
Expand Down Expand Up @@ -158,22 +160,6 @@ export class DataSource {
};
}

// Get recommended fee rates from mempool.space.
// From fastest to slowest: fastestFee > halfHourFee > economyFee > hourFee > minimumFee
async getRecommendedFeeRates(): Promise<FeesRecommended> {
try {
return await this.mempool.bitcoin.fees.getFeesRecommended();
} catch (err: any) {
throw TxBuildError.withComment(ErrorCodes.MEMPOOL_API_RESPONSE_ERROR, err.message ?? JSON.stringify(err));
}
}

// Get the recommended average fee rate.
async getAverageFeeRate(): Promise<number> {
const fees = await this.getRecommendedFeeRates();
return fees.halfHourFee;
}

async getPaymasterOutput(): Promise<TxAddressOutput | undefined> {
try {
const paymasterInfo = await this.service.getRgbppPaymasterInfo();
Expand Down
3 changes: 2 additions & 1 deletion packages/btc/src/transaction/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ export class TxBuilder {
// The transaction is expected be confirmed within half an hour with the fee rate
let averageFeeRate: number | undefined;
if (!feeRate && !this.feeRate) {
averageFeeRate = await this.source.getAverageFeeRate();
const feeRates = await this.source.service.getBtcRecommendedFeeRates();
averageFeeRate = feeRates.fastestFee;
}

// Use the feeRate param if it is specified,
Expand Down
24 changes: 2 additions & 22 deletions packages/btc/tests/DataSource.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
import { describe, expect, it } from 'vitest';
import { source } from './shared/env';
import { service, source } from './shared/env';
import { ErrorCodes } from '../src';

describe('DataSource', () => {
it('Get recommended fee rates', async () => {
const fees = await source.getRecommendedFeeRates();

expect(fees).toBeDefined();
expect(fees.fastestFee).toBeTypeOf('number');
expect(fees.halfHourFee).toBeTypeOf('number');
expect(fees.hourFee).toBeTypeOf('number');
expect(fees.minimumFee).toBeTypeOf('number');

expect(fees.fastestFee).toBeGreaterThanOrEqual(fees.halfHourFee);
expect(fees.halfHourFee).toBeGreaterThanOrEqual(fees.hourFee);
expect(fees.hourFee).toBeGreaterThanOrEqual(fees.minimumFee);
});
it('Get average fee rate', async () => {
const [feeRates, averageFeeRate] = await Promise.all([source.getRecommendedFeeRates(), source.getAverageFeeRate()]);

expect(averageFeeRate).toBeTypeOf('number');
expect(feeRates.halfHourFee).toBeTypeOf('number');
expect(averageFeeRate).toEqual(feeRates.halfHourFee);
});
describe('DataSource', { retry: 3 }, () => {
it('Get OP_RETURN output via getOutput()', async () => {
const output = await source.getOutput('70b250e2a3cc7a33b47f7a4e94e41e1ee2501ce73b393d824db1dd4c872c5348', 0);

Expand Down
7 changes: 6 additions & 1 deletion packages/service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ interface BtcApis {
getBtcBlockHeaderByHash(blockHash: string): Promise<BtcApiBlockHeader>;
getBtcBlockHashByHeight(blockHeight: number): Promise<BtcApiBlockHash>;
getBtcBlockTransactionIdsByHash(blockHash: number): Promise<BtcApiBlockTransactionIds>;
getBtcRecommendedFeeRates(): Promise<BtcApiRecommendedFeeRates>;
getBtcBalance(address: string, params?: BtcApiBalanceParams): Promise<BtcApiBalance>;
getBtcUtxos(address: string, params?: BtcApiUtxoParams): Promise<BtcApiUtxo[]>;
getBtcTransactions(address: string): Promise<BtcApiTransaction[]>;
getBtcTransactions(address: string, params?: BtcApiTransactionParams): Promise<BtcApiTransaction[]>;
getBtcTransaction(txId: string): Promise<BtcApiTransaction>;
sendBtcTransaction(txHex: string): Promise<BtcApiSentTransaction>;
}
Expand Down Expand Up @@ -208,6 +209,10 @@ interface BtcApiSentTransaction {
txid: string;
}

export interface BtcApiTransactionParams {
after_txid?: string;
}

interface BtcApiTransaction {
txid: string;
version: number;
Expand Down
14 changes: 11 additions & 3 deletions packages/service/src/service/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
BtcApiTransaction,
BtcApiUtxo,
BtcApiUtxoParams,
RgbppApiTransactionStateParams,
BtcApiTransactionParams,
BtcApiRecommendedFeeRates,
} from '../types';
import {
RgbppApis,
Expand All @@ -24,6 +25,7 @@ import {
RgbppApiCkbTransactionHash,
RgbppApiAssetsByAddressParams,
RgbppApiRetryCkbTransactionPayload,
RgbppApiTransactionStateParams,
RgbppApiTransactionRetry,
} from '../types';

Expand Down Expand Up @@ -60,6 +62,10 @@ export class BtcAssetsApi extends BtcAssetsApiBase implements BtcApis, RgbppApis
return this.request<BtcApiBlockTransactionIds>(`/bitcoin/v1/block/${blockHash}/txids`);
}

getBtcRecommendedFeeRates() {
return this.request<BtcApiRecommendedFeeRates>(`/bitcoin/v1/fees/recommended`);
}

getBtcBalance(address: string, params?: BtcApiBalanceParams) {
return this.request<BtcApiBalance>(`/bitcoin/v1/address/${address}/balance`, {
params,
Expand All @@ -72,8 +78,10 @@ export class BtcAssetsApi extends BtcAssetsApiBase implements BtcApis, RgbppApis
});
}

getBtcTransactions(address: string) {
return this.request<BtcApiTransaction[]>(`/bitcoin/v1/address/${address}/txs`);
getBtcTransactions(address: string, params?: BtcApiTransactionParams) {
return this.request<BtcApiTransaction[]>(`/bitcoin/v1/address/${address}/txs`, {
params,
});
}

getBtcTransaction(txId: string) {
Expand Down
16 changes: 14 additions & 2 deletions packages/service/src/types/btc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ export interface BtcApis {
getBtcBlockHeaderByHash(blockHash: string): Promise<BtcApiBlockHeader>;
getBtcBlockHashByHeight(blockHeight: number): Promise<BtcApiBlockHash>;
getBtcBlockTransactionIdsByHash(blockHash: number): Promise<BtcApiBlockTransactionIds>;
getBtcRecommendedFeeRates(): Promise<BtcApiRecommendedFeeRates>;
getBtcBalance(address: string, params?: BtcApiBalanceParams): Promise<BtcApiBalance>;
getBtcUtxos(address: string, params?: BtcApiUtxoParams): Promise<BtcApiUtxo[]>;
getBtcTransactions(address: string): Promise<BtcApiTransaction[]>;
getBtcTransactions(address: string, params?: BtcApiTransactionParams): Promise<BtcApiTransaction[]>;
getBtcTransaction(txId: string): Promise<BtcApiTransaction>;
sendBtcTransaction(txHex: string): Promise<BtcApiSentTransaction>;
}

export interface BtcApiBlockchainInfo {
chain: string;
blocks: number;
headers: number;
bestblockhash: number;
difficulty: number;
mediantime: number;
Expand Down Expand Up @@ -48,6 +48,14 @@ export interface BtcApiBlockTransactionIds {
txids: string[];
}

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

export interface BtcApiBalanceParams {
min_satoshi?: number;
}
Expand Down Expand Up @@ -79,6 +87,10 @@ export interface BtcApiSentTransaction {
txid: string;
}

export interface BtcApiTransactionParams {
after_txid?: string;
}

export interface BtcApiTransaction {
txid: string;
version: number;
Expand Down
Loading