Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/Contract_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/Provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface IProvider {
rawCall: (method: string, params?: any[], opts?: any) => Promise<any>
cancelTokenSource: () => any
}
11 changes: 8 additions & 3 deletions src/Qtum.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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: {},
Expand Down
32 changes: 18 additions & 14 deletions src/QtumRPC.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { QtumRPCRaw } from "./QtumRPCRaw"
import { IProvider } from "./Provider"
import { QtumRPCRaw, IRPCCallOption } from "./QtumRPCRaw"

export interface IGetInfoResult {
version: number,
Expand Down Expand Up @@ -282,14 +283,17 @@ export interface IPromiseCancel<T> extends Promise<T> {
cancel: () => void
}

export class QtumRPC extends QtumRPCRaw {
export class QtumRPC {
private _hasTxWaitSupport: boolean | undefined

constructor(public provider: IProvider) {
}

public getInfo(): Promise<IGetInfoResult> {
return this.rawCall("getinfo")
return this.provider.rawCall("getinfo")
}

public sendToContract(req: IRPCSendToContractRequest): Promise<IRPCSendToContractResult> {
public sendToContract(req: IRPCSendToContractRequest): Promise<any> {
const vals = {
...sendToContractRequestDefaults,
...req,
Expand All @@ -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<IRPCCallContractResult> {
Expand All @@ -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<IRPCGetTransactionResult> {
Expand All @@ -338,15 +342,15 @@ 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<IRPCGetTransactionReceiptResult | null> {
// The raw RPC API returns [] if tx id doesn't exist or not mined yet
// 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
Expand All @@ -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),
Expand Down Expand Up @@ -397,24 +401,24 @@ export class QtumRPC extends QtumRPCRaw {
req.minconf,
]

return this.rawCall("searchlogs", args)
return this.provider.rawCall("searchlogs", args)
}

public async checkTransactionWaitSupport(): Promise<boolean> {
if (this._hasTxWaitSupport !== undefined) {
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<string> {
return this.rawCall("fromhexaddress", [hexAddress])
return this.provider.rawCall("fromhexaddress", [hexAddress])
}

public async getHexAddress(address: string): Promise<string> {
return this.rawCall("gethexaddress", [address])
return this.provider.rawCall("gethexaddress", [address])
}
}
4 changes: 3 additions & 1 deletion src/QtumRPCRaw.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -21,7 +23,7 @@ export interface IRPCCallOption {
cancelToken?: CancelToken,
}

export class QtumRPCRaw {
export class QtumRPCRaw implements IProvider {
private idNonce: number
private _api: AxiosInstance

Expand Down
6 changes: 3 additions & 3 deletions src/QtumRPC_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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])
})
})

Expand Down
4 changes: 3 additions & 1 deletion src/test/index.ts
Original file line number Diff line number Diff line change
@@ -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")

Expand Down