|
| 1 | +import { buildRgbppLockArgs, CompatibleXUDTRegistry } from 'rgbpp/ckb'; |
| 2 | +import { buildRgbppTransferTx } from 'rgbpp'; |
| 3 | +import { isMainnet, collector, btcService, btcAccount, btcDataSource, BTC_TESTNET_TYPE } from '../../env'; |
| 4 | +import { saveCkbVirtualTxResult } from '../../shared/utils'; |
| 5 | +import { signAndSendPsbt } from '../../shared/btc-account'; |
| 6 | +import { bitcoin } from 'rgbpp/btc'; |
| 7 | + |
| 8 | +interface RgbppTransferParams { |
| 9 | + rgbppLockArgsList: string[]; |
| 10 | + toBtcAddress: string; |
| 11 | + transferAmount: bigint; |
| 12 | + compatibleXudtTypeScript: CKBComponents.Script; |
| 13 | +} |
| 14 | + |
| 15 | +const transferRusdOnBtc = async ({ |
| 16 | + rgbppLockArgsList, |
| 17 | + toBtcAddress, |
| 18 | + compatibleXudtTypeScript, |
| 19 | + transferAmount, |
| 20 | +}: RgbppTransferParams) => { |
| 21 | + // Refresh the cache by fetching the latest compatible xUDT list from the specified URL. |
| 22 | + // The default URL is: |
| 23 | + // https://raw.githubusercontent.com/utxostack/typeid-contract-cell-deps/main/compatible-udt.json |
| 24 | + // You can set your own trusted URL to fetch the compatible xUDT list. |
| 25 | + // await CompatibleXUDTRegistry.refreshCache("https://your-own-trusted-compatible-xudt-url"); |
| 26 | + await CompatibleXUDTRegistry.refreshCache(); |
| 27 | + |
| 28 | + const { ckbVirtualTxResult, btcPsbtHex } = await buildRgbppTransferTx({ |
| 29 | + ckb: { |
| 30 | + collector, |
| 31 | + xudtTypeArgs: compatibleXudtTypeScript.args, |
| 32 | + rgbppLockArgsList, |
| 33 | + transferAmount, |
| 34 | + compatibleXudtTypeScript, |
| 35 | + }, |
| 36 | + btc: { |
| 37 | + fromAddress: btcAccount.from, |
| 38 | + toAddress: toBtcAddress, |
| 39 | + fromPubkey: btcAccount.fromPubkey, |
| 40 | + dataSource: btcDataSource, |
| 41 | + testnetType: BTC_TESTNET_TYPE, |
| 42 | + }, |
| 43 | + isMainnet, |
| 44 | + }); |
| 45 | + |
| 46 | + // Save ckbVirtualTxResult |
| 47 | + saveCkbVirtualTxResult(ckbVirtualTxResult, '2-btc-transfer'); |
| 48 | + |
| 49 | + // Send BTC tx |
| 50 | + const psbt = bitcoin.Psbt.fromHex(btcPsbtHex); |
| 51 | + const { txId: btcTxId } = await signAndSendPsbt(psbt, btcAccount, btcService); |
| 52 | + console.log(`BTC ${BTC_TESTNET_TYPE} TxId: ${btcTxId}`); |
| 53 | + |
| 54 | + await btcService.sendRgbppCkbTransaction({ btc_txid: btcTxId, ckb_virtual_result: ckbVirtualTxResult }); |
| 55 | + |
| 56 | + try { |
| 57 | + const interval = setInterval(async () => { |
| 58 | + const { state, failedReason } = await btcService.getRgbppTransactionState(btcTxId); |
| 59 | + console.log('state', state); |
| 60 | + if (state === 'completed' || state === 'failed') { |
| 61 | + clearInterval(interval); |
| 62 | + if (state === 'completed') { |
| 63 | + const { txhash: txHash } = await btcService.getRgbppTransactionHash(btcTxId); |
| 64 | + console.info( |
| 65 | + `Rgbpp compatible xUDT asset has been transferred on BTC and the related CKB tx hash is ${txHash}`, |
| 66 | + ); |
| 67 | + } else { |
| 68 | + console.warn(`Rgbpp CKB transaction failed and the reason is ${failedReason} `); |
| 69 | + } |
| 70 | + } |
| 71 | + }, 30 * 1000); |
| 72 | + } catch (error) { |
| 73 | + console.error(error); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +// Please use your real BTC UTXO information on the BTC Testnet |
| 78 | +// BTC Testnet3: https://mempool.space/testnet |
| 79 | +// BTC Signet: https://mempool.space/signet |
| 80 | + |
| 81 | +// rgbppLockArgs: outIndexU32 + btcTxId |
| 82 | +transferRusdOnBtc({ |
| 83 | + rgbppLockArgsList: [buildRgbppLockArgs(4, '44de1b4e3ddaa95cc85cc8b1c60f3e439d343002f0c60980fb4c70841ee0c75e')], |
| 84 | + toBtcAddress: 'tb1qvt7p9g6mw70sealdewtfp0sekquxuru6j3gwmt', |
| 85 | + // Please use your own RGB++ compatible xudt asset's type script |
| 86 | + compatibleXudtTypeScript: { |
| 87 | + codeHash: '0x1142755a044bf2ee358cba9f2da187ce928c91cd4dc8692ded0337efa677d21a', |
| 88 | + hashType: 'type', |
| 89 | + args: '0x878fcc6f1f08d48e87bb1c3b3d5083f23f8a39c5d5c764f253b55b998526439b', |
| 90 | + }, |
| 91 | + transferAmount: BigInt(100_0000), |
| 92 | +}); |
0 commit comments