Trying to do this. Seems more difficult than it should.
TransactionId required to make a transaction? This seems like the chicken before the egg.
How can I find the TXID, I checked the documentation and it's just showing hard coded values.
let send = (wif, toAddress, amount, txID) => {
const keyPair = bitcoin.ECPair.fromWIF(wif)
const txb = new bitcoin.TransactionBuilder()
txb.addInput(txID , 0)
txb.addOutput(toAddress, amount)
txb.sign(0, keyPair)
txb.build().toHex()
}
Where can I find txID?
https://testnet-api.smartbit.com.au/v1/blockchain/address/YOUR_ADDRESS/unspent?limit=1000
Where YOUR_ADDRESS is the address you are sending from.
response.unspent is an Array of objects.
The attributes you want are: txid, n and value_int.
Use txid and n to add the input.
txb.addInput(txid , n)
After which, the transaction now has value_int satoshis to add to outputs.
Track the sub-total of value_ints, e.g inputValue += unspent.value_int.
inputValue cannot be greater than the sum-total of your outputs.
fee = inputValue - outputValue
Where fee is the fee for miners.
Adjust your output values (or your input values!) to adjust your fee.
@junderw could we improve the examples do you think?
Most helpful comment
Where
YOUR_ADDRESSis the address you are sending from.response.unspentis an Array of objects.The attributes you want are:
txid,nandvalue_int.Use
txidandnto add the input.After which, the transaction now has
value_intsatoshis to add to outputs.Track the sub-total of
value_ints, e.ginputValue += unspent.value_int.inputValuecannot be greater than the sum-total of your outputs.Where
feeis the fee for miners.Adjust your output values (or your input values!) to adjust your fee.