How to transfer ether to smart contract by msg.value? - ethereum

I want to transfer some ether form my address to the smart contract, I have tried the code below but it doesn't work. How to transfer ether through msg.sender?
pragma solidity >=0.7.0 <0.9.0;
contract Test {
function testTransfer() external payable {
bool sent = payable(address(this)).send(msg.value);
require(sent, "invalid balance");
}
}
Once I trigger the function with the value of 1 ether, the error output is as below:
I'm sure that I have enough Ether in my address. What should I do to transfer the Ether? Thanks!

The transaction fails because the smart contract is trying to transfer the ether to himself, and the smart contract doesn't have defined the receive function so it can receive ether that way, for your example you could simply remove all the code inside the function and make another function to check the contract balance and it will work
pragma solidity >=0.7.0 <0.9.0;
contract Test {
function testTransfer() external payable {}
function getBalance() external view returns (uint256) {
return address(this).balance;
}
}

Related

how to implement the ERC20 token transferFrom method in solidity smartcontract

Assuming I have a wallet, I want to authorize another wallet to send a transfer on my behalf, how do I implement the above using the following practice.
pragma solidity ^0.8.0;
import 'Token.sol';
contract TransferToken{
function transferFrom(address recipient, uint amount) external {
Token token = Token(0xd9145CCE52D386f254917e481eB44e9943F3555);
token.transferFrom(msg.sender, recipient, amount);
}
}
contract Owner {
function transfer(address recipient, uint amount) external {
Token token = Token(0xd9145CCE52D386f254917e481eB44e9943F39138);
token.approve(0x617F2E2fD72FD9D5503197092aC168c91465E7f2, amount);
TransferToken transferToken = TransferToken(0x617F2E2fD72FD9D5503197092aC168c91465E7f2);
transferToken.transferFrom(recipient, amount);}}
in the above example, i am using the openZappelin implementation for ERC20 on the import (Token.sol)
now my question is this ... since I want to use a real wallet with ERC20 token how do I implement the above example, I am thinking for this I don't need the import anymore, but I don't know how to implement it without too many mistakes already, I very new to solidity and I am using remix for the above implementation
will be glad if i could get a help on this

Why is Chainlink oracle function call failing?

While attempting to fund me contract is tell me it encountered an error without specifying the error. I attempted to fund 0.1 eth through the fund function, and in the terminal it says:
[block:8404521 txIndex:12]
from: 0x8a9...e4303
to: FundMe.fund() 0x542...E109C
value: 100000000000000000 wei
data: 0xb60...d4288
logs: 0
hash: 0x29a...97939
and in the etherscan it says:status fail :
Contract 0x5422f3458be343e378e7a399e16fff548e7e109c
Warning! Error encountered during contract execution [execution reverted]
I tried looking for problems with my code and found none.
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.6 <0.9.0;
import "#chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
//import "#chainlink/contracts/src/v0.8/vendor/SafeMathChainlink.sol"; won't need in later complier versions.
contract FundMe {
mapping(address => uint256) public addressToAmountFunded;
function fund() public payable {
uint256 minimumUSD = 50 * 10 ** 18;
require( getConversionRate(msg.value) >= minimumUSD,"You need to send more Eth");
addressToAmountFunded[msg.sender] += msg.value;
}
function getVersion() public view returns (uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419);
return priceFeed.version();
}
function getPrice() public view returns (uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419);
(,int256 answer,,,)=priceFeed.latestRoundData();
return uint256(answer * 10000000000);
}
//10000000000 = Gwei which is why we added 10 zeros to getPrice(answer) to convert it to Wei amount
function getConversionRate(uint256 ethAmount) public view returns (uint256){
uint256 ethPrice = getPrice();
uint256 ethAmountInUsd = (ethPrice * ethAmount)/ 1000000000000000000; //divide 2x because we added 10*((getPrice)answer))
return ethAmountInUsd;
}
}
Aggregator contract address "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419" belongs to mainnet
From here get the ETH/USD goerli testnet address:"0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e"
Now in order to call chainlink oracle you have to have LINK token in your contract. Get some link token to your conract address from the faucet
Import the token in metamask. you will see the amount
send link token from your metamask to your contract
deploy your contract. if you are using Remix IDE chose the injected provider to connect to metamask. Because chainlink contract is on goerli, so you need to be on Goerli testnet. Once deployment goes through you can call the fund function.
Since the fund function has no argument, you need to send the value alongside the transaction. That is why inside the function you have msg.value to access to the sent amount.
In Remix ide, under "GAS LIMITinput there isVALUEinput. you need to pass the amount in there before you call thefund` function.

Transfer ether from account A to contract and then contract to account B

I am trying to write a code that:
Send 10000000000000000 wei (0.01 ether) from metamask accountA to a smart contract
Send ether from smart contract to accountB
I have written the code bellow and deployed via metamask. The amount 0.01 goes in the smart contract and I can check the balance is correct.
contract ONE {
address payable public owner;
constructor() payable {
owner=payable(msg.sender);
}
receive () external payable {}
function sendViaCall(address payable _to ) payable external {
(bool sent, ) = _to.call{value: msg.value}("");
require(sent, "Failed to send Ether");
}
function GetBalance() public view returns (uint) {
return address(this).balance;
}
However when I try to send ether from the contract to an accountB, this is not working.
I write the address on the box bellow and call the function on metamask, but it is not sending ether to Account B.
what I am doing wrong ?
Appreciate any help.
Your code is running and not throwing error because msg.value is 0. every time you call sendViaCall, you are sending a 0 amount.
If you want to send msg.value alongside the function call on Remix, Remix does not have this functionality (Or maybe I could not figure out). In front end setup, when we call a contract function, we pass the last argument to the function {value:amountToSend} and this was received as msg.value inside the contract function.
If you want to test sending balance, you could use address(this).balance
function sendViaCall(address payable _to ) external payable {
(bool sent, ) = _to.call{value: address(this).balance}("");
require(sent, "Failed to send Ether");
}

Why can't I use this transferEther function to send Ether to the smart contract?

I have this code I have entered into Remix IDE, as ReceivedEther.sol, a standalone smart contract.
I've transferred 0.02 Ether to the smart contract, using MetaMask.
When I checked the smart contract's balance, it returns 200000000000000000, as expected.
If I try to use the transferEther function, however, and enter a number smaller than this - say, 0.005 ETH, or 50000000000000000 as the amount - it doesn't work using MetaMask.
When MetaMask prompts me it's never for that amount. It's for 0 ETH and 0.00322 gas fee (or whatever the gas is). Basically it always set the amount of ETH at 0 and only charges the fee.
Why can't I transfer an amount of ETH using this function in the Remix IDE with MetaMask?
pragma solidity ^0.8.0;
contract ReceivedEther {
function transferEther(address payable _recipient, uint _amount) external returns (bool) {
require(address(this).balance >= _amount, 'Not enough Ether in contract!');
_recipient.transfer(_amount);
return true;
}
/**
* #return contract balance
*/
function contractBalance() external view returns (uint) {
return address(this).balance;
}
}
Your code sends ETH (stated in the _amount variable) from the smart contract to the _recipient. So it doesn't require any ETH to be sent in order to execute the transferEther() function.
If you want your contract to accept ETH, the function that accepts it (or the general fallback() or receive() function) needs to be marked as payable.
Example:
pragma solidity ^0.8.0;
contract ReceivedEther {
receive() external payable {} // note the `payable` keyword
// rest of your implementation
}
Then you can send whathever amount of ETH to the smart contract address (without specifying any function to execute).
See more at https://docs.soliditylang.org/en/v0.8.5/contracts.html#receive-ether-function
If you want to prefill the amount in MetaMask from Remix IDE, you can use the "Value" input in the "Deploy & Run Transactions" tab.

How to fund Chainlink smart contract with other contract?

I'm trying to fund a smart contract with LINK tokens. I get an error "VM error: revert. revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information." My code is simple:
// SPDX-License-Identifier: MIT
pragma solidity >=0.5 <0.9.0;
//Remix Imports
import "https://github.com/smartcontractkit/chainlink/blob/develop/evm-contracts/src/v0.6/ChainlinkClient.sol";
import "https://github.com/smartcontractkit/chainlink/blob/develop/evm-contracts/src/v0.6/vendor/Ownable.sol";
import "https://github.com/smartcontractkit/chainlink/blob/develop/evm-contracts/src/v0.6/vendor/SafeMathChainlink.sol";
import "https://github.com/smartcontractkit/chainlink/blob/develop/evm-contracts/src/v0.6/interfaces/LinkTokenInterface.sol";
import "https://github.com/smartcontractkit/chainlink/blob/master/evm-contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
contract SimpleStorage {
SimpleStorage2[] simpleStorage2s;
function set() public payable returns(address) {
SimpleStorage2 a = new SimpleStorage2();
simpleStorage2s.push(a);
LinkTokenInterface link = LinkTokenInterface(a.getChainlinkToken());
link.transfer(address(a), 100);
return address(a);
}
}
contract SimpleStorage2 is ChainlinkClient, Ownable {
function getChainlinkToken() public view returns (address) {
return chainlinkTokenAddress();
}
}
Solidity compiler 0.6.12. What am I doing wrong? How do I get this to work?
Get the address of the contract that you've deployed, and send it LINK.
You can read the Chainlink documentation for more information.
It looks like you're using remix.
Get the address of your contract.
Paste it into your Metamask
3. Send your contract LINK
You'll get a few notifications to confirm.