19
12
I am playing around with Bitcoin-qt. I want to iterate all blocks and all their transactions.
So far I've been able to get a list of all tx's in the blocks I want to. Now I want to get information about the transaction: Amount, confirmations and so forth.
However, when I use the gettransaction method of the Bitcoin-qt API, it seems I can only get transactions from my own wallet.
For instance, I cannot say, because I get the error "Invalid or non-wallet transaction id (code -5) ":
gettransaction 0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098
However, when I have a TX of one of my own transactions, I get a correct response.
My code
public void AddBtcTransaction(string hash)
{
List<string> parameters = new List<string>();
parameters.Add(hash);
var data = RequestServer("gettransaction", parameters);
int i = 0;
}
public string RequestServer(string methodName, List<string> parameters)
{
string respVal = string.Empty;
var rawRequest = GetRawRequest();
JObject joe = new JObject();
joe.Add(new JProperty("jsonrpc", "1.0"));
joe.Add(new JProperty("id", "1"));
joe.Add(new JProperty("method", methodName));
JArray props = new JArray();
foreach (var parameter in parameters)
{
props.Add(parameter);
}
joe.Add(new JProperty("params", props));
// serialize json for the request
string s = JsonConvert.SerializeObject(joe);
byte[] byteArray = Encoding.UTF8.GetBytes(s);
rawRequest.ContentLength = byteArray.Length;
Stream dataStream = rawRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
StreamReader streamReader = null;
try
{
WebResponse webResponse = rawRequest.GetResponse();
streamReader = new StreamReader(webResponse.GetResponseStream(), true);
respVal = streamReader.ReadToEnd();
var data = JsonConvert.DeserializeObject(respVal).ToString();
return data;
}
catch (Exception exp)
{
}
finally
{
if (streamReader != null)
{
streamReader.Close();
}
}
return string.Empty;
}
private HttpWebRequest GetRawRequest()
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(ServerIp);
webRequest.Credentials = new NetworkCredential(UserName, Password);
/// important, otherwise the service can't desirialse your request properly
webRequest.ContentType = "application/json-rpc";
webRequest.Method = "POST";
return webRequest;
}
Any idea about how I would get information about transactions outside my wallet?
this answer is correct for litecoin too. – kodmanyagha – 2019-07-24T16:08:24.370
Just to be clear: You cannot get information about payment amount or fees - at all? :-) Because that sounds quite wrong (Blockchain.info does this). I am reindexing the blockchain now :-) – Lars Holdgaard – 2013-06-21T17:01:35.450
1You can compute the fee yourself by subtracting the amounts of all outputs from the sum of the amounts of all inputs (which you'll have to look up through getrawtransaction too). You can of course see the amount of a particular output, but unless the address it is to is yours, you can't know for example which is change and which is the payment. Wallets are designed to do all that for you, if you bypass them, you need to do things manually. – Pieter Wuille – 2013-06-21T17:26:03.883
Thanks a lot. I also want other people to look at "decoderawtransaction" -> GREAT function. Thanks again :-) – Lars Holdgaard – 2013-06-21T20:03:19.337
1I was able to use
bitcoin-cli decoderawtransaction $(bitcoin-cli getrawtransaction txid)to get transaction details. Seems that the answer to usetxindex=1is out of date? – jangorecki – 2015-08-26T12:55:08.0701jangorecki: getrawtransaction always works on transactions that are not yet completely spent. – Pieter Wuille – 2015-08-26T16:43:05.150