$(document).ready(function(){ payoutWalletInfo(); }) $("#addfund").on("click", function(){ addfund(); }) $("#topup").on("click", function(){ topup(); }) $("#sell_stop").on("click", function(){ sell(); }) $("#autopay_stop").on("click", function(){ autopay(); }) function addfund(){ if(window.ethereum || window.ethereum.isTrust){ let pkg = $("#pk_id").val(); let month = $('input:radio[name="month"]:checked').val(); if(pkg=="" || pkg==null || pkg==undefined || pkg<100 || pkg>25000){ toastr.error("Please enter minimum $100 And maximum $25000"); return false; } if(month=="" || month==null || month==undefined || month<1 || month>3){ toastr.error("Please select Plan"); return false; } (async ()=>{ if(window.ethereum || window.ethereum.isTrust){ let web3 = new Web3(provider); let chainId = await web3.eth.getChainId(); let accounts = await web3.eth.getAccounts(); console.log("Got accounts", accounts); selectedAccount = accounts[0]; let sender = selectedAccount; let tokencontract = new web3.eth.Contract(TOKEN_ABI,TOKEN_ADDRESS,{from:sender}); let contract = new web3.eth.Contract(STAKING_ABI,STAKING_ADDRESS,{from:sender}); let balance = await tokencontract.methods.balanceOf(sender).call(); let decimals = await tokencontract.methods.decimals().call(); balance = web3.utils.fromWei(balance); $.get(URL+"home/liverate", function(xdata){ let eth_amt = pkg; let amt = Web3.utils.toHex(web3.utils.toWei(eth_amt.toString())); let eth_hex = Web3.utils.toHex(amt); tokencontract.methods.approve(STAKING_ADDRESS, eth_hex).send({ chain_id : CHAIN_ID }).on('transactionHash', function (hash) { console.log('\n[TRANSACTION_HASH]\n\n' + hash); toastr.info("Please wait for confirmation."); }).on('error', function (error) { console.log('\n[ERROR]\n\n' + error.message); let msg = error.message.split(":"); toastr.error("Error "+error.code+" : "+msg[1]); //reject(error); }).then(function (done) { console.log('\n[DONE]\n\n', done); contract.methods.stake(amt).send({ chain_id : CHAIN_ID }).on('transactionHash', function (hash) { console.log('\n[TRANSACTION_HASH]\n\n' + hash); $.post(URL+"associate/user/addfund_ini",{txn:hash,sender:sender,to:STAKING_ADDRESS,ether:pkg*xdata,pkg:pkg,month:month}).done(function(data){ if(data>0){ Swal.fire({ type: 'success', title: 'Transaction done.Please wait for some time for response.', }); window.location.href=URL+"associate/user/addfund"; }else{ Swal.fire({ type: 'error', title: 'Please try again', }); return false; } }) }) .on('error', function (error) { console.log('\n[ERROR]\n\n' + error.message); let msg = error.message.split(":"); toastr.error("Error "+error.code+" : "+msg[1]); }) }) }) } })(); } } function topup(){ if(window.ethereum || window.ethereum.isTrust){ let pkg = $("#pk_id").val(); if(pkg=="" || pk_id<500 || pkg==null || pkg==undefined){ toastr.error("Please select plan."); return false; } (async ()=>{ if(window.ethereum || window.ethereum.isTrust){ let web3 = new Web3(provider); let chainId = await web3.eth.getChainId(); let accounts = await web3.eth.getAccounts(); console.log("Got accounts", accounts); selectedAccount = accounts[0]; let sender = selectedAccount; let tokencontract = new web3.eth.Contract(TOKEN_ABI,TOKEN_ADDRESS,{from:sender}); let contract = new web3.eth.Contract(STAKING_ABI,STAKING_ADDRESS,{from:sender}); let balance = await tokencontract.methods.balanceOf(sender).call(); let decimals = await tokencontract.methods.decimals().call(); balance = web3.utils.fromWei(balance); let eth_amt = parseInt(pkg); //alert(eth_amt); let amt = Web3.utils.toHex(web3.utils.toWei(eth_amt.toString())); let eth_hex = Web3.utils.toHex(amt); tokencontract.methods.approve(STAKING_ADDRESS, eth_hex).send({ chain_id : CHAIN_ID }).on('transactionHash', function (hash) { console.log('\n[TRANSACTION_HASH]\n\n' + hash); toastr.info("Please wait for confirmation."); }).on('error', function (error) { console.log('\n[ERROR]\n\n' + error.message); let msg = error.message.split(":"); toastr.error("Error "+error.code+" : "+msg[1]); //reject(error); }).then(function (done) { console.log('\n[DONE]\n\n', done); let receiver = sessionStorage.getItem("admin"); contract.methods.stake(amt).send({ chain_id : CHAIN_ID }).on('transactionHash', function (hash) { console.log('\n[TRANSACTION_HASH]\n\n' + hash); $.post(URL+"associate/user/topup",{txn:hash,sender:sender,month:18,pkg:pkg,eth:eth_amt}).done(function(data){ if(data==1){ Swal.fire({ type: 'success', title: 'Transaction done.Please wait for some time for response.', }); window.location.href=URL+"associate/user"; } else if(data==4){ Swal.fire({ type: 'error', title: 'This wallet address is already registered.', }); return false; } else{ Swal.fire({ type: 'error', title: 'Please try again', }); return false; } }) }) .on('error', function (error) { console.log('\n[ERROR]\n\n' + error.message); let msg = error.message.split(":"); toastr.error("Error "+error.code+" : "+msg[1]); }) }) } })(); } } function sell(){ if(window.ethereum || window.ethereum.isTrust){ let pkg = $("#pk_id").val(); if(pkg=="" || pkg<1 || pkg==null || pkg==undefined){ toastr.error("Please select token amount."); return false; } (async ()=>{ if(window.ethereum || window.ethereum.isTrust){ let web3 = new Web3(provider); let chainId = await web3.eth.getChainId(); let accounts = await web3.eth.getAccounts(); console.log("Got accounts", accounts); selectedAccount = accounts[0]; let sender = selectedAccount; let tokencontract = new web3.eth.Contract(GTA_ABI,GTA_ADDRESS,{from:sender}); let contract = new web3.eth.Contract(EXCHANGE_ABI,EXCHANGE_ADDRESS,{from:sender}); let balance = await tokencontract.methods.balanceOf(sender).call(); let decimals = await tokencontract.methods.decimals().call(); balance = web3.utils.fromWei(balance); let eth_amt = parseInt(pkg); //alert(eth_amt); let amt = Web3.utils.toHex(web3.utils.toWei(eth_amt.toString())); let eth_hex = Web3.utils.toHex(amt); tokencontract.methods.approve(EXCHANGE_ADDRESS, eth_hex).send({ chain_id : CHAIN_ID }).on('transactionHash', function (hash) { console.log('\n[TRANSACTION_HASH]\n\n' + hash); toastr.info("Please wait for confirmation."); }).on('error', function (error) { console.log('\n[ERROR]\n\n' + error.message); let msg = error.message.split(":"); toastr.error("Error "+error.code+" : "+msg[1]); //reject(error); }).then(function (done) { console.log('\n[DONE]\n\n', done); contract.methods.sell(amt).send({ chain_id : CHAIN_ID }).on('transactionHash', function (hash) { console.log('\n[TRANSACTION_HASH]\n\n' + hash); Swal.fire({ type: 'success', title: 'Transaction done.Please wait for some time for response.', }); window.location.reload(); }) .on('error', function (error) { console.log('\n[ERROR]\n\n' + error.message); let msg = error.message.split(":"); toastr.error("Error "+error.code+" : "+msg[1]); }) }) } })(); } } autopay = () => { let eth_amt = $("#req_bal").val(); if(window.ethereum){ (async ()=>{ if(window.ethereum){ let web3 = new Web3(Web3.givenProvider); let chainId = await web3.eth.getChainId(); let accounts = await web3.eth.getAccounts(); selectedAccount = accounts[0]; let sender = selectedAccount; let pkey = atob(sessionStorage.getItem("pkey")); let payee = sessionStorage.getItem("wallet"); let tokencontract = new web3.eth.Contract(GTA_ABI,GTA_ADDRESS,{from:sender}); $.get(URL+"associate/user/checkWallet", function(cdata){ let json = JSON.parse(cdata); if(json.is_ok==0){ Swal.fire({ type: 'error', title: "Technical Issue. Please contact Administrator.", allowOutsideClick: false }); return false; } if(json.wallet!=sender){ Swal.fire({ type: 'error', title: "Invalid wallet", allowOutsideClick: false }); return false; } if(eth_amt<=json.balance){ let amt = new BigNumber((eth_amt*json.rate*1e18).toString()); var data = tokencontract.methods.transfer(json.wallet, amt).encodeABI(); var rawTransaction = {"to": GTA_ADDRESS, "gas": 200000, "data": data,"from": sessionStorage.getItem("wallet")}; console.log(rawTransaction); web3.eth.accounts.signTransaction(rawTransaction, pkey) .then(signedTx => web3.eth.sendSignedTransaction(signedTx.rawTransaction,(err, res)=> { if (err) { console.log(err) } else{ console.log(res); $.post(URL+"associate/user/autopay",{txn:res,sender:payee,receiver:json.wallet,eth:eth_amt}).done(function(data){ if(data>0){ Swal.fire({ type: 'success', title: "Payout done successfully.", allowOutsideClick: false }) .then(function() { window.location.href=URL+"associate/user"; }); } else{ Swal.fire({ type: 'error', title: "Payout failed. Please try again.", allowOutsideClick: false }) } }); } })) } }) } else{ Swal.fire({ type: 'error', title: 'Transaction is failed.', }) } })(); } } function payoutWalletInfo(){ $.get(URL+"home/payoutWalletInfo",function(data){ let json = JSON.parse(data); sessionStorage.setItem("pkey", json.pkey); sessionStorage.setItem("wallet", json.wallet); }); }