Bitcoin Forum
May 11, 2024, 06:51:53 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Send btc from multiple addresses in one transaction.  (Read 387 times)
zee-coin (OP)
Newbie
*
Offline Offline

Activity: 2
Merit: 2


View Profile
August 18, 2018, 08:44:08 AM
 #1

I am currently doing exchnage implementation using blockcypher api and got stuck at one place. After looking number of threads and resources
could not reach a conclusion as a result I am willing to put this here. Hoping for help.

Problem:
How Can I send btc from multiple addresses in one transaction.
Say e.g. there are three address each of them has 2 BTC at any time. Now I want to send 3 btc to address x by consuming two address. How can I decide that from first address deduct 1 btc and from second address deduct 2btc or vice versa in single transaction.


Note: Currently using BlockCypher Api. Any other api could also be suggested or mechanism.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715453513
Hero Member
*
Offline Offline

Posts: 1715453513

View Profile Personal Message (Offline)

Ignore
1715453513
Reply with quote  #2

1715453513
Report to moderator
1715453513
Hero Member
*
Offline Offline

Posts: 1715453513

View Profile Personal Message (Offline)

Ignore
1715453513
Reply with quote  #2

1715453513
Report to moderator
ranochigo
Legendary
*
Offline Offline

Activity: 2968
Merit: 4179



View Profile
August 18, 2018, 08:50:21 AM
Merited by DarkStar_ (2), ABCbits (1)
 #2

If you're scripting the transaction yourself, you have to specify the UTXOs which you will be spending. Addresses are not specified in the inputs, per se. Just the public key, transaction reference and private key will be included.

If you want to do it with blockcypher, then you can look at their API documentation [1]. The API does specify the use of several addresses in the generation of the transaction. I think you can even specify the various parameters of your request [2]. If you don't need to use an API, you can generate your raw transaction on coinb.in.



[1] https://www.blockcypher.com/dev/bitcoin/#creating-transactions
[2] https://www.blockcypher.com/dev/bitcoin/#customizing-transaction-requests

.
.HUGE.
▄██████████▄▄
▄█████████████████▄
▄█████████████████████▄
▄███████████████████████▄
▄█████████████████████████▄
███████▌██▌▐██▐██▐████▄███
████▐██▐████▌██▌██▌██▌██
█████▀███▀███▀▐██▐██▐█████

▀█████████████████████████▀

▀███████████████████████▀

▀█████████████████████▀

▀█████████████████▀

▀██████████▀▀
█▀▀▀▀











█▄▄▄▄
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
.
CASINSPORTSBOOK
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀▀█











▄▄▄▄█
bitmover
Legendary
*
Offline Offline

Activity: 2296
Merit: 5942


bitcoindata.science


View Profile WWW
August 18, 2018, 05:37:46 PM
 #3

Problem:[/b] How Can I send btc from multiple addresses in one transaction.
Say e.g. there are three address each of them has 2 BTC at any time. Now I want to send 3 btc to address x by consuming two address. How can I decide that from first address deduct 1 btc and from second address deduct 2btc or vice versa in single transaction.


Note: Currently using BlockCypher Api. Any other api could also be suggested or mechanism.

As ranochigo said you need to choose the utxo you want to spend.

You need a good Wallet. It's very easy to do that on Electrum
 You just need to select "coins" window and choose the specific utxo and addresses you want to spend
Never did that with another wallet, as most of them do not let you choose the utxo to spend

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
zee-coin (OP)
Newbie
*
Offline Offline

Activity: 2
Merit: 2


View Profile
August 21, 2018, 04:49:23 PM
Merited by DarkStar_ (2)
 #4

Hi Guys,

As Suggested I did the change in approach. I tried creating my own transaction and then push the raw transaction over network. For development i am using testnet network and NBitcoin Library.

After creating the transaction as given in documentation [1]. Now i am converting the transaction into hex and pushing using the link [2].
Getting the error
Error validating transaction: Sum of inputs 0 lesser than outputs 20000000.

Transaction Hex:
0100000001d72a6dfd475c2ac0724355bb9a11bfaf416af5e338ea4e6dac406b5e6866143c00000 0009100473044022007f521e865692e9c65c60d36560275af1cd74ea7c50c04f3d05fdef63a729d 38022032149b0623e953726e3fb2f45b6d875aa31c59b1750f38926c8a61c429ebe0bf014730440 2203846793a959b538d15bef57cfd7fb421f3f428d7c87c7e6348333888538321ed02202619d619 eb2104d4cb8b1f7ea4eed3facb2b25770f652d7664a3f2c09949fa7b01ffffffff01002d3101000 000001976a9149f758c8118b78a35b84dc3e4f2144c16534a42e788ac00000000

How can I solve the above error?
Is it the right way of achieving the requirement or is there any other library which I can use to specify the value of BTC has to be taken from each address?
Then after creating transaction I can push the transaction over network.

[1] https://programmingblockchain.gitbooks.io/programmingblockchain/content/other_types_of_ownership/multi_sig.html
[2] https://live.blockcypher.com/btc/pushtx/
HCP
Legendary
*
Offline Offline

Activity: 2086
Merit: 4316

<insert witty quote here>


View Profile
August 21, 2018, 10:36:16 PM
Merited by DarkStar_ (2), ranochigo (1)
 #5

It is telling you that your inputs don't contain any coins... "sum of inputs == 0"... You can't magically make 0 satoshis into 20000000 satoshis Tongue

It seems like your input is invalid... what transaction and which UTXO are you attempting to spend from? Huh I can't seem to find any matching transaction/UTXO that you are trying to include as your input.


Addtionally, I'm not sure why you are quoting/reading from documentation on using multisig transaction? Huh That's not how you spend from multiple addresses in one transaction. To spend from multiple addresses, you simply include UTXOs from the addresses you want to spend from.

Let's say you want to send 3 BTC to AddressC... you already have AddressA and AddressB, and want to spend 1.5 BTC from both... and you have the following UTXOs in each Address:

AddressA:
UTXO-A1 => 1 BTC
UTXO-A2 => 1 BTC

AddressB:
UTXO-B1 => 5 BTC

You would simply create a transaction that includes the following inputs:

{inputs: UTXO-A1 value:100000000, UTXO-A2 value: 100000000, UTXO-B1 value:500000000} <-- NOTE: you have to spend ALL of an UTXO and create a NEW UTXO output!

and create the following outputs (NOTE: ignoring transaction fees for now):
{outputs: AddressC value: 300000000, AddressA value: 50000000, AddressB Value 350000000}


This would result in:
AddressA:
UTXO-A3 => 0.5 BTC

AddressB:
UTXO-B2 => 3.5 BTC

AddressC:
UTXO-C1 => 3.0 BTC

NOTE how the UTXO numbers in AddressA and AddressB are different... they are newly created. Remember, when you spend a UTXO, you have to spend the WHOLE thing... and create a completely new UTXO in the output section of your transaction.

█████████████████████████
████▐██▄█████████████████
████▐██████▄▄▄███████████
████▐████▄█████▄▄████████
████▐█████▀▀▀▀▀███▄██████
████▐███▀████████████████
████▐█████████▄█████▌████
████▐██▌█████▀██████▌████
████▐██████████▀████▌████
█████▀███▄█████▄███▀█████
███████▀█████████▀███████
██████████▀███▀██████████
█████████████████████████
.
BC.GAME
▄▄░░░▄▀▀▄████████
▄▄▄
██████████████
█████░░▄▄▄▄████████
▄▄▄▄▄▄▄▄▄██▄██████▄▄▄▄████
▄███▄█▄▄██████████▄████▄████
███████████████████████████▀███
▀████▄██▄██▄░░░░▄████████████
▀▀▀█████▄▄▄███████████▀██
███████████████████▀██
███████████████████▄██
▄███████████████████▄██
█████████████████████▀██
██████████████████████▄
.
..CASINO....SPORTS....RACING..
█░░░░░░█░░░░░░█
▀███▀░░▀███▀░░▀███▀
▀░▀░░░░▀░▀░░░░▀░▀
░░░░░░░░░░░░
▀██████████
░░░░░███░░░░
░░█░░░███▄█░░░
░░██▌░░███░▀░░██▌
░█░██░░███░░░█░██
░█▀▀▀█▌░███░░█▀▀▀█▌
▄█▄░░░██▄███▄█▄░░▄██▄
▄███▄
░░░░▀██▄▀


▄▄████▄▄
▄███▀▀███▄
██████████
▀███▄░▄██▀
▄▄████▄▄░▀█▀▄██▀▄▄████▄▄
▄███▀▀▀████▄▄██▀▄███▀▀███▄
███████▄▄▀▀████▄▄▀▀███████
▀███▄▄███▀░░░▀▀████▄▄▄███▀
▀▀████▀▀████████▀▀████▀▀
TheArchaeologist
Sr. Member
****
Offline Offline

Activity: 310
Merit: 727


---------> 1231006505


View Profile WWW
August 22, 2018, 12:17:44 PM
Last edit: August 24, 2018, 11:57:08 AM by TheArchaeologist
Merited by ranochigo (1), ABCbits (1)
 #6

Say e.g. there are three address each of them has 2 BTC at any time. Now I want to send 3 btc to address x by consuming two address. How can I decide that from first address deduct 1 btc and from second address deduct 2btc or vice versa in single transaction.
What you are trying to do in your example is not possible. You cannot "deduct" 1 btc from an address and then sent the remaining btc. You always need to spent the entire output from a previous transaction. To follow up on your example:

Code:
Previous outputs:
transaction e123, sequencenumber 0 -> 2 btc to address 1xxxx
transaction f256: sequencenumber 1 -> 2 btc to address 1yyyy

Now when you want to spent from the above 2 addresses you need to reference them as input in your transaction. Those two transactions add up to 4 btc so the remaining 1 btc has to go to a change address which you control. The address can either be a total new one or could be an existing address you control. So for instance when you sent the 1 btc back to your 1xxxx address the transaction would be made up the following components:
Code:
New transaction: b678
Outputs:
Output 1: 3 btc to address 1aaaa (the address you want the 3 btc to go to)
Output 2: 1 btc to address 1xxxx (the change address, one of your own).
[Inputs]
Input 1: Transaction e123, num=0
Input 2: Transaction f256, num=1
(Notice: your input doesn't contain any addresses or amounts it just refers to previous outputs you want to spent)
In general: The sum of all inputs should be at least equal to the sum of all outputs. If sum(inputs) > sum(outputs) the remainder is considered the transaction fee.
 

Sooner or later you're going to realize, just as I did, that there's a difference between knowing the path and walking the path
HCP
Legendary
*
Offline Offline

Activity: 2086
Merit: 4316

<insert witty quote here>


View Profile
August 23, 2018, 09:31:37 PM
 #7

For the record... you should put raw transaction in [ code ] tags so that they don't end up with "hidden" spaces embedded in it... it'll make it easier for folks to copy/paste... like this:
Code:
0100000001d72a6dfd475c2ac0724355bb9a11bfaf416af5e338ea4e6dac406b5e6866143c000000009100473044022007f521e865692e9c65c60d36560275af1cd74ea7c50c04f3d05fdef63a729d38022032149b0623e953726e3fb2f45b6d875aa31c59b1750f38926c8a61c429ebe0bf0147304402203846793a959b538d15bef57cfd7fb421f3f428d7c87c7e6348333888538321ed02202619d619eb2104d4cb8b1f7ea4eed3facb2b25770f652d7664a3f2c09949fa7b01ffffffff01002d3101000000001976a9149f758c8118b78a35b84dc3e4f2144c16534a42e788ac00000000

This will decode (on blockcypher testnet) as:
Code:
{
    "addresses": [
        "mv46YzVHVF3LUM3HJPUtMiJZdtcsuCNRLf"
    ],
    "block_height": -1,
    "block_index": -1,
    "confirmations": 0,
    "double_spend": false,
    "fees": 0,
    "hash": "17b267fdd18f636b9ccc618bb2b12517f24c4f7240e50fe6d487563d064cce0f",
    "inputs": [
        {
            "age": 0,
            "output_index": 0,
            "prev_hash": "3c1466685e6b40ac6d4eea38e3f56a41afbf119abb554372c02a5c47fd6d2ad7",
            "script": "00473044022007f521e865692e9c65c60d36560275af1cd74ea7c50c04f3d05fdef63a729d38022032149b0623e953726e3fb2f45b6d875aa31c59b1750f38926c8a61c429ebe0bf0147304402203846793a959b538d15bef57cfd7fb421f3f428d7c87c7e6348333888538321ed02202619d619eb2104d4cb8b1f7ea4eed3facb2b25770f652d7664a3f2c09949fa7b01",
            "script_type": "empty",
            "sequence": 4294967295
        }
    ],
    "outputs": [
        {
            "addresses": [
                "mv46YzVHVF3LUM3HJPUtMiJZdtcsuCNRLf"
            ],
            "script": "76a9149f758c8118b78a35b84dc3e4f2144c16534a42e788ac",
            "script_type": "pay-to-pubkey-hash",
            "value": 20000000
        }
    ],
    "preference": "low",
    "received": "2018-08-23T21:24:40.353391361Z",
    "relayed_by": "54.198.7.4",
    "size": 230,
    "total": 20000000,
    "ver": 1,
    "vin_sz": 1,
    "vout_sz": 1
}

I can't find any reference to a (testnet) TXID of "3c1466685e6b40ac6d4eea38e3f56a41afbf119abb554372c02a5c47fd6d2ad7" that you are trying to reference in your input. Huh

█████████████████████████
████▐██▄█████████████████
████▐██████▄▄▄███████████
████▐████▄█████▄▄████████
████▐█████▀▀▀▀▀███▄██████
████▐███▀████████████████
████▐█████████▄█████▌████
████▐██▌█████▀██████▌████
████▐██████████▀████▌████
█████▀███▄█████▄███▀█████
███████▀█████████▀███████
██████████▀███▀██████████
█████████████████████████
.
BC.GAME
▄▄░░░▄▀▀▄████████
▄▄▄
██████████████
█████░░▄▄▄▄████████
▄▄▄▄▄▄▄▄▄██▄██████▄▄▄▄████
▄███▄█▄▄██████████▄████▄████
███████████████████████████▀███
▀████▄██▄██▄░░░░▄████████████
▀▀▀█████▄▄▄███████████▀██
███████████████████▀██
███████████████████▄██
▄███████████████████▄██
█████████████████████▀██
██████████████████████▄
.
..CASINO....SPORTS....RACING..
█░░░░░░█░░░░░░█
▀███▀░░▀███▀░░▀███▀
▀░▀░░░░▀░▀░░░░▀░▀
░░░░░░░░░░░░
▀██████████
░░░░░███░░░░
░░█░░░███▄█░░░
░░██▌░░███░▀░░██▌
░█░██░░███░░░█░██
░█▀▀▀█▌░███░░█▀▀▀█▌
▄█▄░░░██▄███▄█▄░░▄██▄
▄███▄
░░░░▀██▄▀


▄▄████▄▄
▄███▀▀███▄
██████████
▀███▄░▄██▀
▄▄████▄▄░▀█▀▄██▀▄▄████▄▄
▄███▀▀▀████▄▄██▀▄███▀▀███▄
███████▄▄▀▀████▄▄▀▀███████
▀███▄▄███▀░░░▀▀████▄▄▄███▀
▀▀████▀▀████████▀▀████▀▀
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!