Truffle : VMException while transferring Ether from one smart contract to other

Hi,

I have following 2 smart contracts,

pragma solidity 0.5.16;
contract SC1 {
    address owner;
    constructor() public {
        owner = msg.sender;
    }
    function transferTo(uint amount, address payable dest ) public {
        require(tx.origin == owner);
        dest.transfer(amount);
    }
    function () external payable{
    }
}

==

pragma solidity ^0.5.16;
interface SC1 {  
   function transferTo(uint amount, address payable to   ) external;
}
contract SC2{

    uint public  count;
    address owner;
    constructor() public {
        owner = msg.sender;
    }
    function() external payable  { 
      count++; 
      if (count < 2 ) 
         TxUserWallet(msg.sender).transferTo(msg.sender.balance, address(this));
      
   }
}

truffle script

var assert = require('assert');
const path = require("path");
const fs = require("fs");


module.exports = async function(callback) {
  try {
     let arg1 = ""
     let arg2  = ""
     let  amount = '6'
     const accounts = await web3.eth.getAccounts();
     const acc2 = accounts[2];
     transferFuncName= "transferTo"
     const vic= artifacts.require("SC1");
     const att= artifacts.require("SC2");
     const vicobj = await vic.new();
     const attobj = await att.new();
     result1 = await web3.eth.sendTransaction({to:vicobj.address, from:acc2, value: web3.utils.toWei(‘11’)})
    
     arg2 =  attobj.address
     arg1 = web3.utils.toWei(amount,"ether")
     result2 = await vicobj[transferFuncName](arg1, arg2, {from:accounts[0]})
    }
 catch(error) {
    console.log(error)
  }

  callback()
}

Following is the output:

Using network 'development'.

{ Error: Returned error: VM Exception while processing transaction: revert
at module.exports (/home/zulfi/Truffle_programs/js_search_opcode_js/executingFunc.js:23:46)
at process._tickCallback (internal/process/next_tick.js:68:7)
hijackedStack:
'Error: Returned error: VM Exception while processing transaction: revert\n at Object.ErrorResponse (/home/zulfi/.nvm/versions/node/v10.23.3/lib/node_modules/truffle/build/webpack:/node_modules/web3-core-helpers/src/errors.js:29:1)\n at /home/zulfi/.nvm/versions/node/v10.23.3/lib/node_modules/truffle/build/webpack:/node_modules/web3/node_modules/web3-core-requestmanager/src/index.js:170:1\n at /home/zulfi/.nvm/versions/node/v10.23.3/lib/node_modules/truffle/build/webpack:/packages/provider/wrapper.js:107:1\n

Somebody please guide me.

Zulfi.

Hello @zak100 how are you doing?

I think I found your problem.

In the first transaction, result1 does not have a specific unit in the value.

For example, do you want to transfer 11, but it is not specific? 11 in ether, 11 in gwei or 11 in szabo, etc.

So by default ether is web3.utils.toWei('11', 'ether') if you don't have 11 ether in your wallet the transaction fails.

I hope this solves your problem.

Note: You can use web3.utils.toWei('0.000000001', 'ether') for example