Skip to content

Instantly share code, notes, and snippets.

@dimitrihartt
Created June 15, 2024 01:47
Show Gist options
  • Save dimitrihartt/a1551625521987c008cbb469874ed139 to your computer and use it in GitHub Desktop.
Save dimitrihartt/a1551625521987c008cbb469874ed139 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.26+commit.8a97fa7a.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
// Compatible with OpenZeppelin Contracts ^5.0.0
pragma solidity ^0.8.24;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol";
/// @custom:security-contact dimitrileite@hotmail.com
contract MedShareToken is ERC20, ERC20Burnable, Ownable {
/**
* Network: Ethereum Mainnet
* Aggregator: XAG/USD
* Address: 0x379589227b15F1a12195D3f2d90bBc9F31f95235
* Agregator: ETH/USD
* Address: 0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419
*/
AggregatorV3Interface internal dataFeedXagUsd;
AggregatorV3Interface internal dataFeedEthUsd;
uint256 public xagUsdPrice;
uint256 public ethUsdPrice;
constructor(address initialOwner)
ERC20("MedShareToken", "MDST")
Ownable(initialOwner)
{
_mint(msg.sender, 1000000 * 10**decimals());
dataFeedXagUsd = AggregatorV3Interface(0x379589227b15F1a12195D3f2d90bBc9F31f95235);
dataFeedEthUsd = AggregatorV3Interface(0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419);
}
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
/**
* Sets XagUsd Price Manually
*/
function setXagUsdManually(uint256 price) public onlyOwner {
xagUsdPrice = price;
}
/**
* Sets EthUsd Price Manually
*/
function setEthUsdManually(uint256 price) public onlyOwner {
ethUsdPrice = price;
}
/**
* Returns the latest XagUsd price.
*/
function getXagUsdLatestPrice() public view returns (uint256) {
// prettier-ignore
(
/* uint80 roundID */,
int answer,
/*uint startedAt*/,
/*uint timeStamp*/,
/*uint80 answeredInRound*/
) = dataFeedXagUsd.latestRoundData();
return uint256(answer);
}
/**
* Returns the latest EthUsd price.
*/
function getEthUsdLatestPrice() public view returns (uint256) {
// prettier-ignore
(
/* uint80 roundID */,
int answer,
/*uint startedAt*/,
/*uint timeStamp*/,
/*uint80 answeredInRound*/
) = dataFeedEthUsd.latestRoundData();
return uint256(answer);
}
function investWithEther() external payable {
uint256 amount = msg.value;
//Exchange Ether to MDST
if (ethUsdPrice == 0) {
ethUsdPrice = getEthUsdLatestPrice();
}
uint256 amountInUsd = amount * ethUsdPrice;
_mint(msg.sender, amount);
//updateContractBalances(amount);
}
/*
Which function is called, fallback() or receive()?
send Ether
|
msg.data is empty?
/ \
yes no
/ \
receive() exists? fallback()
/ \
yes no
/ \
receive() fallback()
*/
// Function to receive Ether. msg.data must be empty
receive() external payable {}
// Fallback function is called when msg.data is not empty
fallback() external payable {}
function getContractsBalanceInEther() public view returns (uint256) {
return address(this).balance;
}
function sendViaTransfer(address payable _to) public payable {
// This function is no longer recommended for sending Ether.
_to.transfer(msg.value);
}
function sendViaSend(address payable _to) public payable {
// Send returns a boolean value indicating success or failure.
// This function is not recommended for sending Ether.
bool sent = _to.send(msg.value);
require(sent, "Failed to send Ether");
}
function sendViaCall(address payable _to) public payable {
// Call returns a boolean value indicating success or failure.
// This is the current recommended method to use.
(bool sent, bytes memory data) = _to.call{value: msg.value}("");
require(sent, "Failed to send Ether");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment