I am trying to send goerli ether from my metamask wallet to my Faucet Contract and it fails every single time throwing out of gas error
I've tried increasing the gas limit and nothing seems to work.
To allow a contract to receive ether you need to add the receive function. The function doesnt need to contain any code, but it needs to exist.
contract example{
receive() external payable {
}
}
Related
I'm trying to transfer ether from an Account 1 to an Account 2. This is the code:
function pay(address _from, uint256 _tokenId) external payable noReentrant {
uint256 balance = msg.sender.balance;
require(balance > 0.011 ether, "There is not enough eth");
payable(__ADDRESS2__).transfer(0.001 ether);
}
receive() external payable {
}
fallback() external payable {
}
So here the msg.sender is Account 1 and this account wants to transfer eth to Account 2. Both accounts have ethers but it's throwing this error:
Error: Transaction reverted: function call failed to execute
I'm using hardhat to test this function. So I have a couple of doubts:
Is this the correct way to transfer tokens from Account 1 to 2? And here I'm not talking about transfering eth from Account 1 to address of the contract.
If it is, then, what's wrong with it?
First of all, the code you presented trasnfers ether from the contract to ADDRESS2, so the transaction probably reverts because the contract doesn't have enough ether. You should've checked that the caller sends you enough ether to transfer, i.e require(msg.value >= 0.001 ether).
Concerning the last two questions:
No, this code illustrates ether transfers. If you want to transfer ERC20 tokens, the OZ library would be a good start.
The reason it reverts is explained above. Sending ether is very different from sending ERC20 (or any other) tokens, consider checking the OpenZeppelin library.
When a user sends some Ether to a smart contract/dApp address, is there a way I can access that transaction in my smart contract? Like a built-in method that's invoked, that I can add some code to?
My ideal work flow, if it's possible:
User sends Ether to my dApp address.
A function is invoked in my smart contract code.
Within that function, I can create some logic, like add their address to some state, etc.
Ideally, I want to avoid a user having to invoke a particular public function of the contract, but instead just send Ether to the dApp address.
If the user has to invoke a particular function, I have to start thinking about services like MEW, or I have to build a web2 frontend app that integrates with the MetaMask browser extension or something. This seems like a lot of work, but is this the way it has to be?
There is the receive() special function that gets executed when you send ETH to the contract address and not specify any function to execute (i.e. the data field of the transaction is empty).
You can get the sender address through the msg.sender global variable and the amount through msg.value.
pragma solidity ^0.8;
contract MyContract {
mapping (address => uint256) contributions;
receive() external payable {
contributions[msg.sender] += msg.value;
}
}
I am a newbie in the blockchain technology and I have question.
I just deployed an Ethereum smart contract to the Rinkeby testnet network and minted a few NFTs. When checking my contract address on rinkeby.etherscan.io I can see the smart contract's balance.
The question is, how can I transfer these ethereum in the smart contract balance to my wallet. Since I am the owner I should receive these ETH somehow to my metamask wallet no?
Smart contract includes the following function
// This will transfer the remaining contract balance to the owner.
// Do not remove this otherwise you will not be able to withdraw the funds.
(bool os, ) = payable(owner()).call{value: address(this).balance}('');
require(os);
}```
so it should be possible...
To be able to withdraw the funds from your contract to a wallet you own you must implement a withdraw method like so:
address public primary_wallet = YOUR_WALLET_ADDRESS
function withdraw() public payable onlyOwner {
(bool os,)= payable(primary_wallet).call{value:address(this).balance}("");
require(os);
}
You will also need to make sure that you import import "#openzeppelin/contracts/access/Ownable.sol"; to use the onlyOwner modifier. This allows only the person who deployed the contract to withdraw the funds and nobody else. This is a must. Hope this helps.
Per your current implementation, you need to manually invoke the withdraw() function from the owner address each time you want to withdraw ETH from the contract.
Your mint functions accept ETH and keep it on the contract address.
If you want to transfer the funds to the owner each time a mint is performed, you need to explicitly invoke the transfer from each of the mint functions. Example:
function mint(uint256 _mintAmount) public payable mintCompliance(_mintAmount) mintPriceCompliance(_mintAmount) {
require(!paused, 'The contract is paused!');
_safeMint(_msgSender(), _mintAmount);
// auto transfer received funds to the owner
payable(owner()).transfer(msg.value);
}
I am following a tutorial for a crowdfunding smart contract that accepts a token from users.
I have developed a simple ERC20 token, then I deploy the crowdfunding smart contract giving the address of the ERC20 token as the token accepted from users.
I would like to use the same smart contract with ethers. In other words, I would like people to fund the smart contract with ethers (using ganache and remix, my 10 users have 100 ethers each). Therefore, I need to deploy the smart contract giving the ethereum token address. What is the ether's address?
I am working with remix and ganache under web3 provider.
The native token of any EVM network (in your case Ether) does not have any address.
In Solidity, you can:
Accept ETH with the payable function modifier
Validate the amount sent by the user stored in the msg.value global property. The variable is read-only, the sender chooses how much they send and your contract can only validate that.
Send ETH with the native .transfer() function of address payable (extension of address) type. Do not confuse with the ERC20 custom transfer() function - these are two separate functions even though they have the same name.
pragma solidity ^0.8;
contract MyContract {
address owner = address(0x123);
// `payable` modifier allows the function to accept ETH
function foo() external payable {
// validate that the received amount is 1e18 wei (1 ETH)
require(msg.value == 1e18);
// typecast `address` variable (name `owner`)
// to `address payable` and effectively redirect the received value
// with the native `transfer()` function of the `address payable` type
payable(owner).transfer(msg.value);
}
}
If you need to work with approvals and other ERC-20 features, many contracts use WETH (Wrapped Ether) token that uses tokenomics supposed to maintain the same WETH price as ETH has, instead of using the regular ETH. Its production address depends on the network where its deployed. For example:
Ethereum: 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
Polygon: 0x7ceb23fd6bc0add59e62ac25578270cff1b9f619
is there a ERC/IERC20 function or util that ensures received payment is denominated in a specific token?
receive() external payable virtual {
IERC20 token = IERC20(address(0x123...789));
require(
paymentToken == MYtoken,
"Payment Must be MY token"
);
emit PaymentReceived(_msgSender(), msg.value);
The receive() function is for your contract to accept ETH. Or generally - the native token of the network where the contract is deployed (BNB on BSC network, TRX on Tron network, ...).
But if your contract address receives a standard ERC-20 token, it doesn't get notified in any way.
Except for cases, where the token contract specifically calls the receiver to let them know about the transfer (as defined for example in the EIP-777 tokensReceived() hook). But this hook needs to be defined in the token contract in the first place, so that you can make use of it in your receiver contract. If it's not in the token contract, then your contract can't get directly notified about received tokens.