coinUtils fixes
This commit is contained in:
parent
b7aa655587
commit
5a26f718c5
9 changed files with 63 additions and 71 deletions
|
|
@ -6,7 +6,6 @@ const makeDir = require('make-dir')
|
|||
const inquirer = require('inquirer')
|
||||
const _ = require('lodash/fp')
|
||||
|
||||
const options = require('../options')
|
||||
const coinUtils = require('../coin-utils')
|
||||
|
||||
const common = require('./common')
|
||||
|
|
@ -27,7 +26,7 @@ const PLUGINS = {
|
|||
module.exports = {run}
|
||||
|
||||
function installedFilePath (crypto) {
|
||||
return path.resolve(options.blockchainDir, crypto.code, '.installed')
|
||||
return path.resolve(coinUtils.cryptoDir(crypto), '.installed')
|
||||
}
|
||||
|
||||
function isInstalled (crypto) {
|
||||
|
|
@ -57,7 +56,7 @@ function processCryptos (codes) {
|
|||
|
||||
function setupCrypto (crypto) {
|
||||
logger.info(`Installing ${crypto.display}...`)
|
||||
const cryptoDir = path.resolve(options.blockchainDir, crypto.code)
|
||||
const cryptoDir = coinUtils.cryptoConfigDir(crypto.cryptoCode)
|
||||
makeDir.sync(cryptoDir)
|
||||
const cryptoPlugin = plugin(crypto)
|
||||
const oldDir = process.cwd()
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
const path = require('path')
|
||||
|
||||
const _ = require('lodash/fp')
|
||||
|
||||
const options = require('./options')
|
||||
|
||||
const CRYPTO_CURRENCIES = [
|
||||
{
|
||||
cryptoCode: 'BTC',
|
||||
|
|
@ -48,7 +52,7 @@ const CRYPTO_CURRENCIES = [
|
|||
}
|
||||
]
|
||||
|
||||
module.exports = {buildUrl, cryptoCurrencies, getCryptoCurrency}
|
||||
module.exports = {buildUrl, cryptoDir, cryptoConfigDir, cryptoCurrencies, getCryptoCurrency}
|
||||
|
||||
function getCryptoCurrency (cryptoCode) {
|
||||
const cryptoCurrency = _.find(['cryptoCode', cryptoCode], CRYPTO_CURRENCIES)
|
||||
|
|
@ -70,3 +74,14 @@ function buildUrl (cryptoCode, address) {
|
|||
default: throw new Error(`Unsupported crypto: ${cryptoCode}`)
|
||||
}
|
||||
}
|
||||
|
||||
function cryptoDir (cryptoRec) {
|
||||
const code = cryptoRec.code
|
||||
const blockchainDir = options.blockchainDir
|
||||
return path.resolve(blockchainDir, code)
|
||||
}
|
||||
|
||||
function cryptoConfigDir (cryptoRec) {
|
||||
const configFile = cryptoRec.configFile
|
||||
return path.resolve(cryptoDir(cryptoRec), configFile)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,5 @@
|
|||
const BigNumber = require('bignumber.js')
|
||||
|
||||
const coinUtils = require('../../coin-utils')
|
||||
|
||||
const TEN = new BigNumber(10)
|
||||
|
||||
const PAIRS = {
|
||||
BTC: {
|
||||
USD: 'XXBTZUSD',
|
||||
|
|
@ -29,7 +25,8 @@ const PAIRS = {
|
|||
|
||||
module.exports = {PAIRS, toUnit}
|
||||
|
||||
function toUnit (cryptoAtoms, cryptoCoin) {
|
||||
var scale = TEN.pow(coinUtils.unitScale(cryptoCoin))
|
||||
return cryptoAtoms.div(scale)
|
||||
function toUnit (cryptoAtoms, cryptoCode) {
|
||||
const cryptoRec = coinUtils.getCryptoCurrency(cryptoCode)
|
||||
const unitScale = cryptoRec.unitScale
|
||||
return cryptoAtoms.shift(-unitScale)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +1,18 @@
|
|||
const jsonRpc = require('../../common/json-rpc')
|
||||
|
||||
const path = require('path')
|
||||
const options = require('../../../options')
|
||||
const BN = require('../../../bn')
|
||||
const E = require('../../../error')
|
||||
const coinUtils = require('../../../coin-utils')
|
||||
|
||||
const DEFAULT_PORT = 8332
|
||||
const SATOSHI_SHIFT = 8
|
||||
|
||||
const configPath = path.resolve(options.blockchainDir, 'bitcoin.conf')
|
||||
const cryptoRec = coinUtils.getCryptoCurrency('BTC')
|
||||
const configPath = coinUtils.cryptoDir(cryptoRec)
|
||||
const unitScale = cryptoRec.unitScale
|
||||
const config = jsonRpc.parseConf(configPath)
|
||||
|
||||
const rpcConfig = {
|
||||
username: config.rpcuser,
|
||||
password: config.rpcpassword,
|
||||
port: config.rpcport || DEFAULT_PORT
|
||||
port: config.rpcport || cryptoRec.defaultPort
|
||||
}
|
||||
|
||||
function fetch (method, params) {
|
||||
|
|
@ -29,7 +27,7 @@ function checkCryptoCode (cryptoCode) {
|
|||
function accountBalance (acount, cryptoCode, confirmations) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => fetch('getbalance', ['', confirmations]))
|
||||
.then(r => BN(r).shift(SATOSHI_SHIFT).round())
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
}
|
||||
|
||||
// We want a balance that includes all spends (0 conf) but only deposits that
|
||||
|
|
@ -39,7 +37,7 @@ function balance (account, cryptoCode) {
|
|||
}
|
||||
|
||||
function sendCoins (account, address, cryptoAtoms, cryptoCode) {
|
||||
const coins = cryptoAtoms.shift(-SATOSHI_SHIFT).toFixed(8)
|
||||
const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
|
||||
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => fetch('sendtoaddress', [address, coins]))
|
||||
|
|
@ -56,7 +54,7 @@ function newAddress (account, info) {
|
|||
|
||||
function addressBalance (address, confs) {
|
||||
return fetch('getreceivedbyaddress', [address, confs])
|
||||
.then(r => BN(r).shift(SATOSHI_SHIFT).round())
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
}
|
||||
|
||||
function confirmedBalance (address, cryptoCode) {
|
||||
|
|
|
|||
|
|
@ -1,22 +1,19 @@
|
|||
const jsonRpc = require('../../common/json-rpc')
|
||||
|
||||
const path = require('path')
|
||||
const options = require('../../../options')
|
||||
const coinUtils = require('../../../coin-utils')
|
||||
|
||||
const BN = require('../../../bn')
|
||||
const E = require('../../../error')
|
||||
|
||||
const DEFAULT_PORT = 9998
|
||||
const SATOSHI_SHIFT = 8
|
||||
|
||||
const configPath = path.resolve(options.blockchainDir, 'dash.conf')
|
||||
|
||||
const cryptoRec = coinUtils.getCryptoCurrency('DASH')
|
||||
const configPath = coinUtils.cryptoDir(cryptoRec)
|
||||
const unitScale = cryptoRec.unitScale
|
||||
const config = jsonRpc.parseConf(configPath)
|
||||
|
||||
const rpcConfig = {
|
||||
username: config.rpcuser,
|
||||
password: config.rpcpassword,
|
||||
port: config.rpcport || DEFAULT_PORT
|
||||
port: config.rpcport || cryptoRec.defaultPort
|
||||
}
|
||||
|
||||
function fetch (method, params) {
|
||||
|
|
@ -31,7 +28,7 @@ function checkCryptoCode (cryptoCode) {
|
|||
function accountBalance (acount, cryptoCode, confirmations) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => fetch('getbalance', ['', confirmations]))
|
||||
.then(r => BN(r).shift(SATOSHI_SHIFT).round())
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
}
|
||||
|
||||
// We want a balance that includes all spends (0 conf) but only deposits that
|
||||
|
|
@ -41,7 +38,7 @@ function balance (account, cryptoCode) {
|
|||
}
|
||||
|
||||
function sendCoins (account, address, cryptoAtoms, cryptoCode) {
|
||||
const coins = cryptoAtoms.shift(-SATOSHI_SHIFT).toFixed(8)
|
||||
const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
|
||||
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => fetch('sendtoaddress', [address, coins]))
|
||||
|
|
@ -58,7 +55,7 @@ function newAddress (account, info) {
|
|||
|
||||
function addressBalance (address, confs) {
|
||||
return fetch('getreceivedbyaddress', [address, confs])
|
||||
.then(r => BN(r).shift(SATOSHI_SHIFT).round())
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
}
|
||||
|
||||
function confirmedBalance (address, cryptoCode) {
|
||||
|
|
|
|||
|
|
@ -1,18 +1,22 @@
|
|||
'use strict'
|
||||
|
||||
const _ = require('lodash/fp')
|
||||
const Web3 = require('web3')
|
||||
const web3 = new Web3()
|
||||
const hdkey = require('ethereumjs-wallet/hdkey')
|
||||
const Tx = require('ethereumjs-tx')
|
||||
const pify = require('pify')
|
||||
|
||||
const coinUtils = require('../../../coin-utils')
|
||||
|
||||
const NAME = 'geth'
|
||||
exports.SUPPORTED_MODULES = ['wallet']
|
||||
|
||||
const paymentPrefixPath = "m/44'/60'/0'/0'"
|
||||
const defaultPrefixPath = "m/44'/60'/1'/0'"
|
||||
|
||||
const cryptoRec = coinUtils.getCryptoCurrency('ETH')
|
||||
const defaultPort = cryptoRec.defaultPort
|
||||
|
||||
module.exports = {
|
||||
NAME,
|
||||
balance,
|
||||
|
|
@ -27,7 +31,7 @@ module.exports = {
|
|||
}
|
||||
|
||||
if (!web3.isConnected()) {
|
||||
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'))
|
||||
web3.setProvider(new web3.providers.HttpProvider(`http://localhost:${defaultPort}`))
|
||||
}
|
||||
|
||||
const hex = bigNum => '0x' + bigNum.truncated().toString(16)
|
||||
|
|
@ -38,7 +42,6 @@ function privateKey (account) {
|
|||
|
||||
function sendCoins (account, toAddress, cryptoAtoms, cryptoCode) {
|
||||
return generateTx(toAddress, defaultWallet(account), cryptoAtoms, false)
|
||||
.then(_.tap(r => console.log('DEBUG113: %s', r)))
|
||||
.then(pify(web3.eth.sendRawTransaction))
|
||||
}
|
||||
|
||||
|
|
@ -58,7 +61,6 @@ const confirmedBalance = address => _balance(false, address)
|
|||
function _balance (includePending, address) {
|
||||
const block = includePending ? 'pending' : undefined
|
||||
|
||||
console.log('DEBUG140: %s', address)
|
||||
return pify(web3.eth.getBalance)(address.toLowerCase(), block)
|
||||
}
|
||||
|
||||
|
|
@ -80,7 +82,6 @@ function generateTx (_toAddress, wallet, amount, includesFee) {
|
|||
|
||||
return Promise.all(promises)
|
||||
.then(arr => {
|
||||
console.log('DEBUG111')
|
||||
const gas = arr[0]
|
||||
const gasPrice = arr[1]
|
||||
const txCount = arr[2]
|
||||
|
|
@ -97,7 +98,6 @@ function generateTx (_toAddress, wallet, amount, includesFee) {
|
|||
from: fromAddress,
|
||||
value: hex(toSend)
|
||||
}
|
||||
console.log('DEBUG112: %j', rawTx)
|
||||
|
||||
const tx = new Tx(rawTx)
|
||||
const privateKey = wallet.getPrivateKey()
|
||||
|
|
@ -117,26 +117,19 @@ function defaultAddress (account) {
|
|||
}
|
||||
|
||||
function sweep (account, cryptoCode, hdIndex) {
|
||||
console.log('DEBUG115: %d', hdIndex)
|
||||
const wallet = paymentHdNode(account).deriveChild(hdIndex).getWallet()
|
||||
const fromAddress = wallet.getChecksumAddressString()
|
||||
|
||||
console.log('DEBUG115.1: %s', fromAddress)
|
||||
|
||||
return confirmedBalance(fromAddress)
|
||||
.then(r => {
|
||||
console.log('DEBUG116.0: %j', r)
|
||||
if (r.eq(0)) return
|
||||
|
||||
console.log('DEBUG116')
|
||||
|
||||
return generateTx(defaultAddress(account), wallet, r, true)
|
||||
.then(signedTx => pify(web3.eth.sendRawTransaction)(signedTx))
|
||||
})
|
||||
}
|
||||
|
||||
function newAddress (account, info) {
|
||||
console.log('DEBUG120: %d', info.hdIndex)
|
||||
const childNode = paymentHdNode(account).deriveChild(info.hdIndex)
|
||||
return Promise.resolve(childNode.getWallet().getChecksumAddressString())
|
||||
}
|
||||
|
|
@ -149,7 +142,6 @@ function getStatus (account, toAddress, cryptoAtoms, cryptoCode) {
|
|||
|
||||
return pendingBalance(toAddress)
|
||||
.then(pending => {
|
||||
console.log('DEBUG114: %s', pending.toString())
|
||||
if (pending.gte(cryptoAtoms)) return {status: 'published'}
|
||||
if (pending.gt(0)) return {status: 'insufficientFunds'}
|
||||
return {status: 'notSeen'}
|
||||
|
|
|
|||
|
|
@ -1,26 +1,21 @@
|
|||
const jsonRpc = require('../../common/json-rpc')
|
||||
|
||||
const path = require('path')
|
||||
const options = require('../../../options')
|
||||
const coinUtils = require('../../../coin-utils')
|
||||
|
||||
const BN = require('../../../bn')
|
||||
const E = require('../../../error')
|
||||
|
||||
const DEFAULT_PORT = 9332
|
||||
const SATOSHI_SHIFT = 8
|
||||
|
||||
const configPath = path.resolve(options.blockchainDir, 'litecoin.conf')
|
||||
const cryptoRec = coinUtils.getCryptoCurrency('LTC')
|
||||
const configPath = coinUtils.cryptoDir(cryptoRec)
|
||||
const unitScale = cryptoRec.unitScale
|
||||
const config = jsonRpc.parseConf(configPath)
|
||||
|
||||
const rpcConfig = {
|
||||
username: config.rpcuser,
|
||||
password: config.rpcpassword,
|
||||
port: config.rpcport || DEFAULT_PORT
|
||||
port: config.rpcport || cryptoRec.defaultPort
|
||||
}
|
||||
|
||||
console.log('DEBUG101: %j', configPath)
|
||||
console.log('DEBUG100: %j', rpcConfig)
|
||||
|
||||
function fetch (method, params) {
|
||||
return jsonRpc.fetch(rpcConfig, method, params)
|
||||
}
|
||||
|
|
@ -33,7 +28,7 @@ function checkCryptoCode (cryptoCode) {
|
|||
function accountBalance (acount, cryptoCode, confirmations) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => fetch('getbalance', ['', confirmations]))
|
||||
.then(r => BN(r).shift(SATOSHI_SHIFT).round())
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
}
|
||||
|
||||
// We want a balance that includes all spends (0 conf) but only deposits that
|
||||
|
|
@ -43,7 +38,7 @@ function balance (account, cryptoCode) {
|
|||
}
|
||||
|
||||
function sendCoins (account, address, cryptoAtoms, cryptoCode) {
|
||||
const coins = cryptoAtoms.shift(-SATOSHI_SHIFT).toFixed(8)
|
||||
const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
|
||||
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => fetch('sendtoaddress', [address, coins]))
|
||||
|
|
@ -60,7 +55,7 @@ function newAddress (account, info) {
|
|||
|
||||
function addressBalance (address, confs) {
|
||||
return fetch('getreceivedbyaddress', [address, confs])
|
||||
.then(r => BN(r).shift(SATOSHI_SHIFT).round())
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
}
|
||||
|
||||
function confirmedBalance (address, cryptoCode) {
|
||||
|
|
|
|||
|
|
@ -12,8 +12,9 @@ const CONFIRM_TIME = 180 * SECONDS
|
|||
let t0
|
||||
|
||||
function _balance (cryptoCode) {
|
||||
const unitScale = coinUtils.unitScale(cryptoCode)
|
||||
return BN(10).pow(unitScale).mul(10)
|
||||
const cryptoRec = coinUtils.getCryptoCurrency(cryptoCode)
|
||||
const unitScale = cryptoRec.unitScale
|
||||
BN(10).shift(unitScale).round()
|
||||
}
|
||||
|
||||
function balance (account, cryptoCode) {
|
||||
|
|
|
|||
|
|
@ -1,21 +1,19 @@
|
|||
const jsonRpc = require('../../common/json-rpc')
|
||||
|
||||
const path = require('path')
|
||||
const options = require('../../../options')
|
||||
const coinUtils = require('../../../coin-utils')
|
||||
|
||||
const BN = require('../../../bn')
|
||||
const E = require('../../../error')
|
||||
|
||||
const DEFAULT_PORT = 8232
|
||||
const SATOSHI_SHIFT = 8
|
||||
|
||||
const configPath = path.resolve(options.blockchainDir, 'zcash.conf')
|
||||
const cryptoRec = coinUtils.getCryptoCurrency('ZEC')
|
||||
const configPath = coinUtils.cryptoDir(cryptoRec)
|
||||
const unitScale = cryptoRec.unitScale
|
||||
const config = jsonRpc.parseConf(configPath)
|
||||
|
||||
const rpcConfig = {
|
||||
username: config.rpcuser,
|
||||
password: config.rpcpassword,
|
||||
port: config.rpcport || DEFAULT_PORT
|
||||
port: config.rpcport || cryptoRec.defaultPort
|
||||
}
|
||||
|
||||
function fetch (method, params) {
|
||||
|
|
@ -30,7 +28,7 @@ function checkCryptoCode (cryptoCode) {
|
|||
function accountBalance (acount, cryptoCode, confirmations) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => fetch('getbalance', ['', confirmations]))
|
||||
.then(r => BN(r).shift(SATOSHI_SHIFT).round())
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
}
|
||||
|
||||
// We want a balance that includes all spends (0 conf) but only deposits that
|
||||
|
|
@ -40,7 +38,7 @@ function balance (account, cryptoCode) {
|
|||
}
|
||||
|
||||
function sendCoins (account, address, cryptoAtoms, cryptoCode) {
|
||||
const coins = cryptoAtoms.shift(-SATOSHI_SHIFT).toFixed(8)
|
||||
const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
|
||||
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => fetch('sendtoaddress', [address, coins]))
|
||||
|
|
@ -57,7 +55,7 @@ function newAddress (account, info) {
|
|||
|
||||
function addressBalance (address, confs) {
|
||||
return fetch('getreceivedbyaddress', [address, confs])
|
||||
.then(r => BN(r).shift(SATOSHI_SHIFT).round())
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
}
|
||||
|
||||
function confirmedBalance (address, cryptoCode) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue