Skip to content
This repository was archived by the owner on Sep 3, 2019. It is now read-only.

Commit bb2869a

Browse files
authored
Add req params to rpc result (#55)
* Add request object with params to send call * Update version * Change send to async func and add req object correctly * Change request to args * Run linter * Add test for args obj in send call * Use lodash method for undefined * Import lodash
1 parent a44610b commit bb2869a

7 files changed

Lines changed: 58 additions & 29 deletions

File tree

package-lock.json

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "qweb3",
33
"namespace": "bodhi",
4-
"version": "0.7.4",
4+
"version": "0.7.5",
55
"description": "Qtum JavaScript API comunicating to qtum node over RPC",
66
"main": "./src/index.js",
77
"repository": {

src/contract.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,39 @@ class Contract {
4646
* @param params Parameters of the contract method.
4747
* @return The transaction id of the sendtocontract.
4848
*/
49-
send(methodName, params) {
49+
async send(methodName, params) {
5050
// Throw if methodArgs or senderAddress is not defined in params
5151
Utils.paramsCheck('send', params, ['methodArgs', 'senderAddress']);
5252

5353
const {
5454
methodArgs, amount, gasLimit, gasPrice, senderAddress,
5555
} = params;
5656
const { method: methodObj, args } = this.validateMethodAndArgs(methodName, methodArgs);
57+
const amt = amount || SEND_AMOUNT;
58+
const limit = gasLimit || SEND_GASLIMIT;
59+
const price = gasPrice || SEND_GASPRICE;
5760
const options = {
5861
method: 'sendtocontract',
5962
params: [
6063
this.address,
6164
this.constructDataHex(methodObj, args),
62-
amount || SEND_AMOUNT,
63-
gasLimit || SEND_GASLIMIT,
64-
gasPrice || SEND_GASPRICE,
65+
amt,
66+
limit,
67+
price,
6568
senderAddress,
6669
],
6770
};
6871

69-
return this.provider.request(options);
72+
// Add request object with params used for request
73+
const result = await this.provider.request(options);
74+
result.args = {
75+
contractAddress: this.address,
76+
amount: amt,
77+
gasLimit: limit,
78+
gasPrice: price,
79+
};
80+
81+
return result;
7082
}
7183

7284
/*

src/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,13 @@ class Utils {
153153
* @return {Boolean}
154154
*/
155155
static isQtumAddress(address) {
156-
if (_.isUndefined(address)){
156+
if (_.isUndefined(address)) {
157157
return false;
158158
}
159159

160160
if (_.size(address) !== 34) {
161161
return false;
162-
};
162+
}
163163

164164
if (!address.startsWith('q') && !address.startsWith('Q')) {
165165
return false;

test/config/config.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const _ = require('lodash');
2+
13
// Load environment variables
24
require('dotenv').config();
35

@@ -8,33 +10,31 @@ const Config = {
810
ENCRYPTED_WALLET_TESTS: false,
911
};
1012

11-
const qtumRPCAddress = "QTUM_RPC_ADDRESS" in process.env ? String(new Buffer(process.env["QTUM_RPC_ADDRESS"])) : undefined;
12-
const qtumAddress = "SENDER_ADDRESS" in process.env ? String(new Buffer(process.env["SENDER_ADDRESS"])) : undefined;
13+
const qtumRPCAddress = 'QTUM_RPC_ADDRESS' in process.env ? String(new Buffer(process.env.QTUM_RPC_ADDRESS)) : undefined;
14+
const qtumAddress = 'SENDER_ADDRESS' in process.env ? String(new Buffer(process.env.SENDER_ADDRESS)) : undefined;
1315

14-
/* Returns the default Qtum address
16+
/* Returns the default Qtum address
1517
* @return {String} default Qtum address.
1618
*/
1719
function getDefaultQtumAddress() {
18-
if (typeof qtumAddress === "undefined") {
20+
if (_.isUndefined(qtumAddress)) {
1921
return Config.SENDER_ADDRESS;
20-
} else {
21-
return qtumAddress;
2222
}
23-
};
23+
return qtumAddress;
24+
}
2425

25-
/* Returns the Qtum network RPC url
26+
/* Returns the Qtum network RPC url
2627
* @return {String} the Qtum network RPC url.
2728
*/
2829
function getQtumRPCAddress() {
29-
if (typeof qtumRPCAddress === "undefined") {
30+
if (_.isUndefined(qtumRPCAddress)) {
3031
return Config.QTUM_RPC_ADDRESS;
31-
} else {
32-
return qtumRPCAddress;
3332
}
34-
};
33+
return qtumRPCAddress;
34+
}
3535

36-
module.exports = {
36+
module.exports = {
3737
Config,
3838
getQtumRPCAddress,
3939
getDefaultQtumAddress,
40-
}
40+
};

test/contract.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ describe('Contract', () => {
7575
assert.isDefined(res.sender);
7676
assert.isDefined(res.hash160);
7777
});
78+
79+
it('returns the args object with the sent params', async () => {
80+
const res = {
81+
"txid": "60c14ddc003a84a0947db1ac9cbfb01f1d3253b7948dc95343d7dd5a9d5900e9",
82+
"sender": "qUNeKdqh269AwJWxDFnxUAqKSJ9deSUsgF",
83+
"hash160": "76a177b79b8ef37437dce27a38fa2653eb6d8241",
84+
"args": {
85+
"contractAddress": "f6177bc9812eeb531907621af6641a41133dea9e",
86+
"amount": 0,
87+
"gasLimit": 250000,
88+
"gasPrice": 4e-7
89+
}
90+
};
91+
92+
assert.isString(res.args.contractAddress);
93+
assert.isNumber(res.args.amount);
94+
assert.isNumber(res.args.gasLimit);
95+
assert.isNumber(res.args.gasPrice);
96+
});
7897
});
7998

8099
describe('constructDataHex()', () => {

test/qweb3.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const chai = require('chai');
44
const assert = chai.assert;
55
const expect = chai.expect;
66

7-
const bs58 = require('bs58')
7+
const bs58 = require('bs58');
88

99
const Qweb3 = require('../src/qweb3');
1010
const Formatter = require('../src/formatter');
@@ -310,8 +310,8 @@ describe('Qweb3', () => {
310310
/** ******** RAW TRANSACTIONS ********* */
311311
describe('getHexAddress()', () => {
312312
it('returns the hex address', async () => {
313-
const hexDecodedAddress = bs58.decode(QTUM_ADDRESS).toString('hex')
314-
var hexadecimalAddress = await qweb3.getHexAddress(QTUM_ADDRESS)
313+
const hexDecodedAddress = bs58.decode(QTUM_ADDRESS).toString('hex');
314+
const hexadecimalAddress = await qweb3.getHexAddress(QTUM_ADDRESS);
315315
assert.isString(hexadecimalAddress);
316316
assert.lengthOf(hexadecimalAddress, 40);
317317
assert.include(hexDecodedAddress, hexadecimalAddress);
@@ -320,7 +320,7 @@ describe('Qweb3', () => {
320320

321321
describe('fromHexAddress()', () => {
322322
it('returns the qtum address', async () => {
323-
const qtumAddress = await qweb3.fromHexAddress('17e7888aa7412a735f336d2f6d784caefabb6fa3')
323+
const qtumAddress = await qweb3.fromHexAddress('17e7888aa7412a735f336d2f6d784caefabb6fa3');
324324
assert.isString(qtumAddress);
325325
assert.lengthOf(qtumAddress, 34);
326326
});

0 commit comments

Comments
 (0)