{ "language": "Solidity", "sources": { "src/contracts/token/UserRedemption.sol": { "content": "// SPDX-License-Identifier: MIT\npragma solidity 0.8.7;\n\nimport \"@openzeppelin/contracts/access/Ownable.sol\";\nimport \"../factory/FactoryInterface.sol\";\nimport \"@openzeppelin/contracts/token/ERC20/IERC20.sol\";\n\ncontract UserRedemption is Ownable {\n event UserBurn(address indexed who, uint256 indexed amount, uint256 indexed nonce);\n\n struct Req {\n uint256 amount;\n address requester;\n string ipfsHash;\n }\n\n FactoryInterface public immutable factory;\n IERC20 public immutable token;\n\n address public signer;\n address public feeReceiver;\n\n uint256 public feeBPS;\n uint256 public feeFlat;\n uint256 public feesCollected;\n\n mapping(address => uint256) public user_req_nonce;\n\n Req[] public reqs;\n\n string public constant version = \"0\";\n bytes32 public constant DOMAIN_TYPEHASH = keccak256(\"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract,bytes32 salt)\");\n bytes32 public constant WHITELIST_TYPEHASH = keccak256(\"Whitelist(address addr,uint256 amount,uint256 nonce)\");\n\n modifier only(address who) {\n require(msg.sender == who, \"incorrect permissions\");\n _;\n }\n\n constructor(\n address _signer,\n address _factory,\n address _token,\n address _feeReceiver,\n uint256 _feeBPS,\n uint256 _feeFlat,\n address _owner\n ) {\n signer = _signer;\n factory = FactoryInterface(_factory);\n token = IERC20(_token);\n feeReceiver = _feeReceiver;\n feeBPS = _feeBPS;\n feeFlat = _feeFlat;\n _transferOwnership(_owner);\n }\n\n function burn(\n uint256 amount,\n string calldata _ipfsHash,\n bytes calldata signature\n ) external {\n uint256 fee = fee(amount);\n\n require(amount >= fee, \"fee less than minimum\");\n\n feesCollected += fee;\n\n bytes32 _hash = keccak256(\n abi.encodePacked(\n \"\\x19\\x01\",\n DOMAIN_SEPARATOR(),\n keccak256(\n abi.encode(\n WHITELIST_TYPEHASH, \n msg.sender, \n amount, \n user_req_nonce[msg.sender]++\n ))\n )\n );\n\n require(signer == recoverSigner(_hash, signature), \"invalid signer\");\n\n reqs.push(Req({amount: amount - fee, requester: msg.sender, ipfsHash: _ipfsHash}));\n\n token.transferFrom(msg.sender, address(this), amount);\n\n emit UserBurn(msg.sender, amount, reqs.length - 1);\n }\n\n function batchBurn(uint256 amount) external onlyOwner {\n require(amount <= token.balanceOf(address(this)) - feesCollected, \"invalid amount\");\n token.approve(address(factory), amount);\n factory.burn(amount, \"\");\n }\n\n function takeFees() external only(feeReceiver) {\n token.transfer(feeReceiver, feesCollected);\n feesCollected = 0;\n }\n\n function changeSigner(address _signer) external onlyOwner {\n signer = _signer;\n }\n\n function changeFeeReciever(address _receiver) external onlyOwner {\n feeReceiver = _receiver;\n }\n\n function changeFeeBPS(uint256 _fee) external onlyOwner {\n feeBPS = _fee;\n }\n\n function changeFeeFlat(uint256 _fee) external onlyOwner {\n feeFlat = _fee;\n }\n\n function setMerchantDepositAddress(string memory addr) external onlyOwner {\n factory.setMerchantDepositAddress(addr);\n }\n\n function removeFunds(uint256 amount) external onlyOwner {\n require(amount <= token.balanceOf(address(this)) - feesCollected, \"invalid amount\");\n token.transfer(msg.sender, amount);\n }\n\n //////////////////////// VIEW ////////////////////////\n\n function DOMAIN_SEPARATOR() public view returns (bytes32) {\n return\n keccak256(\n abi.encode(\n DOMAIN_TYPEHASH,\n keccak256(\"User Redemption Contract\"),\n keccak256(bytes(version)),\n block.chainid,\n address(this),\n 0x146eb79745af938dd35c008f08e6a37823a1278b392df477f9849e461956c27a\n )\n );\n }\n\n // can never be less than feeFlat\n function fee(uint256 amount) public view returns (uint256) {\n return (amount * feeBPS / 10000) + feeFlat;\n }\n\n //////////////////////// INTERNAL ////////////////////////\n function recoverSigner(bytes32 messageHash, bytes memory _signature) internal pure returns (address) {\n bytes32 r;\n bytes32 s;\n uint8 v;\n assembly {\n // first 32 bytes, after the length prefix\n r := mload(add(_signature, 32))\n // second 32 bytes\n s := mload(add(_signature, 64))\n // final byte (first byte of the next 32 bytes)\n v := byte(0, mload(add(_signature, 96)))\n }\n\n return ecrecover(messageHash, v, r, s);\n }\n}\n" }, "lib/openzeppelin-contracts/contracts/access/Ownable.sol": { "content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)\n\npragma solidity ^0.8.0;\n\nimport \"../utils/Context.sol\";\n\n/**\n * @dev Contract module which provides a basic access control mechanism, where\n * there is an account (an owner) that can be granted exclusive access to\n * specific functions.\n *\n * By default, the owner account will be the one that deploys the contract. This\n * can later be changed with {transferOwnership}.\n *\n * This module is used through inheritance. It will make available the modifier\n * `onlyOwner`, which can be applied to your functions to restrict their use to\n * the owner.\n */\nabstract contract Ownable is Context {\n address private _owner;\n\n event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);\n\n /**\n * @dev Initializes the contract setting the deployer as the initial owner.\n */\n constructor() {\n _transferOwnership(_msgSender());\n }\n\n /**\n * @dev Throws if called by any account other than the owner.\n */\n modifier onlyOwner() {\n _checkOwner();\n _;\n }\n\n /**\n * @dev Returns the address of the current owner.\n */\n function owner() public view virtual returns (address) {\n return _owner;\n }\n\n /**\n * @dev Throws if the sender is not the owner.\n */\n function _checkOwner() internal view virtual {\n require(owner() == _msgSender(), \"Ownable: caller is not the owner\");\n }\n\n /**\n * @dev Leaves the contract without owner. It will not be possible to call\n * `onlyOwner` functions anymore. Can only be called by the current owner.\n *\n * NOTE: Renouncing ownership will leave the contract without an owner,\n * thereby removing any functionality that is only available to the owner.\n */\n function renounceOwnership() public virtual onlyOwner {\n _transferOwnership(address(0));\n }\n\n /**\n * @dev Transfers ownership of the contract to a new account (`newOwner`).\n * Can only be called by the current owner.\n */\n function transferOwnership(address newOwner) public virtual onlyOwner {\n require(newOwner != address(0), \"Ownable: new owner is the zero address\");\n _transferOwnership(newOwner);\n }\n\n /**\n * @dev Transfers ownership of the contract to a new account (`newOwner`).\n * Internal function without access restriction.\n */\n function _transferOwnership(address newOwner) internal virtual {\n address oldOwner = _owner;\n _owner = newOwner;\n emit OwnershipTransferred(oldOwner, newOwner);\n }\n}\n" }, "src/contracts/factory/FactoryInterface.sol": { "content": "// SPDX-License-Identifier: MIT\npragma solidity 0.8.7;\n\ninterface FactoryInterface {\n event IssuerDepositAddressSet(address indexed merchant, address indexed sender, string depositAddress);\n\n event MerchantDepositAddressSet(address indexed merchant, string depositAddress);\n\n event MintRequestAdd(\n uint256 indexed nonce,\n address indexed requester,\n uint256 amount,\n string depositAddress,\n string txid,\n uint256 timestamp,\n bytes32 requestHash\n );\n\n event MintRequestCancel(uint256 indexed nonce, address indexed requester, bytes32 requestHash);\n\n event MintConfirmed(\n uint256 indexed nonce,\n address indexed requester,\n uint256 amount,\n string depositAddress,\n string txid,\n uint256 timestamp,\n bytes32 requestHash\n );\n\n event MintRejected(\n uint256 indexed nonce,\n address indexed requester,\n uint256 amount,\n string depositAddress,\n string txid,\n uint256 timestamp,\n bytes32 requestHash\n );\n\n event Burned(\n uint256 indexed nonce,\n address indexed requester,\n uint256 amount,\n string depositAddress,\n uint256 timestamp,\n bytes32 requestHash\n );\n\n event BurnConfirmed(\n uint256 indexed nonce,\n address indexed requester,\n uint256 amount,\n string depositAddress,\n string txid,\n uint256 timestamp,\n bytes32 inputRequestHash\n );\n\n ///=============================================================================================\n /// Data Structres\n ///=============================================================================================\n\n enum RequestStatus {\n NULL,\n PENDING,\n CANCELED,\n APPROVED,\n REJECTED\n }\n\n struct Request {\n address requester; // sender of the request.\n uint256 amount; // amount of token to mint/burn.\n string depositAddress; // issuer's asset address in mint, merchant's asset address in burn.\n string txid; // asset txid for sending/redeeming asset in the mint/burn process.\n uint256 nonce; // serial number allocated for each request.\n uint256 timestamp; // time of the request creation.\n RequestStatus status; // status of the request.\n }\n\n function pause() external;\n\n function unpause() external;\n\n function setIssuerDepositAddress(address merchant, string memory depositAddress) external returns (bool);\n\n function setMerchantDepositAddress(string memory depositAddress) external returns (bool);\n\n function setMerchantMintLimit(address merchant, uint256 amount) external returns (bool);\n\n function setMerchantBurnLimit(address merchant, uint256 amount) external returns (bool);\n\n function addMintRequest(\n uint256 amount,\n string memory txid,\n string memory depositAddress\n ) external returns (uint256);\n\n function cancelMintRequest(bytes32 requestHash) external returns (bool);\n\n function confirmMintRequest(bytes32 requestHash) external returns (bool);\n\n function rejectMintRequest(bytes32 requestHash) external returns (bool);\n\n function burn(uint256 amount, string memory txid) external returns (bool);\n\n function confirmBurnRequest(bytes32 requestHash) external returns (bool);\n\n function getMintRequestsLength() external view returns (uint256 length);\n\n function getBurnRequestsLength() external view returns (uint256 length);\n\n function getBurnRequest(uint256 nonce)\n external\n view\n returns (\n uint256 requestNonce,\n address requester,\n uint256 amount,\n string memory depositAddress,\n string memory txid,\n uint256 timestamp,\n string memory status,\n bytes32 requestHash\n );\n\n function getMintRequest(uint256 nonce)\n external\n view\n returns (\n uint256 requestNonce,\n address requester,\n uint256 amount,\n string memory depositAddress,\n string memory txid,\n uint256 timestamp,\n string memory status,\n bytes32 requestHash\n );\n}\n" }, "lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol": { "content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)\n\npragma solidity ^0.8.0;\n\n/**\n * @dev Interface of the ERC20 standard as defined in the EIP.\n */\ninterface IERC20 {\n /**\n * @dev Emitted when `value` tokens are moved from one account (`from`) to\n * another (`to`).\n *\n * Note that `value` may be zero.\n */\n event Transfer(address indexed from, address indexed to, uint256 value);\n\n /**\n * @dev Emitted when the allowance of a `spender` for an `owner` is set by\n * a call to {approve}. `value` is the new allowance.\n */\n event Approval(address indexed owner, address indexed spender, uint256 value);\n\n /**\n * @dev Returns the amount of tokens in existence.\n */\n function totalSupply() external view returns (uint256);\n\n /**\n * @dev Returns the amount of tokens owned by `account`.\n */\n function balanceOf(address account) external view returns (uint256);\n\n /**\n * @dev Moves `amount` tokens from the caller's account to `to`.\n *\n * Returns a boolean value indicating whether the operation succeeded.\n *\n * Emits a {Transfer} event.\n */\n function transfer(address to, uint256 amount) external returns (bool);\n\n /**\n * @dev Returns the remaining number of tokens that `spender` will be\n * allowed to spend on behalf of `owner` through {transferFrom}. This is\n * zero by default.\n *\n * This value changes when {approve} or {transferFrom} are called.\n */\n function allowance(address owner, address spender) external view returns (uint256);\n\n /**\n * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.\n *\n * Returns a boolean value indicating whether the operation succeeded.\n *\n * IMPORTANT: Beware that changing an allowance with this method brings the risk\n * that someone may use both the old and the new allowance by unfortunate\n * transaction ordering. One possible solution to mitigate this race\n * condition is to first reduce the spender's allowance to 0 and set the\n * desired value afterwards:\n * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729\n *\n * Emits an {Approval} event.\n */\n function approve(address spender, uint256 amount) external returns (bool);\n\n /**\n * @dev Moves `amount` tokens from `from` to `to` using the\n * allowance mechanism. `amount` is then deducted from the caller's\n * allowance.\n *\n * Returns a boolean value indicating whether the operation succeeded.\n *\n * Emits a {Transfer} event.\n */\n function transferFrom(\n address from,\n address to,\n uint256 amount\n ) external returns (bool);\n}\n" }, "lib/openzeppelin-contracts/contracts/utils/Context.sol": { "content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)\n\npragma solidity ^0.8.0;\n\n/**\n * @dev Provides information about the current execution context, including the\n * sender of the transaction and its data. While these are generally available\n * via msg.sender and msg.data, they should not be accessed in such a direct\n * manner, since when dealing with meta-transactions the account sending and\n * paying for execution may not be the actual sender (as far as an application\n * is concerned).\n *\n * This contract is only required for intermediate, library-like contracts.\n */\nabstract contract Context {\n function _msgSender() internal view virtual returns (address) {\n return msg.sender;\n }\n\n function _msgData() internal view virtual returns (bytes calldata) {\n return msg.data;\n }\n}\n" } }, "settings": { "remappings": [ "@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/", "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/", "ds-test/=lib/forge-std/lib/ds-test/src/", "forge-std/=lib/forge-std/src/", "openzeppelin-contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/", "openzeppelin-contracts/=lib/openzeppelin-contracts/" ], "optimizer": { "enabled": true, "runs": 200 }, "metadata": { "bytecodeHash": "ipfs" }, "outputSelection": { "*": { "*": [ "evm.bytecode", "evm.deployedBytecode", "devdoc", "userdoc", "metadata", "abi" ] } }, "evmVersion": "london", "libraries": {} } }