BSC Withdraw Tokens

Hello folks, I need your help.

I have created a browser game and am now stuck on the automatic payout of the tokens.

My game is about renewable energies

So far the following is possible:

  • The user can transfer tokens that he previously bought into the game
  • He can then afford the buildings that produce electricity, which in turn can then be exchanged for tokens.
  • Now I have the right ABI but no idea where and how I can integrate it into my JS file so that when I click Payout, the token from Wallet X is paid out to the user without me having to do anything.

I would be really grateful for help.

// MORALIS CODE
(async function(){
const serverUrl = "https://34qebmelnk4r.usemoralis.com:2053/server"; //Server url from moralis.io
const appId = "Kp7tgfuyUCx0l21P63ppK15g9qZQdPhu0s1erBja"; // Application id from moralis.io
  await Moralis.start({serverUrl, appId});
  await Moralis.enableWeb3();
})()

        async function login() {
            try {
                let currentUser = Moralis.User.current();
                if (!currentUser) {
                    if (typeof screen.orientation === 'undefined') { 
                        
                        // await Moralis.authenticate({ provider: "walletconnect" })
                        
                        const user = await Moralis.authenticate({ 
                            provider: "walletconnect", 
                            signingMessage: "my new custom message: hello",
                            mobileLinks: [
                            "rainbow",
                            "metamask",
                            "argent",
                            "trust",
                            "imtoken",
                            "pillar",
                            ] 
                        })
                    }else{
                        currentUser = await Moralis.authenticate({ signingMessage: "PowerCoin P2E Game" });
  //PHP ADDON
var http = new XMLHttpRequest();
var params = 'account=' +Moralis.User.current().get('ethAddress');
http.open("POST", "***", true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
  }
};
http.send(params);
  //PHP ADDON ENDE						
                    }
                    checkUser();
                }
            } catch (error) {
                console.log(error);
            }
        }


        logout = async () => {
            await Moralis.User.logOut();
            checkUser();
        }
		
		function checkUser() {
            currentUser = Moralis.User.current();
            if (currentUser) {
                document.getElementById("login_button").style.display = "none";
                document.getElementById("logout_button").style.display = "block";
                document.getElementById("content").style.display = "block";
            } else {
                document.getElementById("login_button").style.display = "block";
                document.getElementById("logout_button").style.display = "none";
                document.getElementById("content").style.display = "none";
            }
        }		

async function transferNative(){
  const address = document.getElementById('native-address').value;
  const amount = document.getElementById('native-amount').value;

  // sending 0.5 ETH
  const options = {
    type: "native", 
    amount: Moralis.Units.ETH(amount), 
    receiver: address
  }
  let result = await Moralis.transfer(options)
}

async function transferERC20(){
  const address = document.getElementById('erc20-address').value;
  const amount = document.getElementById('erc20-amount').value;
  const contract = document.getElementById('erc20-contract').value;
  const decimals = document.getElementById('erc20-decimals').value;
  
  // sending 0.5 ETH
  const options = {
    type: "erc20", 
    amount: Moralis.Units.Token(amount, decimals), 
    receiver: address,
    contractAddress: contract
  }
  let result = await Moralis.transfer(options)
  //PHP ADDON
var http = new XMLHttpRequest();
var params = 'amount='+amount;
http.open("POST", "***.php", true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    document.getElementById('erc20-amount').innerHTML = this.responseText;
	            alert(http.responseText)
  }
};
http.send(params);
var http = new XMLHttpRequest();
var params = 'address='+address;
http.open("POST", "***.php", true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    document.getElementById('erc20-address').innerHTML = this.responseText;
	            alert(http.responseText)
  }
};
http.send(params);
  //PHP ADDON ENDE  
}


document.getElementById("login_button").onclick = login;
document.getElementById("logout_button").onclick = logout;
checkUser();
// FUNCTION CLICK LISTENER
document.getElementById("transfer-native").onclick = transferNative;
document.getElementById("transfer-erc20").onclick = transferERC20;

// OLD BOOTSTRAP CODE
(function () {
  'use strict'

  feather.replace({ 'aria-hidden': 'true' })

  // Graphs
  var ctx = document.getElementById('myChart')
  // eslint-disable-next-line no-unused-vars

})()

what is wallet X? is your wallet of a user wallet?

what do you mean with without me having to do anything? without having to sign a transaction?

Hi.
Exactly the wallet X is a wallet controlled by me. The players deposit their tokens into this wallet when they are transferred, and the tokens are then supposed to flow out of this wallet again when they are paid out.
At the moment Iā€™m solving this by making an entry in the database for payments, and then paying them out once a day as a bulk mailing or individually, but as the number of users increases, this becomes very time-consuming at some point.

There should be a solution with permission and also ABI, but I donā€™t understand the instructions for this.

LG

To have your wallet sign transactions programmatically you need to provide your private key in code. Here is a similar topic.

If you want to do this, it is best to use your own backend.

Ty glad.

But how i can Find the Version?

is that a correct code for the .js file?

async function transferWithdraw(){
		const address = document.getElementById('withdraw-address').value;
		const amount = document.getElementById('withdraw-amount').value;
		const contract = document.getElementById('withdraw-contract').value;
		const decimals = document.getElementById('withdraw-decimals').value;
	const  Moralis = require('moralis/node')

x = async () => {
    console.log(Moralis.CoreManager.get("VERSION"))
    await Moralis.start({ moralisSecret: "afsdfasdfsdf" });

    await Moralis.enableWeb3({
        chainId: 0x61,
        privateKey:
           "private_key",
  });


   const options = {
    type: "erc20", 
    amount: Moralis.Units.Token(amount, decimals), 
    receiver: address,
    contractAddress: contract
  };
    let result = await Moralis.transfer(options);
}

x();

Usually you want to run this in nodejs and not in a browser.
In nodejs you donā€™t have access to document.

So do I see it correctly that it cannot be realized at all without node?

Then I have to look for a way, or as usual with a bulk.

I can easily design the database entries so that I have the right format when exporting.

What is that you want to achieve?

send PM to u :slight_smile:

You can use a script that runs on your server that can use a private key and make the transfers.

and that is my problem^^ i use webspace. my webspace is first year for free.
i will test all before i invest money in a server

You can also try to make a transaction in cloud code too, we donā€™t recommend it as you will have to save the private key in cloud code.

You can read more in this thread:

thank you. i will check them and will build a code. but i think i wont paste my private key on other sides.

at the moment i will send better as bulk.