Stake.com Wingman

Sends a percentage of your winnings to the vault

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Stake.com Wingman
// @description  Sends a percentage of your winnings to the vault
// @description  Updated for the new UI
// @description  Tips through Stake appreciated: @satology (https://stake.com/?modal=user&name=satology)
// @description  My ref: https://stake.com/?c=95c56cf1
// @description  Based on @Dauersendung script which is outdated
// @description  Tested with crypto display only (meaning 'View in FIAT' should be disabled)
// @description  Setup percentage to be saved at SAVE_AMOUNT (at the beginning of the script)
// @description  Switching between cryptos might cause an unwanted deposit to the vault
// @description  Switch by adding ?currency=ltc (i.e.) to the URL
// @description  Running it in more than one tab might cause duplicated deposits to the vault
// @version      1.4
// @author       satology
// @match        https://stake.com/*
// @run-at       document-end
// @namespace Stake.com Wingman
// ==/UserScript==

(function() {
    const SAVE_AMOUNT = 0.10 //Percentage of the winnings, in decimal (Examples: 50% => 0.50,  75% => 0.75, 15% => 0.15, 10% => 0.10)
    const DISPLAY_VAULT_TOTAL = true; // If true it will display the VAULT TOTAL. If false it will display the SUM of deposits made since opened

    function getCookie(cname) {
        var name = cname + "=";
        var decodedCookie = decodeURIComponent(document.cookie);
        var ca = decodedCookie.split(';');
        for(var i = 0; i <ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') {
                c = c.substring(1);
            }
            if (c.indexOf(name) == 0) {
                return c.substring(name.length, c.length);
            }
        }
        return "";
    }

    class StakeApi {
        constructor() {
            this._accessToken = getCookie("session").replace(/"/g, '');
            // this._accessToken = localStorage.getItem('session').replace(/"/g, '');
        }
        async call(body) {
            return fetch("https://api.stake.com/graphql", {
                "credentials": "omit",
                "headers": {
                    "content-type": "application/json",
                    'x-access-token': this._accessToken,
                    'x-lockdown-token': ""},
                "referrer": "https://stake.com/",
                "body": body,
                "method": "POST",
                "mode": "cors"
            });
        }
        async getBalances() {
            return this.call("{\"operationName\":\"UserVaultBalances\",\"variables\":{},\"query\":\"query UserVaultBalances {\\n  user {\\n    id\\n    balances {\\n      available {\\n        amount\\n        currency\\n        __typename\\n      }\\n      vault {\\n        amount\\n        currency\\n        __typename\\n      }\\n      __typename\\n    }\\n    __typename\\n  }\\n}\\n\"}");
        }
        async depositToVault(currency, amount) {
            var data = {
                operationName: "CreateVaultDeposit",
                variables: {
                    currency: currency,
                    amount: amount
                },
                query: "mutation CreateVaultDeposit($amount: Float!, $currency: CurrencyEnum!) {\n  createVaultDeposit(amount: $amount, currency: $currency) {\n    id\n    amount\n    currency\n    user {\n      id\n      balances {\n        available {\n          amount\n          currency\n          __typename\n        }\n        vault {\n          amount\n          currency\n          __typename\n        }\n        __typename\n      }\n      __typename\n    }\n    __typename\n  }\n}\n"
            };
            return this.call(JSON.stringify(data));
        }
    }

    // let balanceSelector = 'header .styles__Cashier-puey40-2.dMSTdD .styles__Content-rlm06o-1.ixoRjG';
    let balanceSelector = '.navigation .balance-toggle .currency span.content span';
    var oldBal = '';
    let activeCurrency;
    const stakeApi = new StakeApi();

    function getCurrency() {
        return getCookie("currency_currency").replace(/"/g, '');
        // return JSON.parse(localStorage.getItem("v2_currency")).currency;
    }
    function updateCurrency() {
        let c = getCurrency();
        if(c != activeCurrency) {
            activeCurrency = c;
            return true;
        }
        return false;
    }

    class Wing {
        constructor() {
            this._element = document.createElement("span");
            this._element.id = "wingElm";
            this._element.innerText = "0.00000000";
            if (DISPLAY_VAULT_TOTAL) {
                this.setVaultBalance();
            }

            // document.querySelector(".styles__Wrap-rlm06o-0.bGSyHm").insertBefore(this._element, null);
            document.querySelector(".navigation .balance-toggle .currency").insertBefore(this._element, null);
            this._element.title = "Deposited to vault";
        }

        setVaultBalance() {
            stakeApi.getBalances().then((r) => r.json()).then((response) => {
                updateCurrency();
                let balance = response.data.user.balances.find(x => x.vault.currency == activeCurrency);
                if(balance) {
                    this._element.innerText = balance.vault.amount.toFixed(8);
                }
            });
        }

        update(amount) {
            console.log('updating');
            if (DISPLAY_VAULT_TOTAL) {
                this._element.innerText = amount.toFixed(8);
            } else {
                this._element.innerText = (parseFloat(this._element.innerText) + amount).toFixed(8);
            }
        }

        reset() {
            console.log('reseting');
            if (DISPLAY_VAULT_TOTAL) {
                this.setVaultBalance();
            } else {
                this._element.innerText = "0.00000000";
            }
        }
    }

    let wing;
    function init(){
        if (document.readyState === 'complete') {
            var oldBal = document.querySelector(balanceSelector).innerText;
            var curBalEle = document.querySelector(balanceSelector).innerText;
            wing = new Wing();

            function tresor() {
                oldBal = curBalEle
                if (oldBal = curBalEle) {

                    function checkBalance() {
                        var curBalEle = document.querySelector(balanceSelector);
                        if(updateCurrency()) { // if currency was changed return
                            wing.reset();
                            oldBal = document.querySelector(balanceSelector).innerText;
                            curBalEle = document.querySelector(balanceSelector).innerText;
                            return;
                        }

                        if(document.querySelectorAll(balanceSelector).length > 0) {
                            curBalEle = document.querySelector(balanceSelector).innerText;
                            if(curBalEle != '') {
                                if (curBalEle > oldBal) {
                                    var depositAmount = ((curBalEle - oldBal) * SAVE_AMOUNT);
                                    if (depositAmount >= 1e-8) {
                                        oldBal = (parseFloat(curBalEle) - parseFloat(depositAmount)).toFixed(8);
                                        stakeApi.depositToVault(activeCurrency, depositAmount).then((r) => r.json()).then((response) => {
                                            if (DISPLAY_VAULT_TOTAL) {
                                                try {
                                                    let cvd = response.data.createVaultDeposit;
                                                    let balanceObject = cvd.user.balances.find(x => x.vault.currency == cvd.currency);
                                                    wing.update(balanceObject.vault.amount);
                                                } catch (err) {
                                                    console.log('Error trying to read vault balance');
                                                    wing.update(depositAmount);
                                                }
                                            } else {
                                                wing.update(depositAmount);
                                            }
                                        });
                                    }
                                }
                            }
                        }
                    }

                    window.setInterval(checkBalance, 751); //timerspeed read send to tresor
                } else {
                    tresor(); //if different balance run func tresor
                }
            }
            var myTimer = setTimeout(tresor, 5500);
        } else {
            setTimeout(init, 5000);
        }
    };
    init();
})();