-
Notifications
You must be signed in to change notification settings - Fork 20
/
4-transfer-spore.ts
81 lines (70 loc) · 2.85 KB
/
4-transfer-spore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { buildRgbppLockArgs } from 'rgbpp/ckb';
import { genTransferSporeCkbVirtualTx, sendRgbppUtxos } from 'rgbpp';
import { getSporeTypeScript, Hex } from 'rgbpp/ckb';
import { serializeScript } from '@nervosnetwork/ckb-sdk-utils';
import { isMainnet, collector, btcDataSource, btcService, btcAccount, BTC_TESTNET_TYPE } from '../env';
import { saveCkbVirtualTxResult } from '../shared/utils';
import { signAndSendPsbt } from '../shared/btc-account';
interface SporeTransferParams {
sporeRgbppLockArgs: Hex;
toBtcAddress: string;
sporeTypeArgs: Hex;
}
const transferSpore = async ({ sporeRgbppLockArgs, toBtcAddress, sporeTypeArgs }: SporeTransferParams) => {
const sporeTypeBytes = serializeScript({
...getSporeTypeScript(isMainnet),
args: sporeTypeArgs,
});
const ckbVirtualTxResult = await genTransferSporeCkbVirtualTx({
collector,
sporeRgbppLockArgs,
sporeTypeBytes,
isMainnet,
btcTestnetType: BTC_TESTNET_TYPE,
});
// Save ckbVirtualTxResult
saveCkbVirtualTxResult(ckbVirtualTxResult, '4-transfer-spore');
const { commitment, ckbRawTx, needPaymasterCell } = ckbVirtualTxResult;
// Send BTC tx
const psbt = await sendRgbppUtxos({
ckbVirtualTx: ckbRawTx,
commitment,
tos: [toBtcAddress],
needPaymaster: needPaymasterCell,
ckbCollector: collector,
from: btcAccount.from,
fromPubkey: btcAccount.fromPubkey,
source: btcDataSource,
feeRate: 30,
});
const { txId: btcTxId } = await signAndSendPsbt(psbt, btcAccount, btcService);
console.log('BTC TxId: ', btcTxId);
await btcService.sendRgbppCkbTransaction({ btc_txid: btcTxId, ckb_virtual_result: ckbVirtualTxResult });
try {
const interval = setInterval(async () => {
const { state, failedReason } = await btcService.getRgbppTransactionState(btcTxId);
console.log('state', state);
if (state === 'completed' || state === 'failed') {
clearInterval(interval);
if (state === 'completed') {
const { txhash: txHash } = await btcService.getRgbppTransactionHash(btcTxId);
console.info(`Rgbpp spore has been transferred on BTC and the related CKB tx hash is ${txHash}`);
} else {
console.warn(`Rgbpp CKB transaction failed and the reason is ${failedReason} `);
}
}
}, 30 * 1000);
} catch (error) {
console.error(error);
}
};
// Please use your real BTC UTXO information on the BTC Testnet
// BTC Testnet3: https://mempool.space/testnet
// BTC Signet: https://mempool.space/signet
// rgbppLockArgs: outIndexU32 + btcTxId
transferSpore({
sporeRgbppLockArgs: buildRgbppLockArgs(2, 'd5868dbde4be5e49876b496449df10150c356843afb6f94b08f8d81f394bb350'),
toBtcAddress: 'tb1qhp9fh9qsfeyh0yhewgu27ndqhs5qlrqwau28m7',
// Please use your own RGB++ spore asset's sporeTypeArgs
sporeTypeArgs: '0x42898ea77062256f46e8f1b861d526ae47810ecc51ab50477945d5fa90452706',
});