Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(tests): Refactor integration test cases #234

Merged
merged 7 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions .github/workflows/integration-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ name: Integration Tests

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

Expand All @@ -60,8 +59,8 @@ jobs:
if: ${{ matrix.env_set == 'spore' }}
run: pnpm run integration:spore
env:
VITE_SERVICE_URL: ${{ secrets.SERVICE_URL }}
VITE_SERVICE_TOKEN: ${{ secrets.SERVICE_TOKEN }}
VITE_SERVICE_ORIGIN: ${{ secrets.SERVICE_ORIGIN }}
VITE_SERVICE_URL: https://api.signet.rgbpp.io
VITE_SERVICE_TOKEN: ${{ secrets.SIGNET_SERVICE_TOKEN }}
VITE_SERVICE_ORIGIN: https://api.signet.rgbpp.io
INTEGRATION_CKB_PRIVATE_KEY: ${{ secrets.INTEGRATION_CKB_SPORE_PRIVATE_KEY }}
INTEGRATION_BTC_PRIVATE_KEY: ${{ secrets.INTEGRATION_BTC_SPORE_PRIVATE_KEY }}
6 changes: 3 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ jobs:
- name: Run tests for packages
run: pnpm run test:packages
env:
VITE_BTC_SERVICE_URL: ${{ secrets.SERVICE_URL }}
VITE_BTC_SERVICE_TOKEN: ${{ secrets.SERVICE_TOKEN }}
VITE_BTC_SERVICE_ORIGIN: ${{ secrets.SERVICE_ORIGIN }}
VITE_BTC_SERVICE_URL: https://btc-assets-api.testnet.mibao.pro
VITE_BTC_SERVICE_TOKEN: ${{ secrets.TESTNET_SERVICE_TOKEN }}
VITE_BTC_SERVICE_ORIGIN: https://btc-assets-api.testnet.mibao.pro
31 changes: 22 additions & 9 deletions tests/rgbpp/env.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import { AddressPrefix, privateKeyToAddress } from '@nervosnetwork/ckb-sdk-utils';
import { DataSource, BtcAssetsApi } from 'rgbpp';
import {
blake160,
bytesToHex,
privateKeyToPublicKey,
scriptToAddress,
systemScripts,
} from '@nervosnetwork/ckb-sdk-utils';
import { DataSource, BtcAssetsApi, AddressType } from 'rgbpp';
import { ECPair, ECPairInterface, bitcoin, NetworkType } from 'rgbpp/btc';
import dotenv from 'dotenv';
import { Collector } from 'rgbpp/ckb';
import { createBtcAccount } from '../../examples/rgbpp/shared/btc-account';

dotenv.config({ path: __dirname + '/.env' });

export const isMainnet = false;

export const BTC_TESTNET_TYPE = 'Signet';

export const collector = new Collector({
ckbNodeUrl: 'https://testnet.ckb.dev/rpc',
ckbIndexerUrl: 'https://testnet.ckb.dev/indexer',
});
export const CKB_PRIVATE_KEY = process.env.INTEGRATION_CKB_PRIVATE_KEY!;
export const ckbAddress = privateKeyToAddress(CKB_PRIVATE_KEY, {
prefix: isMainnet ? AddressPrefix.Mainnet : AddressPrefix.Testnet,
});
const secp256k1Lock: CKBComponents.Script = {
...systemScripts.SECP256K1_BLAKE160,
args: bytesToHex(blake160(privateKeyToPublicKey(CKB_PRIVATE_KEY))),
};
export const ckbAddress = scriptToAddress(secp256k1Lock, isMainnet);

export const BTC_PRIVATE_KEY = process.env.INTEGRATION_BTC_PRIVATE_KEY!;
export const BTC_SERVICE_URL = process.env.VITE_SERVICE_URL!;
Expand All @@ -24,11 +35,13 @@ export const BTC_SERVICE_ORIGIN = process.env.VITE_SERVICE_ORIGIN!;

const network = isMainnet ? bitcoin.networks.bitcoin : bitcoin.networks.testnet;
export const btcKeyPair: ECPairInterface = ECPair.fromPrivateKey(Buffer.from(BTC_PRIVATE_KEY, 'hex'), { network });
export const { address: btcAddress } = bitcoin.payments.p2wpkh({
pubkey: btcKeyPair.publicKey,
network,
});

// Read more about the available address types:
// - P2WPKH: https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wpkh
// - P2TR: https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki
const addressType = AddressType.P2WPKH;
const networkType = isMainnet ? NetworkType.MAINNET : NetworkType.TESTNET;
export const btcAccount = createBtcAccount(BTC_PRIVATE_KEY, addressType, networkType);

export const btcService = BtcAssetsApi.fromToken(BTC_SERVICE_URL, BTC_SERVICE_TOKEN, BTC_SERVICE_ORIGIN);
export const btcDataSource = new DataSource(btcService, networkType);
16 changes: 7 additions & 9 deletions tests/rgbpp/shared/prepare-utxo.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { sendBtc } from 'rgbpp/btc';
import { getFastestFeeRate, writeStepLog } from './utils';
import { writeStepLog } from './utils';
import { BtcAssetsApiError } from 'rgbpp/service';
import { btcAddress, btcDataSource, btcKeyPair, btcService } from '../env';
import { btcAccount, btcDataSource, btcKeyPair, btcService } from '../env';

const prepareUtxo = async (index: string | number) => {
const feeRate = await getFastestFeeRate();
console.log('feeRate = ', feeRate);
console.log(btcAddress);
console.log(btcAccount.from);

// Send BTC tx
const psbt = await sendBtc({
from: btcAddress!,
from: btcAccount.from!,
tos: [
{
address: btcAddress!,
address: btcAccount.from!,
value: 546,
minUtxoSatoshi: 546,
},
],
feeRate: feeRate,
feeRate: 1,
source: btcDataSource,
});

Expand All @@ -31,7 +29,7 @@ const prepareUtxo = async (index: string | number) => {
console.log(tx.toHex());

const { txid: btcTxId } = await btcService.sendBtcTransaction(tx.toHex());
console.log(`explorer: https://mempool.space/testnet/tx/${btcTxId}`);
console.log(`explorer: https://mempool.space/signet/tx/${btcTxId}`);

writeStepLog(String(index), {
txid: btcTxId,
Expand Down
22 changes: 9 additions & 13 deletions tests/rgbpp/spore/4-transfer-spore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ 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, btcAddress, btcDataSource, btcKeyPair, btcService } from '../env';
import { getFastestFeeRate, readStepLog } from '../shared/utils';
import { isMainnet, collector, btcDataSource, btcService, btcAccount, BTC_TESTNET_TYPE } from '../env';
import { readStepLog } from '../shared/utils';
import { saveCkbVirtualTxResult } from '../../../examples/rgbpp/shared/utils';
import { signAndSendPsbt } from '../../../examples/rgbpp/shared/btc-account';

interface SporeTransferParams {
sporeRgbppLockArgs: Hex;
Expand All @@ -20,14 +21,12 @@ const transferSpore = async ({ sporeRgbppLockArgs, toBtcAddress, sporeTypeArgs }
args: sporeTypeArgs,
});

const feeRate = await getFastestFeeRate();
console.log('feeRate = ', feeRate);

const ckbVirtualTxResult = await genTransferSporeCkbVirtualTx({
collector,
sporeRgbppLockArgs,
sporeTypeBytes,
isMainnet,
btcTestnetType: BTC_TESTNET_TYPE,
});

// Save ckbVirtualTxResult
Expand All @@ -42,18 +41,15 @@ const transferSpore = async ({ sporeRgbppLockArgs, toBtcAddress, sporeTypeArgs }
tos: [toBtcAddress],
needPaymaster: needPaymasterCell,
ckbCollector: collector,
from: btcAddress!,
from: btcAccount.from,
fromPubkey: btcAccount.fromPubkey,
source: btcDataSource,
feeRate: feeRate,
feeRate: 1,
});
psbt.signAllInputs(btcKeyPair);
psbt.finalizeAllInputs();

const btcTx = psbt.extractTransaction();
const { txid: btcTxId } = await btcService.sendBtcTransaction(btcTx.toHex());

const { txId: btcTxId } = await signAndSendPsbt(psbt, btcAccount, btcService);
console.log('BTC TxId: ', btcTxId);
console.log(`explorer: https://mempool.space/testnet/tx/${btcTxId}`);
console.log(`explorer: https://mempool.space/signet/tx/${btcTxId}`);

await btcService.sendRgbppCkbTransaction({ btc_txid: btcTxId, ckb_virtual_result: ckbVirtualTxResult });

Expand Down
24 changes: 10 additions & 14 deletions tests/rgbpp/spore/5-leap-spore-to-ckb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { buildRgbppLockArgs } from 'rgbpp/ckb';
import { genLeapSporeFromBtcToCkbVirtualTx, sendRgbppUtxos } from 'rgbpp';
import { getSporeTypeScript, Hex } from 'rgbpp/ckb';
import { serializeScript } from '@nervosnetwork/ckb-sdk-utils';
import { isMainnet, collector, btcAddress, btcDataSource, btcKeyPair, btcService } from '../env';
import { getFastestFeeRate, readStepLog } from '../shared/utils';
import { isMainnet, collector, btcDataSource, btcService, btcAccount, BTC_TESTNET_TYPE } from '../env';
import { readStepLog } from '../shared/utils';
import { saveCkbVirtualTxResult } from '../../../examples/rgbpp/shared/utils';
import { signAndSendPsbt } from '../../../examples/rgbpp/shared/btc-account';

interface SporeLeapParams {
sporeRgbppLockArgs: Hex;
Expand All @@ -20,15 +21,13 @@ const leapSporeFromBtcToCkb = async ({ sporeRgbppLockArgs, toCkbAddress, sporeTy
args: sporeTypeArgs,
});

const feeRate = await getFastestFeeRate();
console.log('feeRate = ', feeRate);

const ckbVirtualTxResult = await genLeapSporeFromBtcToCkbVirtualTx({
collector,
sporeRgbppLockArgs,
sporeTypeBytes,
toCkbAddress,
isMainnet,
btcTestnetType: BTC_TESTNET_TYPE,
});

// Save ckbVirtualTxResult
Expand All @@ -40,21 +39,18 @@ const leapSporeFromBtcToCkb = async ({ sporeRgbppLockArgs, toCkbAddress, sporeTy
const psbt = await sendRgbppUtxos({
ckbVirtualTx: ckbRawTx,
commitment,
tos: [btcAddress!],
tos: [btcAccount.from],
needPaymaster: needPaymasterCell,
ckbCollector: collector,
from: btcAddress!,
from: btcAccount.from,
fromPubkey: btcAccount.fromPubkey,
source: btcDataSource,
feeRate: feeRate,
feeRate: 1,
});
psbt.signAllInputs(btcKeyPair);
psbt.finalizeAllInputs();

const btcTx = psbt.extractTransaction();
const { txid: btcTxId } = await btcService.sendBtcTransaction(btcTx.toHex());

const { txId: btcTxId } = await signAndSendPsbt(psbt, btcAccount, btcService);
console.log('BTC TxId: ', btcTxId);
console.log(`explorer: https://mempool.space/testnet/tx/${btcTxId}`);
console.log(`explorer: https://mempool.space/signet/tx/${btcTxId}`);

await btcService.sendRgbppCkbTransaction({ btc_txid: btcTxId, ckb_virtual_result: ckbVirtualTxResult });

Expand Down
4 changes: 2 additions & 2 deletions tests/rgbpp/spore/launch/1-prepare-cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
genRgbppLockScript,
getSecp256k1CellDep,
} from 'rgbpp/ckb';
import { ckbAddress, isMainnet, collector, CKB_PRIVATE_KEY } from '../../env';
import { ckbAddress, isMainnet, collector, CKB_PRIVATE_KEY, BTC_TESTNET_TYPE } from '../../env';
import { CLUSTER_DATA } from './0-cluster-info';
import { readStepLog } from '../../shared/utils';

Expand All @@ -36,7 +36,7 @@ const prepareClusterCell = async ({ outIndex, btcTxId }: { outIndex: number; btc

const outputs: CKBComponents.CellOutput[] = [
{
lock: genRgbppLockScript(buildRgbppLockArgs(outIndex, btcTxId), isMainnet),
lock: genRgbppLockScript(buildRgbppLockArgs(outIndex, btcTxId), isMainnet, BTC_TESTNET_TYPE),
capacity: append0x(clusterCellCapacity.toString(16)),
},
];
Expand Down
27 changes: 11 additions & 16 deletions tests/rgbpp/spore/launch/2-create-cluster.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import { BtcAssetsApiError, genCreateClusterCkbVirtualTx, sendRgbppUtxos } from 'rgbpp';
import { isMainnet, collector, btcAddress, btcDataSource, btcKeyPair, btcService } from '../../env';
import { isMainnet, collector, btcDataSource, btcService, btcAccount, BTC_TESTNET_TYPE } from '../../env';
import { CLUSTER_DATA } from './0-cluster-info';
import { transactionToHex } from 'rgbpp/btc';
import {
appendCkbTxWitnesses,
buildRgbppLockArgs,
generateClusterCreateCoBuild,
sendCkbTx,
updateCkbTxWithRealBtcTxId,
} from 'rgbpp/ckb';
import { getFastestFeeRate, readStepLog, writeStepLog } from '../../shared/utils';
import { readStepLog, writeStepLog } from '../../shared/utils';
import { saveCkbVirtualTxResult } from '../../../../examples/rgbpp/shared/utils';
import { signAndSendPsbt } from '../../../../examples/rgbpp/shared/btc-account';

// Warning: Before runing this file, please run 1-prepare-cluster.ts
const createCluster = async ({ ownerRgbppLockArgs }: { ownerRgbppLockArgs: string }) => {
console.log(btcAddress);
console.log(btcAccount.from);
const { retry } = await import('zx');
await retry(20, '10s', async () => {
const feeRate = await getFastestFeeRate();
console.log('feeRate = ', feeRate);

const ckbVirtualTxResult = await genCreateClusterCkbVirtualTx({
collector,
rgbppLockArgs: ownerRgbppLockArgs,
clusterData: CLUSTER_DATA,
isMainnet,
ckbFeeRate: BigInt(2000),
btcTestnetType: BTC_TESTNET_TYPE,
});

// Save ckbVirtualTxResult
Expand All @@ -42,26 +40,23 @@ const createCluster = async ({ ownerRgbppLockArgs }: { ownerRgbppLockArgs: strin
const psbt = await sendRgbppUtxos({
ckbVirtualTx: ckbRawTx,
commitment,
tos: [btcAddress!],
tos: [btcAccount.from],
needPaymaster: needPaymasterCell,
ckbCollector: collector,
from: btcAddress!,
from: btcAccount.from,
fromPubkey: btcAccount.fromPubkey,
source: btcDataSource,
feeRate: feeRate,
feeRate: 1,
});
psbt.signAllInputs(btcKeyPair);
psbt.finalizeAllInputs();

const btcTx = psbt.extractTransaction();
const btcTxBytes = transactionToHex(btcTx, false);
const { txid: btcTxId } = await btcService.sendBtcTransaction(btcTx.toHex());
const { txId: btcTxId, rawTxHex: btcTxBytes } = await signAndSendPsbt(psbt, btcAccount, btcService);

writeStepLog('create-cluster-id', {
txid: btcTxId,
index: 1,
});
console.log('BTC TxId: ', btcTxId);
console.log(`explorer: https://mempool.space/testnet/tx/${btcTxId}`);
console.log(`explorer: https://mempool.space/signet/tx/${btcTxId}`);

const interval = setInterval(async () => {
try {
Expand Down
Loading