Skip to content

Commit f3ffaf4

Browse files
authored
Merge pull request #283 from ckb-cell/ref/sdk-service-rpc
refactor(rgbpp|service): Add `send_rgbpp_group_txs` RPC to SDK Service
2 parents 82d37ab + 04bf42a commit f3ffaf4

File tree

6 files changed

+50
-48
lines changed

6 files changed

+50
-48
lines changed

.changeset/rotten-carrots-raise.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'rgbpp': minor
3+
---
4+
5+
refactor: update response of sending RGB++ group txs

apps/service/src/rgbpp/rgbpp.service.ts

+26-17
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ import { Inject } from '@nestjs/common';
22
import { DataSource, NetworkType } from 'rgbpp/btc';
33
import { BtcAssetsApi, RgbppApiTransactionState } from 'rgbpp/service';
44
import { BTCTestnetType, Collector, Hex, append0x } from 'rgbpp/ckb';
5-
import { buildRgbppTransferTx, buildRgbppTransferAllTxs } from 'rgbpp';
5+
import {
6+
buildRgbppTransferTx,
7+
buildRgbppTransferAllTxs,
8+
sendRgbppTxGroups,
9+
RgbppTxGroup,
10+
SentRgbppTxGroup,
11+
} from 'rgbpp';
612
import { RpcHandler, RpcMethodHandler } from '../json-rpc/json-rpc.decorators.js';
713
import { toSnakeCase, toCamelCase, SnakeCased } from '../utils/case.js';
8-
import { ensureSafeJson } from '../utils/json.js';
914
import {
1015
RgbppTransferReq,
1116
RgbppCkbBtcTransaction,
@@ -14,7 +19,7 @@ import {
1419
RgbppCkbTxHashReq,
1520
BtcTxSendReq,
1621
RgbppTransferAllReq,
17-
RgbppTransferAllRes,
22+
RgbppTransferAllResp,
1823
} from './types.js';
1924

2025
@RpcHandler()
@@ -57,9 +62,11 @@ export class RgbppService {
5762
}
5863

5964
@RpcMethodHandler({ name: 'generate_rgbpp_transfer_all_txs' })
60-
public async generateRgbppTransferAllTxs(request: [RgbppTransferAllReq]): Promise<SnakeCased<RgbppTransferAllRes>> {
65+
public async generateRgbppTransferAllTxs(
66+
request: [RgbppTransferAllReq],
67+
): Promise<SnakeCased<RgbppTransferAllResp>[]> {
6168
const params = toCamelCase(request[0]);
62-
const result = await buildRgbppTransferAllTxs({
69+
const { transactions } = await buildRgbppTransferAllTxs({
6370
ckb: {
6471
collector: this.ckbCollector,
6572
xudtTypeArgs: params.ckb.xudtTypeArgs,
@@ -78,18 +85,12 @@ export class RgbppService {
7885
isMainnet: this.isMainnet,
7986
});
8087

81-
return ensureSafeJson<SnakeCased<RgbppTransferAllRes>>(
82-
toSnakeCase<RgbppTransferAllRes>({
83-
...result,
84-
transactions: result.transactions.map((group) => {
85-
return {
86-
...group,
87-
ckb: {
88-
...group.ckb,
89-
virtualTxResult: JSON.stringify(group.ckb.virtualTxResult),
90-
},
91-
};
92-
}),
88+
return transactions.map(({ ckb, btc }) =>
89+
toSnakeCase<RgbppTransferAllResp>({
90+
ckbVirtualTxResult: JSON.stringify(ckb.virtualTxResult),
91+
btcPsbtHex: btc.psbtHex,
92+
btcFeeRate: btc.feeRate,
93+
btcFee: btc.fee,
9394
}),
9495
);
9596
}
@@ -127,4 +128,12 @@ export class RgbppService {
127128
const { txid } = await this.btcAssetsApi.sendBtcTransaction(txHex);
128129
return txid;
129130
}
131+
132+
@RpcMethodHandler({ name: 'send_rgbpp_group_txs' })
133+
public async sendRgbppGroupTxs(request: [RgbppTxGroup[]]): Promise<SnakeCased<SentRgbppTxGroup>[]> {
134+
const txGroups = toCamelCase(request[0]);
135+
const sentGroups = await sendRgbppTxGroups({ txGroups, btcService: this.btcAssetsApi });
136+
const result = sentGroups.map((group) => toSnakeCase<SentRgbppTxGroup>(group));
137+
return result;
138+
}
130139
}

apps/service/src/rgbpp/types.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { RgbppTransferAllTxGroup, RgbppTransferAllTxsResult } from 'rgbpp';
21
import { AddressToPubkeyMap } from 'rgbpp/btc';
32
import { Hex } from 'rgbpp/ckb';
43

@@ -72,12 +71,9 @@ export interface RgbppTransferAllReq {
7271
};
7372
}
7473

75-
export interface RgbppTransferAllRes extends Omit<RgbppTransferAllTxsResult, 'transactions'> {
76-
transactions: RgbppTransferAllGroupWithStringCkbVtx[];
77-
}
78-
79-
export interface RgbppTransferAllGroupWithStringCkbVtx extends Omit<RgbppTransferAllTxGroup, 'ckb'> {
80-
ckb: Omit<RgbppTransferAllTxGroup['ckb'], 'virtualTxResult'> & {
81-
virtualTxResult: string;
82-
};
74+
export interface RgbppTransferAllResp {
75+
ckbVirtualTxResult: string;
76+
btcPsbtHex: string;
77+
btcFeeRate: number;
78+
btcFee: number;
8379
}

examples/rgbpp/xudt/btc-transfer-all/1-btc-transfer-all.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const rgbppTransferAllTxs = async ({ xudtTypeArgs, fromAddress, toAddress }: Tes
3535
const psbt = bitcoin.Psbt.fromHex(group.btc.psbtHex);
3636

3737
// Sign transactions
38-
await signPsbt(psbt, btcAccount);
38+
signPsbt(psbt, btcAccount);
3939

4040
psbt.finalizeAllInputs();
4141

packages/rgbpp/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export type {
6666
RgbppTransferAllTxGroup,
6767
} from './rgbpp/types/xudt';
6868
export type { TransactionGroupSummary } from './rgbpp/summary/asset-summarizer';
69+
export type { RgbppTxGroup, SentRgbppTxGroup } from './rgbpp/utils/transaction';
6970
export { RgbppError, RgbppErrorCodes } from './rgbpp/error';
7071
export { buildRgbppTransferTx } from './rgbpp/xudt/btc-transfer';
7172
export { buildRgbppTransferAllTxs } from './rgbpp/xudt/btc-transfer-all';

packages/rgbpp/src/rgbpp/utils/transaction.ts

+12-21
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
import { BaseCkbVirtualTxResult } from '@rgbpp-sdk/ckb';
2-
import { BtcAssetsApi } from '@rgbpp-sdk/service';
2+
import { BtcAssetsApi, BtcAssetsApiError } from '@rgbpp-sdk/service';
33

44
export interface RgbppTxGroup {
55
ckbVirtualTxResult: BaseCkbVirtualTxResult | string;
66
btcTxHex: string;
77
}
88

99
export interface SentRgbppTxGroup {
10-
btc: {
11-
txId?: string;
12-
error?: Error | unknown;
13-
};
14-
ckb: {
15-
error?: Error | unknown;
16-
};
10+
btcTxId?: string;
11+
error?: string;
1712
}
1813

1914
export async function sendRgbppTxGroups(props: {
@@ -22,25 +17,21 @@ export async function sendRgbppTxGroups(props: {
2217
}): Promise<SentRgbppTxGroup[]> {
2318
const results: SentRgbppTxGroup[] = [];
2419
for (const group of props.txGroups) {
25-
const result: SentRgbppTxGroup = {
26-
ckb: {},
27-
btc: {},
28-
};
29-
try {
30-
const sent = await props.btcService.sendBtcTransaction(group.btcTxHex);
31-
result.btc.txId = sent.txid;
32-
} catch (e) {
33-
result.btc.error = e;
34-
}
3520
try {
21+
const { txid } = await props.btcService.sendBtcTransaction(group.btcTxHex);
3622
await props.btcService.sendRgbppCkbTransaction({
37-
btc_txid: result.btc.txId!,
23+
btc_txid: txid,
3824
ckb_virtual_result: group.ckbVirtualTxResult,
3925
});
26+
results.push({ btcTxId: txid });
4027
} catch (e) {
41-
result.ckb.error = e;
28+
console.error(e);
29+
if (e instanceof BtcAssetsApiError) {
30+
results.push({ error: e.message });
31+
} else {
32+
results.push({ error: 'Sending the RGB++ group transactions failed' });
33+
}
4234
}
43-
results.push(result);
4435
}
4536

4637
return results;

0 commit comments

Comments
 (0)