-
In lesson 3, we learned how to use solidity to send money from the wallet to a smart contract and then send it back from the smart contract to the wallet. I want to know if we can write a smart contract that can send eth from one wallet to another wallet. My attempt is that I can send money from one wallet to the smart contract and then send the eth in the smart contract to another wallet. Given the public key of two wallets, I programmed the following code but the transaction failed. Could anyone help me figure it out? The environment is in remix and Injected Web 3. pragma solidity >=0.8.0;
// Get the latest ETH/USD price from chainlink price feed
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract myContract {
address payable[] recipients;
mapping(address => uint256) public balanceAccount;
address public owner;
constructor() {
owner = msg.sender;
}
function send_ETH(address payable recipient, uint256 amount) public {
//set the minimum amount of dollar to transfer
uint256 minimumUSD = 0.01 * 10 ** 18;
amount = minimumUSD;
require(getConversionRate(amount) >= minimumUSD, "You need to spend more ETH!");
this.invest(amount);
this.fund(recipient);
}
function invest(uint256 amount) payable external{
//transfer amount ETH from metadata wallet to smart contract
recordTransaction(address(this), amount, false);
recordTransaction(owner, amount, true);
address payable contractAddress = payable(address(this));
contractAddress.send(amount);
}
function fund(address payable recipient) external {
//transfer amount ETH from this smart contract to the recipient
recordTransaction(address(this), address(this).balance, true);
recordTransaction(recipient, address(this).balance, false);
recipient.send(address(this).balance);
}
function recordTransaction(address recipient, uint256 deposit, bool out) private {
if (out) {
balanceAccount[recipient] -= deposit;
} else {
balanceAccount[recipient] += deposit;
}
}
function getVersion() public view returns (uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
return priceFeed.version();
}
function getPrice() public view returns(uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
(,int256 answer,,,) = priceFeed.latestRoundData();
// ETH/USD rate in 18 digit
return uint256(answer * 10000000000);
}
function getConversionRate(uint256 ethAmount) public view returns (uint256){
uint256 ethPrice = getPrice();
uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000;
// the actual ETH/USD conversation rate, after adjusting the extra 0s.
return ethAmountInUsd;
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello @hck007 this is a tricky question as is not actually desirable to do it like that, as you will have to pay gas fees twice, but if you are doing it as a learning exercise is nice. A correct approach for something like that is a little bit more complex, but here you have a detailed guide: Remember this is for ant ERC20 token, including ETH |
Beta Was this translation helpful? Give feedback.
Hello @hck007 this is a tricky question as is not actually desirable to do it like that, as you will have to pay gas fees twice, but if you are doing it as a learning exercise is nice. A correct approach for something like that is a little bit more complex, but here you have a detailed guide:
https://medium.com/coinmonks/build-a-smart-contract-that-transfers-erc20-token-from-your-wallet-to-other-addresses-or-erc20-ee8dc35f40f6
Remember this is for ant ERC20 token, including ETH