From 54e6c5ac3944371a247a5199df82ce4daa3c1c2f Mon Sep 17 00:00:00 2001 From: 1948810 Date: Fri, 5 Jan 2024 15:33:27 +0100 Subject: [PATCH] add buy ether with Geth --- smart_contract/Geth.sol | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/smart_contract/Geth.sol b/smart_contract/Geth.sol index 7d47386..9b696a8 100644 --- a/smart_contract/Geth.sol +++ b/smart_contract/Geth.sol @@ -36,6 +36,17 @@ contract Geth is ERC20, Ownable { _mint(msg.sender, tokenAmount); } + /** + * @dev function to buy Ether with tokens and transfer them to the buyer. + */ + function purchaseWei(uint256 tokenAmount) external { + uint256 WeiAmount = calculateWeiAmount(tokenAmount); + + + // Trasferisci i token all'acquirente + payable(msg.sender).transfer(WeiAmount); + } + /** * @dev function to calculate the amount of tokens to be transferred to the buyer. * Returns the amount of tokens that can be purchased with the sent amount of Ether. @@ -44,6 +55,14 @@ contract Geth is ERC20, Ownable { return weiAmount / GETH_TO_WEI; } + /** + * @dev function to calculate the amount of wei to be transferred to the buyer. + * Returns the amount of wei that can be purchased with the sent amount of Geth. + */ + function calculateWeiAmount(uint256 tokenAmount) private pure returns (uint256) { + return GETH_TO_WEI * tokenAmount; + } + /** * @dev function to set the {operator} smart contract address. * Notice this function can be called only by the contract owner. @@ -66,4 +85,5 @@ contract Geth is ERC20, Ownable { } return super.allowance(owner, spender); } + } \ No newline at end of file