bitcore icon indicating copy to clipboard operation
bitcore copied to clipboard

change method does not work

Open Sequoya42 opened this issue 2 years ago • 0 comments

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.

Sequoya42 avatar Jul 13 '23 15:07 Sequoya42