Skip to content

Commit

Permalink
Merge pull request #265 from ckb-cell/release/0.5.0
Browse files Browse the repository at this point in the history
Merge release/0.5.0 to main branch
  • Loading branch information
duanyytop committed Jul 31, 2024
2 parents b802eb1 + 2cf54a0 commit a93f14c
Show file tree
Hide file tree
Showing 46 changed files with 1,559 additions and 1,988 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/integration-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ name: Integration Tests

on:
workflow_dispatch:
pull_request:
# Run integration-tests every day
schedule:
- cron: '59 0 * * *'
Expand Down Expand Up @@ -48,9 +49,9 @@ jobs:
if: ${{ matrix.env_set == 'xudt' }}
run: pnpm run integration:xudt
env:
VITE_SERVICE_URL: https://api.signet.rgbpp.io
VITE_SERVICE_TOKEN: ${{ secrets.SIGNET_SERVICE_TOKEN }}
VITE_SERVICE_ORIGIN: https://api.signet.rgbpp.io
VITE_SERVICE_URL: https://btc-assets-api.testnet.mibao.pro
VITE_SERVICE_TOKEN: ${{ secrets.TESTNET_SERVICE_TOKEN }}
VITE_SERVICE_ORIGIN: https://btc-assets-api.testnet.mibao.pro
INTEGRATION_CKB_PRIVATE_KEY: ${{ secrets.INTEGRATION_CKB_PRIVATE_KEY }}
INTEGRATION_BTC_PRIVATE_KEY: ${{ secrets.INTEGRATION_BTC_PRIVATE_KEY }}

Expand All @@ -59,8 +60,8 @@ jobs:
if: ${{ matrix.env_set == 'spore' }}
run: pnpm run integration:spore
env:
VITE_SERVICE_URL: https://api.signet.rgbpp.io
VITE_SERVICE_TOKEN: ${{ secrets.SIGNET_SERVICE_TOKEN }}
VITE_SERVICE_ORIGIN: https://api.signet.rgbpp.io
VITE_SERVICE_URL: https://btc-assets-api.testnet.mibao.pro
VITE_SERVICE_TOKEN: ${{ secrets.TESTNET_SERVICE_TOKEN }}
VITE_SERVICE_ORIGIN: https://btc-assets-api.testnet.mibao.pro
INTEGRATION_CKB_PRIVATE_KEY: ${{ secrets.INTEGRATION_CKB_SPORE_PRIVATE_KEY }}
INTEGRATION_BTC_PRIVATE_KEY: ${{ secrets.INTEGRATION_BTC_SPORE_PRIVATE_KEY }}
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
with:
script: |
const packages = JSON.parse(process.env.PACKAGES)
const packagesTable = packages.map((p) => `| ${p.name} | \`${p.version}\` |`)
const packagesTable = packages.map((p) => `| ${p.name} | \`${p.version}\` |`).join('\n')
const body = ['New official version of the rgbpp-sdk packages have been released:', '| Name | Version |', '| --- | --- |', packagesTable].join('\n')
github.rest.repos.createCommitComment({
commit_sha: context.sha,
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:
with:
script: |
const packages = JSON.parse(process.env.PACKAGES)
const packagesTable = packages.map((p) => `| ${p.name} | \`${p.version}\` |`)
const packagesTable = packages.map((p) => `| ${p.name} | \`${p.version}\` |`).join('\n')
const body = ['New snapshot version of the rgbpp-sdk packages have been released:', '| Name | Version |', '| --- | --- |', packagesTable].join('\n')
github.rest.repos.createCommitComment({
commit_sha: context.sha,
Expand Down
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ This repository offers utilities for Bitcoin and RGB++ asset integration.
- [@rgbpp-sdk/ckb](./packages/ckb): Nervos CKB part of the SDK
- [@rgbpp-sdk/service](./packages/service): Wrapped interfaces of `Bitcoin/RGB++ Assets Service`


## RGB++ Code Examples

- Find code examples at https://github.com/ckb-cell/rgbpp-sdk/tree/develop/examples/rgbpp


## Related CKB Scripts (Contracts)
- [CKB Bitcoin SPV Type Script](https://github.com/ckb-cell/ckb-bitcoin-spv-contracts/tree/master/contracts/ckb-bitcoin-spv-type-lock): A [type script](https://docs.nervos.org/docs/basics/glossary#type-script) for [Bitcoin SPV](https://bitcoinwiki.org/wiki/simplified-payment-verification) clients which synchronize [Bitcoin](https://bitcoin.org) state into [CKB](https://github.com/nervosnetwork/ckb)
- [CKB Bitcoin SPV Type Script](https://github.com/ckb-cell/ckb-bitcoin-spv-contracts/tree/master/contracts/ckb-bitcoin-spv-type-lock): A [type script](https://docs-old.nervos.org/docs/basics/glossary#type-script) for [Bitcoin SPV](https://bitcoinwiki.org/wiki/simplified-payment-verification) clients which synchronize [Bitcoin](https://bitcoin.org) state into [CKB](https://github.com/nervosnetwork/ckb)

- **RGB++ scripts/contracts**: [RgbppLockScript](https://github.com/ckb-cell/rgbpp/tree/main/contracts/rgbpp-lock) and [BtcTimeLockScript](https://github.com/ckb-cell/rgbpp/tree/main/contracts/btc-time-lock)
* design docs: https://github.com/ckb-cell/RGBPlusPlus-design/blob/main/docs/lockscript-design-prd-en.md
* testnet deployment: https://pudge.explorer.nervos.org/scripts#RGB++
* mainnet deployment: https://explorer.nervos.org/scripts#RGB++

- [RgbppLockScript and BtcTimeLockScript](https://github.com/ckb-cell/rgbpp-sdk/blob/63df2dcd95b1b735b5d235e156e4361a3c87b0ac/packages/ckb/src/constants/index.ts#L12-L206)
* design: https://github.com/ckb-cell/RGBPlusPlus-design/blob/main/docs/light-paper-en.md
* testnet: https://pudge.explorer.nervos.org/scripts#RGB++
* mainnet: https://explorer.nervos.org/scripts#RGB++

## RGB++ Asset Workflow Overview

Expand Down Expand Up @@ -50,9 +53,11 @@ This repository offers utilities for Bitcoin and RGB++ asset integration.
2. continuously fetch request from the queue through a **cron job**
3. check whether the **confirmations** of `req.rgbpp_btc_txid` is sufficient
4. generate the **witnesses for RgbppLocks** in the `rgbpp_ckb_tx_virtual`
5. add a **paymaster cell** into `rgbpp_ckb_tx_virtual`.inputs if the CKB capacity is insufficient
5. add a **paymaster cell** into `rgbpp_ckb_tx_virtual` inputs if the CKB capacity is insufficient
1. need to **verify the existence of paymaster UTXO** in the rgbpp_btc_tx
> based on the exchange rates of BTC and CKB, [the paymaster BTC UTXO's value](https://api.rgbpp.io/docs/static/index.html#/RGB%2B%2B/get_rgbpp_v1_paymaster_info) required to subsidize a paymaster CKB cell is approximately: `paymaster_utxo_sats ~= 316 * ${ckb_price} / ${btc_price} * 100000000`
2. sign the paymaster cell and the entire transaction if needed

6. **finalize** the `rgbpp_ckb_tx_virtual` to a `rgbpp_ckb_tx`
7. **broadcast** `rgbpp_ckb_tx` and mark the job as completed upon tx-confirmation

Expand Down
2 changes: 1 addition & 1 deletion examples/rgbpp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint:fix": "tsc && eslint --fix --ext .js,.ts . && prettier --write '**/*.{js,ts}'"
},
"dependencies": {
"@nervosnetwork/ckb-sdk-utils": "^0.109.1",
"@nervosnetwork/ckb-sdk-utils": "0.109.1",
"rgbpp": "workspace:*"
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions examples/rgbpp/spore/launch/2-create-cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const createCluster = async ({ ownerRgbppLockArgs }: { ownerRgbppLockArgs: strin
const newCkbRawTx = updateCkbTxWithRealBtcTxId({ ckbRawTx, btcTxId, isMainnet });

console.log('The cluster rgbpp lock args: ', newCkbRawTx.outputs[0].lock.args);
console.log('The cluster rgbpp lock args -- btc tx id: ', btcTxId);
console.log('The cluster rgbpp lock args -- btc tx out index: 1');

const ckbTx = await appendCkbTxWitnesses({
ckbRawTx: newCkbRawTx,
Expand Down
2 changes: 2 additions & 0 deletions examples/rgbpp/spore/launch/3-create-spores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ const createSpores = async ({ clusterRgbppLockArgs, receivers }: SporeCreatePara
// Update CKB transaction with the real BTC txId
const newCkbRawTx = updateCkbTxWithRealBtcTxId({ ckbRawTx, btcTxId, isMainnet });
console.log('The new cluster rgbpp lock args: ', newCkbRawTx.outputs[0].lock.args);
console.log('The new cluster rgbpp lock args -- btc tx id: ', btcTxId);
console.log('The new cluster rgbpp lock args -- btc tx out index: 1');

const ckbTx = await appendCkbTxWitnesses({
ckbRawTx: newCkbRawTx,
Expand Down
7 changes: 4 additions & 3 deletions examples/rgbpp/xudt/3-btc-leap-ckb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const leapFromBtcToCKB = async ({ rgbppLockArgsList, toCkbAddress, xudtTypeArgs,
toCkbAddress,
isMainnet,
btcTestnetType: BTC_TESTNET_TYPE,
// btcConfirmationBlocks: 20, // default value is 6
});

// Save ckbVirtualTxResult
Expand Down Expand Up @@ -74,9 +75,9 @@ const leapFromBtcToCKB = async ({ rgbppLockArgsList, toCkbAddress, xudtTypeArgs,

// rgbppLockArgs: outIndexU32 + btcTxId
leapFromBtcToCKB({
rgbppLockArgsList: [buildRgbppLockArgs(1, '6edd4b9327506fab09fb9a0f5e5f35136a6a94bd4c9dd79af04921618fa6c800')],
toCkbAddress: 'ckt1qrfrwcdnvssswdwpn3s9v8fp87emat306ctjwsm3nmlkjg8qyza2cqgqq9kxr7vy7yknezj0vj0xptx6thk6pwyr0sxamv6q',
rgbppLockArgsList: [buildRgbppLockArgs(1, '52148de63ddd4dcd8ba1d2e105d43c62adf6ccd7e7098c24616640cd5e485c03')],
toCkbAddress: 'ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq0e4xk4rmg5jdkn8aams492a7jlg73ue0gc0ddfj',
// Please use your own RGB++ xudt asset's xudtTypeArgs
xudtTypeArgs: '0x1ba116c119d1cfd98a53e9d1a615cf2af2bb87d95515c9d217d367054cfc696b',
xudtTypeArgs: '0x562e4e8a2f64a3e9c24beb4b7dd002d0ad3b842d0cc77924328e36ad114e3ebe',
transferAmount: BigInt(800_0000_0000),
});
2 changes: 1 addition & 1 deletion examples/rgbpp/xudt/4-unlock-btc-time-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ const unlockBtcTimeCell = async ({ btcTimeCellArgs }: { btcTimeCellArgs: string
// The btcTimeCellArgs is from the outputs[0].lock.args(BTC Time lock args) of the 3-btc-leap-ckb.ts CKB transaction
unlockBtcTimeCell({
btcTimeCellArgs:
'0x7f000000100000005b0000005f0000004b000000100000003000000031000000d23761b364210735c19c60561d213fb3beae2fd6172743719eff6920e020baac011600000000016c61f984f12d3c8a4f649e60acda5deda0b8837c06000000799a0f55202939e6801924c87c66df75932ecf79774370beebe31eaa94b6d8b8',
'0x7d00000010000000590000005d000000490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce80114000000f9a9ad51ed14936d33f7bb854aaefa5f47a3ccbd880d0100ffc34d3d23f86df84a23a3b2cf72b45c8a309fec417ab196bee8e7a74483e05f',
});
4 changes: 2 additions & 2 deletions examples/xudt-on-ckb/2-transfer-xudt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
calculateTransactionFee,
NoXudtLiveCellError,
fetchTypeIdCellDeps,
getXudtTypeScript,
} from 'rgbpp/ckb';
import { CKB_PRIVATE_KEY, ckbAddress, collector, isMainnet } from './env';

Expand Down Expand Up @@ -139,8 +140,7 @@ const XUDT_TOKEN_INFO: RgbppTokenInfo = {
transferXudt({
// The xudtType comes from 1-issue-xudt
xudtType: {
codeHash: '0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb',
hashType: 'type',
...getXudtTypeScript(isMainnet),
args: '0x562e4e8a2f64a3e9c24beb4b7dd002d0ad3b842d0cc77924328e36ad114e3ebe',
},
receivers: [
Expand Down
151 changes: 151 additions & 0 deletions examples/xudt-on-ckb/3-generate-btc-time-cell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { addressToScript, getTransactionSize } from '@nervosnetwork/ckb-sdk-utils';
import {
getSecp256k1CellDep,
NoLiveCellError,
calculateUdtCellCapacity,
MAX_FEE,
MIN_CAPACITY,
append0x,
u128ToLe,
SECP256K1_WITNESS_LOCK_SIZE,
calculateTransactionFee,
NoXudtLiveCellError,
fetchTypeIdCellDeps,
calculateRgbppCellCapacity,
getBtcTimeLockScript,
genBtcTimeLockArgs,
getXudtTypeScript,
} from 'rgbpp/ckb';
import { CKB_PRIVATE_KEY, ckbAddress, collector, isMainnet } from './env';

interface BtcTimeCellParams {
xudtType: CKBComponents.Script;
toCkbAddress: string;
xudtAmount: bigint;
btcTxId: string;
after: number;
}

/**
* Generate btc time cell with custom btc txid, after and target lock script
* @param xudtType The xUDT type script that comes from 1-issue-xudt or 2-transfer-xudt
* BTC time lock args:
* table BTCTimeLock {
to_lock_script: Script,
after: Uint32,
btc_txid: Byte32,
}
*/
const generateBtcTimeCell = async ({ xudtType, toCkbAddress, xudtAmount, btcTxId, after }: BtcTimeCellParams) => {
const fromLock = addressToScript(ckbAddress);

const xudtCells = await collector.getCells({
lock: fromLock,
type: xudtType,
});
if (!xudtCells || xudtCells.length === 0) {
throw new NoXudtLiveCellError('The address has no xudt cells');
}

const btcTimeOutputCapacity = calculateRgbppCellCapacity(xudtType);
let sumXudtOutputCapacity = btcTimeOutputCapacity;

const {
inputs: udtInputs,
sumInputsCapacity: sumXudtInputsCapacity,
sumAmount,
} = collector.collectUdtInputs({
liveCells: xudtCells,
needAmount: xudtAmount,
});
let actualInputsCapacity = sumXudtInputsCapacity;
let inputs = udtInputs;

const outputs: CKBComponents.CellOutput[] = [
{
lock: {
...getBtcTimeLockScript(isMainnet),
args: genBtcTimeLockArgs(addressToScript(toCkbAddress), btcTxId, after),
},
type: xudtType,
capacity: append0x(btcTimeOutputCapacity.toString(16)),
},
];
const outputsData = [append0x(u128ToLe(xudtAmount))];

if (sumAmount > xudtAmount) {
const xudtChangeCapacity = calculateUdtCellCapacity(fromLock);
outputs.push({
lock: fromLock,
type: xudtType,
capacity: append0x(xudtChangeCapacity.toString(16)),
});
outputsData.push(append0x(u128ToLe(sumAmount - xudtAmount)));
sumXudtOutputCapacity += xudtChangeCapacity;
}

const txFee = MAX_FEE;
if (sumXudtInputsCapacity <= sumXudtOutputCapacity) {
let emptyCells = await collector.getCells({
lock: fromLock,
});
if (!emptyCells || emptyCells.length === 0) {
throw new NoLiveCellError('The address has no empty cells');
}
emptyCells = emptyCells.filter((cell) => !cell.output.type);
const needCapacity = sumXudtOutputCapacity - sumXudtInputsCapacity;
const { inputs: emptyInputs, sumInputsCapacity: sumEmptyCapacity } = collector.collectInputs(
emptyCells,
needCapacity,
txFee,
{ minCapacity: MIN_CAPACITY },
);
inputs = [...inputs, ...emptyInputs];
actualInputsCapacity += sumEmptyCapacity;
}

let changeCapacity = actualInputsCapacity - sumXudtOutputCapacity;
outputs.push({
lock: fromLock,
capacity: append0x(changeCapacity.toString(16)),
});
outputsData.push('0x');

const emptyWitness = { lock: '', inputType: '', outputType: '' };
const witnesses = inputs.map((_, index) => (index === 0 ? emptyWitness : '0x'));

const cellDeps = [getSecp256k1CellDep(isMainnet), ...(await fetchTypeIdCellDeps(isMainnet, { xudt: true }))];

const unsignedTx = {
version: '0x0',
cellDeps,
headerDeps: [],
inputs,
outputs,
outputsData,
witnesses,
};

if (txFee === MAX_FEE) {
const txSize = getTransactionSize(unsignedTx) + SECP256K1_WITNESS_LOCK_SIZE;
const estimatedTxFee = calculateTransactionFee(txSize);
changeCapacity -= estimatedTxFee;
unsignedTx.outputs[unsignedTx.outputs.length - 1].capacity = append0x(changeCapacity.toString(16));
}

const signedTx = collector.getCkb().signTransaction(CKB_PRIVATE_KEY)(unsignedTx);
const txHash = await collector.getCkb().rpc.sendTransaction(signedTx, 'passthrough');

console.info(`xUDT asset has been transferred to BTC time lock and CKB tx hash is ${txHash}`);
};

generateBtcTimeCell({
xudtType: {
...getXudtTypeScript(isMainnet),
args: '0x562e4e8a2f64a3e9c24beb4b7dd002d0ad3b842d0cc77924328e36ad114e3ebe',
},
toCkbAddress: 'ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq0e4xk4rmg5jdkn8aams492a7jlg73ue0gc0ddfj',
xudtAmount: BigInt(1000) * BigInt(10 ** 8),
btcTxId: '5fe08344a7e7e8be96b17a41ec9f308a5cb472cfb2a3234af86df8233d4dc3ff',
after: 69000,
});
2 changes: 1 addition & 1 deletion examples/xudt-on-ckb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint:fix": "tsc && eslint --fix --ext .ts . && prettier --write '**/*.ts'"
},
"dependencies": {
"@nervosnetwork/ckb-sdk-utils": "^0.109.1",
"@nervosnetwork/ckb-sdk-utils": "0.109.1",
"rgbpp": "^0.3.0"
},
"devDependencies": {
Expand Down
17 changes: 16 additions & 1 deletion packages/btc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
# @rgbpp-sdk/btc

## 0.4.0
## v0.5.0

### Minor Changes

- [#261](https://github.com/ckb-cell/rgbpp-sdk/pull/261): Batch fetch CKB RGB++ live cells to construct BTC transaction ([@duanyytop](https://github.com/duanyytop))

- Batch fetch CKB RGB++ live cells to construct BTC transaction
- Remove useless fields for RGB++ lock args list

### Patch Changes

- Updated dependencies [[`9afc2a9`](https://github.com/ckb-cell/rgbpp-sdk/commit/9afc2a911e6a4ba8a200755b01159b5b149e4010), [`8f99429`](https://github.com/ckb-cell/rgbpp-sdk/commit/8f99429de45899e5169771e87e73603318a49ae8), [`475b3c3`](https://github.com/ckb-cell/rgbpp-sdk/commit/475b3c35ab1a25ba3aae28123f2820460101c889)]:
- @rgbpp-sdk/service@0.5.0
- @rgbpp-sdk/ckb@0.5.0

## v0.4.0

### Minor Changes

Expand Down
4 changes: 2 additions & 2 deletions packages/btc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rgbpp-sdk/btc",
"version": "0.4.0",
"version": "0.5.0",
"scripts": {
"test": "vitest",
"build": "tsc -p tsconfig.build.json",
Expand All @@ -18,7 +18,7 @@
"dependencies": {
"@bitcoinerlab/secp256k1": "^1.1.1",
"@ckb-lumos/codec": "0.22.2",
"@nervosnetwork/ckb-types": "^0.109.1",
"@nervosnetwork/ckb-types": "0.109.1",
"@rgbpp-sdk/ckb": "workspace:^",
"@rgbpp-sdk/service": "workspace:^",
"bip32": "^4.0.0",
Expand Down
Loading

1 comment on commit a93f14c

@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 official version of the rgbpp-sdk packages have been released:

Name Version
@rgbpp-sdk/btc 0.5.0
@rgbpp-sdk/ckb 0.5.0
rgbpp 0.5.0
@rgbpp-sdk/service 0.5.0

Please sign in to comment.