api: use satoshi values for everything.

This commit is contained in:
Christopher Jeffrey 2017-07-12 20:23:51 -07:00
parent 47adb5a5a7
commit 2f51fd1c50
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
11 changed files with 2708 additions and 2722 deletions

19
bin/cli
View File

@ -6,6 +6,7 @@ const Config = require('../lib/node/config');
const util = require('../lib/utils/util');
const Client = require('../lib/http/client');
const Wallet = require('../lib/http/wallet');
const Amount = require('../lib/btc/amount');
function CLI() {
this.config = new Config('bcoin');
@ -60,6 +61,8 @@ CLI.prototype.createWallet = async function createWallet() {
}
wallet = await this.client.createWallet(options);
wallet.state.confirmed = Amount.btc(wallet.state.confirmed);
wallet.state.unconfirmed = Amount.btc(wallet.state.unconfirmed);
this.log(wallet);
};
@ -150,6 +153,8 @@ CLI.prototype.getAccounts = async function getAccounts() {
CLI.prototype.getWallet = async function getWallet() {
let info = await this.wallet.getInfo();
info.state.confirmed = Amount.btc(info.state.confirmed);
info.state.unconfirmed = Amount.btc(info.state.unconfirmed);
this.log(info);
};
@ -268,6 +273,8 @@ CLI.prototype.listenWallet = async function listenWallet() {
CLI.prototype.getBalance = async function getBalance() {
let account = this.config.str('account');
let balance = await this.wallet.getBalance(account);
balance.confirmed = Amount.btc(balance.confirmed);
balance.unconfirmed = Amount.btc(balance.unconfirmed);
this.log(balance);
};
@ -282,12 +289,12 @@ CLI.prototype.sendTX = async function sendTX() {
if (this.config.has('script')) {
output = {
script: this.config.str('script'),
value: this.config.str([0, 'value'])
value: this.config.amt([0, 'value'])
};
} else {
output = {
address: this.config.str([0, 'address']),
value: this.config.str([1, 'value'])
value: this.config.amt([1, 'value'])
};
}
@ -296,7 +303,7 @@ CLI.prototype.sendTX = async function sendTX() {
passphrase: this.config.str('passphrase'),
outputs: [output],
smart: this.config.bool('smart'),
rate: this.config.str('rate'),
rate: this.config.amt('rate'),
subtractFee: this.config.bool('subtract-fee')
};
@ -311,12 +318,12 @@ CLI.prototype.createTX = async function createTX() {
if (this.config.has('script')) {
output = {
script: this.config.str('script'),
value: this.config.str([0, 'value'])
value: this.config.amt([0, 'value'])
};
} else {
output = {
address: this.config.str([0, 'address']),
value: this.config.str([1, 'value'])
value: this.config.amt([1, 'value'])
};
}
@ -325,7 +332,7 @@ CLI.prototype.createTX = async function createTX() {
passphrase: this.config.str('passphrase'),
outputs: [output],
smart: this.config.bool('smart'),
rate: this.config.str('rate')
rate: this.config.amt('rate')
};
tx = await this.wallet.createTX(options);

View File

@ -10,7 +10,6 @@
const Network = require('../protocol/network');
const AsyncObject = require('../utils/asyncobject');
const RPCClient = require('./rpcclient');
const Amount = require('../btc/amount');
const util = require('../utils/util');
const request = require('./request');
@ -638,12 +637,9 @@ HTTPClient.prototype.send = function send(id, options) {
if (!body.outputs)
body.outputs = [];
if (body.rate)
body.rate = Amount.btc(body.rate);
body.outputs = body.outputs.map((output) => {
return {
value: Amount.btc(output.value),
value: output.value,
address: output.address,
script: toHex(output.script)
};
@ -688,12 +684,9 @@ HTTPClient.prototype.createTX = function createTX(id, options) {
if (!body.outputs)
body.outputs = [];
if (body.rate)
body.rate = Amount.btc(body.rate);
body.outputs = body.outputs.map((output) => {
return {
value: Amount.btc(output.value),
value: output.value,
address: output.address,
script: toHex(output.script)
};

View File

@ -12,7 +12,6 @@ const path = require('path');
const HTTPBase = require('./base');
const util = require('../utils/util');
const base58 = require('../utils/base58');
const Amount = require('../btc/amount');
const Bloom = require('../utils/bloom');
const TX = require('../primitives/tx');
const Outpoint = require('../primitives/outpoint');
@ -328,13 +327,13 @@ HTTPServer.prototype.initRouter = function initRouter() {
let fee;
if (!this.fees) {
res.send(200, { rate: Amount.btc(this.network.feeRate) });
res.send(200, { rate: this.network.feeRate });
return;
}
fee = this.fees.estimateFee(blocks);
res.send(200, { rate: Amount.btc(fee) });
res.send(200, { rate: fee });
});
// Reset chain
@ -495,18 +494,11 @@ HTTPServer.prototype.handleAuth = function handleAuth(socket) {
socket.hook('estimate fee', (args) => {
let valid = new Validator([args]);
let blocks = valid.u32(0);
let rate;
if (!this.fees) {
rate = this.network.feeRate;
rate = Amount.btc(rate);
return rate;
}
if (!this.fees)
return this.network.feeRate;
rate = this.fees.estimateFee(blocks);
rate = Amount.btc(rate);
return rate;
return this.fees.estimateFee(blocks);
});
socket.hook('send', (args) => {

View File

@ -245,7 +245,7 @@ Coin.prototype.getJSON = function getJSON(network, minimal) {
return {
version: this.version,
height: this.height,
value: Amount.btc(this.value),
value: this.value,
script: this.script.toJSON(),
address: addr,
coinbase: this.coinbase,
@ -265,12 +265,12 @@ Coin.prototype.fromJSON = function fromJSON(json) {
assert(util.isUInt32(json.version), 'Version must be a uint32.');
assert(json.height === -1 || util.isUInt32(json.height),
'Height must be a uint32.');
assert(typeof json.value === 'string', 'Value must be a string.');
assert(util.isUInt53(json.value), 'Value must be a uint53.');
assert(typeof json.coinbase === 'boolean', 'Coinbase must be a boolean.');
this.version = json.version;
this.height = json.height;
this.value = Amount.value(json.value);
this.value = json.value;
this.script.fromJSON(json.script);
this.coinbase = json.coinbase;

View File

@ -189,7 +189,7 @@ Output.prototype.getJSON = function getJSON(network) {
addr = addr.toString(network);
return {
value: Amount.btc(this.value),
value: this.value,
script: this.script.toJSON(),
address: addr
};
@ -248,8 +248,8 @@ Output.prototype.isDust = function isDust(rate) {
Output.prototype.fromJSON = function fromJSON(json) {
assert(json, 'Output data is required.');
assert(typeof json.value === 'string', 'Value must be a string.');
this.value = Amount.value(json.value);
assert(util.isUInt53(json.value), 'Value must be a uint53.');
this.value = json.value;
this.script.fromJSON(json.script);
return this;
};

View File

@ -2119,9 +2119,6 @@ TX.prototype.getJSON = function getJSON(network, view, entry, index) {
// Rate can exceed 53 bits in testing.
if (!util.isSafeInteger(rate))
rate = 0;
fee = Amount.btc(fee);
rate = Amount.btc(rate);
}
if (entry) {

View File

@ -13,7 +13,6 @@ const AsyncObject = require('../utils/asyncobject');
const TX = require('../primitives/tx');
const {BlockMeta} = require('./records');
const Headers = require('../primitives/headers');
const Amount = require('../btc/amount');
const util = require('../utils/util');
const BufferReader = require('../utils/reader');
@ -284,8 +283,7 @@ WalletClient.prototype.resetFilter = function resetFilter() {
WalletClient.prototype.estimateFee = function estimateFee(blocks) {
return new Promise((resolve, reject) => {
this.socket.emit('estimate fee', blocks,
wrap(resolve, reject, Amount.value));
this.socket.emit('estimate fee', blocks, wrap(resolve, reject));
});
};

View File

@ -381,12 +381,12 @@ HTTPServer.prototype.initRouter = function initRouter() {
let valid = req.valid();
let passphrase = valid.str('passphrase');
let outputs = valid.array('outputs');
let options, tx, details, output, script;
let options, tx, details;
options = {
rate: valid.amt('rate'),
rate: valid.u64('rate'),
blocks: valid.u32('blocks'),
maxFee: valid.amt('maxFee'),
maxFee: valid.u64('maxFee'),
selection: valid.str('selection'),
smart: valid.bool('smart'),
subtractFee: valid.bool('subtractFee'),
@ -394,9 +394,9 @@ HTTPServer.prototype.initRouter = function initRouter() {
outputs: []
};
for (output of outputs) {
valid = new Validator([output]);
script = null;
for (let output of outputs) {
let valid = new Validator([output]);
let script = null;
if (valid.has('script')) {
script = valid.buf('script');
@ -406,7 +406,7 @@ HTTPServer.prototype.initRouter = function initRouter() {
options.outputs.push({
script: script,
address: valid.str('address'),
value: valid.amt('value')
value: valid.u64('value')
});
}
@ -422,11 +422,11 @@ HTTPServer.prototype.initRouter = function initRouter() {
let valid = req.valid();
let passphrase = valid.str('passphrase');
let outputs = valid.array('outputs');
let options, tx, output, script;
let options, tx;
options = {
rate: valid.amt('rate'),
maxFee: valid.amt('maxFee'),
rate: valid.u64('rate'),
maxFee: valid.u64('maxFee'),
selection: valid.str('selection'),
smart: valid.bool('smart'),
subtractFee: valid.bool('subtractFee'),
@ -434,9 +434,9 @@ HTTPServer.prototype.initRouter = function initRouter() {
outputs: []
};
for (output of outputs) {
valid = new Validator([output]);
script = null;
for (let output of outputs) {
let valid = new Validator([output]);
let script = null;
if (valid.has('script')) {
script = valid.buf('script');
@ -446,7 +446,7 @@ HTTPServer.prototype.initRouter = function initRouter() {
options.outputs.push({
script: script,
address: valid.str('address'),
value: valid.amt('value')
value: valid.u64('value')
});
}

View File

@ -2574,8 +2574,8 @@ Balance.prototype.toJSON = function toJSON(minimal) {
wid: !minimal ? this.wid : undefined,
id: !minimal ? this.id : undefined,
account: !minimal ? this.account : undefined,
unconfirmed: Amount.btc(this.unconfirmed),
confirmed: Amount.btc(this.confirmed)
unconfirmed: this.unconfirmed,
confirmed: this.confirmed
};
};
@ -2708,8 +2708,8 @@ TXDBState.prototype.toJSON = function toJSON(minimal) {
id: !minimal ? this.id : undefined,
tx: this.tx,
coin: this.coin,
unconfirmed: Amount.btc(this.unconfirmed),
confirmed: Amount.btc(this.confirmed)
unconfirmed: this.unconfirmed,
confirmed: this.confirmed
};
};
@ -2996,8 +2996,8 @@ Details.prototype.toJSON = function toJSON() {
index: this.index,
size: this.size,
virtualSize: this.vsize,
fee: Amount.btc(fee),
rate: Amount.btc(rate),
fee: fee,
rate: rate,
confirmations: this.getDepth(),
inputs: this.inputs.map((input) => {
return input.getJSON(this.network);
@ -3044,7 +3044,7 @@ DetailsMember.prototype.toJSON = function toJSON() {
DetailsMember.prototype.getJSON = function getJSON(network) {
return {
value: Amount.btc(this.value),
value: this.value,
address: this.address
? this.address.toString(network)
: null,

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,6 @@ const assert = require('assert');
const consensus = require('../lib/protocol/consensus');
const encoding = require('../lib/utils/encoding');
const co = require('../lib/utils/co');
const Amount = require('../lib/btc/amount');
const Address = require('../lib/primitives/address');
const Script = require('../lib/script/script');
const Outpoint = require('../lib/primitives/outpoint');
@ -94,16 +93,16 @@ describe('HTTP', function() {
assert.equal(receive.type, 'pubkeyhash');
assert.equal(receive.branch, 0);
assert(balance);
assert.equal(Amount.value(balance.confirmed), 0);
assert.equal(Amount.value(balance.unconfirmed), 201840);
assert.equal(balance.confirmed, 0);
assert.equal(balance.unconfirmed, 201840);
assert(details);
assert.equal(details.hash, tx.rhash());
});
it('should get balance', async () => {
let balance = await wallet.getBalance();
assert.equal(Amount.value(balance.confirmed), 0);
assert.equal(Amount.value(balance.unconfirmed), 201840);
assert.equal(balance.confirmed, 0);
assert.equal(balance.unconfirmed, 201840);
});
it('should send a tx', async () => {
@ -124,8 +123,8 @@ describe('HTTP', function() {
assert.equal(tx.inputs.length, 1);
assert.equal(tx.outputs.length, 2);
value += Amount.value(tx.outputs[0].value);
value += Amount.value(tx.outputs[1].value);
value += tx.outputs[0].value;
value += tx.outputs[1].value;
assert.equal(value, 48190);
hash = tx.hash;
@ -146,7 +145,7 @@ describe('HTTP', function() {
it('should get balance', async () => {
let balance = await wallet.getBalance();
assert.equal(Amount.value(balance.unconfirmed), 199570);
assert.equal(balance.unconfirmed, 199570);
});
it('should execute an rpc call', async () => {