Author

Topic: sendtoaddress sending 0 amount every time PHP (Read 304 times)

staff
Activity: 3458
Merit: 6793
Just writing some code
September 05, 2019, 01:10:39 PM
#17
So I used createwallet and created a sender wallet and a receiver wallet to test with where I will mine to the sender wallet and try sending it to the receiver address. From here I'm not sure how to switch between wallets. I have 3 wallet.dat files now, two of which are in folders regtest/wallets/sender and regtest/wallets/receiver so I'm assuming bitcoind automatically grabs the wallet.dat in regtest/wallets/wallet.dat. How do I switch between receiver and senders wallet.dat's to test this? I tried looking at this link of outdaated calls to see if there is an RPC call for it, theres not in the outdated API:

https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list

The link to the updated list does not list API calls. It has the header that says it but its blank then moves on to hash byte order:

https://bitcoin.org/en/developer-reference#bitcoin-core-apis
You can get a list of available RPCs from Bitcoin Core directly using the help command, and the help documentation for each command using help where is the command you are interested in.

In order to load a wallet, you use the loadwallet command. In order to send RPCs to that specific wallet, you use the -rpcwallet= option in bitcoin-cli where is the name of the wallet you want to access. To use the sender wallet, you would have -rpcwallet=sender, for the receiver, -rpcwallet=receiver. And for the default wallet (the one in regtest/wallets/wallet.dat), use -rpcwallet= as the default wallet has no name.
newbie
Activity: 18
Merit: 11
September 05, 2019, 02:35:08 AM
#16
So I used createwallet and created a sender wallet and a receiver wallet to test with where I will mine to the sender wallet and try sending it to the receiver address. From here I'm not sure how to switch between wallets. I have 3 wallet.dat files now, two of which are in folders regtest/wallets/sender and regtest/wallets/receiver so I'm assuming bitcoind automatically grabs the wallet.dat in regtest/wallets/wallet.dat. How do I switch between receiver and senders wallet.dat's to test this? I tried looking at this link of outdaated calls to see if there is an RPC call for it, theres not in the outdated API:

https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list

The link to the updated list does not list API calls. It has the header that says it but its blank then moves on to hash byte order:

https://bitcoin.org/en/developer-reference#bitcoin-core-apis
legendary
Activity: 2618
Merit: 6452
Self-proclaimed Genius
September 04, 2019, 09:48:17 PM
#15
In electrum you can send transactions to other addresses in your wallet and it transfers it all to the new address, how come its not doing this behavior in regtest CLI? The coins are not stored in that address, when I use generatetoaddress I use a different address than the one I'm sending it to.

I'll try using createwallet
You need to specify the address when you call "sendtoaddress" like: bitcoin-cli sendtoaddress "other bitcoin address" 2.1 (including the quotation marks)
"getnewaddress" doesn't automatically select the one generated as the next receiving address.

Next, I just tested Electrum, it also indicates any self-send transaction with "0 amount sent" like BitcoinCore does.
Even sending to a different address from the same wallet.
staff
Activity: 3458
Merit: 6793
Just writing some code
September 04, 2019, 01:15:09 PM
#14
In electrum you can send transactions to other addresses in your wallet and it transfers it all to the new address, how come its not doing this behavior in regtest CLI? The coins are not stored in that address, when I use generatetoaddress I use a different address than the one I'm sending it to.
They are all being sent to a new address. But that new address is part of your wallet. A wallet is a collection of multiple addresses and their scripts and keys. Bitcoin Core does not treat eat address separately, they are all bundled together as a single wallet. Since you are sending coins from one address in your wallet to another address, the net value going in and out (ignoring fees) does not change with that transaction, so it lists the amount of that transaction as 0.
newbie
Activity: 18
Merit: 11
September 04, 2019, 12:26:45 AM
#13
In electrum you can send transactions to other addresses in your wallet and it transfers it all to the new address, how come its not doing this behavior in regtest CLI? The coins are not stored in that address, when I use generatetoaddress I use a different address than the one I'm sending it to.

I'll try using createwallet
staff
Activity: 3458
Merit: 6793
Just writing some code
September 03, 2019, 05:18:05 PM
#12
You are sending money back to yourself, of course it's going to register as 0 amount.

Amount is calculated by: amount_in - amount_out - fees. amount_in is the total value of the outputs that belong to you. amount_out is the total value of the inputs that are yours. fees is the transaction fees paid. You have 2.4 + 47.59993360 = 49.99993360 in as that's the amount you are sending + the change output; no Bitcoin is going to anyone else. You have 50 out as that is the value of the input that is being spent.And -0.00006640 as the transaction fee itself. So the final amount is 49.99993360 - 50 - -0.00006640 = 0.

In order to get an amount that is not 0, you need to send Bitcoin to an address that is not part of that wallet. You can easily do this by creating another wallet using createwallet, getting an address from it, and sending coins there.
newbie
Activity: 18
Merit: 11
September 03, 2019, 05:03:32 PM
#11
Still the same issue when I send to address generated with legacy flag (produces address that begins with an m instead of one that begins with a 2). I think it has to do with this. How do I make it not send from the same address?

Code:
"details": [
    {
      "address": "mgt64uGFQHg6Q6hw6NBK2ZQib3zqek7CHQ",
      "category": "send",
      "amount": -15.00000000,
      "label": "-addresstype",
      "vout": 1,
      "fee": -0.00006720,
      "abandoned": false
    },
    {
      "address": "mgt64uGFQHg6Q6hw6NBK2ZQib3zqek7CHQ",
      "category": "receive",
      "amount": 15.00000000,
      "label": "-addresstype",
      "vout": 1
    }
  ],
legendary
Activity: 3472
Merit: 10611
September 02, 2019, 11:35:46 PM
#10
I use getnewaddress to generate my addresses, how do I set the type of address?

you have to pass the optional parameter called "address_type" with "legacy". i believe the command should be typed like this:
Code:
getnewaddress -addresstype legacy
more info: https://bitcoincore.org/en/doc/0.18.0/rpc/wallet/getnewaddress/
newbie
Activity: 18
Merit: 11
September 02, 2019, 11:22:17 PM
#9
I use getnewaddress to generate my addresses, how do I set the type of address?
legendary
Activity: 3472
Merit: 10611
September 02, 2019, 10:55:19 PM
#8
this may be another bug in bitcoin core similar to decoderawtransaction which fails to decode transactions containing witnesses properly unless you explicitly pass a Boolean (true) to tell it the transaction is a SegWit one. https://bitcointalksearch.org/topic/possible-bug-in-decoderawtransaction-in-1602-5014781

try using a legacy transaction and see if the amount you get is zero again. this means use a regular address like a P2PKH (address starting with 1 on mainnet) and spend coins from that address. if you don't see the same problem then it must be the bug i mentioned.

How do I do that in regtest mode? I'm using the current version.

when you mine, just send the new coins to a P2PKH output instead of a P2SH (P2WPKH nested in script hash) like the one you used above. then spend those coins. that means first sending the coins to an address starting with m or n and then spending from that address.
newbie
Activity: 18
Merit: 11
September 02, 2019, 10:40:27 PM
#7
this may be another bug in bitcoin core similar to decoderawtransaction which fails to decode transactions containing witnesses properly unless you explicitly pass a Boolean (true) to tell it the transaction is a SegWit one. https://bitcointalksearch.org/topic/possible-bug-in-decoderawtransaction-in-1602-5014781

try using a legacy transaction and see if the amount you get is zero again. this means use a regular address like a P2PKH (address starting with 1 on mainnet) and spend coins from that address. if you don't see the same problem then it must be the bug i mentioned.

How do I do that in regtest mode? I'm using the current version.
legendary
Activity: 3472
Merit: 10611
September 02, 2019, 10:06:24 PM
#6
this may be another bug in bitcoin core similar to decoderawtransaction which fails to decode transactions containing witnesses properly unless you explicitly pass a Boolean (true) to tell it the transaction is a SegWit one. https://bitcointalksearch.org/topic/possible-bug-in-decoderawtransaction-in-1602-5014781

try using a legacy transaction and see if the amount you get is zero again. this means use a regular address like a P2PKH (address starting with 1 on mainnet) and spend coins from that address. if you don't see the same problem then it must be the bug i mentioned.
newbie
Activity: 18
Merit: 11
September 02, 2019, 05:48:30 PM
#5
I'm using regtest where I have my own private blockchain not testnet. I have a balance of like 700 BTC in addresses I generated because I mined several hundred blocks.
legendary
Activity: 3472
Merit: 3217
Playbet.io - Crypto Casino and Sportsbook
September 02, 2019, 05:33:18 PM
#4
It looks like you are trying to make a transaction without a balance I think you need to have a Bitcoin Testnet balance first before you can make it work.

You can get free Bitcoin Testnet first from this links below:

- https://en.bitcoin.it/wiki/Testnet#Faucets
- http://tpfaucet.appspot.com/

Other Testnet coins:

- https://github.com/cashuwallet/cashuwallet/wiki/Testnet-Faucet-List



Use this testnet explorer if you are receiving Bitcoin Testnet https://live.blockcypher.com/btc-testnet/ or you can check the testnet address if you have a balance.
newbie
Activity: 18
Merit: 11
September 02, 2019, 03:26:05 PM
#3
I dont think its the library, just tried sending a transaction using the cli with same result.

Code:
./bitcoin-cli -regtest sendtoaddress [address] 2.1

Returns a transaction ID, then when I run

Code:
./bitcoin-cli -regtest gettransaction [txid]

The amount still says 0..

This is my most recent return for gettransaction:

Code:
./bitcoin-cli -regtest gettransaction f8d306e9649aad81e1bac5f1e61cb752f886a0d5340b4ad9ac305fb5027182cc
{
  "amount": 0.00000000,
  "fee": -0.00006640,
  "confirmations": 0,
  "trusted": true,
  "txid": "f8d306e9649aad81e1bac5f1e61cb752f886a0d5340b4ad9ac305fb5027182cc",
  "walletconflicts": [
  ],
  "time": 1567455718,
  "timereceived": 1567455718,
  "bip125-replaceable": "no",
  "details": [
    {
      "address": "2N72hQwNLCXYmDnyBFDXwcUAvppm1bLLzX8",
      "category": "send",
      "amount": -2.40000000,
      "label": "",
      "vout": 0,
      "fee": -0.00006640,
      "abandoned": false
    },
    {
      "address": "2N72hQwNLCXYmDnyBFDXwcUAvppm1bLLzX8",
      "category": "receive",
      "amount": 2.40000000,
      "label": "",
      "vout": 0
    }
  ],
  "hex": "02000000000101519e1d7e4a983b8135148dbb32198e23b7500a407048805bd9ffd97d38fd163300000000171600148e193f47083de7efbe286f289e7034c084a7f70bfeffffff02001c4e0e0000000017a91497333f2c2bdf79ec3637fbef6f331fabb2f451aa8710bcb71b0100000017a914b9d9b20cb6cbf043cae1b361bc391d102dc6d49c870247304402207f71c6bdde891742d274d227f51d9f0e8a0477c2776c057278f035d9606eb0bf022014cb9951b6144462fe7423bf99f137c38846a3c647cf5426b27412ced26a47b201210340ce4e488b13b4c9dedabe231722211a2e4f02237ea347e81b804a714fac2d4500000000"

It's looking like its maybe sending and receiving to the same address which I dont know how its possible because I literally just generated the address with getnewaddress.
legendary
Activity: 3150
Merit: 2185
Playgram - The Telegram Casino
September 02, 2019, 03:18:11 PM
#2
Is this the library you are using?
https://github.com/aceat64/EasyBitcoin-PHP

Because that library appears to be outdated:

easybitcoin seems to be no longer working, or needs some tweaking.

I have written a php function using curl.
You can use this php function, if you are also looking for a solution:
Just pass the method in the function and make sure to edit out your rpc details inside the function.

Code:

function bitcoin($method){
$ch curl_init();
$rpcuser='rpcusername';
$rpcpassword='rpcpassword'
$host='nodeIP';  //IP address of your node or localhost, if running locally
$port='port';

curl_setopt($chCURLOPT_URL"http://$rpcuser:$rpcpassword@$host:$port/");
curl_setopt($chCURLOPT_RETURNTRANSFER1);
curl_setopt($chCURLOPT_POSTFIELDS"{\"jsonrpc\":\"1.0\",\"id\":\"curltext\",\"method\":\"$method\",\"params\":[]}");
curl_setopt($chCURLOPT_POST1);

$headers = array();
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($chCURLOPT_HTTPHEADER$headers);

$result curl_exec($ch);
if (
curl_errno($ch)) {
    echo 
'Error:' curl_error($ch);
}
curl_close ($ch);
return(
$result);
}
$method="getblockchaininfo"//bitcoin-cli method like getchaininfo,getwalletinfo etceter
$lol=bitcoin($method);
?>



Cheers!


It also says so on the GitHub page itself:

A simple class for making calls to Bitcoin's API using PHP. This is an old library, I suggest instead using https://github.com/denpamusic/php-bitcoinrpc


If you are referring to another PHP library please post a link for clarification.

newbie
Activity: 18
Merit: 11
September 02, 2019, 02:52:37 PM
#1
So using the easybitcoin.php library I'm making a test wallet controller for regtest. I have it working to generate blocks and new addresses but transactions arent working properly. I have a form that sends which address and which amount and the script is as follows for processing the transaction:

Code:
$amount= (float) number_format($_POST['amount'], 8);
$bitcoin->settxfee(0.0004);
$txid = $bitcoin->sendtoaddress($_POST['sendTo'], $amount);

Doing that when I get txinfo I get a txid and all that but the amount is 0 every time. Same if I used the code like this:

Code:
$amount= $_POST['amount'];
$bitcoin->settxfee(0.0004);
$txid = $bitcoin->sendtoaddress($_POST['sendTo'], (float) $amount);

This is very frustrating as it makes it difficult to test my app. I also hard coded 3.32 for example so it was a float to begin with and still the transaction is sent with a 0 amount. What is going on?


Code:
$bitcoin->settxfee(0.0004);
$txid = $bitcoin->sendtoaddress($_POST['sendTo'], 3.32);

All of them I get a $txid and using $txinfo = gettransaction($txid) I can get the txid using $txinfo['txid'] but $txinfo['amount'] always returns as 0.
Jump to: