Skip to content

Commit

Permalink
feat: upgrade rgbpp sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
ahonn committed Mar 21, 2024
1 parent cae61ba commit 47858b2
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 16 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"@fastify/sensible": "^5.5.0",
"@fastify/swagger": "^8.14.0",
"@fastify/swagger-ui": "^3.0.0",
"@rgbpp-sdk/btc": "0.0.0-snap-20240314081840",
"@rgbpp-sdk/ckb": "0.0.0-snap-20240314081840",
"@rgbpp-sdk/btc": "0.0.0-snap-20240321053723",
"@rgbpp-sdk/ckb": "0.0.0-snap-20240321053723",
"@sentry/node": "^7.102.1",
"@sentry/profiling-node": "^7.102.1",
"awilix": "^10.0.1",
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

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

3 changes: 3 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ const envSchema = z.object({
PAYMASTER_CELL_CAPACITY: z.coerce.number().default(220),
PAYMASTER_CELL_PRESET_COUNT: z.coerce.number().default(500),
PAYMASTER_CELL_REFILL_THRESHOLD: z.coerce.number().default(0.3),

UNLOCKER_CELL_BATCH_SIZE: z.coerce.number().default(100),

TRANSACTION_QUEUE_JOB_DELAY: z.coerce.number().default(120 * 1000),
TRANSACTION_SPV_SERVICE_URL: z.string(),
});

export type Env = z.infer<typeof envSchema>;
Expand Down
44 changes: 44 additions & 0 deletions src/services/bitcoind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,45 @@ import { addLoggerInterceptor } from '../utils/interceptors';
import { Cradle } from '../container';
import { randomUUID } from 'node:crypto';

type TransactionCategory = 'send' | 'receive' | 'generate' | 'immature' | 'orphan';

type Bip125Replaceable = 'yes' | 'no' | 'unknown';

interface TransactionDetail {
involvesWatchonly?: boolean;
address: string;
category: TransactionCategory;
amount: number;
label?: string;
vout: number;
fee?: number;
abandoned?: boolean;
}

interface Transaction {
amount: number;
fee?: number;
confirmations: number;
generated?: boolean;
trusted?: boolean;
blockhash?: string;
blockheight?: number;
blockindex?: number;
blocktime?: number;
txid: string;
walletconflicts: string[];
time: number;
timereceived: number;
comment?: string;
bip125_replaceable?: Bip125Replaceable;
details: TransactionDetail[];
hex: string;
decoded?: unknown;
}

/**
* Bitcoind, a wrapper for Bitcoin Core JSON-RPC
*/
export default class Bitcoind {
private request: AxiosInstance;

Expand Down Expand Up @@ -48,4 +87,9 @@ export default class Bitcoind {
public async sendRawTransaction(txHex: string) {
return this.callMethod<string>('sendrawtransaction', [txHex]);
}

/// https://developer.bitcoin.org/reference/rpc/gettransaction.html
public async getTransaction(txid: string) {
return this.callMethod<Transaction>('gettransaction', [txid]);
}
}
21 changes: 15 additions & 6 deletions src/services/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { Cradle } from '../container';
import { DelayedError, Job, Queue, Worker } from 'bullmq';
import { AxiosError } from 'axios';
import { CKBRawTransaction, CKBVirtualResult } from '../routes/rgbpp/types';
import { Transaction } from '../routes/bitcoin/types';
import { opReturnScriptPubKeyToData } from '@rgbpp-sdk/btc';
import { appendCkbTxWitnesses, sendCkbTx, Collector } from '@rgbpp-sdk/ckb';
import { appendCkbTxWitnesses, SPVService, sendCkbTx, Collector } from '@rgbpp-sdk/ckb';
import { calculateCommitment } from '@rgbpp-sdk/ckb/lib/utils/rgbpp';
import * as Sentry from '@sentry/node';
import { Transaction } from '../routes/bitcoin/types';

export interface ITransactionRequest {
txid: string;
Expand Down Expand Up @@ -59,6 +59,7 @@ export default class TransactionManager implements ITransactionManager {
private cradle: Cradle;
private queue: Queue<ITransactionRequest>;
private worker: Worker<ITransactionRequest>;
private spvService: SPVService;

constructor(cradle: Cradle) {
this.cradle = cradle;
Expand All @@ -70,6 +71,7 @@ export default class TransactionManager implements ITransactionManager {
autorun: false,
concurrency: 10,
});
this.spvService = new SPVService(cradle.env.TRANSACTION_SPV_SERVICE_URL);
}

/**
Expand Down Expand Up @@ -116,8 +118,8 @@ export default class TransactionManager implements ITransactionManager {
*/
private async moveJobToDelayed(job: Job<ITransactionRequest>, token?: string) {
this.cradle.logger.info(`[TransactionManager] Moving job ${job.id} to delayed queue`);
// FIXME: choose a better delay time
await job.moveToDelayed(Date.now() + 1000 * 60, token);
const timestamp = Date.now() + this.cradle.env.TRANSACTION_QUEUE_JOB_DELAY;
await job.moveToDelayed(timestamp, token);
// https://docs.bullmq.io/patterns/process-step-jobs#delaying
throw new DelayedError();
}
Expand All @@ -143,8 +145,15 @@ export default class TransactionManager implements ITransactionManager {
if (!isVerified) {
throw new InvalidTransactionError(job.data);
}
const { ckbVirtualResult } = job.data;
let signedTx = appendCkbTxWitnesses(ckbVirtualResult)!;
const { ckbVirtualResult, txid } = job.data;
const { hex, blockindex } = await this.cradle.bitcoind.getTransaction(txid);
let signedTx = await appendCkbTxWitnesses({
...ckbVirtualResult,
spvService: this.spvService,
btcTxId: txid,
btcTxBytes: hex,
btcTxIndexInBlock: blockindex!,
})!;

// append paymaster cell and sign the transaction if needed
if (ckbVirtualResult.needPaymasterCell) {
Expand Down

0 comments on commit 47858b2

Please sign in to comment.