Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. Kryptokurse von CoinMarketCap abfragen

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    Kryptokurse von CoinMarketCap abfragen

    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      smartysmart last edited by smartysmart

      Hallo Leute,
      CoinMarketCap bietet eine kostenlose API an um die aktuellen Kryptokurse für alle Kryptowährungen abzufragen.
      Leider fehlen mir die Programmierkenntnisse um mit der API Dokumentation ein Blocky oder Adapter zu basteln.

      Ist hier jemand der sich damit auskennt, zeit und Lust hat mir dabei zu helfen ?

      https://coinmarketcap.com/api/pricing/

      https://coinmarketcap.com/api/documentation/v1/

      VG

      haus-automatisierung 1 Reply Last reply Reply Quote 0
      • haus-automatisierung
        haus-automatisierung Developer Most Active @smartysmart last edited by

        @smartysmart Mit Blockly geht das aktuell leider nicht, weil man einen custom header setzen muss.

        Aus dem Beispiel:

        curl -H "X-CMC_PRO_API_KEY: b54bcf4d-1bca-4e8e-9a24-22ff2c3d462c" -H "Accept: application/json" -d "start=1&limit=5000&convert=USD" -G https://sandbox-api.coinmarketcap.com/v1/cryptocurrency/listings/latest
        

        wäre also in einer eigenen Funktion:

        return httpGetAsync(
            'https://sandbox-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?start=1&limit=5000&convert=USD',
            {
                timeout: 1000,
                headers: {
                    'X-CMC_PRO_API_KEY': 'b54bcf4d-1bca-4e8e-9a24-22ff2c3d462c',
                    'Accept': 'application/json'
                }
            }
        );
        
        S 1 Reply Last reply Reply Quote 0
        • S
          smartysmart @haus-automatisierung last edited by

          @haus-automatisierung Danke für den Denkanstoß

          Mithilfe von Claude Ai konnte ich dann mein Vorhaben doch noch umsetzen.
          Also alle die die sowas noch haben wollen, das wäre meine Lösung.

          on({ id: '0_userdata.0.Kryptokurse.kurse-abrufen' /* kurse-abrufen */, change: 'ne' }, async (obj) => {
            let value = obj.state.val;
            let oldValue = obj.oldState.val;
            if (getState('0_userdata.0.Kryptokurse.kurse-abrufen').val == true) {
          
                                      const axios = require('axios');
          
          const apiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
          const apiUrl = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest';
          
          const params = {
            symbol: 'BTC',
            convert: 'USD'
          };
          
          const headers = {
            'X-CMC_PRO_API_KEY': apiKey
          };
          
          axios.get(apiUrl, { params, headers })
            .then(response => {
              let BTCKurs = response.data.data.BTC.quote.USD.price;
              
              // Runden auf 2 Nachkommastellen
              BTCKurs = Number(BTCKurs.toFixed(2));
              
              console.log('BTC-Kurs (gerundet):', BTCKurs);
              
              // Setzen des gerundeten Wertes in ioBroker
              setState('0_userdata.0.Kryptokurse.BTC', BTCKurs, true, (err) => {
                if (err) {
                  console.error('Fehler beim Setzen des BTC-Kurses in ioBroker:', err);
                } else {
                  console.log('Gerundeter BTC-Kurs erfolgreich in ioBroker gesetzt');
                }
              });
            })
            .catch(error => {
              console.error('Fehler beim Abrufen des BTC-Kurses:', error);
            });
          
            }
          });
          
          
          
          
          haus-automatisierung 2 Replies Last reply Reply Quote 0
          • haus-automatisierung
            haus-automatisierung Developer Most Active @smartysmart last edited by

            @smartysmart Ist schon erstaunlich, wie umständlich der AI-Generierte Code immer ist... Wozu mache ich mir hier eigentlich die Arbeit und schlag eine Lösung vor?!

            1 Reply Last reply Reply Quote 2
            • haus-automatisierung
              haus-automatisierung Developer Most Active @smartysmart last edited by haus-automatisierung

              @smartysmart Bitte mal so testen - ist viel sauberer:

              const apiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
              
              on({ id: '0_userdata.0.Kryptokurse.kurse-abrufen', val: true }, async (obj) => {
                  httpGet(
                      'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=BTC&convert=USD',
                      {
                          timeout: 1000,
                          headers: {
                              'X-CMC_PRO_API_KEY': apiKey,
                              'Accept': 'application/json'
                          }
                      },
                      (err, response) => {
                          if (!err) {
                              const usdPrice = JSON.parse(response.data).data.BTC.quote.USD.price;
                              setState('0_userdata.0.Kryptokurse.BTC', Number(usdPrice.toFixed(2)), true);
                          } else {
                              console.error(err);
                          }
                      }
                  );
              });
              
              S 1 Reply Last reply Reply Quote 1
              • S
                shellyrulestheworld @haus-automatisierung last edited by

                Hallo @haus-automatisierung

                ich habe deinen Code ausprobiert. Ich habe ihn in ein JS Script kopiert und die Datenpunkte unter 0_userdata.0 angelegt.

                bceca24f-2fe8-431d-b10f-5d32431fd57f-image.png

                e5219685-1c11-4b23-a5fd-c37e52978449-image.png

                leider werden diese nicht befüllt. Hast du eine Idee, was ich falsch mache?

                S 1 Reply Last reply Reply Quote 0
                • S
                  shellyrulestheworld @shellyrulestheworld last edited by

                  
                  javascript.0
                  2024-08-19 17:57:49.999	error	script.js.BTC.BTC_Kurse: timeout of 1000ms exceeded
                  
                  javascript.0
                  2024-08-19 17:57:49.998	error	script.js.BTC.BTC_Kurse: httpGet(url=https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=BTC&convert=USD, error=timeout of 1000ms exceeded)
                  
                  S 1 Reply Last reply Reply Quote 0
                  • S
                    shellyrulestheworld @shellyrulestheworld last edited by

                    jetzt habe ich den Timeout erhöht und es funktioniert. Herzlichen Dank für deinen Code...

                    OliverIO 1 Reply Last reply Reply Quote 1
                    • OliverIO
                      OliverIO @shellyrulestheworld last edited by

                      @shellyrulestheworld

                      ist hier evtl nicht so wichtig, aber toFixed rundet nicht kaufmännisch

                      (1.104).toFixed(2) = 1.10
                      (1.105).toFixed(2) = 1.10
                      (1.106).toFixed(2) = 1.11
                      

                      besser ist

                      (Math.round(1.104*100)/100).toFixed(2) = 1.10
                      (Math.round(1.105*100)/100).toFixed(2) = 1.11
                      (Math.round(1.106*100)/100).toFixed(2) = 1.11
                      
                      

                      toFixed ist nur als Formatierungsfunktion zu gebrauchen um 0 aufzufüllen damit man auf die Anzahl nachkommastellen kommt

                      Als allgemeine Funktion kann man sowas nehmen, diese Funktion bildet auch andere Zahlsysteme ab, aber alles was anders wie 10 ist, muss man noch weitere Schritte vornehmen.
                      also einfach nur

                      console.log(toFixedNumber(1.105,2));
                      
                      function toFixedNumber(num, digits, base){
                        const pow = Math.pow(base ?? 10, digits);
                        return Math.round(num*pow) / pow;
                      }```
                      1 Reply Last reply Reply Quote 1
                      • First post
                        Last post

                      Support us

                      ioBroker
                      Community Adapters
                      Donate

                      501
                      Online

                      31.9k
                      Users

                      80.1k
                      Topics

                      1.3m
                      Posts

                      4
                      9
                      523
                      Loading More Posts
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes
                      Reply
                      • Reply as topic
                      Log in to reply
                      Community
                      Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                      The ioBroker Community 2014-2023
                      logo