Skip to content

Commit

Permalink
Merge pull request #286 from ckb-cell/test/btc-transfer-all
Browse files Browse the repository at this point in the history
test: Add integration tests for btc-transfer-all
  • Loading branch information
Dawn-githup authored Sep 4, 2024
2 parents 3ee71df + 0a31a2c commit ebcefc3
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tests/rgbpp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"format": "prettier --write '**/*.{js,ts}'",
"lint": "tsc && eslint . && prettier --check '**/*.{js,ts}'",
"lint:fix": "tsc && eslint --fix --ext .js,.ts . && prettier --write '**/*.{js,ts}'",
"integration:xudt": "npx tsx shared/prepare-utxo.ts && npx tsx xudt/xudt-on-ckb/1-issue-xudt.ts && npx tsx xudt/xudt-on-ckb/2-transfer-xudt.ts && npx tsx xudt/1-ckb-leap-btc.ts && npx tsx xudt/2-btc-transfer.ts && npx tsx xudt/3-btc-leap-ckb.ts",
"integration:xudt": "npx tsx shared/prepare-utxo.ts && npx tsx xudt/xudt-on-ckb/1-issue-xudt.ts && npx tsx xudt/xudt-on-ckb/2-transfer-xudt.ts && npx tsx xudt/1-ckb-leap-btc.ts && npx tsx xudt/2-btc-transfer.ts && npx tsx xudt/3-btc-leap-ckb.ts && npx tsx xudt/btc-transfer-all/1-btc-transfer-all.ts",
"integration:spore": "npx tsx shared/prepare-utxo.ts && npx tsx spore/launch/1-prepare-cluster.ts && npx tsx spore/launch/2-create-cluster.ts && npx tsx spore/launch/3-create-spores.ts && npx tsx spore/4-transfer-spore.ts && npx tsx spore/5-leap-spore-to-ckb.ts"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion tests/rgbpp/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"target": "ES2015",
"lib": ["dom", "esnext"],
"module": "ES2015",
"module": "esnext",
"composite": false,
"resolveJsonModule": true,
"strictNullChecks": true,
Expand Down
113 changes: 113 additions & 0 deletions tests/rgbpp/xudt/btc-transfer-all/1-btc-transfer-all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { bitcoin } from 'rgbpp/btc';
import { buildRgbppTransferAllTxs, sendRgbppTxGroups } from 'rgbpp';
import { btcDataSource, isMainnet, collector, btcAccount } from '../../env';
import { signPsbt } from '../../../../examples/rgbpp/shared/btc-account';
import { saveCkbVirtualTxResult } from '../../../../examples/rgbpp/shared/utils';
import { readStepLog } from '../../shared/utils';

interface TestParams {
xudtTypeArgs: string;
fromAddress: string;
toAddress: string;
}

const rgbppTransferAllTxs = async ({ xudtTypeArgs, fromAddress, toAddress }: TestParams) => {
const result = await buildRgbppTransferAllTxs({
ckb: {
xudtTypeArgs,
collector,
},
btc: {
assetAddresses: [fromAddress],
fromAddress: fromAddress,
toAddress: toAddress,
dataSource: btcDataSource,
feeRate: 5,
},
isMainnet,
});

console.log('result.transactions.length', result.transactions.length);
console.log('result.summary.included.assets', result.summary.included.xudtAssets);
console.log('result.summary.excluded.assets', result.summary.excluded.xudtAssets);

const signedGroups = await Promise.all(
result.transactions.map(async (group) => {
const psbt = bitcoin.Psbt.fromHex(group.btc.psbtHex);

// Sign transactions
signPsbt(psbt, btcAccount);

psbt.finalizeAllInputs();

return {
ckbVirtualTxResult: JSON.stringify(group.ckb.virtualTxResult),
btcTxHex: psbt.extractTransaction().toHex(),
};
}),
);

const signedGroupsData = JSON.parse(JSON.stringify(signedGroups, null, 2));

// Save signedGroupsData
saveCkbVirtualTxResult(signedGroupsData, '1-btc-transfer-all');

console.log('signedGroups', signedGroupsData);

// Send transactions
const sentGroups = await sendRgbppTxGroups({
txGroups: signedGroups,
btcService: btcDataSource.service,
});
console.log('sentGroups', JSON.stringify(sentGroups, null, 2));

const successfulTxIds = sentGroups
.filter((group) => group.btcTxId)
.map((group) => `https://mempool.space/testnet/tx/${group.btcTxId}`);

console.log('Successful Transactions:', successfulTxIds.join('\n'));

try {
const interval = setInterval(async () => {
try {
const states = await Promise.all(
sentGroups
.filter((group) => group.btcTxId)
.map(async (group) => {
const btcTxId = group.btcTxId!;
const { state, failedReason } = await btcDataSource.service.getRgbppTransactionState(btcTxId);
return { btcTxId, state, failedReason };
}),
);

for (const { btcTxId, state, failedReason } of states) {
console.log(`State for transaction ${btcTxId}: ${state}`);
if (state === 'completed' || state === 'failed') {
if (state === 'completed') {
const { txhash: txHash } = await btcDataSource.service.getRgbppTransactionHash(btcTxId);
console.info(`Rgbpp asset has been transferred on BTC and the related CKB tx hash is ${txHash}`);
console.info(`Explorer: https://pudge.explorer.nervos.org/transaction/${txHash}`);
} else {
console.warn(`Rgbpp CKB transaction failed and the reason is ${failedReason}`);
}
}
}

if (states.every(({ state }) => state === 'completed' || state === 'failed')) {
clearInterval(interval);
}
} catch (error) {
console.error('Error during transaction state tracking:', error);
}
}, 30 * 1000);
} catch (error) {
console.error('Error during transaction state tracking:', error);
}
};

rgbppTransferAllTxs({
// Please use your own RGB++ xudt asset's xudtTypeArgs
xudtTypeArgs: readStepLog('xUDT-type-script').args,
fromAddress: btcAccount.from,
toAddress: 'tb1qdnvvnyhc5wegxgh0udwaej04n8w08ahrr0w4q9',
}).catch(console.error);

1 comment on commit ebcefc3

@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-20240904113103
@rgbpp-sdk/ckb 0.0.0-snap-20240904113103
rgbpp 0.0.0-snap-20240904113103
@rgbpp-sdk/service 0.0.0-snap-20240904113103

Please sign in to comment.