Skip to content

Commit

Permalink
Merge pull request #147 from ckb-cell/develop
Browse files Browse the repository at this point in the history
Merge develop to main branch (release v2.2.0)
  • Loading branch information
Flouse authored May 23, 2024
2 parents 2503793 + 83ffe68 commit fcb4551
Show file tree
Hide file tree
Showing 29 changed files with 1,360 additions and 200 deletions.
13 changes: 3 additions & 10 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Bitcoin network, testnet by default
NETWORK=testnet
# LOGGER_LEVEL=info

# Set /token/generate default domain param
# DOMAIN=localhost
Expand Down Expand Up @@ -31,9 +30,9 @@ JWT_SECRET=<your_secret>
# JWT_DENYLIST=

# Bitcoin data provider, support mempool and electrs
# use mempool.space as default, electrs as fallback
# change to electrs if you want to use electrs as default and mempool.space as fallback
BITCOIN_DATA_PROVIDER=mempool
# use electrs as default, mempool as fallback
# change to mempool if you want to use mempool.space as default and electrs as fallback
BITCOIN_DATA_PROVIDER=electrs
# Bitcoin Mempool.space API URL
# optinal when BITCOIN_DATA_PROVIDER=electrs
BITCOIN_MEMPOOL_SPACE_API_URL=https://mempool.space
Expand All @@ -51,10 +50,6 @@ CKB_RPC_URL=https://testnet.ckb.dev/rpc
PAYMASTER_PRIVATE_KEY=
# Paymaster cell capacity in shannons
PAYMASTER_CELL_CAPACITY=31600000000
# Paymaster cell queue preset count, used to refill paymaster cell.
PAYMASTER_CELL_PRESET_COUNT=500
# Paymaster cell refill threshold, refill paymaster cell when the balance is less than this threshold.
PAYMASTER_CELL_REFILL_THRESHOLD=0.3
# Check the paymaster BTC UTXO when processing rgb++ ckb transaction
PAYMASTER_RECEIVE_UTXO_CHECK=false
# Paymaster bitcoin address, used to receive BTC from users
Expand All @@ -66,8 +61,6 @@ PAYMASTER_BTC_CONTAINER_FEE_SATS=7000
UNLOCKER_CRON_SCHEDULE='*/5 * * * *'
# BTCTimeLock cell unlock batch size
UNLOCKER_CELL_BATCH_SIZE=100
# BTCTimeLock cell unlocker monitor slug, used for monitoring unlocker status on sentry
UNLOCKER_MONITOR_SLUG=btctimelock-cells-unlocker

# RGB++ CKB transaction Queue cron job delay in milliseconds
# the /rgbpp/v1/transaction/ckb-tx endpoint is called, the transaction will be added to the queue
Expand Down
13 changes: 3 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ Update the configuration values:
```env
# Bitcoin network, testnet by default
NETWORK=testnet
# LOGGER_LEVEL=info
# Set /token/generate default domain param
# DOMAIN=localhost
Expand Down Expand Up @@ -61,9 +60,9 @@ JWT_SECRET=<your_secret>
# JWT_DENYLIST=
# Bitcoin data provider, support mempool and electrs
# use mempool.space as default, electrs as fallback
# change to electrs if you want to use electrs as default and mempool.space as fallback
BITCOIN_DATA_PROVIDER=mempool
# use electrs as default, mempool as fallback
# change to mempool if you want to use mempool.space as default and electrs as fallback
BITCOIN_DATA_PROVIDER=electrs
# Bitcoin Mempool.space API URL
# optinal when BITCOIN_DATA_PROVIDER=electrs
BITCOIN_MEMPOOL_SPACE_API_URL=https://mempool.space
Expand All @@ -81,10 +80,6 @@ CKB_RPC_URL=https://testnet.ckb.dev/rpc
PAYMASTER_PRIVATE_KEY=
# Paymaster cell capacity in shannons
PAYMASTER_CELL_CAPACITY=31600000000
# Paymaster cell queue preset count, used to refill paymaster cell.
PAYMASTER_CELL_PRESET_COUNT=500
# Paymaster cell refill threshold, refill paymaster cell when the balance is less than this threshold.
PAYMASTER_CELL_REFILL_THRESHOLD=0.3
# Check the paymaster BTC UTXO when processing rgb++ ckb transaction
PAYMASTER_RECEIVE_UTXO_CHECK=false
# Paymaster bitcoin address, used to receive BTC from users
Expand All @@ -96,8 +91,6 @@ PAYMASTER_BTC_CONTAINER_FEE_SATS=7000
UNLOCKER_CRON_SCHEDULE='*/5 * * * *'
# BTCTimeLock cell unlock batch size
UNLOCKER_CELL_BATCH_SIZE=100
# BTCTimeLock cell unlocker monitor slug, used for monitoring unlocker status on sentry
UNLOCKER_MONITOR_SLUG=btctimelock-cells-unlocker
# RGB++ CKB transaction Queue cron job delay in milliseconds
# the /rgbpp/v1/transaction/ckb-tx endpoint is called, the transaction will be added to the queue
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "btc-assets-api",
"version": "2.1.0",
"version": "2.2.0",
"title": "Bitcoin/RGB++ Assets API",
"description": "",
"main": "index.js",
Expand Down Expand Up @@ -41,9 +41,9 @@
"@fastify/swagger-ui": "^3.0.0",
"@immobiliarelabs/fastify-sentry": "^8.0.1",
"@nervosnetwork/ckb-sdk-utils": "^0.109.1",
"@rgbpp-sdk/btc": "0.0.0-snap-20240430102443",
"@rgbpp-sdk/ckb": "0.0.0-snap-20240430102443",
"@rgbpp-sdk/service": "0.0.0-snap-20240430102443",
"@rgbpp-sdk/btc": "^0.1.0",
"@rgbpp-sdk/ckb": "^0.1.0",
"@rgbpp-sdk/service": "^0.1.0",
"@sentry/node": "^7.102.1",
"@sentry/profiling-node": "^7.102.1",
"async-retry": "^1.3.3",
Expand Down
30 changes: 15 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/@types/fastify/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Paymaster from '../../services/paymaster';
import SPVClient from '../../services/spv';
import CKBClient from '../../services/ckb';
import BitcoinClient from '../../services/bitcoin';
import RgbppCollector from '../../services/rgbpp';
import UTXOSyncer from '../../services/utxo';

declare module 'fastify' {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand All @@ -15,5 +17,7 @@ declare module 'fastify' {
spv: SPVClient;
paymaster: Paymaster;
transactionProcessor: TransactionProcessor;
rgbppCollector: RgbppCollector;
utxoSyncer: UTXOSyncer;
}
}
6 changes: 6 additions & 0 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import Unlocker from './services/unlocker';
import SPVClient from './services/spv';
import CKBClient from './services/ckb';
import BitcoinClient from './services/bitcoin';
import RgbppCollector from './services/rgbpp';
import UTXOSyncer from './services/utxo';

export interface Cradle {
env: typeof env;
Expand All @@ -19,6 +21,8 @@ export interface Cradle {
paymaster: Paymaster;
unlocker: Unlocker;
transactionProcessor: TransactionProcessor;
rgbppCollector: RgbppCollector;
utxoSyncer: UTXOSyncer;
}

const container = createContainer<Cradle>({
Expand All @@ -40,6 +44,8 @@ container.register({
paymaster: asClass(Paymaster).singleton(),
transactionProcessor: asClass(TransactionProcessor).singleton(),
unlocker: asClass(Unlocker).singleton(),
rgbppCollector: asClass(RgbppCollector).singleton(),
utxoSyncer: asClass(UTXOSyncer).singleton(),
});

export default container;
42 changes: 42 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,48 @@ const envSchema = z
.enum(['true', 'false'])
.default('false')
.transform((value) => value === 'true'),

/**
* UTXO sync data cache enable flag, used to cache the UTXO sync data
* enable by default
*/
UTXO_SYNC_DATA_CACHE_ENABLE: z
.enum(['true', 'false'])
.default('true')
.transform((value) => value === 'true'),
/**
* UTXO sync repeat base duration, used to set the UTXO sync repeat interval
* repeat job start interval is 10 seconds by default
*/
UTXO_SYNC_REPEAT_BASE_DURATION: z.coerce.number().default(10 * 1000),
/**
* UTXO sync repeat max duration, used to maximum the UTXO sync repeat interval
* 1 hour by default
*/
UTXO_SYNC_REPEAT_MAX_DURATION: z.coerce.number().default(60 * 60 * 1000),
/**
* UTXO sync repeat expired duration, used to remove the expired UTXO sync job
* 336 hours by default
*/
UTXO_SYNC_REPEAT_EXPRIED_DURATION: z.coerce.number().default(336 * 60 * 60 * 1000),
/**
* UTXO sync data cache expire duration, used to cache the UTXO sync data
* 30 minutes by default
*/
UTXO_SYNC_DATA_CACHE_EXPIRE: z.coerce.number().default(30 * 60 * 1000),

/**
* RGB++ collect data cache enable flag, used to cache the RGB++ collect data
* enable by default
*/
RGBPP_COLLECT_DATA_CACHE_ENABLE: z
.enum(['true', 'false'])
.default('true')
.transform((value) => value === 'true'),
/**
* RGB++ collect data cache expire duration, used to cache the RGB++ collect data
*/
RGBPP_COLLECT_DATA_CACHE_EXPIRE: z.coerce.number().default(30 * 60 * 1000),
})
.and(
z.union([
Expand Down
45 changes: 43 additions & 2 deletions src/plugins/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import TransactionProcessor from '../services/transaction';
import cron from 'fastify-cron';
import { Env } from '../env';
import Unlocker from '../services/unlocker';
import RgbppCollector from '../services/rgbpp';
import UTXOSyncer from '../services/utxo';

export default fp(async (fastify) => {
try {
Expand Down Expand Up @@ -48,10 +50,10 @@ export default fp(async (fastify) => {
fastify.addHook('onReady', async () => {
transactionProcessor.startProcess({
onActive: (job) => {
fastify.log.info(`Job active: ${job.id}`);
fastify.log.info(`[TransactionProcessor] job active: ${job.id}`);
},
onCompleted: (job) => {
fastify.log.info(`Job completed: ${job.id}`);
fastify.log.info(`[TransactionProcessor] job completed: ${job.id}`);
},
});
});
Expand All @@ -78,6 +80,45 @@ export default fp(async (fastify) => {
},
};

if (env.UTXO_SYNC_DATA_CACHE_ENABLE) {
const utxoSyncer: UTXOSyncer = fastify.container.resolve('utxoSyncer');
fastify.addHook('onReady', async () => {
utxoSyncer.startProcess({
onActive: (job) => {
fastify.log.info(`[UTXOSyncer] job active: ${job.id}`);
},
onCompleted: async (job) => {
fastify.log.info(`[UTXOSyncer] job completed: ${job.id}`);
if (env.RGBPP_COLLECT_DATA_CACHE_ENABLE) {
const { btcAddress, utxos } = job.returnvalue;
const rgbppCollector: RgbppCollector = fastify.container.resolve('rgbppCollector');
await rgbppCollector.enqueueCollectJob(btcAddress, utxos, true);
}
},
});
});
fastify.addHook('onClose', async () => {
utxoSyncer.closeProcess();
});
}

if (env.RGBPP_COLLECT_DATA_CACHE_ENABLE) {
const rgbppCollector: RgbppCollector = fastify.container.resolve('rgbppCollector');
fastify.addHook('onReady', async () => {
rgbppCollector.startProcess({
onActive: (job) => {
fastify.log.info(`[RgbppCollector] job active: ${job.id}`);
},
onCompleted: (job) => {
fastify.log.info(`[RgbppCollector] job completed: ${job.id}`);
},
});
});
fastify.addHook('onClose', async () => {
rgbppCollector.closeProcess();
});
}

// processing unlock BTC_TIME_LOCK cells
const unlocker: Unlocker = fastify.container.resolve('unlocker');
const monitorSlug = env.UNLOCKER_MONITOR_SLUG;
Expand Down
Loading

0 comments on commit fcb4551

Please sign in to comment.