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

refactor(rgbpp|service): Add send_rgbpp_group_txs RPC to SDK Service #283

Merged
merged 7 commits into from
Aug 19, 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/rotten-carrots-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'rgbpp': minor
---

refactor: update response of sending RGB++ group txs
43 changes: 26 additions & 17 deletions apps/service/src/rgbpp/rgbpp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import { Inject } from '@nestjs/common';
import { DataSource, NetworkType } from 'rgbpp/btc';
import { BtcAssetsApi, RgbppApiTransactionState } from 'rgbpp/service';
import { BTCTestnetType, Collector, Hex, append0x } from 'rgbpp/ckb';
import { buildRgbppTransferTx, buildRgbppTransferAllTxs } from 'rgbpp';
import {
buildRgbppTransferTx,
buildRgbppTransferAllTxs,
sendRgbppTxGroups,
RgbppTxGroup,
SentRgbppTxGroup,
} from 'rgbpp';
import { RpcHandler, RpcMethodHandler } from '../json-rpc/json-rpc.decorators.js';
import { toSnakeCase, toCamelCase, SnakeCased } from '../utils/case.js';
import { ensureSafeJson } from '../utils/json.js';
import {
RgbppTransferReq,
RgbppCkbBtcTransaction,
Expand All @@ -14,7 +19,7 @@ import {
RgbppCkbTxHashReq,
BtcTxSendReq,
RgbppTransferAllReq,
RgbppTransferAllRes,
RgbppTransferAllResp,
} from './types.js';

@RpcHandler()
Expand Down Expand Up @@ -57,9 +62,11 @@ export class RgbppService {
}

@RpcMethodHandler({ name: 'generate_rgbpp_transfer_all_txs' })
public async generateRgbppTransferAllTxs(request: [RgbppTransferAllReq]): Promise<SnakeCased<RgbppTransferAllRes>> {
public async generateRgbppTransferAllTxs(
request: [RgbppTransferAllReq],
): Promise<SnakeCased<RgbppTransferAllResp>[]> {
const params = toCamelCase(request[0]);
const result = await buildRgbppTransferAllTxs({
const { transactions } = await buildRgbppTransferAllTxs({
ckb: {
collector: this.ckbCollector,
xudtTypeArgs: params.ckb.xudtTypeArgs,
Expand All @@ -78,18 +85,12 @@ export class RgbppService {
isMainnet: this.isMainnet,
});

return ensureSafeJson<SnakeCased<RgbppTransferAllRes>>(
toSnakeCase<RgbppTransferAllRes>({
...result,
transactions: result.transactions.map((group) => {
return {
...group,
ckb: {
...group.ckb,
virtualTxResult: JSON.stringify(group.ckb.virtualTxResult),
},
};
}),
return transactions.map(({ ckb, btc }) =>
toSnakeCase<RgbppTransferAllResp>({
ckbVirtualTxResult: JSON.stringify(ckb.virtualTxResult),
btcPsbtHex: btc.psbtHex,
btcFeeRate: btc.feeRate,
btcFee: btc.fee,
}),
);
}
Expand Down Expand Up @@ -127,4 +128,12 @@ export class RgbppService {
const { txid } = await this.btcAssetsApi.sendBtcTransaction(txHex);
return txid;
}

@RpcMethodHandler({ name: 'send_rgbpp_group_txs' })
public async sendRgbppGroupTxs(request: [RgbppTxGroup[]]): Promise<SnakeCased<SentRgbppTxGroup>[]> {
const txGroups = toCamelCase(request[0]);
const sentGroups = await sendRgbppTxGroups({ txGroups, btcService: this.btcAssetsApi });
const result = sentGroups.map((group) => toSnakeCase<SentRgbppTxGroup>(group));
return result;
}
}
14 changes: 5 additions & 9 deletions apps/service/src/rgbpp/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { RgbppTransferAllTxGroup, RgbppTransferAllTxsResult } from 'rgbpp';
import { AddressToPubkeyMap } from 'rgbpp/btc';
import { Hex } from 'rgbpp/ckb';

Expand Down Expand Up @@ -72,12 +71,9 @@ export interface RgbppTransferAllReq {
};
}

export interface RgbppTransferAllRes extends Omit<RgbppTransferAllTxsResult, 'transactions'> {
transactions: RgbppTransferAllGroupWithStringCkbVtx[];
}

export interface RgbppTransferAllGroupWithStringCkbVtx extends Omit<RgbppTransferAllTxGroup, 'ckb'> {
ckb: Omit<RgbppTransferAllTxGroup['ckb'], 'virtualTxResult'> & {
virtualTxResult: string;
};
export interface RgbppTransferAllResp {
ckbVirtualTxResult: string;
btcPsbtHex: string;
btcFeeRate: number;
btcFee: number;
}
2 changes: 1 addition & 1 deletion examples/rgbpp/xudt/btc-transfer-all/1-btc-transfer-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const rgbppTransferAllTxs = async ({ xudtTypeArgs, fromAddress, toAddress }: Tes
const psbt = bitcoin.Psbt.fromHex(group.btc.psbtHex);

// Sign transactions
await signPsbt(psbt, btcAccount);
signPsbt(psbt, btcAccount);

psbt.finalizeAllInputs();

Expand Down
1 change: 1 addition & 0 deletions packages/rgbpp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export type {
RgbppTransferAllTxGroup,
} from './rgbpp/types/xudt';
export type { TransactionGroupSummary } from './rgbpp/summary/asset-summarizer';
export type { RgbppTxGroup, SentRgbppTxGroup } from './rgbpp/utils/transaction';
export { RgbppError, RgbppErrorCodes } from './rgbpp/error';
export { buildRgbppTransferTx } from './rgbpp/xudt/btc-transfer';
export { buildRgbppTransferAllTxs } from './rgbpp/xudt/btc-transfer-all';
Expand Down
33 changes: 12 additions & 21 deletions packages/rgbpp/src/rgbpp/utils/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { BaseCkbVirtualTxResult } from '@rgbpp-sdk/ckb';
import { BtcAssetsApi } from '@rgbpp-sdk/service';
import { BtcAssetsApi, BtcAssetsApiError } from '@rgbpp-sdk/service';

export interface RgbppTxGroup {
ckbVirtualTxResult: BaseCkbVirtualTxResult | string;
btcTxHex: string;
}

export interface SentRgbppTxGroup {
btc: {
txId?: string;
error?: Error | unknown;
};
ckb: {
error?: Error | unknown;
};
btcTxId?: string;
error?: string;
ShookLyngs marked this conversation as resolved.
Show resolved Hide resolved
}

export async function sendRgbppTxGroups(props: {
Expand All @@ -22,25 +17,21 @@ export async function sendRgbppTxGroups(props: {
}): Promise<SentRgbppTxGroup[]> {
const results: SentRgbppTxGroup[] = [];
for (const group of props.txGroups) {
const result: SentRgbppTxGroup = {
ckb: {},
btc: {},
};
try {
const sent = await props.btcService.sendBtcTransaction(group.btcTxHex);
result.btc.txId = sent.txid;
} catch (e) {
result.btc.error = e;
}
try {
const { txid } = await props.btcService.sendBtcTransaction(group.btcTxHex);
await props.btcService.sendRgbppCkbTransaction({
btc_txid: result.btc.txId!,
btc_txid: txid,
ckb_virtual_result: group.ckbVirtualTxResult,
});
results.push({ btcTxId: txid });
} catch (e) {
result.ckb.error = e;
console.error(e);
if (e instanceof BtcAssetsApiError) {
results.push({ error: e.message });
} else {
results.push({ error: 'Sending the RGB++ group transactions failed' });
ShookLyngs marked this conversation as resolved.
Show resolved Hide resolved
}
}
results.push(result);
}

return results;
Expand Down
Loading