Skip to content

Commit

Permalink
Merge pull request #269 from ckb-cell/feat/too-large-tx-warning
Browse files Browse the repository at this point in the history
feat(spore-example): Check spore creation tx size
  • Loading branch information
duanyytop authored Aug 6, 2024
2 parents 2752193 + 2c07ab8 commit f2a0b65
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion examples/rgbpp/spore/launch/3-create-spores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ import {
sendCkbTx,
updateCkbTxWithRealBtcTxId,
RawSporeData,
remove0x,
SporeCreateVirtualTxResult,
} from 'rgbpp/ckb';
import { utf8ToBuffer } from 'rgbpp/btc';
import { saveCkbVirtualTxResult } from '../../shared/utils';
import { signAndSendPsbt } from '../../shared/btc-account';
import { serializeRawTransaction } from '@nervosnetwork/ckb-sdk-utils';

const RECOMMENDED_MAX_CKB_TX_SIZE = 60 * 1024;

interface SporeCreateParams {
clusterRgbppLockArgs: Hex;
Expand All @@ -31,7 +36,22 @@ interface SporeCreateParams {
}[];
}

// Warning: Before runing this file for the first time, please run 2-prepare-cluster.ts
const estimateCkbTxSize = (ckbVirtualTxResult: SporeCreateVirtualTxResult) => {
const { ckbRawTx, clusterCell } = ckbVirtualTxResult;
const rawTxSize = remove0x(serializeRawTransaction(ckbRawTx)).length / 2;

const coBuild = generateSporeCreateCoBuild({
// The first output is cluster cell and the rest of the outputs are spore cells
sporeOutputs: ckbRawTx.outputs.slice(1),
sporeOutputsData: ckbRawTx.outputsData.slice(1),
clusterCell,
clusterOutputCell: ckbRawTx.outputs[0],
});
const coBuildSize = remove0x(coBuild).length / 2;
return rawTxSize + coBuildSize;
};

// Warning: Before running this file for the first time, please run 2-prepare-cluster.ts
const createSpores = async ({ clusterRgbppLockArgs, receivers }: SporeCreateParams) => {
const ckbVirtualTxResult = await genCreateSporeCkbVirtualTx({
collector,
Expand All @@ -42,6 +62,13 @@ const createSpores = async ({ clusterRgbppLockArgs, receivers }: SporeCreatePara
btcTestnetType: BTC_TESTNET_TYPE,
});

const ckbTxSize = estimateCkbTxSize(ckbVirtualTxResult);
if (ckbTxSize > RECOMMENDED_MAX_CKB_TX_SIZE) {
throw new Error(
`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.`,
);
}

// Save ckbVirtualTxResult
saveCkbVirtualTxResult(ckbVirtualTxResult, '3-create-spores');

Expand Down

1 comment on commit f2a0b65

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New snapshot version of the rgbpp-sdk packages have been released:

Name Version
@rgbpp-sdk/btc 0.0.0-snap-20240806084142
@rgbpp-sdk/ckb 0.0.0-snap-20240806084142
rgbpp 0.0.0-snap-20240806084142
@rgbpp-sdk/service 0.0.0-snap-20240806084142

Please sign in to comment.