StakeCube API – PHP Klasse

Veröffentlicht am: 31.05.2021

Wir haben für die Plattform StakeCube eine erste PHP Klasse zum Implementierung der Schnittstelle programmiert.

/**
 * Free to Use Powered by https://www.krypto-magazin.de/
 * Have Fun 🙂 Greetings Alex
 */
class stakecube
{
    // API Data
    var $private_key = '';
    var $key         = '';

    // Constructor
    function __construct($key,$private_key)
    {
        $this->private_key = $private_key;
        $this->key         = $key;
    }

    /**
     * @param $pair
     * @param $interval
     * @return mixed
     * Get Market Data
     */
    function GetChartData($pair,$interval){
        // Secure String
        $pair = strtoupper($pair);
        $url = "https://stakecube.io/api/v2/exchange/spot/ohlcData?market=$pair&interval=$interval";

        return $this->api_call_get($url);
    }

    /**
     * @param $pair
     * @return mixed
     * Get Orderbook Data
     */
    function GetOrderbook($pair){
        // Secure String
        $pair = strtoupper($pair);
        $url = "https://stakecube.io/api/v2/exchange/spot/orderbook?market=$pair";
        return $this->api_call_get($url);
    }


    /**
     * @param $url_send
     * @return mixed
     * Call Main Rest Funktion
     */
    function api_call_get($url_send){

        $headers = [
            'Cache-Control: no-cache',
            'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
            'X-API-KEY: ' . $this->key
        ];

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url_send);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $output = curl_exec($ch);
        curl_close($ch);

        // Return Result as Object
        return json_decode($output);
    }

    /**
     * @param $url
     * @param $add_code
     * @return mixed
     * POST CALL
     */
    function api_call_post($url,$add_code){

        $headers = [
            'Cache-Control: no-cache',
            'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
            'X-API-KEY: ' . $this->key
        ];

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$add_code);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $output = curl_exec($ch);
        curl_close($ch);

        // Return Result as Object
        return json_decode($output);
    }

    /**
     * @return mixed
     * Get Account Info
     */
    function Get_Account_Info(){
        // URL Data
        $url = "https://stakecube.io/api/v2/user/account?";

        // Timestamp
        $timestamp      = round(microtime(true) * 1000);

        // Nonce und Body
        $add_code =  "nonce=$timestamp";

        // Create Signatur and add to String
        $sigantur = "&signature=" . hash_hmac("sha256", $add_code, $this->private_key);

        return $this->api_call_get($url . $add_code . $sigantur);

    }

    /**
     * @param $market
     * @param $limit
     * @return mixed
     * Load Trades
     */
    function Get_My_Trades($market,$limit){
        // URL Data
        $url = "https://stakecube.io/api/v2/exchange/spot/myTrades?";

        // Timestamp
        $timestamp      = round(microtime(true) * 1000);

        // Nonce und Body
        $add_code =  "market=$market&limit=$limit&nonce=$timestamp";

        // Create Signatur and add to String
        $sigantur = "&signature=" . hash_hmac("sha256", $add_code, $this->private_key);

        return $this->api_call_get($url . $add_code . $sigantur);
    }

    /**
     * @param $market
     * @param $limit
     * @return mixed
     * Load Order History
     */
    function Get_My_Order_History($market,$limit){

        // Secure Data
        $market = strtoupper($market);

        // URL Data
        $url = "https://stakecube.io/api/v2/exchange/spot/myOrderHistory?";

        // Timestamp
        $timestamp      = round(microtime(true) * 1000);

        // Nonce und Body
        $add_code =  "market=$market&limit=$limit&nonce=$timestamp";

        // Create Signatur and add to String
        $sigantur = "&signature=" . hash_hmac("sha256", $add_code, $this->private_key);

        return $this->api_call_get($url . $add_code . $sigantur);
    }

    /**
     * @param $market
     * @param $side
     * @param $price
     * @param $amount
     * @return mixed
     * Create a Limit Order
     */
    function Create_Limit_Order($market,$side,$price,$amount){

        // Secure Data
        $price = str_replace(",",".",$price);
        if($side != "BUY" && $side != "SELL"){
            return false;
        }
        // Secure Data
        $market = strtoupper($market);
        $market = htmlspecialchars($market);

        $amount = floatval($amount);
        $price  = floatval($price);

        // URL Data
        $url = "https://stakecube.io/api/v2/exchange/spot/order?";

        // Timestamp
        $timestamp      = round(microtime(true) * 1000);

        // Nonce und Body
        $add_code =  "market=$market&side=$side&price=$price&amount=$amount&nonce=$timestamp";

        // Create Signatur and add to String
        $sigantur = "&signature=" . hash_hmac("sha256", $add_code, $this->private_key);

        return $this->api_call_post($url,$add_code . $sigantur);
    }

    /**
     * @param $market
     * @return mixed
     * Cancle all Orders
     */
    function Cancel_All($market){
        $market = strtoupper($market);

        // URL Data
        $url = "https://stakecube.io/api/v2/exchange/spot/cancelAll?";

        // Timestamp
        $timestamp      = round(microtime(true) * 1000);

        // Nonce und Body
        $add_code =  "market=$market&nonce=$timestamp";

        // Create Signatur and add to String
        $sigantur = "&signature=" . hash_hmac("sha256", $add_code, $this->private_key);

        return $this->api_call_post($url,$add_code . $sigantur);
    }

    /**
     * @param $coin
     * @return mixed
     * Get Spcific Coin Info
     */
    function Get_Coin_Data($coin){
        $coin = strtoupper($coin);

        // Get Account Info
        $daten = $this->Get_Account_Info();
        $wallets = array();
        $wallets = $daten->result->wallets;

        foreach($wallets as $val){
            if($val->asset == $coin){
                return $val;
                break;
            }
        }
    }

    /**
     * @param $coin
     * @param $market
     * @return mixed
     * Sell all Coins from One Type
     */
    function SellAllCoin($coin,$market){
        $scc_data = $this->Get_Coin_Data($coin);
        $data = $this->GetOrderbook($market);

        $summe = 0;
        $preis = 0;

        foreach($data->result->bids as $val){
            $summe += $val->amount;
            if($summe >= $scc_data->balance){
                $preis = $val->price;
                break;
            }
        }
        $sell_balance = $scc_data->balance * 0.97;
        return $this->Create_Limit_Order($market,"SELL",$preis,$sell_balance);
    }

}

Und hier eine Beispielanwendung zum Verkauf von Coins über den Exchange:

/**
 * User: alex
 * Date: 15.02.21
 * Time: 11:46
 */
include_once 'config.php';
include_once 'stakecube.php';

$sc = new stakecube($key,$priv_key);

$obj = $sc->SellAllCoin("kydc","kydc_scc");
echo "<pre>";
print_r($obj);
echo "</pre>";
$obj = $sc->SellAllCoin("scc","scc_btc");
echo "<pre>";
print_r($obj);
echo "</pre>";
$obj = $sc->SellAllCoin("ltc","ltc_btc");
echo "<pre>";
print_r($obj);
echo "</pre>";
echo "</pre>";
$obj = $sc->SellAllCoin("doge","doge_btc");
echo "<pre>";
print_r($obj);
echo "</pre>";

Newsletter
jede Woche die neuesten Artikel und exklusive Insights von Kryptomagazin
 

Das Krypto-Magazin kann man nicht nur lesen, folge uns auch auf YouTube. Auf dem YouTube Kanal erfährst du spannende Hintergründe zu Projekten, exklusive Interviews und bekommst einen Einblick hinter die Kulissen des Krypto-Magazin.

Kanal abonnieren

Schreibe einen Kommentar

You have to agree to the comment policy.

Weitere Artikel zum Thema

YieldApp vs Nexo vs StakeCube

StakeCube Update 2022 – V3, Mining, Exchange

StakeCube startet seinen NFT Marktplatz

StakeCube Masternode gegen Hacker schützen

Ich kaufe mir 16 DASH Masternode Anteile

StakeCube eine erfolgreiche Woche