All files / abi TronLink.js

29.41% Statements 15/51
59.09% Branches 13/22
10.53% Functions 2/19
29.41% Lines 15/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117        1x 1x                                   28x 4x   24x   7x       6x   1x       15x   15x 9x   6x 6x   6x       3x                                                                                                                                    
import { Address } from "./base/Address";
import { TokenTrc20 } from "./TokenTrc20";
export default class TronLink {
    constructor(tronWeb) {
        this.tronWeb = tronWeb;
        this.tokens = {};
    }
    isInstalled() {
        return !!this.tronWeb;
    }
    isLoggedIn() {
        return this.tronWeb && this.tronWeb.ready;
    }
    isUnlocked() {
        return this.isLoggedIn();
    }
    getAccountAddress() {
        return this.tronWeb.defaultAddress.base58;
    }
    NewContract(abi = [], address = false) {
        return new this.tronWeb.Contract(this.tronWeb, abi, address);
    }
    convertAddress(address, fromFormat, toFormat) {
        if (fromFormat == toFormat) {
            throw "From and To address formats are equal";
        }
        switch (toFormat) {
            case "hex":
                switch (fromFormat) {
                    case "base58":
                    case "tron":
                    case "trx":
                        return "0x" + this.tronWeb.address.toHex(address);
                }
                break;
            case "base58":
            case "tron":
            case "trx":
                switch (fromFormat) {
                    case "hex":
                        if (!Address.isHexAddress(address)) {
                            throw "Invalid hex address";
                        }
                        Eif (address.startsWith("0x")) {
                            address = address.substr(2);
                        }
                        return this.tronWeb.address.fromHex(address);
                }
                break;
        }
        throw "Invalid address formats";
    }
    async coinTRX() {
        let wallet_trx_coin = 0;
        await this.tronWeb.trx.getBalance(this.getAccountAddress(), (e, balance) => {
            wallet_trx_coin = balance;
        });
        return wallet_trx_coin;
    }
    async getCoin(trc20_coin) {
        return await this.getThirdTokenBalance(this.getAccountAddress(), trc20_coin);
    }
    async coinDP() {
        return await this.getThirdTokenBalance(this.getAccountAddress(), "TXHvwxYbqsDqTCQ9KxNFj4SkuXy7EF2AHR");
    }
    async coinCOLA() {
        return await this.getThirdTokenBalance(this.getAccountAddress(), "TSNWgunSeGUQqBKK4bM31iLw3bn9SBWWTG");
    }
    async coinBTC() {
        return await this.getThirdTokenBalance(this.getAccountAddress(), "TN3W4H6rK2ce4vX9YnFQHwKENnHjoxb3m9");
    }
    async coinETH() {
        return await this.getThirdTokenBalance(this.getAccountAddress(), "THb4CqiFdwNHsWsQCs4JhzwjMWys4aqCbF");
    }
    async coinSUN() {
        return await this.getThirdTokenBalance(this.getAccountAddress(), "TKkeiboTkxXKJpbmVFbv4a8ov5rAfRDMf9");
    }
    async coinUSDT() {
        return await this.getThirdTokenBalance(this.getAccountAddress(), "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t");
    }
    async getThirdTokenBalance(address, trc20_address) {
        if (!this.isLoggedIn()) {
            throw "wallet is not login";
        }
        let contract;
        if (!this.tokens.hasOwnProperty(trc20_address)) {
            contract = new TokenTrc20(this.tronWeb);
            contract.setDebug(false);
            await contract.init(trc20_address);
            const a = await contract.balanceOf(address);
            const d = await contract.decimals();
            this.tokens[trc20_address] = {
                instance: contract,
                address: trc20_address,
                decimal: d,
                hold: {}
            };
            this.tokens[trc20_address].hold[address] = a;
        }
        else {
            contract = this.tokens[trc20_address].instance;
            const aa = await contract.balanceOf(address);
            const dec = await contract.decimals();
            this.tokens[trc20_address].decimal = dec;
            this.tokens[trc20_address].hold[address] = aa;
        }
        return this.tokens[trc20_address];
    }
    getListedCoins() {
        return this.tokens;
    }
    explainTrc20(payload) {
        const me = this.getAccountAddress();
        return payload.hold[me];
    }
}