Contract Name:
BV2toBV3Swap
Contract Source Code:
<i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../Utility/owner.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
interface IERC20 {
function transferFrom(address from, address to, uint256 amount) external returns (bool);
function approve(address spender, uint256 amount) external returns (bool);
function transfer(address to, uint256 amount) external returns (bool);
function decimals() external view returns (uint8);
}
contract BV2toBV3Swap is Owner, ReentrancyGuard {
IERC20 public bv2Token;
IERC20 public bv3Token;
uint256 public totalBV2Swapped;
uint256 public bv3Counter;
function setBV2TokenAddress(address _bv2TokenAddress) external onlyOwner {
bv2Token = IERC20(_bv2TokenAddress);
}
function setBV3TokenAddress(address _bv3TokenAddress) external onlyOwner {
bv3Token = IERC20(_bv3TokenAddress);
}
function approveBV2() external {
uint256 maxUint = type(uint256).max;
bv2Token.approve(address(this), maxUint);
}
// Users must approve this contract to spend their BV2 tokens before calling this function
function swapBV2ForBV3(uint256 _bv2Amount) external nonReentrant {
require(bv2Token.decimals() == 10, "BV2 token does not have 10 decimals");
require(bv3Token.decimals() == 18, "BV3 token does not have 18 decimals");
uint256 bv3Amount = (_bv2Amount * (10 ** 8)) * 2; // Convert BV2 amount to BV3 amount
require(bv2Token.transferFrom(msg.sender, address(this), _bv2Amount), "Failed to transfer BV2 tokens");
require(bv3Token.transfer(msg.sender, bv3Amount), "Failed to transfer BV3 tokens");
totalBV2Swapped += _bv2Amount;
}
// View function to see the total BV2 tokens swapped
function getTotalBV2Swapped() external view returns (uint256) {
return totalBV2Swapped;
}
// Function to allow the owner to withdraw BV3 tokens using transferFrom
function withdrawBV3(uint256 _amount) external onlyOwner {
require(bv3Token.transferFrom(address(this), msg.sender, _amount), "Failed to withdraw BV3 tokens");
bv3Counter -= _amount;
}
// Function for the owner to send BV3 tokens using transferFrom and adjust the BV3 counter
function sendBV3(address _from, uint256 _amount) external onlyOwner {
require(bv3Token.transferFrom(_from, address(this), _amount), "Failed to send BV3 tokens");
bv3Counter += _amount;
}
function getBV3Counter() external view returns (uint256) {
return bv3Counter;
}
// Other functions and code...
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// 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;
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Owner {
address public _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor() {
_owner = msg.sender;
emit OwnershipTransferred(address(0), _owner);
}
modifier onlyOwner() {
require(isOwner(), "You are not the owner");
_;
}
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0), "New owner is the zero address");
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
function isOwner() public view returns (bool) {
return msg.sender == _owner;
}
function getOwner() public view returns (address) {
return _owner;
}
}