bitpay / bitcore

A full stack for bitcoin and blockchain-based applications
https://bitcore.io/
MIT License
4.8k stars 2.07k forks source link

change method does not work #3609

Open Sequoya42 opened 12 months ago

Sequoya42 commented 12 months ago

Assuming for example that from contains 1 doge, send 0.5 to to, expect from to end up at 0.5 (minus fees). All parameters are valid. Does not work. Rest of the funds are sent to miner.

https://sochain.com/tx/DOGE/35d551e393f5c6822fcd0f0989cce980f75d40824b136f31980e3393810a49a4

Using the following code

  const transactionFee = 2e6; // 0.02 doge

  let tx = new Transaction().fee(transactionFee)
    .from(from.utxos)
    .to(to.address, Math.floor(amount))
    .change(from.address)
    .sign(from.privkey);

Force me to do something like

  let tx = new Transaction().fee(transactionFee)
    .from(from.utxos)
    .to(to.address, Math.floor(amount))
    .to(from.address, balance - amount - transactionFee - taxTransfer)
    .change(from.address)
    .sign(from.privkey);

For it to work. According to the documentation :

var transaction = new Transaction()
    .from(utxos)          // Feed information about what unspent outputs one can use
    .to(address, amount)  // Add an output with the given amount of satoshis
    .change(address)      // Sets up a change address where the rest of the funds will go
    .sign(privkeySet)     // Signs all the inputs it can
Change function should send the rest of the fund back to the from address.