|
| 1 | +pragma solidity 0.8.12; |
| 2 | +// Copyright Ocean Protocol contributors |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import '../interfaces/IERC20.sol'; |
| 6 | +import '../utils/SafeERC20.sol'; |
| 7 | +import '@openzeppelin/contracts/security/ReentrancyGuard.sol'; |
| 8 | +import '@openzeppelin/contracts/token/ERC20/extensions/IERC20Permit.sol'; |
| 9 | +import '@openzeppelin/contracts/access/Ownable.sol'; |
| 10 | + |
| 11 | +/** |
| 12 | + * @title GrantsSwap |
| 13 | + * @dev Contract that allows swapping input tokens for COMPY tokens at a 1:1 ratio. |
| 14 | + * Users can swap the input token for COMPY (one-way swap only). |
| 15 | + * The swap maintains a 1:1 ratio in token units (accounting for decimals). |
| 16 | + */ |
| 17 | +contract GrantsSwap is ReentrancyGuard, Ownable { |
| 18 | + using SafeERC20 for IERC20; |
| 19 | + |
| 20 | + // The COMPY token address |
| 21 | + IERC20 public immutable compyToken; |
| 22 | + |
| 23 | + // The input token address (the token that can be swapped with COMPY) |
| 24 | + IERC20 public immutable inputToken; |
| 25 | + |
| 26 | + // Decimals for COMPY token (6) |
| 27 | + uint8 private immutable compyDecimals; |
| 28 | + |
| 29 | + // Decimals for input token |
| 30 | + uint8 private immutable inputDecimals; |
| 31 | + |
| 32 | + // Events |
| 33 | + event Swap( |
| 34 | + address indexed user, |
| 35 | + uint256 inputTokenAmount, |
| 36 | + uint256 compyAmount |
| 37 | + ); |
| 38 | + |
| 39 | + event Withdraw( |
| 40 | + address indexed token, |
| 41 | + address indexed to, |
| 42 | + uint256 amount |
| 43 | + ); |
| 44 | + |
| 45 | + /** |
| 46 | + * @dev Constructor for GrantsSwap |
| 47 | + * @param _compyToken Address of the COMPY token |
| 48 | + * @param _inputToken Address of the input token that can be swapped with COMPY |
| 49 | + */ |
| 50 | + constructor(address _compyToken, address _inputToken) { |
| 51 | + require(_compyToken != address(0), "GrantsSwap: COMPY token cannot be zero address"); |
| 52 | + require(_inputToken != address(0), "GrantsSwap: input token cannot be zero address"); |
| 53 | + require(_compyToken != _inputToken, "GrantsSwap: tokens must be different"); |
| 54 | + |
| 55 | + compyToken = IERC20(_compyToken); |
| 56 | + inputToken = IERC20(_inputToken); |
| 57 | + |
| 58 | + // Get decimals from tokens |
| 59 | + compyDecimals = IERC20(_compyToken).decimals(); |
| 60 | + inputDecimals = IERC20(_inputToken).decimals(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @dev Swap input tokens for COMPY tokens at 1:1 ratio (accounting for decimals) |
| 65 | + * @param amount Amount of input tokens to swap (in input token's smallest unit) |
| 66 | + */ |
| 67 | + function swapToCOMPY(uint256 amount) external nonReentrant { |
| 68 | + require(amount > 0, "GrantsSwap: amount must be greater than zero"); |
| 69 | + |
| 70 | + // Calculate equivalent amount in COMPY's smallest unit (1:1 ratio) |
| 71 | + uint256 compyAmount = convertAmount(amount, inputDecimals, compyDecimals); |
| 72 | + |
| 73 | + // Transfer input tokens from user to this contract |
| 74 | + inputToken.safeTransferFrom(msg.sender, address(this), amount); |
| 75 | + |
| 76 | + // Transfer COMPY from this contract to user (1:1 ratio) |
| 77 | + compyToken.safeTransfer(msg.sender, compyAmount); |
| 78 | + |
| 79 | + emit Swap(msg.sender, amount, compyAmount); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @dev Swap input tokens for COMPY tokens at 1:1 ratio using ERC20Permit (accounting for decimals) |
| 84 | + * This function allows users to swap without a separate approval transaction by using a permit signature. |
| 85 | + * @param amount Amount of input tokens to swap (in input token's smallest unit) |
| 86 | + * @param deadline The time at which the permit expires (unix timestamp) |
| 87 | + * @param v The recovery byte of the signature |
| 88 | + * @param r Half of the ECDSA signature pair |
| 89 | + * @param s Half of the ECDSA signature pair |
| 90 | + */ |
| 91 | + function swapToCOMPYwithPermit( |
| 92 | + uint256 amount, |
| 93 | + uint256 deadline, |
| 94 | + uint8 v, |
| 95 | + bytes32 r, |
| 96 | + bytes32 s |
| 97 | + ) external nonReentrant { |
| 98 | + require(amount > 0, "GrantsSwap: amount must be greater than zero"); |
| 99 | + |
| 100 | + // Use permit to approve this contract to spend user's input tokens |
| 101 | + IERC20Permit(address(inputToken)).permit( |
| 102 | + msg.sender, |
| 103 | + address(this), |
| 104 | + amount, |
| 105 | + deadline, |
| 106 | + v, |
| 107 | + r, |
| 108 | + s |
| 109 | + ); |
| 110 | + |
| 111 | + // Calculate equivalent amount in COMPY's smallest unit (1:1 ratio) |
| 112 | + uint256 compyAmount = convertAmount(amount, inputDecimals, compyDecimals); |
| 113 | + |
| 114 | + // Transfer input tokens from user to this contract |
| 115 | + inputToken.safeTransferFrom(msg.sender, address(this), amount); |
| 116 | + |
| 117 | + // Transfer COMPY from this contract to user (1:1 ratio) |
| 118 | + compyToken.safeTransfer(msg.sender, compyAmount); |
| 119 | + |
| 120 | + emit Swap(msg.sender, amount, compyAmount); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @dev Withdraw tokens from the contract (only owner) |
| 125 | + * @param token Address of the token to withdraw (address(0) for native ETH, but not supported in this contract) |
| 126 | + * @param to Address to send the tokens to |
| 127 | + * @param amount Amount of tokens to withdraw |
| 128 | + */ |
| 129 | + function withdrawTokens( |
| 130 | + address token, |
| 131 | + address to, |
| 132 | + uint256 amount |
| 133 | + ) external onlyOwner { |
| 134 | + require(to != address(0), "GrantsSwap: cannot withdraw to zero address"); |
| 135 | + require(amount > 0, "GrantsSwap: amount must be greater than zero"); |
| 136 | + require(token != address(0), "GrantsSwap: token address cannot be zero"); |
| 137 | + |
| 138 | + IERC20(token).safeTransfer(to, amount); |
| 139 | + |
| 140 | + emit Withdraw(token, to, amount); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @dev Convert amount from one token's decimals to another (for 1:1 token unit ratio) |
| 145 | + * @param amount Amount in source token's smallest unit |
| 146 | + * @param sourceDecimals Decimals of source token |
| 147 | + * @param targetDecimals Decimals of target token |
| 148 | + * @return Converted amount in target token's smallest unit |
| 149 | + */ |
| 150 | + function convertAmount(uint256 amount, uint8 sourceDecimals, uint8 targetDecimals) internal pure returns (uint256) { |
| 151 | + if (sourceDecimals == targetDecimals) { |
| 152 | + return amount; |
| 153 | + } else if (sourceDecimals < targetDecimals) { |
| 154 | + // Multiply by 10^(targetDecimals - sourceDecimals) |
| 155 | + return amount * (10 ** (targetDecimals - sourceDecimals)); |
| 156 | + } else { |
| 157 | + // Divide by 10^(sourceDecimals - targetDecimals) |
| 158 | + return amount / (10 ** (sourceDecimals - targetDecimals)); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @dev Get the balance of COMPY tokens held by this contract |
| 164 | + * @return uint256 Balance of COMPY tokens |
| 165 | + */ |
| 166 | + function getCOMPYBalance() external view returns (uint256) { |
| 167 | + return compyToken.balanceOf(address(this)); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @dev Get the balance of input tokens held by this contract |
| 172 | + * @return uint256 Balance of input tokens |
| 173 | + */ |
| 174 | + function getInputTokenBalance() external view returns (uint256) { |
| 175 | + return inputToken.balanceOf(address(this)); |
| 176 | + } |
| 177 | +} |
0 commit comments