-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve rpc service and publish to npm
- Loading branch information
Showing
12 changed files
with
209 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,24 @@ | ||
# Bitcoin Query | ||
A javascript Bitcoin library for `node.js`. Written in `TypeScript`. Connect to `Mysql` database for saving data | ||
A javascript Bitcoin library for `node.js`. Written in `TypeScript`. | ||
|
||
|
||
# Use can trust the source | ||
This source doesn't have any address wallet or connect string. you can trust the source code `100%` | ||
|
||
## Installing | ||
the source uses Nodejs, Typescript for the server, and [Sequlize](https://sequelize.org/) for query data from [Mysql database](https://dev.mysql.com/downloads/installer/). Btw the source also has used [pnpm](https://pnpm.io/installation) for saving disk and highest speed. | ||
Before installing, you need to make sure that [Bitcoin core](https://bitcoin.org/en/download) was installed and runned | ||
|
||
``` | ||
pnpm i | ||
pnpm dev | ||
pnpm i bitcoin-query | ||
``` | ||
|
||
## Example | ||
|
||
``` | ||
import RPCServices from "bitcoin-query" | ||
const rpcServices = new RPCServices(url) // url is a bitcoin core connecting string | ||
rpcServices.getBlockCount() // get height number of latest block | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { Block, BlockStats, ResponseRPC } from "../types" | ||
import { TransactionRaw } from "../types/transacton" | ||
export declare class RPCService { | ||
url: string | ||
constructor(url: string) | ||
getBlockCount: () => Promise<number> | ||
getBlock: (hash: string) => Promise<ResponseRPC<Block>> | ||
getBlockStats: (height: number) => Promise<ResponseRPC<BlockStats>> | ||
getRawTransaction: ({ | ||
transactionHash, | ||
blockHash, | ||
}: { | ||
transactionHash: string | ||
blockHash: string | ||
}) => Promise<ResponseRPC<TransactionRaw>> | ||
} | ||
export default RPCService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
"use strict" | ||
var __awaiter = | ||
(this && this.__awaiter) || | ||
function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { | ||
return value instanceof P | ||
? value | ||
: new P(function (resolve) { | ||
resolve(value) | ||
}) | ||
} | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { | ||
try { | ||
step(generator.next(value)) | ||
} catch (e) { | ||
reject(e) | ||
} | ||
} | ||
function rejected(value) { | ||
try { | ||
step(generator["throw"](value)) | ||
} catch (e) { | ||
reject(e) | ||
} | ||
} | ||
function step(result) { | ||
result.done | ||
? resolve(result.value) | ||
: adopt(result.value).then(fulfilled, rejected) | ||
} | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()) | ||
}) | ||
} | ||
var __importDefault = | ||
(this && this.__importDefault) || | ||
function (mod) { | ||
return mod && mod.__esModule ? mod : { default: mod } | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }) | ||
exports.RPCService = void 0 | ||
const axios_1 = __importDefault(require("../axios/axios")) | ||
class RPCService { | ||
constructor(url) { | ||
this.getBlockCount = () => | ||
__awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const dataString = `{"jsonrpc":"1.0","id":"curltext","method":"getblockcount","params":[]}` | ||
const res = yield axios_1.default.post("/", dataString) | ||
return res.data | ||
} catch (error) { | ||
throw Error(error) | ||
} | ||
}) | ||
this.getBlock = (hash) => | ||
__awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const dataString = `{"jsonrpc":"1.0","id":"curltext","method":"getblock","params":["${hash}"]}` | ||
const res = yield axios_1.default.post("/", dataString) | ||
return res.data | ||
} catch (error) { | ||
throw Error(error) | ||
} | ||
}) | ||
this.getBlockStats = (height) => | ||
__awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const dataString = `{"jsonrpc":"1.0","id":"curltext","method":"getblockstats","params":[${height}]}` | ||
const res = yield axios_1.default.post("/", dataString) | ||
return res.data | ||
} catch (error) { | ||
throw Error(error) | ||
} | ||
}) | ||
this.getRawTransaction = ({ transactionHash, blockHash }) => | ||
__awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const dataString = `{"jsonrpc":"1.0","id":"curltext","method":"getrawtransaction","params":["${transactionHash}", 2, "${blockHash}"]}` | ||
const res = yield axios_1.default.post("/", dataString) | ||
return res.data | ||
} catch (error) { | ||
throw Error(error) | ||
} | ||
}) | ||
if (!url) { | ||
throw Error( | ||
"URL required, it is look like http://${USER}:${PASS}@127.0.0.1:8332/", | ||
) | ||
} | ||
this.url = url | ||
axios_1.default.defaults.baseURL = url | ||
axios_1.default.defaults.headers.common["Content-Type"] = "text/plain" | ||
} | ||
} | ||
exports.RPCService = RPCService | ||
exports.default = RPCService |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.