bitcoinjs / coinselect

An unspent transaction output (UTXO) selection module for bitcoin.
MIT License
180 stars 101 forks source link

Inputs and outputs always undefined . #38

Closed alishaagupta closed 5 years ago

alishaagupta commented 5 years ago

My testnet btc address has .53 btc and i am trying to select minimum utxos to send .01 btc to an address. but it always shows undefined inputs and outputs!

dcousens commented 5 years ago

Show your code

alishaagupta commented 5 years ago
let coinSelect = require('coinselect/blackjack')
var utxo = 
[{"txid":"afb9fba00cb7324567a582684d1ab76fca6697c49327bb1f9b81580e5347cd38","vout":0,"address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH","account":"","scriptPubKey":"76a914c71f6c7dcc76d87440e47d3fd2623625f9d0e4c788ac","amount":0.0022,"confirmations":1,"spendable":false,"solvable":false,"safe":true},{"txid":"6bd287720f09229ec6a7f46979f52df5650509d6de2c91e23c4e60b62184c150","vout":1,"address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH","account":"","scriptPubKey":"76a914c71f6c7dcc76d87440e47d3fd2623625f9d0e4c788ac","amount":0.0021,"confirmations":1,"spendable":false,"solvable":false,"safe":true},{"txid":"32800034f85f2a58bdc69c43ebec8375af082fa405c836d4e251d96b8b6ae255","vout":1,"address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH","account":"","scriptPubKey":"76a914c71f6c7dcc76d87440e47d3fd2623625f9d0e4c788ac","amount":0.0022,"confirmations":1,"spendable":false,"solvable":false,"safe":true},{"txid":"870c85930ce38c53a59ab2fe8eba4ffaf13059e3c650e341744eb281ce3e6287","vout":1,"address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH","account":"","scriptPubKey":"76a914c71f6c7dcc76d87440e47d3fd2623625f9d0e4c788ac","amount":0.0021,"confirmations":1,"spendable":false,"solvable":false,"safe":true},{"txid":"3195afe4de7479fb39e7552c4b21dbb46d09966f9b67c3235d1480b69742ee8d","vout":1,"address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH","account":"","scriptPubKey":"76a914c71f6c7dcc76d87440e47d3fd2623625f9d0e4c788ac","amount":0.50966016,"confirmations":4,"spendable":false,"solvable":false,"safe":true},{"txid":"4149bb1d8d9398bb841b155b6173b677f8f7605dfdb55e02017f66796547d692","vout":0,"address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH","account":"","scriptPubKey":"76a914c71f6c7dcc76d87440e47d3fd2623625f9d0e4c788ac","amount":0.0023,"confirmations":1,"spendable":false,"solvable":false,"safe":true},{"txid":"dd0edd417bde9fbb6eb890ad161b117c3418dbb10f89d4335da8e6fc535a1496","vout":0,"address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH","account":"","scriptPubKey":"76a914c71f6c7dcc76d87440e47d3fd2623625f9d0e4c788ac","amount":0.0022,"confirmations":1,"spendable":false,"solvable":false,"safe":true},{"txid":"5ec55becfca7bbc9aea711aead34dbd75d0e735f4f8ded5e2201669bb96755a7","vout":1,"address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH","account":"","scriptPubKey":"76a914c71f6c7dcc76d87440e47d3fd2623625f9d0e4c788ac","amount":0.0023,"confirmations":1,"spendable":false,"solvable":false,"safe":true},{"txid":"dda5a2ebbbc27d09590f0c8b54bafcaa988f1894f7f729c499fb0243a93f56bd","vout":1,"address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH","account":"","scriptPubKey":"76a914c71f6c7dcc76d87440e47d3fd2623625f9d0e4c788ac","amount":0.0022,"confirmations":1,"spendable":false,"solvable":false,"safe":true},{"txid":"1365201aa0d83aa18845b6e94afe2abf027ee98361f60175a673482cc263d8c0","vout":0,"address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH","account":"","scriptPubKey":"76a914c71f6c7dcc76d87440e47d3fd2623625f9d0e4c788ac","amount":0.0021,"confirmations":0,"spendable":false,"solvable":false,"safe":false},{"txid":"c86b5bf1d5f42eec5ca84a2c80ea9c387790957c6997592c77d45023b76ec9d2","vout":1,"address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH","account":"","scriptPubKey":"76a914c71f6c7dcc76d87440e47d3fd2623625f9d0e4c788ac","amount":0.0023,"confirmations":1,"spendable":false,"solvable":false,"safe":true},{"txid":"9aea0b284cda841463c5a431678885098721fcec946cce8206bbe2451d2175d4","vout":1,"address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH","account":"","scriptPubKey":"76a914c71f6c7dcc76d87440e47d3fd2623625f9d0e4c788ac","amount":0.0023,"confirmations":1,"spendable":false,"solvable":false,"safe":true},{"txid":"6f3efbb5e2a3a8c62b1dcf63024aa129ade7ab7f464f3421a72371424bc21fef","vout":1,"address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH","account":"","scriptPubKey":"76a914c71f6c7dcc76d87440e47d3fd2623625f9d0e4c788ac","amount":0.0022,"confirmations":1,"spendable":false,"solvable":false,"safe":true},{"txid":"1fd21fe59d3dd887ca2b7a17e273fad92b107bd7a9aea9432129054969750bfd","vout":0,"address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH","account":"","scriptPubKey":"76a914c71f6c7dcc76d87440e47d3fd2623625f9d0e4c788ac","amount":0.0022,"confirmations":1,"spendable":false,"solvable":false,"safe":true}]

let feeRate = 1 // satoshis per byte
let utxos = utxo
let targets = [
  {
    address: 'mn6jRt7sg54DdQy3DbmCGr79rHDtU6hP2q',
    value: 0.01
  } ,
  {
    address: 'myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH',
    value: 0.52846016
  }
]

// ...
let { inputs, outputs, fee } = coinSelect(utxos, targets, feeRate)

// the accumulated fee is always returned for analysis
console.log(fee)

console.log('output' + JSON.stringify(outputs));
// .inputs and .outputs will be undefined if no solution was found
if (!inputs || !outputs) return

let txb = new bitcoin.TransactionBuilder()

inputs.forEach(input => txb.addInput(input.txId, input.vout))
outputs.forEach(output => {
  // watch out, outputs may have been added that you need to provide
  // an output address/script for
  if (!output.address) {
    output.address = wallet.getChangeAddress()
    wallet.nextChangeAddress()
  }

  txb.addOutput(output.address, output.value)
})
alishaagupta commented 5 years ago

what are the units of value in the targets? I believe it could be that issue

alishaagupta commented 5 years ago

@dcousens

dcousens commented 5 years ago

https://github.com/bitcoinjs/coinselect/commit/288f24d221e5f1e5beac883043cc706914b2b2bd

Units are in satoshis.

dcousens commented 5 years ago

Please read and follow the examples, the utxos need a .value field too (in satoshis).

alishaagupta commented 5 years ago

@dcousens I editted my code but still it shows undefined.

let coinSelect = require('coinselect/blackjack') let feeRate = 1 // satoshis per byte let utxos = [ {txId:"afb9fba00cb7324567a582684d1ab76fca6697c49327bb1f9b81580e5347cd38","address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH", vout:0, value: 220000 },

{txId:"6bd287720f09229ec6a7f46979f52df5650509d6de2c91e23c4e60b62184c150","address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH", vout:1, value: 210000},

{txId:"32800034f85f2a58bdc69c43ebec8375af082fa405c836d4e251d96b8b6ae255","address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH", vout:1, value: 220000},

{txId:"870c85930ce38c53a59ab2fe8eba4ffaf13059e3c650e341744eb281ce3e6287","address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH", vout:1, value: 210000},

{txId:"3195afe4de7479fb39e7552c4b21dbb46d09966f9b67c3235d1480b69742ee8d","address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH", vout:1, value: 50966016},

{txId:"4149bb1d8d9398bb841b155b6173b677f8f7605dfdb55e02017f66796547d692","address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH", vout:0, value: 230000},

{txId:"dd0edd417bde9fbb6eb890ad161b117c3418dbb10f89d4335da8e6fc535a1496","address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH", vout:0, value: 220000},

{txId:"5ec55becfca7bbc9aea711aead34dbd75d0e735f4f8ded5e2201669bb96755a7","address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH", vout:1, value: 230000},

{txId:"dda5a2ebbbc27d09590f0c8b54bafcaa988f1894f7f729c499fb0243a93f56bd","address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH", vout:1, value: 220000},

{txId:"1365201aa0d83aa18845b6e94afe2abf027ee98361f60175a673482cc263d8c0","address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH", vout:0, value: 210000},

{txId:"c86b5bf1d5f42eec5ca84a2c80ea9c387790957c6997592c77d45023b76ec9d2","address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH", vout:1, value: 230000},

{txId:"9aea0b284cda841463c5a431678885098721fcec946cce8206bbe2451d2175d4","address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH", vout:1, value: 230000},

{txId:"6f3efbb5e2a3a8c62b1dcf63024aa129ade7ab7f464f3421a72371424bc21fef","address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH", vout:1, value: 220000},

{txId:"1fd21fe59d3dd887ca2b7a17e273fad92b107bd7a9aea9432129054969750bfd","address":"myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH", vout:0, value: 220000} ]

let targets = [ { address: 'mn6jRt7sg54DdQy3DbmCGr79rHDtU6hP2q', // value: 0.01 value: 1000000 //satoshis } , { address: 'myfpQbH489pyt7nDFrWz8ZrrkcnmtrbyKH', // value: 0.52846016 value: 52846010 //satoshis } ]

// ... let { inputs, outputs, fee } = coinSelect(utxos, targets, feeRate)

// the accumulated fee is always returned for analysis console.log(fee)

console.log('output ' + JSON.stringify(inputs)); // .inputs and .outputs will be undefined if no solution was found if (!inputs || !outputs) return

let txb = new bitcoin.TransactionBuilder()

inputs.forEach(input => txb.addInput(input.txId, input.vout)) outputs.forEach(output => { // watch out, outputs may have been added that you need to provide // an output address/script for if (!output.address) { output.address = wallet.getChangeAddress() wallet.nextChangeAddress() }

txb.addOutput(output.address, output.value) })

console.log('tx ' + txb);

alishaagupta commented 5 years ago

@dcousens I can understand there must be some silly mistake but I could not figure out . Need help urgently!

dcousens commented 5 years ago

Have you tried a non exact algorithm? (Something other than blackjack)