function payout(key){ if(window.ethereum || window.ethereum.isTrust){ let hash = key.split("|"); let id = hash[0]; let user_id = hash[1]; let receiver = hash[2]; let eth_amt = hash[3]; let usd_amt = parseFloat(hash[4]); usd_amt=usd_amt.toFixed(2); let pay = hash[5]; (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; //wei = web3.utils.toBN(wei); // let wei_balance = await web3.eth.getBalance(sender); // let eth_balance = web3.utils.fromWei(wei); // if(isNaN(eth_balance)){ // eth_balance = 0; // } let token_contract = (pay=="working")?TOKEN_ADDRESS:BUSD_ADDRESS; let token_abi = (pay=="working")?TOKEN_ABI:BUSD_ABI; let tokencontract = new web3.eth.Contract(token_abi,token_contract,{from:sender}); let balance = await tokencontract.methods.balanceOf(sender).call(); let decimals = await tokencontract.methods.decimals().call(); $.get(URL+"home/liverate", function(xdata){ let amt = eth_amt*xdata; var wei = (pay=="working")?web3.utils.toWei(usd_amt.toString()):web3.utils.toWei(eth_amt.toString()); balance = web3.utils.fromWei(balance); //let eth_hex = Web3.utils.toHex(eth_amt*1e18); //let eth_hex = Web3.utils.toHex(eth_amt * Math.pow(10, decimals)); tokencontract.methods.approve(TOKEN_ADDRESS, wei).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); tokencontract.methods.transfer(receiver,wei).send({ value : 0, chain_id : CHAIN_ID }).on('transactionHash', function (hash) { console.log('\n[TRANSACTION_HASH]\n\n' + hash); $.post(URL+"admincoin/payment/approve",{txn:hash,sender:sender,to:receiver,id:id,user_id:user_id,amt:eth_amt,usd:usd_amt,pay:pay}).done(function(data){ if(data>0){ Swal.fire({ type: 'success', title: "Payout done successfully.", allowOutsideClick: false }).then(function(){ window.location.href = URL+"admincoin/payment/requests/working"; } ); } else if(data==0){ Swal.fire({ type: 'error', title: "Payout failed. Please try again later.", }); } }); }) .on('error', function (error) { console.log('\n[ERROR]\n\n' + error.message); let msg = error.message.split(":"); toastr.error("Error "+error.code+" : "+msg[1]); }) }) }) } else{ Swal.fire({ type: 'error', title: 'Transaction is failed.', }) } })(); } }