Bitcoin Forum

Bitcoin => Wallet software => Topic started by: cdnbcguy on July 23, 2011, 02:28:15 AM



Title: I need a line of C code
Post by: cdnbcguy on July 23, 2011, 02:28:15 AM
I am adding a few things to the excellent QT bitcoin client and I need a line of C code that will get me the price of BTC in USD from Mt Gox.

I'm kind of new to working with APIs (yes I know, no excuse, really...)

Can anyone help me with this, please?


Title: Re: I need a line of C code
Post by: error on July 23, 2011, 04:47:42 AM
It'll take quite a bit more than one line of code.


Title: Re: I need a line of C code
Post by: wumpus on July 24, 2011, 12:49:21 AM
Yes, it'd take somewhat more than one line of C++ code, at least to do it right. the biggest problem I see is that all clients would be requesting the MtGox API data all the time, resulting in possible DDoS. Also, it should be an option, because not everyone might want reveal to MtGox that they are running bitcoin...

Also, MtGox is only one of the exchanges, and people will probably want quotes for other currencies than the USD as well.

I wonder if there is some more efficient way of distributing the exchange real-time quote information that doesn't involve polling all of them all the time...


Title: Re: I need a line of C code
Post by: martin on July 24, 2011, 02:29:23 PM
I wonder if there is some more efficient way of distributing the exchange real-time quote information that doesn't involve polling all of them all the time...

Exchanges could broadcast a message to the network, signed with their private key to ensure authenticity. That way clients which are interested just listen for broadcast messages and display the values contained within, if I remember correctly there is already support within the protocol for broadcasts but only the dev team can send them, so with only a little modification it should be possible to modify it so that anyone can send them.


Title: Re: I need a line of C code
Post by: Pieter Wuille on July 24, 2011, 02:53:46 PM
Maybe you could follow this idea: http://forum.bitcoin.org/index.php?topic=8773.0

In short: allow anyone to publish an time-dependent exchange rate to another currency through https, and map changes in the exchange rate to virtual "interest" transactions in the client view.


Title: Re: I need a line of C code
Post by: wumpus on July 24, 2011, 05:22:43 PM
Exchanges could broadcast a message to the network, signed with their private key to ensure authenticity.
Incredible idea! That way, the exchange rates could be published through the network itself, instead of having to rely on centralized auxiliary sources.

Maybe you could follow this idea: http://forum.bitcoin.org/index.php?topic=8773.0

In short: allow anyone to publish an time-dependent exchange rate to another currency through https, and map changes in the exchange rate to virtual "interest" transactions in the client view.

Yes, I think this would be similar to my idea; at the time of a transaction coming in, attach the current exchange rates  to it as client metadata. There'd be some problems with this approach, though, for example what if you don't run the client for a while. You'd need historical exchange data to reconstruct the exchange rate at the time of the transaction... unless you really embed the exchange rates in the block chain, but that might be overkill and a waste of space.




Title: Re: I need a line of C code
Post by: wumpus on July 26, 2011, 06:54:49 AM
Would XMPP be suitable for net-wide distribution of small messages such as exchange rate prices?

I'm afraid it needs an account per client though, which isn't very handy...


Title: Re: I need a line of C code
Post by: TYDIRocks on July 29, 2011, 03:44:33 AM
You could make a simple webpage that has the prices from various places and have the client just grab it from there.


Title: Re: I need a line of C code
Post by: wumpus on July 31, 2011, 05:57:33 PM
You could make a simple webpage that has the prices from various places and have the client just grab it from there.
And who pays for the bandwidth?


Title: Re: I need a line of C code
Post by: cdnbcguy on July 31, 2011, 07:09:16 PM
I only actually wanted to get the price every hour or so...


Title: Re: I need a line of C code
Post by: vector76 on August 03, 2011, 10:21:01 AM
Use curl to fetch a page into memory, as demonstrated here: http://curl.haxx.se/libcurl/c/getinmemory.html

Then point it at this url which has time-weighted prices: http://bitcoincharts.com/t/weighted_prices.json

Then write some code to parse out the currency and interval you want, or use a json library to do the parsing for you.



Title: Re: I need a line of C code
Post by: cdnbcguy on August 12, 2011, 12:51:05 PM
It'll take quite a bit more than one line of code.

Point. Ruby has spoiled me.


Title: Re: I need a line of C code
Post by: Matoking on August 21, 2011, 01:56:25 PM
I use this library to parse JSON :
http://ereilin.tumblr.com/post/6857765046/lightweight-json-parser-serializer-class-for-qt

Dump it and include it into your project like any other .cpp or .h file and use Json::parse() to do the parsing magic.

And before that I use QNetworkAccessManager to retrieve market data from Mt. Gox which I then parse using the above library.

This is how I parsed the ticker data in my cuteCoin application (it looks a bit crappy but works)

Code:
void Networking::updateTradeData()
{
    networkManager->get(QNetworkRequest(QUrl("https://mtgox.com/api/0/data/ticker.php")))->ignoreSslErrors();
    qDebug("GET https://mtgox.com/api/0/data/ticker.php");

    connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(tradeDataLoaded(QNetworkReply*)));
}

...

void Networking::tradeDataLoaded(QNetworkReply *reply)
{
    QByteArray replyBytes = reply->readAll();
    QString replyString = QString::fromUtf8(replyBytes);

    QString lastPrice;
    QString highPrice;
    QString lowPrice;

    qDebug("GET https://mtgox.com/api/0/data/ticker.php DONE!");

    bool parseSuccess;
    QVariantMap tradeData = Json::parse(replyString, parseSuccess).toMap();

    if (!parseSuccess) {
        qDebug("Failed to parse trade data");
        qDebug(QString("Returned : %1").arg(replyString).toAscii());
        return;
    }

    QVariantMap tickerData;
    tickerData = tradeData.value("ticker").toMap();

    lastPrice = tickerData.value("last").toString();
    highPrice = tickerData.value("high").toString();
    lowPrice = tickerData.value("low").toString();

    reply->deleteLater();

    disconnect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(tradeDataLoaded(QNetworkReply*)));

    tradeDataParsed(lastPrice, highPrice, lowPrice);
}



Title: Re: I need a line of C code
Post by: antares on January 16, 2012, 11:22:20 PM
why bother with the traffic or DDos?
MtGox maintains channels on Freenode, namely #mtgox-live which could be fetched. IRC is already implemented within bitcoin, so this would probably require even less code than fetching mtgox api(as mtgox api uses ssl, which makes the thing take that much code in the first place)

EDIT: I will make this patch later tonight and post it here, however youll have to wait since I'm still at work.