Skip to content

Commit

Permalink
add buy ether with Geth
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandroscifoni committed Jan 5, 2024
1 parent ebf07eb commit 54e6c5a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions smart_contract/Geth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -66,4 +85,5 @@ contract Geth is ERC20, Ownable {
}
return super.allowance(owner, spender);
}

}

0 comments on commit 54e6c5a

Please sign in to comment.