// File: @openzeppelin/contracts/security/ReentrancyGuard.sol
// OpenZeppelin Contracts (last updated v4.9.0) (security/ReentrancyGuard.sol)
pragma solidity ^0.8.0;
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor() {
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be _NOT_ENTERED
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
}
function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == _ENTERED;
}
}
// File: swappingGem.sol
pragma solidity ^0.8.0;
interface IERC20 {
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
function transfer(address recipient, uint256 amount) external returns (bool);
function balanceOf(address account) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
}
interface IERC20Burnable {
function burnFrom(address account, uint256 amount) external;
}
contract GemSwaps is ReentrancyGuard {
IERC20 public mGem;
IERC20 public Mandox;
uint256 public exchangeRate;
address public owner;
constructor(address _mGem, address _Mandox, uint256 _exchangeRate) {
mGem = IERC20(_mGem);
Mandox = IERC20(_Mandox);
exchangeRate = _exchangeRate;
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "Caller is not the owner");
_;
}
function setExchangeRate(uint256 _newRate) external onlyOwner {
exchangeRate = _newRate;
}
function exchangemGemForMandox(uint256 _amountOfmGem) external nonReentrant {
// Convert the amount from whole tokens to the smallest unit
uint256 mGemAmount = _amountOfmGem * 10**9;
require(mGem.allowance(msg.sender, address(this)) >= mGemAmount, "Contract needs allowance to burn mGEM");
require(Mandox.balanceOf(address(this)) >= mGemAmount * exchangeRate, "Insufficient Mandox balance in contract");
// Burn mGem
IERC20Burnable(address(mGem)).burnFrom(msg.sender, mGemAmount);
// Calculate the Mandox transfer amount
uint256 mandoxTransferAmount = mGemAmount * exchangeRate;
// Transfer Mandox to the user
require(Mandox.transfer(msg.sender, mandoxTransferAmount), "Transfer of Mandox failed");
}
// Function to withdraw ERC-20 tokens
function rescueTokens(address _tokenAddr) external onlyOwner {
IERC20 token = IERC20(_tokenAddr);
uint256 tokenBalance = token.balanceOf(address(this));
require(tokenBalance > 0, "No tokens to rescue");
token.transfer(owner, tokenBalance);
}
// Function to withdraw Ether
function rescueEther() external onlyOwner {
uint256 etherBalance = address(this).balance;
require(etherBalance > 0, "No Ether to rescue");
payable(owner).transfer(etherBalance);
}
// Fallback function
receive() external payable {}
}
{
"compilationTarget": {
"GemSwaps.sol": "GemSwaps"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 800
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_mGem","type":"address"},{"internalType":"address","name":"_Mandox","type":"address"},{"internalType":"uint256","name":"_exchangeRate","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"Mandox","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"exchangeRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amountOfmGem","type":"uint256"}],"name":"exchangemGemForMandox","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"mGem","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"rescueEther","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_tokenAddr","type":"address"}],"name":"rescueTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_newRate","type":"uint256"}],"name":"setExchangeRate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]