From 49bf75ad2e0eda2d84cfcd3f8d5bcce3dfe06914 Mon Sep 17 00:00:00 2001 From: Jerry Chen Date: Wed, 27 Jun 2018 11:11:51 +0800 Subject: [PATCH 1/5] using raw rpc provider --- src/Provider.ts | 4 ++++ src/Qtum.ts | 11 ++++++++--- src/QtumRPC.ts | 28 ++++++++++++++++------------ src/QtumRPCRaw.ts | 4 +++- 4 files changed, 31 insertions(+), 16 deletions(-) create mode 100644 src/Provider.ts diff --git a/src/Provider.ts b/src/Provider.ts new file mode 100644 index 0000000..bd4c339 --- /dev/null +++ b/src/Provider.ts @@ -0,0 +1,4 @@ +export interface IProvider { + rawCall: (method: string, params?: any[], opts?: any) => Promise + cancelTokenSource: () => any +} diff --git a/src/Qtum.ts b/src/Qtum.ts index f707228..ccd1c87 100644 --- a/src/Qtum.ts +++ b/src/Qtum.ts @@ -1,4 +1,6 @@ +import { IProvider } from "./Provider" import { QtumRPC } from "./QtumRPC" +import { QtumRPCRaw } from "./QtumRPCRaw" import { IContractsRepoData, ContractsRepo } from "./ContractsRepo" import { Contract } from "./Contract" @@ -8,12 +10,15 @@ import { Contract } from "./Contract" * @param providerURL URL of the qtumd RPC service. * @param repoData Information about Solidity contracts. */ -export class Qtum extends QtumRPC { +export class Qtum { private repo: ContractsRepo + private rawRpc: QtumRPC constructor(providerURL: string, repoData?: IContractsRepoData) { - super(providerURL) - this.repo = new ContractsRepo(this, { + const provider: IProvider = new QtumRPCRaw(providerURL) + + this.rawRpc = new QtumRPC(provider) + this.repo = new ContractsRepo(this.rawRpc, { // massage the repoData by providing empty default properties contracts: {}, libraries: {}, diff --git a/src/QtumRPC.ts b/src/QtumRPC.ts index 7e23f5c..15d6174 100644 --- a/src/QtumRPC.ts +++ b/src/QtumRPC.ts @@ -1,3 +1,4 @@ +import { IProvider } from "./Provider" import { QtumRPCRaw } from "./QtumRPCRaw" export interface IGetInfoResult { @@ -282,11 +283,14 @@ export interface IPromiseCancel extends Promise { cancel: () => void } -export class QtumRPC extends QtumRPCRaw { +export class QtumRPC { private _hasTxWaitSupport: boolean | undefined + constructor(private provider: IProvider) { + } + public getInfo(): Promise { - return this.rawCall("getinfo") + return this.provider.rawCall("getinfo") } public sendToContract(req: IRPCSendToContractRequest): Promise { @@ -307,7 +311,7 @@ export class QtumRPC extends QtumRPCRaw { args.push(vals.senderAddress) } - return this.rawCall("sendtocontract", args) + return this.provider.rawCall("sendtocontract", args) } public callContract(req: IRPCCallContractRequest): Promise { @@ -320,7 +324,7 @@ export class QtumRPC extends QtumRPCRaw { args.push(req.senderAddress) } - return this.rawCall("callcontract", args) + return this.provider.rawCall("callcontract", args) } public getTransaction(req: IRPCGetTransactionRequest): Promise { @@ -338,7 +342,7 @@ export class QtumRPC extends QtumRPCRaw { args.push(req.waitconf) } - return this.rawCall("gettransaction", args) + return this.provider.rawCall("gettransaction", args) } public async getTransactionReceipt(req: IRPCGetTransactionRequest): Promise { @@ -346,7 +350,7 @@ export class QtumRPC extends QtumRPCRaw { // When transaction is mined, the API returns [receipt] // // We'll do the unwrapping here. - const result: IRPCGetTransactionReceiptResult[] = await this.rawCall("gettransactionreceipt", [req.txid]) + const result: IRPCGetTransactionReceiptResult[] = await this.provider.rawCall("gettransactionreceipt", [req.txid]) if (result.length === 0) { return null @@ -366,9 +370,9 @@ export class QtumRPC extends QtumRPCRaw { req.minconf, ] - const cancelTokenSource = this.cancelTokenSource() + const cancelTokenSource = this.provider.cancelTokenSource() - const p = this.rawCall("waitforlogs", args, { cancelToken: cancelTokenSource.token }) + const p = this.provider.rawCall("waitforlogs", args, { cancelToken: cancelTokenSource.token }) return Object.assign(p, { cancel: cancelTokenSource.cancel.bind(cancelTokenSource), @@ -397,7 +401,7 @@ export class QtumRPC extends QtumRPCRaw { req.minconf, ] - return this.rawCall("searchlogs", args) + return this.provider.rawCall("searchlogs", args) } public async checkTransactionWaitSupport(): Promise { @@ -405,16 +409,16 @@ export class QtumRPC extends QtumRPCRaw { return this._hasTxWaitSupport } - const helpmsg: string = await this.rawCall("help", ["gettransaction"]) + const helpmsg: string = await this.provider.rawCall("help", ["gettransaction"]) this._hasTxWaitSupport = helpmsg.split("\n")[0].indexOf("waitconf") !== -1 return this._hasTxWaitSupport } public async fromHexAddress(hexAddress: string): Promise { - return this.rawCall("fromhexaddress", [hexAddress]) + return this.provider.rawCall("fromhexaddress", [hexAddress]) } public async getHexAddress(address: string): Promise { - return this.rawCall("gethexaddress", [address]) + return this.provider.rawCall("gethexaddress", [address]) } } diff --git a/src/QtumRPCRaw.ts b/src/QtumRPCRaw.ts index c551946..5642295 100644 --- a/src/QtumRPCRaw.ts +++ b/src/QtumRPCRaw.ts @@ -1,4 +1,6 @@ import axios, { AxiosInstance, AxiosPromise, AxiosRequestConfig, CancelToken, CancelTokenSource } from "axios" +import { IProvider } from "./Provider" + const URL = require("url-parse") import { sleep } from "./sleep" @@ -21,7 +23,7 @@ export interface IRPCCallOption { cancelToken?: CancelToken, } -export class QtumRPCRaw { +export class QtumRPCRaw implements IProvider { private idNonce: number private _api: AxiosInstance From 68c2e1faa097a3ec8938d150a463bb70f4efe39a Mon Sep 17 00:00:00 2001 From: Jerry Chen Date: Mon, 2 Jul 2018 19:20:39 +0800 Subject: [PATCH 2/5] modify QtumRPC to using provider --- src/Contract_test.ts | 2 +- src/QtumRPC.ts | 30 +++++++++++++++++++----------- src/test/index.ts | 4 +++- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/Contract_test.ts b/src/Contract_test.ts index 695e502..c10f87e 100644 --- a/src/Contract_test.ts +++ b/src/Contract_test.ts @@ -8,7 +8,7 @@ describe("Contract", () => { // don't act as sender const { sender: _, - ...info, + ...info } = repoData.contracts["test/contracts/Methods.sol"] const contract = new Contract(rpc, info) diff --git a/src/QtumRPC.ts b/src/QtumRPC.ts index 15d6174..2fdd308 100644 --- a/src/QtumRPC.ts +++ b/src/QtumRPC.ts @@ -1,5 +1,5 @@ import { IProvider } from "./Provider" -import { QtumRPCRaw } from "./QtumRPCRaw" +import { QtumRPCRaw, IRPCCallOption } from "./QtumRPCRaw" export interface IGetInfoResult { version: number, @@ -289,8 +289,16 @@ export class QtumRPC { constructor(private provider: IProvider) { } + public async rawCall( + method: string, + params: any[] = [], + opts: IRPCCallOption = {}, + ): Promise { + return this.provider.rawCall(method, params, opts); + } + public getInfo(): Promise { - return this.provider.rawCall("getinfo") + return this.rawCall("getinfo"); } public sendToContract(req: IRPCSendToContractRequest): Promise { @@ -311,7 +319,7 @@ export class QtumRPC { args.push(vals.senderAddress) } - return this.provider.rawCall("sendtocontract", args) + return this.rawCall("sendtocontract", args) } public callContract(req: IRPCCallContractRequest): Promise { @@ -324,7 +332,7 @@ export class QtumRPC { args.push(req.senderAddress) } - return this.provider.rawCall("callcontract", args) + return this.rawCall("callcontract", args) } public getTransaction(req: IRPCGetTransactionRequest): Promise { @@ -342,7 +350,7 @@ export class QtumRPC { args.push(req.waitconf) } - return this.provider.rawCall("gettransaction", args) + return this.rawCall("gettransaction", args) } public async getTransactionReceipt(req: IRPCGetTransactionRequest): Promise { @@ -350,7 +358,7 @@ export class QtumRPC { // When transaction is mined, the API returns [receipt] // // We'll do the unwrapping here. - const result: IRPCGetTransactionReceiptResult[] = await this.provider.rawCall("gettransactionreceipt", [req.txid]) + const result: IRPCGetTransactionReceiptResult[] = await this.rawCall("gettransactionreceipt", [req.txid]) if (result.length === 0) { return null @@ -372,7 +380,7 @@ export class QtumRPC { const cancelTokenSource = this.provider.cancelTokenSource() - const p = this.provider.rawCall("waitforlogs", args, { cancelToken: cancelTokenSource.token }) + const p = this.rawCall("waitforlogs", args, { cancelToken: cancelTokenSource.token }) return Object.assign(p, { cancel: cancelTokenSource.cancel.bind(cancelTokenSource), @@ -401,7 +409,7 @@ export class QtumRPC { req.minconf, ] - return this.provider.rawCall("searchlogs", args) + return this.rawCall("searchlogs", args) } public async checkTransactionWaitSupport(): Promise { @@ -409,16 +417,16 @@ export class QtumRPC { return this._hasTxWaitSupport } - const helpmsg: string = await this.provider.rawCall("help", ["gettransaction"]) + const helpmsg: string = await this.rawCall("help", ["gettransaction"]) this._hasTxWaitSupport = helpmsg.split("\n")[0].indexOf("waitconf") !== -1 return this._hasTxWaitSupport } public async fromHexAddress(hexAddress: string): Promise { - return this.provider.rawCall("fromhexaddress", [hexAddress]) + return this.rawCall("fromhexaddress", [hexAddress]) } public async getHexAddress(address: string): Promise { - return this.provider.rawCall("gethexaddress", [address]) + return this.rawCall("gethexaddress", [address]) } } diff --git a/src/test/index.ts b/src/test/index.ts index a2614b8..9182d2f 100644 --- a/src/test/index.ts +++ b/src/test/index.ts @@ -1,10 +1,12 @@ import { assert } from "chai" +import { QtumRPCRaw } from "../QtumRPCRaw" + import { QtumRPC } from "../QtumRPC" export const rpcURL = "http://qtum:test@localhost:5889" -export const rpc = new QtumRPC(rpcURL) +export const rpc = new QtumRPC(new QtumRPCRaw(rpcURL)) export const repoData = require("../../solar.development.json") From f1ac0d37676d1368a0545092de761442aa096dc7 Mon Sep 17 00:00:00 2001 From: Jerry Chen Date: Tue, 3 Jul 2018 16:58:25 +0800 Subject: [PATCH 3/5] delegate method call to provider --- src/Contract_test.ts | 2 +- src/QtumRPC.ts | 32 ++++++++++++-------------------- src/QtumRPC_test.ts | 6 +++--- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/Contract_test.ts b/src/Contract_test.ts index c10f87e..b745178 100644 --- a/src/Contract_test.ts +++ b/src/Contract_test.ts @@ -107,7 +107,7 @@ describe("Contract", () => { assert.equal(tx.confirmations, 0) - await rpc.rawCall("generate", [1]) + await rpc.provider.rawCall("generate", [1]) const receipt = await tx.confirm(1, (r) => { assert.equal(r.confirmations, 1) diff --git a/src/QtumRPC.ts b/src/QtumRPC.ts index 2fdd308..9ffef2a 100644 --- a/src/QtumRPC.ts +++ b/src/QtumRPC.ts @@ -286,22 +286,14 @@ export interface IPromiseCancel extends Promise { export class QtumRPC { private _hasTxWaitSupport: boolean | undefined - constructor(private provider: IProvider) { - } - - public async rawCall( - method: string, - params: any[] = [], - opts: IRPCCallOption = {}, - ): Promise { - return this.provider.rawCall(method, params, opts); + constructor(public provider: IProvider) { } public getInfo(): Promise { - return this.rawCall("getinfo"); + return this.provider.rawCall("getinfo") } - public sendToContract(req: IRPCSendToContractRequest): Promise { + public sendToContract(req: IRPCSendToContractRequest): Promise { const vals = { ...sendToContractRequestDefaults, ...req, @@ -319,7 +311,7 @@ export class QtumRPC { args.push(vals.senderAddress) } - return this.rawCall("sendtocontract", args) + return this.provider.rawCall("sendtocontract", args) } public callContract(req: IRPCCallContractRequest): Promise { @@ -332,7 +324,7 @@ export class QtumRPC { args.push(req.senderAddress) } - return this.rawCall("callcontract", args) + return this.provider.rawCall("callcontract", args) } public getTransaction(req: IRPCGetTransactionRequest): Promise { @@ -350,7 +342,7 @@ export class QtumRPC { args.push(req.waitconf) } - return this.rawCall("gettransaction", args) + return this.provider.rawCall("gettransaction", args) } public async getTransactionReceipt(req: IRPCGetTransactionRequest): Promise { @@ -358,7 +350,7 @@ export class QtumRPC { // When transaction is mined, the API returns [receipt] // // We'll do the unwrapping here. - const result: IRPCGetTransactionReceiptResult[] = await this.rawCall("gettransactionreceipt", [req.txid]) + const result: IRPCGetTransactionReceiptResult[] = await this.provider.rawCall("gettransactionreceipt", [req.txid]) if (result.length === 0) { return null @@ -380,7 +372,7 @@ export class QtumRPC { const cancelTokenSource = this.provider.cancelTokenSource() - const p = this.rawCall("waitforlogs", args, { cancelToken: cancelTokenSource.token }) + const p = this.provider.rawCall("waitforlogs", args, { cancelToken: cancelTokenSource.token }) return Object.assign(p, { cancel: cancelTokenSource.cancel.bind(cancelTokenSource), @@ -409,7 +401,7 @@ export class QtumRPC { req.minconf, ] - return this.rawCall("searchlogs", args) + return this.provider.rawCall("searchlogs", args) } public async checkTransactionWaitSupport(): Promise { @@ -417,16 +409,16 @@ export class QtumRPC { return this._hasTxWaitSupport } - const helpmsg: string = await this.rawCall("help", ["gettransaction"]) + const helpmsg: string = await this.provider.rawCall("help", ["gettransaction"]) this._hasTxWaitSupport = helpmsg.split("\n")[0].indexOf("waitconf") !== -1 return this._hasTxWaitSupport } public async fromHexAddress(hexAddress: string): Promise { - return this.rawCall("fromhexaddress", [hexAddress]) + return this.provider.rawCall("fromhexaddress", [hexAddress]) } public async getHexAddress(address: string): Promise { - return this.rawCall("gethexaddress", [address]) + return this.provider.rawCall("gethexaddress", [address]) } } diff --git a/src/QtumRPC_test.ts b/src/QtumRPC_test.ts index 2276801..5bbfadf 100644 --- a/src/QtumRPC_test.ts +++ b/src/QtumRPC_test.ts @@ -7,7 +7,7 @@ import { rpc, assertThrow } from "./test" // import { } from "mocha" describe("QtumRPC", () => { it("can make RPC call", async () => { - const info = await rpc.rawCall("getinfo") + const info = await rpc.provider.rawCall("getinfo") assert.isNotEmpty(info) assert.hasAllKeys(info, [ "version", @@ -32,13 +32,13 @@ describe("QtumRPC", () => { it("throws error if method is not found", async () => { await assertThrow(async () => { - return rpc.rawCall("unknown-method") + return rpc.provider.rawCall("unknown-method") }) }) it("throws error if calling method using invalid params", async () => { await assertThrow(async () => { - return rpc.rawCall("getinfo", [1, 2]) + return rpc.provider.rawCall("getinfo", [1, 2]) }) }) From d9261479dbb180c24591a0824289eccf2bf34ff1 Mon Sep 17 00:00:00 2001 From: Jerry Chen Date: Thu, 12 Jul 2018 15:08:25 +0800 Subject: [PATCH 4/5] remove generic type define --- src/Provider.ts | 4 ++-- src/QtumRPC.ts | 2 +- src/QtumRPCRaw.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Provider.ts b/src/Provider.ts index bd4c339..19d6116 100644 --- a/src/Provider.ts +++ b/src/Provider.ts @@ -1,4 +1,4 @@ -export interface IProvider { - rawCall: (method: string, params?: any[], opts?: any) => Promise +export interface IProvider { + rawCall: (method: string, params?: any[], opts?: any) => Promise cancelTokenSource: () => any } diff --git a/src/QtumRPC.ts b/src/QtumRPC.ts index 9ffef2a..a83ed69 100644 --- a/src/QtumRPC.ts +++ b/src/QtumRPC.ts @@ -286,7 +286,7 @@ export interface IPromiseCancel extends Promise { export class QtumRPC { private _hasTxWaitSupport: boolean | undefined - constructor(public provider: IProvider) { + constructor(public provider: IProvider) { } public getInfo(): Promise { diff --git a/src/QtumRPCRaw.ts b/src/QtumRPCRaw.ts index 5642295..fe998ea 100644 --- a/src/QtumRPCRaw.ts +++ b/src/QtumRPCRaw.ts @@ -23,7 +23,7 @@ export interface IRPCCallOption { cancelToken?: CancelToken, } -export class QtumRPCRaw implements IProvider { +export class QtumRPCRaw implements IProvider { private idNonce: number private _api: AxiosInstance From d38ab6003a779011fee6e8f243396bc805e8abdc Mon Sep 17 00:00:00 2001 From: Jerry Chen Date: Fri, 13 Jul 2018 23:14:09 +0800 Subject: [PATCH 5/5] remove generic type --- src/Qtum.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Qtum.ts b/src/Qtum.ts index ccd1c87..ba8406f 100644 --- a/src/Qtum.ts +++ b/src/Qtum.ts @@ -15,7 +15,7 @@ export class Qtum { private rawRpc: QtumRPC constructor(providerURL: string, repoData?: IContractsRepoData) { - const provider: IProvider = new QtumRPCRaw(providerURL) + const provider: IProvider = new QtumRPCRaw(providerURL) this.rawRpc = new QtumRPC(provider) this.repo = new ContractsRepo(this.rawRpc, {