i want to convert web3 from tron web3 to polygon web3

Viewed 24

I want to convert web3 from tron web3 to polygon web3 but I do not know how to do it.

(function() {
    'use strict';

    const ABI = [{"constant":true,"inputs":[],"name":"contractInfo","outputs":[{"internalType":"uint256","name":"_last_id","type":"uint256"},{"internalType":"uint256","name":"_turnover","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"_id","type":"uint256"}],"name":"getUserById","outputs":[{"internalType":"address","name":"addr","type":"address"},{"internalType":"address","name":"upline","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"_upline_id","type":"uint256"}],"name":"register","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[],"name":"upgrade","outputs":[{"internalType":"uint8","name":"level","type":"uint8"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"_addr","type":"address"}],"name":"userInfo","outputs":[{"internalType":"uint256","name":"id","type":"uint256"},{"internalType":"address","name":"upline","type":"address"},{"internalType":"uint8","name":"level","type":"uint8"},{"internalType":"uint256","name":"profit","type":"uint256"},{"internalType":"uint256","name":"lost","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"_addr","type":"address"}],"name":"userStructure","outputs":[{"internalType":"uint256[12]","name":"reinvests","type":"uint256[12]"},{"internalType":"uint256[12][4]","name":"referrals","type":"uint256[12][4]"},{"internalType":"uint256[12][3]","name":"referrals_line1","type":"uint256[12][3]"},{"internalType":"uint8[12][3]","name":"overflow_line1","type":"uint8[12][3]"},{"internalType":"uint256[12][8]","name":"referrals_line2","type":"uint256[12][8]"},{"internalType":"uint8[12][8]","name":"overflow_line2","type":"uint8[12][8]"}],"payable":false,"stateMutability":"view","type":"function"}];

    let contract;

    let VueTRON = {
        data() {
            return {
                tron: { 
                    tronWeb: false,
                    auth: false,
                    account: ''
                }
            };
        },
        created() {
            let self = this,
                tries = 0;

            setTimeout(function initTimer() {
                if(!window.tronWeb) return ++tries < 50 ? setTimeout(initTimer, 100) : null;

                self.tron.tronWeb = !!window.tronWeb;

                window.tronWeb.on('addressChanged', function() {
                    self.tron.account = window.tronWeb.defaultAddress.base58;
                });

                setTimeout(function chechAuth() {
                    self.tron.auth = window.tronWeb && window.tronWeb.ready;
                    if(!self.tron.auth) setTimeout(chechAuth, 200);
                    else self.tron.account = window.tronWeb.defaultAddress.base58;
                }, 200);
            }, 100);
        },
        methods: {
            getTronWeb() {
                return new Promise((resolve, reject) => {
                    window.tronWeb ? resolve(window.tronWeb) : reject('TronWeb not found');
                });
            },
            awaitTx(tx) {
                return new Promise((resolve, reject) => {
                    setTimeout(() => {
                        resolve();
                    }, 3000);
                });
            }
        }
    };
    
    new Vue({
        mixins: [VueTRON],
        el: '#App',
        data: {
            account: null,
            contract_address: document.querySelector('[name="contract"]').content,
            upline_id: 1,
            edit_upline_id: false,
            contract: {
                users: 0
            },
            user: {
                id: 0,
                upline: '',
                level: 0,
                profit: 0,
                lost: 0,
                referrals: [],
                reinvests: [],
                levels: []
            },
            levels: [],
            rates: {},
        },
        mounted() {
            for(let i = 0; i < 12; i++) {
                this.levels.push(i > 0 ? (this.levels[i - 1] * (i > 6 ? 1.5 : 2)) : 25);
                this.user.referrals.push([0,0,0,0]);
                this.user.levels.push(Object.assign({}, {line1:[{id:0,dir:0},{id:0,dir:0},{id:0,dir:0}],line2:[{id:0,dir:0},{id:0,dir:0},{id:0,dir:0},{id:0,dir:0},{id:0,dir:0},{id:0,dir:0},{id:0,dir:0},{id:0,dir:0}]}));
            }

            // Upline
            let m = (location.hash + '; ' + document.cookie).match(/ref=(\d+)/i);
            if(m) {
                document.cookie = "ref=" + m[1] + "; path=/; expires=" + (new Date(new Date().getTime() + 86400 * 365 * 1000)).toUTCString();
                this.upline_id = parseInt(m[1]);
            }

            // Rates
            fetch('https://api.smartcontract.ru/gateway/rates_usd.json').then(r => r.json()).then(r => {
                this.rates = r.data;
            });

            // Coookie policy
            if(!document.cookie.match(/coopolice=1/)) {
                this.notice('This website uses cookies for functionality, analytics and advertising purposes as described in our Privacy Policy. If you agree to our use of cookies, please continue to use our site.<br/><br/><a href="javascript:void()" style="color:#fff; text-decoration:none"><b>OK</b></a>', '007eff', 0)
                .then(() => { document.cookie = 'coopolice=1; Max-Age=31536000; path=/'; });
            }
        },
        watch: {
            'tron.account'() {
                this.getTronWeb().then(tronWeb => {
                    contract = tronWeb.contract(ABI, tronWeb.address.toHex(this.contract_address));

                    // Support
                    let m = location.search.match(/(?:id|addr|address|account)=(T[1-9A-HJ-NP-Za-km-z]{33}|\d+)/);
                    if(m) {
                        if(/^\d+$/.test(m[1])) {
                            contract.getUserById(m[1]).call().then(res => {
                                if(res.addr.substr(2) != '0000000000000000000000000000000000000000') {
                                    this.account = tronWeb.address.fromHex(res.addr);
                                    this.updateInfo();
                                }
                            });
                        }
                        else this.account = m[1];
                    }

                    this.updateInfo();
                });
            }
        },
        methods: {
            // colors: primary = 007eff; success = 4caf50; warning = fb8c00; error = e53935
            notice(msg, color = '007eff', time = 3000) {
                return new Promise((resolve, reject) => {
                    let wrap = $('<div style="box-sizing:border-box; position:fixed; left:calc(50% - 160px); box-shadow:0 5px 25px rgba(0,0,0,0.2); width:320px; top:40px; background:#' + (color ? color : '007eff') + '; border-radius:10px; color:#fff; padding:20px 20px; text-transform:none; font:16px/1.2 Tahoma, sans-serif; cursor:pointer; z-index:999999; text-align:center;">' + msg + '</div>')
                        .on('click', () => { wrap.remove(); resolve(); })
                        .appendTo('body');
                    if(time > 0) setTimeout(() => { wrap.remove(); }, time);
                });
            },
            copyText(value) {
                let s = document.createElement('input');
                s.value = value;
                document.body.appendChild(s);

                if(navigator.userAgent.match(/ipad|ipod|iphone/i)) {
                    s.contentEditable = true;
                    s.readOnly = false;
                    let range = document.createRange();
                    range.selectNodeContents(s);
                    let sel = window.getSelection();
                    sel.removeAllRanges();
                    sel.addRange(range);
                    s.setSelectionRange(0, 999999);
                }
                else s.select();

                try { document.execCommand('copy'); this.notice('Copied!', '4caf50'); }
                catch (err) { this.notice('Copy error', 'e53935'); }

                s.remove();
            },
            updateInfo() {
                this.getTronWeb().then(tronWeb => {
                    contract.contractInfo().call().then(res => {
                        this.contract.users = parseInt(res._last_id) || 0;
                    });

                    contract.userInfo(this.account ? this.account : this.tron.account).call().then(res => {
                        this.user.id = parseInt(res.id) || 0;
                        this.user.upline = res.upline.substr(2) != '0000000000000000000000000000000000000000' ? tronWeb.address.fromHex(res.upline) : '';
                        this.user.level = parseInt(res.level) || 0;
                        this.user.profit = parseInt(tronWeb.fromSun(res.profit));
                        this.user.lost = parseInt(tronWeb.fromSun(res.lost));

                        if(this.user.level > 3) $('.js-b-block_toggle:not(.opened)').click();
                    });

                    contract.userStructure(this.account ? this.account : this.tron.account).call().then(res => {
                        this.user.reinvests = res.reinvests.map(v => parseInt(v) || 0);

                        for(let l = 0; l < 12; l++) {
                            for(let i = 0; i < 4; i++) {
                                this.user.referrals[l][i] = parseInt(res.referrals[i][l]);
                            }

                            for(let i = 0; i < 3; i++) {
                                this.user.levels[l].line1[i].id = parseInt(res.referrals_line1[i][l]);
                                this.user.levels[l].line1[i].dir = parseInt(res.overflow_line1[i][l]);
                            }

                            for(let i = 0; i < 8; i++) {
                                this.user.levels[l].line2[i].id = parseInt(res.referrals_line2[i][l]);
                                this.user.levels[l].line2[i].dir = parseInt(res.overflow_line2[i][l]);
                            }
                        }
                    });
                });
            },
            register() {
                if(!this.tron.account) return this.notice('To join the project you need to use the TRON wallet', 'fb8c00');
                
                this.getTronWeb().then(tronWeb => {
                    contract.getUserById(this.upline_id).call().then(res => {
                        if(res.addr.substr(2) != '0000000000000000000000000000000000000000') {
                            contract.register(this.upline_id).send({
                                callValue: tronWeb.toSun((this.levels[0] * 1.04).toFixed(4))
                            }).then(tx => {
                                this.notice('<i class="fas fa-cog fa-spin"></i> &nbsp; Transaction sent', '4caf50', 5000);

                                this.awaitTx(tx).then(() => {
                                    this.updateInfo();
                                });
                            });
                        }
                        else this.notice('The specified sponsor ID is not registered', 'fb8c00');
                    });
                });
            },
            upgrade() {
                if(!this.tron.account) return this.notice('To join the project you need to use the TRON wallet', 'fb8c00');
                
                this.getTronWeb().then(tronWeb => {
                    contract.upgrade().send({
                        callValue: tronWeb.toSun((this.levels[this.user.level + 1] * 1.04).toFixed(4))
                    }).then(tx => {
                        this.notice('<i class="fas fa-cog fa-spin"></i> &nbsp; Transaction sent', '4caf50', 5000);

                        this.awaitTx(tx).then(() => {
                            this.updateInfo();
                        });
                    });
                });
            },
        }
    });
})();
0 Answers
Related