0
// SPDX-License_Identifier: MIT
pragma solidity >=0.6.6 <0.9.0;
//2.56
//import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import "hardhat/console.sol";
//console.log('msg.value :',msg.value);


interface AggregatorV3Interface {



  function decimals() external view returns (uint8);
  function description() external view returns (string memory);
  function version() external view returns (uint256);

  // getRoundData and latestRoundData should both raise "No data present"
  // if they do not have data to report, instead of returning unset values
  // which could be misinterpreted as actual reported values.
  function getRoundData(uint80 _roundId)
    external
    view
    returns (
      uint80 roundId,
      int256 answer,
      uint256 startedAt,
      uint256 updatedAt,
      uint80 answeredInRound
    );

  function latestRoundData()
    external
    view
    returns (
      uint80 roundId,
      int256 answer,
      uint256 startedAt,
      uint256 updatedAt,
      uint80 answeredInRound
    );
}

contract fundme
{
    //using SafeMathChainlink for uint256;
    mapping (address=> uint256) public indirizzoAvalore;


    function fund() public payable 
    {

       // uint256 minmumusd=50*10**18;
        uint256 minimousd=getConversion(1500000000000000);//0.5 dollari in wei per 1 eth a  3250
        //come l'if,converte il valore di msg.value tramite la funzione e controlla che sia >= del minimo, se non è così manda la scritta è fa un revert
        //revert inverte la transazione
        //require(condizione,se è falsa)

        require(getConversion(msg.value) >= minimousd, "You need to spend more ETH!");
        indirizzoAvalore[msg.sender]+=msg.value;
        // eth to usd conversion rate
    }

    /*function getversion() public view returns(uint256)
    {
        AggregatorV3Interface priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331);
        return priceFeed.version();
    }*/

    function getPrice() public view returns(uint256)
    {
       AggregatorV3Interface priceFeed= AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331);
       (,int256 answer,,,) = priceFeed.latestRoundData();
       return uint256(answer*10000000000);//10000000000
    }

    function getConversion(uint256 ethAmount) public view returns(uint256)
    {
      //ethAmount * ethPrice= costo per singolo wei 
      //1*3250908865370000000000=3250, 3250 il valore di un singolo wei
      //
        uint256 ethPrice = getPrice();
        uint256 ethAmountusd = (ethPrice * ethAmount)/1000000000000000000;
        return ethAmountusd;
    }

    function withdraw(address payable adr2) payable public 
    {
      //150000000000000
      //dichiarazione variabile addres adr1, e assegnazione valore del indirirzzo del chiamante 
      //  address adr1=msg.sender;
      // casting da addres normale a addrs payable
     // address payable adr2 = payable(adr1);
     // uint amount = indirizzoAvalore[adr2];
      //trasferimneto soldi da contratto a adr2 in base aquanto aveva mandato
      //---adr2.transfer(indirizzoAvalore[adr1]);
      // get the amount of Ether stored in this contract


     msg.sender.transfer(balance);

    }

}

This is the code, I've tryed multiple times, but it doesn't send anything, I only pay the transaction price when I call withdraw, I can't understand, looks like the amount is 0 but I pay the fee.

compiler 0.8.7

Antonio Carito
  • 2,445
  • 3
  • 10
  • 23
  • To transfer the contract's balance to another address you could do recipient.transfer(address(this).balance). Just be aware that in the future it might fail if the recipient is a contract, see this for the details https://ethereum.stackexchange.com/questions/19341/address-send-vs-address-transfer-best-practice-usage. – Ismael Apr 14 '22 at 14:18

1 Answers1

0

If I understand your code correctly, the function you want to edit is "withdraw". In that case, I'll suggest yo try the send method, if what you are sending is ether or the main token of the network. It could be something like:

function withdraw (address user, uint256 amount) external payable returns (bool) {
        if (!payable(user).send(amount)) {
            return false;
        } else {
            balance -= amount;
            return true;
        }
    }

If what you are trying to send is an instance of ERC20 or other protocol, you have to check if it is ownable and who owns it. If it is ownable you're missing the approval in which the account owner of the tokens allows your smart contract to transfer token from an account to another. Check for the "approve" method of token ERC20.

Hope it helps.

belFus
  • 11
  • 3