Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I know a user can send ETH to the contract/function manually, but is there way to request a specific amount directly in the code so that it is added to the gas fee, for example, the way dxsale.app does it when creating a presale (see screenshot below) - it adds the 0.1 ETH cost of presale to the 0.0072 gas fee for a total of 0.1072.

Dxsale.app Presale cost is included in the transaction fee

Can this be done in an ERC20 contract? I know I can receive ETH in a payable function, e.g.

    function deposit() payable public {
        require(msg.value > 0, "You need to send some Ether");
    }

but can I specify the amount I want to receive (for a flat fee), so that it is added to the tx cost?

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
164 views
Welcome To Ask or Share your Answers For Others

1 Answer

TLDR: The contract function is executed after the transaction has been sent. So it's not possible to to set the transaction params from the contract.


is there way to request a specific amount directly in the code

Not in the Solidity code. You can only throw an exception if the sent amount is not an expected value.

function deposit() payable public {
    require(msg.value == 0.1 ether, "You need to send 0.1 Ether");
}

You can specify the amount when you're creating the transaction request. For example MetaMask uses the Ethereum Provider API, so you can predefine the amount that MetaMask will show.

const params = [
  {
    from: '0xb60e8dd61c5d32be8058bb8eb970870f07233155',
    to: '0xd46e8dd67c5d32be8058bb8eb970870f07244567',
    value: '0x9184e72a', // 2441406250
  },
];

ethereum
  .request({
    method: 'eth_sendTransaction',
    params,
  });

But the user can override this option in their wallet, or they can always submit a transaction using a different way (not requested with the custom value).


The value (both in the params JS object and the Solidity msg.value global variable) always excludes the gas fees.

So if you're sending 0.1 ETH, the gas fees are added on top of that and the total cost might be 0.10012345. The params.value will contain 0.1 ETH (hex number in wei), as well as the msg.value will contain 0.1 ETH (decimal number in wei).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...