Skip to content

Commit

Permalink
fix(rpc): binding the node of method instances to the one of rpc inst…
Browse files Browse the repository at this point in the history
…ance.
  • Loading branch information
Keith-CY committed Jun 17, 2019
1 parent a037fb0 commit f0b486a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 34 deletions.
4 changes: 2 additions & 2 deletions packages/ckb-sdk-core/__tests__/successFixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"target": {
"codeHash": "f1951123466e4479842387a66fabfd6b65fc87fd84ae8e6cd3053edb27fff2fd",
"outPoint": {
"blockHash": "fc3ad90e38598032598c90b4ad4fefb420bacabaa7c5b40111daca7dfcc1f9d4",
"blockHash": "c3b42742ea8658c93de23f16a10c43212bd8b24a6bde5a93b8446dba3fb2a728",
"cell": {
"txHash": "3f09b95f8886723cc850db0beb9c153169151c663f3e8f832dc04421fbb1f382",
"txHash": "6950a2491d7c0ccac11fcb985cbb3f6b5a70eaf6fff952c08a20d1b99c01fb98",
"index": "1"
}
}
Expand Down
6 changes: 1 addition & 5 deletions packages/ckb-sdk-rpc/__tests__/ckb-rpc-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,11 @@ describe('ckb-rpc settings and helpers', () => {
expect(rpc.methods.length).toBe(17)
})

it('has initialized node url of http://localhost:8114', () => {
expect(rpc.methods[0].constructor.node.url).toBe('http://localhost:8114')
})

it('set node url to http://test.localhost:8114', () => {
const url = 'http://test.localhost:8114'
rpc.setNode({
url,
})
expect(rpc.methods[0].constructor.node.url).toBe(url)
expect(rpc.node.url).toBe(url)
})
})
8 changes: 2 additions & 6 deletions packages/ckb-sdk-rpc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@ class CKBRPC extends DefaultRPC {
}

public setNode(node: CKBComponents.Node): CKBComponents.Node {
this.node = {
...this.node,
...node,
}
Method.setNode(this.node)
Object.assign(this.node, node)
return this.node
}

public addMethod = (options: CKBComponents.Method) => {
const method = new Method(options)
const method = new Method(this.node, options)
this.methods.push(method)

Object.defineProperty(this, options.name, {
Expand Down
34 changes: 13 additions & 21 deletions packages/ckb-sdk-rpc/src/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,28 @@ import axios from 'axios'
import { DebugLevel, LogColor } from './enum'

class Method {
private _options: CKBComponents.Method = {
static debugLevel = DebugLevel.Off

private options: CKBComponents.Method = {
name: '',
method: '',
paramsFormatters: [],
resultFormatters: undefined,
}

static debugLevel = DebugLevel.Off

static node: CKBComponents.Node = {
url: '',
}

static setNode = (node: CKBComponents.Node) => {
Method.node = {
...Method.node,
...node,
}
}
private node: CKBComponents.Node

constructor(options: CKBComponents.Method) {
this._options = options
constructor(node: CKBComponents.Node, options: CKBComponents.Method) {
this.node = node
this.options = options
}

public call = (...params: (string | number)[]) => {
const data = params.map((p, i) => (this._options.paramsFormatters[i] && this._options.paramsFormatters[i](p)) || p)
const data = params.map((p, i) => (this.options.paramsFormatters[i] && this.options.paramsFormatters[i](p)) || p)
const id = Math.round(Math.random() * 10000)
const payload = {
id,
method: this._options.method,
method: this.options.method,
params: data,
jsonrpc: '2.0',
}
Expand All @@ -41,7 +33,7 @@ class Method {
'content-type': 'application/json',
},
data: payload,
url: Method.node.url,
url: this.node.url,
}).then(res => {
if (res.data.id !== id) {
throw new Error('JSONRPC id not match')
Expand All @@ -50,11 +42,11 @@ class Method {
/* eslint-disabled */
console.group()
console.group()
console.info(LogColor.Cyan, `\n----- ${this._options.name} request -----`, LogColor.Reset)
console.info(LogColor.Cyan, `\n----- ${this.options.name} request -----`, LogColor.Reset)
console.info(JSON.stringify(payload, null, 2))
console.groupEnd()
console.group()
console.info(LogColor.Cyan, `----- ${this._options.name} response -----`, LogColor.Reset)
console.info(LogColor.Cyan, `----- ${this.options.name} response -----`, LogColor.Reset)
console.info(JSON.stringify(res.data, null, 2))
console.groupEnd()
console.groupEnd()
Expand All @@ -63,7 +55,7 @@ class Method {
if (res.data.error) {
throw new Error(JSON.stringify(res.data.error))
}
return this._options.resultFormatters ? this._options.resultFormatters(res.data.result) : res.data.result
return this.options.resultFormatters ? this.options.resultFormatters(res.data.result) : res.data.result
})
}
}
Expand Down

0 comments on commit f0b486a

Please sign in to comment.