|
| 1 | +import { addressToScript, getTransactionSize } from '@nervosnetwork/ckb-sdk-utils'; |
| 2 | +import { |
| 3 | + getSecp256k1CellDep, |
| 4 | + NoLiveCellError, |
| 5 | + calculateUdtCellCapacity, |
| 6 | + MAX_FEE, |
| 7 | + MIN_CAPACITY, |
| 8 | + append0x, |
| 9 | + u128ToLe, |
| 10 | + SECP256K1_WITNESS_LOCK_SIZE, |
| 11 | + calculateTransactionFee, |
| 12 | + NoXudtLiveCellError, |
| 13 | + fetchTypeIdCellDeps, |
| 14 | + calculateRgbppCellCapacity, |
| 15 | + getBtcTimeLockScript, |
| 16 | + genBtcTimeLockArgs, |
| 17 | + getXudtTypeScript, |
| 18 | +} from 'rgbpp/ckb'; |
| 19 | +import { CKB_PRIVATE_KEY, ckbAddress, collector, isMainnet } from './env'; |
| 20 | + |
| 21 | +interface BtcTimeCellParams { |
| 22 | + xudtType: CKBComponents.Script; |
| 23 | + toCkbAddress: string; |
| 24 | + xudtAmount: bigint; |
| 25 | + btcTxId: string; |
| 26 | + after: number; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Generate btc time cell with custom btc txid, after and target lock script |
| 31 | + * @param xudtType The xUDT type script that comes from 1-issue-xudt or 2-transfer-xudt |
| 32 | + * BTC time lock args: |
| 33 | + * table BTCTimeLock { |
| 34 | + to_lock_script: Script, |
| 35 | + after: Uint32, |
| 36 | + btc_txid: Byte32, |
| 37 | + } |
| 38 | + */ |
| 39 | +const generateBtcTimeCell = async ({ xudtType, toCkbAddress, xudtAmount, btcTxId, after }: BtcTimeCellParams) => { |
| 40 | + const fromLock = addressToScript(ckbAddress); |
| 41 | + |
| 42 | + const xudtCells = await collector.getCells({ |
| 43 | + lock: fromLock, |
| 44 | + type: xudtType, |
| 45 | + }); |
| 46 | + if (!xudtCells || xudtCells.length === 0) { |
| 47 | + throw new NoXudtLiveCellError('The address has no xudt cells'); |
| 48 | + } |
| 49 | + |
| 50 | + const btcTimeOutputCapacity = calculateRgbppCellCapacity(xudtType); |
| 51 | + let sumXudtOutputCapacity = btcTimeOutputCapacity; |
| 52 | + |
| 53 | + const { |
| 54 | + inputs: udtInputs, |
| 55 | + sumInputsCapacity: sumXudtInputsCapacity, |
| 56 | + sumAmount, |
| 57 | + } = collector.collectUdtInputs({ |
| 58 | + liveCells: xudtCells, |
| 59 | + needAmount: xudtAmount, |
| 60 | + }); |
| 61 | + let actualInputsCapacity = sumXudtInputsCapacity; |
| 62 | + let inputs = udtInputs; |
| 63 | + |
| 64 | + const outputs: CKBComponents.CellOutput[] = [ |
| 65 | + { |
| 66 | + lock: { |
| 67 | + ...getBtcTimeLockScript(isMainnet), |
| 68 | + args: genBtcTimeLockArgs(addressToScript(toCkbAddress), btcTxId, after), |
| 69 | + }, |
| 70 | + type: xudtType, |
| 71 | + capacity: append0x(btcTimeOutputCapacity.toString(16)), |
| 72 | + }, |
| 73 | + ]; |
| 74 | + const outputsData = [append0x(u128ToLe(xudtAmount))]; |
| 75 | + |
| 76 | + if (sumAmount > xudtAmount) { |
| 77 | + const xudtChangeCapacity = calculateUdtCellCapacity(fromLock); |
| 78 | + outputs.push({ |
| 79 | + lock: fromLock, |
| 80 | + type: xudtType, |
| 81 | + capacity: append0x(xudtChangeCapacity.toString(16)), |
| 82 | + }); |
| 83 | + outputsData.push(append0x(u128ToLe(sumAmount - xudtAmount))); |
| 84 | + sumXudtOutputCapacity += xudtChangeCapacity; |
| 85 | + } |
| 86 | + |
| 87 | + const txFee = MAX_FEE; |
| 88 | + if (sumXudtInputsCapacity <= sumXudtOutputCapacity) { |
| 89 | + let emptyCells = await collector.getCells({ |
| 90 | + lock: fromLock, |
| 91 | + }); |
| 92 | + if (!emptyCells || emptyCells.length === 0) { |
| 93 | + throw new NoLiveCellError('The address has no empty cells'); |
| 94 | + } |
| 95 | + emptyCells = emptyCells.filter((cell) => !cell.output.type); |
| 96 | + const needCapacity = sumXudtOutputCapacity - sumXudtInputsCapacity; |
| 97 | + const { inputs: emptyInputs, sumInputsCapacity: sumEmptyCapacity } = collector.collectInputs( |
| 98 | + emptyCells, |
| 99 | + needCapacity, |
| 100 | + txFee, |
| 101 | + { minCapacity: MIN_CAPACITY }, |
| 102 | + ); |
| 103 | + inputs = [...inputs, ...emptyInputs]; |
| 104 | + actualInputsCapacity += sumEmptyCapacity; |
| 105 | + } |
| 106 | + |
| 107 | + let changeCapacity = actualInputsCapacity - sumXudtOutputCapacity; |
| 108 | + outputs.push({ |
| 109 | + lock: fromLock, |
| 110 | + capacity: append0x(changeCapacity.toString(16)), |
| 111 | + }); |
| 112 | + outputsData.push('0x'); |
| 113 | + |
| 114 | + const emptyWitness = { lock: '', inputType: '', outputType: '' }; |
| 115 | + const witnesses = inputs.map((_, index) => (index === 0 ? emptyWitness : '0x')); |
| 116 | + |
| 117 | + const cellDeps = [getSecp256k1CellDep(isMainnet), ...(await fetchTypeIdCellDeps(isMainnet, { xudt: true }))]; |
| 118 | + |
| 119 | + const unsignedTx = { |
| 120 | + version: '0x0', |
| 121 | + cellDeps, |
| 122 | + headerDeps: [], |
| 123 | + inputs, |
| 124 | + outputs, |
| 125 | + outputsData, |
| 126 | + witnesses, |
| 127 | + }; |
| 128 | + |
| 129 | + if (txFee === MAX_FEE) { |
| 130 | + const txSize = getTransactionSize(unsignedTx) + SECP256K1_WITNESS_LOCK_SIZE; |
| 131 | + const estimatedTxFee = calculateTransactionFee(txSize); |
| 132 | + changeCapacity -= estimatedTxFee; |
| 133 | + unsignedTx.outputs[unsignedTx.outputs.length - 1].capacity = append0x(changeCapacity.toString(16)); |
| 134 | + } |
| 135 | + |
| 136 | + const signedTx = collector.getCkb().signTransaction(CKB_PRIVATE_KEY)(unsignedTx); |
| 137 | + const txHash = await collector.getCkb().rpc.sendTransaction(signedTx, 'passthrough'); |
| 138 | + |
| 139 | + console.info(`xUDT asset has been transferred to BTC time lock and CKB tx hash is ${txHash}`); |
| 140 | +}; |
| 141 | + |
| 142 | +generateBtcTimeCell({ |
| 143 | + xudtType: { |
| 144 | + ...getXudtTypeScript(isMainnet), |
| 145 | + args: '0x562e4e8a2f64a3e9c24beb4b7dd002d0ad3b842d0cc77924328e36ad114e3ebe', |
| 146 | + }, |
| 147 | + toCkbAddress: 'ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq0e4xk4rmg5jdkn8aams492a7jlg73ue0gc0ddfj', |
| 148 | + xudtAmount: BigInt(1000) * BigInt(10 ** 8), |
| 149 | + btcTxId: '5fe08344a7e7e8be96b17a41ec9f308a5cb472cfb2a3234af86df8233d4dc3ff', |
| 150 | + after: 69000, |
| 151 | +}); |
0 commit comments