Skip to content

Commit

Permalink
Merge PR #784 from 'rithvikvibhu/wrpc-gettx-fee'
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Dec 20, 2022
2 parents eb5e6a8 + f49419b commit 234a597
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/wallet/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ class RPC extends RPCBase {
}

const det = [];
const fee = details.getFee();
let sent = 0;
let received = 0;

Expand Down Expand Up @@ -680,7 +681,7 @@ class RPC extends RPCBase {
: null,
category: 'send',
amount: -(Amount.coin(member.value, true)),
fee: -(Amount.coin(details.fee, true)),
fee: -(Amount.coin(fee, true)),
vout: i
});

Expand Down Expand Up @@ -2498,8 +2499,8 @@ class RPC extends RPCBase {
return mtx.getJSON(this.network);
}

_validateBatch(args, help, method) {
if (help || args.length < 1 || args.length > 2) {
_validateBatch(args, help, method) {
if (help || args.length < 1 || args.length > 2) {
throw new RPCError(errs.MISC_ERROR,
`${method} [["type", ...args], ...] ( options )`);
}
Expand Down
72 changes: 72 additions & 0 deletions test/wallet-rpc-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const HDPrivateKey = require('../lib/hd/private');
const Script = require('../lib/script/script');
const Address = require('../lib/primitives/address');
const rules = require('../lib/covenants/rules');
const Amount = require('../lib/ui/amount');

const {types} = rules;
const {forValue} = require('./util/common');
Expand Down Expand Up @@ -952,4 +953,75 @@ describe('Wallet RPC Methods', function() {
await nclient.execute('generatetoaddress', [1, addr]);
});
});

describe('gettransaction', function () {
let alexAddr, barrieAddr;

before(async () => {
await wclient.createWallet('alex');
await wclient.createWallet('barrie');
await wclient.execute('selectwallet', ['alex']);
alexAddr = await wclient.execute('getnewaddress', []);
await wclient.execute('selectwallet', ['barrie']);
barrieAddr = await wclient.execute('getnewaddress', []);
});

async function getCoinbaseTXID(height) {
const block = await nclient.execute('getblockbyheight', [height]);
return block.tx[0];
}

it('should mine 10 tx to each wallet and gettransaction', async () => {
await nclient.execute('generatetoaddress', [10, alexAddr]);
await nclient.execute('generatetoaddress', [10, barrieAddr]);

const height = await nclient.execute('getblockcount', []);

await wclient.execute('selectwallet', ['barrie']);
for (let i = 0; i < 10; i++) {
const txid = await getCoinbaseTXID(height - i);
const json = await wclient.execute('gettransaction', [txid]);
assert.strictEqual(json.amount, 2000);
assert.strictEqual(json.details[0].category, 'receive');
assert.strictEqual(json.details[0].amount, 2000);
assert.strictEqual(json.details[0].fee, undefined);
}

await wclient.execute('selectwallet', ['alex']);
for (let i = 10; i < 20; i++) {
const txid = await getCoinbaseTXID(height - i);
const json = await wclient.execute('gettransaction', [txid]);
assert.strictEqual(json.amount, 2000);
assert.strictEqual(json.details[0].category, 'receive');
assert.strictEqual(json.details[0].amount, 2000);
assert.strictEqual(json.details[0].fee, undefined);
}
});

it('should receive from Barrie to Alex and gettransaction', async () => {
await wclient.execute('selectwallet', ['barrie']);
const txid = await wclient.execute('sendtoaddress', [alexAddr, 10]);
await wclient.execute('selectwallet', ['alex']);
const json = await wclient.execute('gettransaction', [txid]);
assert.strictEqual(json.amount, 10);
assert.strictEqual(json.details[0].category, 'receive');
assert.strictEqual(json.details[0].amount, 10);
assert.strictEqual(json.details[0].fee, undefined);
});

it('should send from Alex to Barrie and gettransaction', async () => {
await wclient.execute('selectwallet', ['alex']);
const txid = await wclient.execute('sendtoaddress', [barrieAddr, 21]);
await wclient.execute('selectwallet', ['alex']);
const json = await wclient.execute('gettransaction', [txid]);
assert.strictEqual(json.amount, -21);
assert.strictEqual(json.details[0].category, 'send');
assert.strictEqual(json.details[0].amount, -21);

const vsize = 140; // 1-in, 2-out pkh
const fee = vsize * network.feeRate / 1000;
const amount = Amount.fromBase(fee).toCoins(); // returned in whole HNS units
assert.strictEqual(json.details[0].fee, amount * -1); // fees are negative
});
});
});

0 comments on commit 234a597

Please sign in to comment.