Skip to content
This repository was archived by the owner on May 27, 2025. It is now read-only.

Commit f2a0b65

Browse files
authored
Merge pull request #269 from ckb-cell/feat/too-large-tx-warning
feat(spore-example): Check spore creation tx size
2 parents 2752193 + 2c07ab8 commit f2a0b65

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

examples/rgbpp/spore/launch/3-create-spores.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ import {
1818
sendCkbTx,
1919
updateCkbTxWithRealBtcTxId,
2020
RawSporeData,
21+
remove0x,
22+
SporeCreateVirtualTxResult,
2123
} from 'rgbpp/ckb';
2224
import { utf8ToBuffer } from 'rgbpp/btc';
2325
import { saveCkbVirtualTxResult } from '../../shared/utils';
2426
import { signAndSendPsbt } from '../../shared/btc-account';
27+
import { serializeRawTransaction } from '@nervosnetwork/ckb-sdk-utils';
28+
29+
const RECOMMENDED_MAX_CKB_TX_SIZE = 60 * 1024;
2530

2631
interface SporeCreateParams {
2732
clusterRgbppLockArgs: Hex;
@@ -31,7 +36,22 @@ interface SporeCreateParams {
3136
}[];
3237
}
3338

34-
// Warning: Before runing this file for the first time, please run 2-prepare-cluster.ts
39+
const estimateCkbTxSize = (ckbVirtualTxResult: SporeCreateVirtualTxResult) => {
40+
const { ckbRawTx, clusterCell } = ckbVirtualTxResult;
41+
const rawTxSize = remove0x(serializeRawTransaction(ckbRawTx)).length / 2;
42+
43+
const coBuild = generateSporeCreateCoBuild({
44+
// The first output is cluster cell and the rest of the outputs are spore cells
45+
sporeOutputs: ckbRawTx.outputs.slice(1),
46+
sporeOutputsData: ckbRawTx.outputsData.slice(1),
47+
clusterCell,
48+
clusterOutputCell: ckbRawTx.outputs[0],
49+
});
50+
const coBuildSize = remove0x(coBuild).length / 2;
51+
return rawTxSize + coBuildSize;
52+
};
53+
54+
// Warning: Before running this file for the first time, please run 2-prepare-cluster.ts
3555
const createSpores = async ({ clusterRgbppLockArgs, receivers }: SporeCreateParams) => {
3656
const ckbVirtualTxResult = await genCreateSporeCkbVirtualTx({
3757
collector,
@@ -42,6 +62,13 @@ const createSpores = async ({ clusterRgbppLockArgs, receivers }: SporeCreatePara
4262
btcTestnetType: BTC_TESTNET_TYPE,
4363
});
4464

65+
const ckbTxSize = estimateCkbTxSize(ckbVirtualTxResult);
66+
if (ckbTxSize > RECOMMENDED_MAX_CKB_TX_SIZE) {
67+
throw new Error(
68+
`The estimated size(${ckbTxSize} bytes) of the CKB transaction is too large, which may cause the transaction to fail to be properly submitted to the blockchain. It is strongly recommended to reduce the number of Spore receivers to reduce the size of the CKB transaction to below 60K bytes.`,
69+
);
70+
}
71+
4572
// Save ckbVirtualTxResult
4673
saveCkbVirtualTxResult(ckbVirtualTxResult, '3-create-spores');
4774

0 commit comments

Comments
 (0)