|
"content": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\nimport \"@openzeppelin/contracts/token/ERC20/IERC20.sol\";\nimport \"@openzeppelin/contracts/access/Ownable.sol\";\nimport \"@openzeppelin/contracts/utils/math/SafeMath.sol\";\nimport \"./IEscrow.sol\";\nimport \"./IModerator.sol\";\nimport \"./IProduct.sol\";\n\ncontract Escrow is IEscrow, Ownable {\n using SafeMath for uint256;\n //moderator contract\n address private moderatorAddress;\n\n IModerator moderatorContract;\n\n // product contract\n address private productAddress;\n IProduct productContract;\n\n\n // total app num\n uint256 private maxAppNum;\n\n // app owner\n // appId => address\n mapping(uint256 => address) private appOwner;\n\n //how many seconds after order paid, can buyer make dispute\n // appId => interval\n mapping(uint256 => uint256) private appIntervalDispute;\n\n //how many seconds after order paid, can seller claim order\n // appId => interval\n mapping(uint256 => uint256) private appIntervalClaim;\n\n //how many seconds after dispute made, if seller does not response, buyer can claim the refund\n // appId => interval\n mapping(uint256 => uint256) private appIntervalRefuse;\n\n // app uri\n // appId => string\n mapping(uint256 => string) private appURI;\n\n // app name\n // appId => string\n mapping(uint256 => string) private appName;\n\n // app mod commission (For each mod and app owner if possible)\n mapping(uint256 => uint8) private appModCommission;\n\n // app owner commission\n mapping(uint256 => uint8) private appOwnerCommission;\n\n // modA resolution for order.\n // orderId => modA resolution : 0 not resolved, 1 agree refund, 2 disagree refund.\n mapping(uint256 => uint8) private orderModAResolution;\n\n // modB resolution for order.\n // orderId => modB resolution : 0 not resolved, 1 agree refund, 2 disagree refund.\n mapping(uint256 => uint8) private orderModBResolution;\n\n // total order num\n uint256 public maxOrderId;\n\n //Struct Order\n struct Order {\n uint256 appId; //app id\n uint256 amount; //order amount\n address coinAddress; //coin contract address\n address buyer; //buyer address\n uint256 productId; // product id\n address seller; //seller address\n uint256 createdTime; //order created timestamp\n uint256 claimTime; //timestamp after when seller can claim if there is no dispute\n uint8 status; //order status, 1 paid, 2 buyer ask refund, 3 completed, 4 seller refuse dispute, 5 buyer or seller escalate, so voters can vote\n \n }\n\n // orderId => Order\n mapping(uint256 => Order) public orderBook;\n //Struct Dispute\n struct Dispute {\n uint256 refund; // refund amount\n uint256 modAId; //the mod that chosen by buyer\n uint256 modBId; // the mod that chosen by seller\n uint256 refuseExpired; // after it, if seller does not refuse refund, buyer can claim the refund\n }\n\n // orderId => Dispute\n mapping(uint256 => Dispute) public disputeBook;\n\n // user balance (userAddress => mapping(coinAddress => balance))\n mapping(address => mapping(address => uint256)) private userBalance;\n\n //Withdraw event\n event Withdraw(\n address indexed user, //user wallet address\n uint256 indexed amount, //withdraw amount\n address indexed coinContract //withdraw coin contract\n );\n\n //Create new APP event\n event NewApp(uint256 indexed appId); //appId\n\n //Create order event\n event PayOrder(\n uint256 indexed orderId,\n uint256 indexed appOrderId,\n address indexed coinAddress,\n uint256 amount,\n uint256 productId,\n address buyer,\n address seller,\n uint256 appId\n );\n\n //Confirm Done event\n event ConfirmDone(uint256 indexed appId, uint256 indexed orderId);\n\n //Ask refund event\n event AskRefund(\n uint256 indexed appId,\n uint256 indexed orderId,\n uint256 indexed refund,\n uint256 modAId\n );\n\n //Cancel refund event\n event CancelRefund(uint256 indexed appId, uint256 indexed orderId);\n\n //Refuse refund event\n event RefuseRefund(\n uint256 indexed appId, \n uint256 indexed orderId,\n uint256 indexed modBId\n );\n\n //Escalate dispute event\n event Escalate(uint256 indexed appId, uint256 indexed orderId);\n\n //Resolve to Agree or Disagree refund\n event Resolve(\n address indexed user,\n bool indexed isAgree,\n uint256 indexed orderId,\n uint256 appId,\n uint8 modType // 0 both modA&modB, 1 modA, 2 modB, 3 app Owner \n );\n\n //Resolved now event\n event ResolvedFinally(\n uint256 indexed appId,\n uint256 indexed orderId,\n uint8 indexed refundType //0 disagree win, 1 agree win, 2 seller refund\n );\n\n //Cash out event\n event Claim(\n address indexed user,\n uint256 indexed appId,\n uint256 indexed orderId\n );\n\n //User Balance Changed event\n event UserBalanceChanged(\n address indexed user,\n bool indexed isIn,\n uint256 indexed amount,\n address coinAddress,\n uint256 appId,\n uint256 orderId\n );\n\n constructor(address _modAddress, address _prodAddress) payable {\n moderatorAddress = _modAddress;\n moderatorContract = IModerator(_modAddress);\n\n productAddress = _prodAddress;\n productContract = IProduct(_prodAddress);\n\n }\n\n // Function to deposit Ether into this contract.\n // Call this function along with some Ether.\n // The balance of this contract will be automatically updated.\n receive() external payable {}\n\n // get Moderator contract address\n function getModAddress() external view override returns (address)\n {\n return moderatorAddress;\n }\n\n // get Product contract address\n function getProductAddress() external view override returns (address)\n {\n return productAddress;\n }\n\n // get app owner address\n function getAppOwner(uint256 appId) public view returns (address) {\n return appOwner[appId];\n }\n\n // get app Interval Dispute timestamp\n function getAppIntervalDispute(uint256 appId) public view returns(uint256) {\n return appIntervalDispute[appId];\n }\n\n // get app Interval Claim timestamp\n function getAppIntervalClaim(uint256 appId) public view returns(uint256) {\n return appIntervalClaim[appId];\n }\n\n // get app Interval Refuse timestamp\n function getAppIntervalRefuse(uint256 appId) public view returns(uint256) {\n return appIntervalRefuse[appId];\n }\n\n // get app URI\n function getAppURI(uint256 appId) public view returns(string memory) {\n return appURI[appId];\n }\n\n // get app Name\n function getAppName(uint256 appId) public view returns(string memory) {\n return appName[appId];\n }\n\n // get app Mod Commission\n function getAppModCommission(uint256 appId) public view returns(uint8) {\n return appModCommission[appId];\n }\n\n // get app Owner Commission\n function getAppOwnerCommission(uint256 appId) public view returns(uint8) {\n return appOwnerCommission[appId];\n }\n\n // get order ModA Resolution\n function getModAResolution(uint256 orderId) public view returns(uint8) {\n return orderModAResolution[orderId];\n }\n\n // get order ModB Resolution\n function getModBResolution(uint256 orderId) public view returns(uint8) {\n return orderModBResolution[orderId];\n }\n\n // get user balance\n function getUserBalance(address userAddress, address coinAddress) public view returns(uint256) {\n return userBalance[userAddress][coinAddress];\n }\n\n // get order detail\n function getOrderDetail(uint256 orderId) public view returns(Order memory) {\n return orderBook[orderId];\n }\n\n // get dispute detail\n function getDisputeDetail(uint256 orderId) public view returns(Dispute memory) {\n return disputeBook[orderId];\n }\n\n //Create new APP\n function newApp(\n address _appOwner,\n string memory _appName,\n string memory websiteURI\n ) public onlyOwner returns (uint256) {\n uint256 appId = maxAppNum.add(1);\n appOwner[appId] = _appOwner;\n appURI[appId] = websiteURI;\n appName[appId] = _appName;\n appIntervalDispute[appId] = uint256(3000000);// buyer can only dispute in 1 month after order paid.\n appIntervalClaim[appId] = uint256(3000000); // seller can claim if buyer does not dispute over 1 month after order paid.\n appIntervalRefuse[appId] = uint256(259200); // buyer can claim if seller does not refuse dispute over 3 days after dispute .\n appModCommission[appId] = uint8(1);\n appOwnerCommission[appId] = uint8(0);\n maxAppNum = appId;\n emit NewApp(appId);\n\n return appId;\n }\n\n //Transfer app owner to a new address\n function setAppOwner(uint256 appId, address _newOwner)\n public\n returns (bool)\n {\n // Only app owner\n require(\n _msgSender() == appOwner[appId],\n \"Escrow: only app owner can set app owner\"\n );\n require(_newOwner != address(0), \"Escrow: new owner is the zero address\");\n appOwner[appId] = _newOwner;\n\n return true;\n }\n\n //Set mod commission\n //Only app owner\n function setModCommission(uint256 appId, uint8 _commission)\n public\n returns (bool)\n {\n // Only app owner\n require(\n _msgSender() == appOwner[appId],\n \"Escrow: only app owner can set mod commission\"\n );\n require(_commission < 15, \"Escrow: commission must be less than 15\");\n appModCommission[appId] = _commission;\n return true;\n }\n\n //Set app owner commission\n function setAppOwnerCommission(uint256 appId, uint8 _commission)\n public\n returns (bool)\n {\n // Only app owner\n require(\n _msgSender() == appOwner[appId],\n \"Escrow: only app owner can set app owner commission\"\n );\n require(_commission < 45, \"Escrow: commission must be less than 45\");\n appOwnerCommission[appId] = _commission;\n return true;\n }\n\n //Set dispute interval\n function setIntervalDispute(uint256 appId, uint256 _seconds)\n public\n returns (bool)\n {\n // Only app owner\n require(\n _msgSender() == appOwner[appId],\n \"Escrow: only app owner can set dispute interval\"\n );\n require(_seconds > 10, \"Escrow: interval time too small!\");\n require(_seconds < 10000000, \"Escrow: interval time too big!\");\n appIntervalDispute[appId] = _seconds;\n return true;\n }\n\n //Set refuse interval\n function setIntervalRefuse(uint256 appId, uint256 _seconds)\n public\n returns (bool)\n {\n // Only app owner\n require(\n _msgSender() == appOwner[appId],\n \"Escrow: only app owner can set refuse interval\"\n );\n require(_seconds > 10, \"Escrow: interval time too small!\");\n require(_seconds < 10000000, \"Escrow: interval time too big!\");\n appIntervalRefuse[appId] = _seconds;\n return true;\n }\n\n //Set claim interval\n function setIntervalClaim(uint256 appId, uint256 _seconds)\n public\n returns (bool)\n {\n // Only app owner\n require(\n _msgSender() == appOwner[appId],\n \"Escrow: only app owner can set claim interval\"\n );\n require(_seconds > 20, \"Escrow: interval time too small!\");\n require(_seconds < 10000000, \"Escrow: interval time too big!\");\n appIntervalClaim[appId] = _seconds;\n return true;\n }\n\n function getMaxModId() public view returns (uint256) {\n return moderatorContract.getMaxModId();\n }\n\n function getModOwner(uint256 modId) public view returns (address) {\n return moderatorContract.getModOwner(modId);\n }\n\n //Pay Order\n function payOrder(\n uint256 appId,\n uint256 amount,\n address coinAddress,\n uint256 productId,\n uint256 appOrderId\n ) public payable returns (uint256) {\n require(\n appId > 0 &&\n appId <= maxAppNum &&\n appOrderId > 0 &&\n amount > 0,\n \"Escrow: all the ids should be bigger than 0\"\n );\n // Product should not be blocked\n require(!productContract.isProductBlocked(productId),\"product should not be blocked\");\n \n //Native Currency\n if (coinAddress == address(0)) {\n require(msg.value == amount, \"Escrow: Wrong amount or wrong value sent\");\n //send native currency to this contract\n payable(this).transfer(amount);\n } else {\n IERC20 buyCoinContract = IERC20(coinAddress);\n //send ERC20 to this contract\n buyCoinContract.transferFrom(_msgSender(), address(this), amount);\n }\n maxOrderId = maxOrderId.add(1);\n // store order information\n Order memory _order;\n _order.appId = appId;\n _order.coinAddress = coinAddress;\n _order.amount = amount;\n _order.buyer = _msgSender();\n _order.seller = productContract.getProdOwner(productId);\n _order.productId = productId;\n _order.createdTime = block.timestamp;\n _order.claimTime = block.timestamp.add(appIntervalClaim[appId]);\n _order.status = uint8(1);\n \n orderBook[maxOrderId] = _order;\n\n // emit event\n emit PayOrder(\n maxOrderId,\n appOrderId,\n coinAddress,\n amount,\n productId,\n _msgSender(),\n productContract.getProdOwner(productId),\n appId\n );\n\n return maxOrderId;\n }\n\n //confirm order received, and money will be sent to seller's balance\n //triggled by buyer\n function confirmDone(uint256 orderId) public {\n require(\n _msgSender() == orderBook[orderId].buyer,\n \"Escrow: only buyer can confirm done\"\n );\n\n require(\n orderBook[orderId].status == uint8(1) ||\n orderBook[orderId].status == uint8(2) ||\n orderBook[orderId].status == uint8(4),\n \"Escrow: order status must be equal to just paid or refund asked or dispute refused\"\n );\n\n // send money to seller's balance\n userBalance[orderBook[orderId].seller][\n orderBook[orderId].coinAddress\n ] = userBalance[orderBook[orderId].seller][\n orderBook[orderId].coinAddress\n ].add(orderBook[orderId].amount);\n emit UserBalanceChanged(\n orderBook[orderId].seller,\n true,\n orderBook[orderId].amount,\n orderBook[orderId].coinAddress,\n orderBook[orderId].appId,\n orderId\n );\n\n // set order status to completed\n orderBook[orderId].status = uint8(3);\n\n // update product sold\n productContract.updateProdScore(orderBook[orderId].productId, true);\n\n //emit event\n emit ConfirmDone(orderBook[orderId].appId, orderId);\n }\n\n //ask refund\n //triggled by buyer\n function askRefund(uint256 orderId, uint256 refund, uint256 modAId) public {\n require(\n _msgSender() == orderBook[orderId].buyer,\n \"Escrow: only buyer can make dispute\"\n );\n\n require(\n orderBook[orderId].status == uint8(1) ||\n orderBook[orderId].status == uint8(2),\n \"Escrow: order status must be equal to just paid or refund asked\"\n );\n\n require(\n block.timestamp < orderBook[orderId].createdTime.add(appIntervalDispute[orderBook[orderId].appId]),\n \"Escrow: it is too late to make dispute\"\n );\n\n require(refund > 0 && refund <= orderBook[orderId].amount, \n \"Escrow: refund amount must be bigger than 0 and not bigger than paid amount\");\n\n //Mod Id should be validated\n require(modAId <= moderatorContract.getMaxModId(), \"Escrow: mod id is too big\");\n\n // update order status \n if (orderBook[orderId].status == uint8(1)) {\n orderBook[orderId].status = uint8(2);\n }\n // update dispute modAId\n disputeBook[orderId].modAId = modAId;\n // update refund of dispute\n disputeBook[orderId].refund = refund;\n \n // update refuse expired\n disputeBook[orderId].refuseExpired = block.timestamp.add(appIntervalRefuse[orderBook[orderId].appId]);\n //emit event\n emit AskRefund(orderBook[orderId].appId, orderId, refund, modAId);\n }\n\n //cancel refund\n //triggled by buyer\n function cancelRefund(uint256 orderId) public {\n require(\n _msgSender() == orderBook[orderId].buyer,\n \"Escrow: only buyer can cancel refund\"\n );\n\n require(\n orderBook[orderId].status == uint8(2) ||\n orderBook[orderId].status == uint8(4),\n \"Escrow: order status must be equal to refund asked or refund refused\"\n );\n\n //update order status to paid\n orderBook[orderId].status = uint8(1);\n\n emit CancelRefund(orderBook[orderId].appId, orderId);\n }\n\n //refuse refund\n //triggled by seller\n function refuseRefund(uint256 orderId, uint256 modBId) public {\n require(\n _msgSender() == orderBook[orderId].seller,\n \"Escrow: only seller can refuse dispute\"\n );\n\n require(\n orderBook[orderId].status == uint8(2),\n \"Escrow: order status must be equal to refund asked\"\n );\n\n require(\n modBId > 0 && modBId <= moderatorContract.getMaxModId(),\n \"Escrow: modB id does not exists\"\n );\n\n // update modBId of dispute\n disputeBook[orderId].modBId = modBId;\n\n //update order status to refund refused\n orderBook[orderId].status = uint8(4);\n\n emit RefuseRefund(orderBook[orderId].appId, orderId, modBId);\n }\n\n //escalate, so mods can vote\n //triggled by seller or buyer\n function escalate(uint256 orderId) public {\n require(\n _msgSender() == orderBook[orderId].seller ||\n _msgSender() == orderBook[orderId].buyer,\n \"Escrow: only seller or buyer can escalate\"\n );\n\n require(\n orderBook[orderId].status == uint8(4),\n \"Escrow: order status must be equal to refund refused by seller\"\n );\n\n //update order status to escalated dispute, ready for mods to vote\n orderBook[orderId].status = uint8(5);\n\n emit Escalate(orderBook[orderId].appId, orderId);\n }\n\n // if seller agreed refund, then refund immediately\n // otherwise let mods or appOwner(if need) to judge\n \n function agreeRefund(uint256 orderId) public {\n //if seller agreed refund, then refund immediately\n if (_msgSender() == orderBook[orderId].seller) {\n require(\n orderBook[orderId].status == uint8(2) ||\n orderBook[orderId].status == uint8(4) ||\n orderBook[orderId].status == uint8(5),\n \"Escrow: order status must be at refund asked or refund refused or dispute esclated\"\n );\n sellerAgree(orderId);\n } else {\n require(\n orderBook[orderId].status == uint8(5),\n \"Escrow: mod can only vote on dispute escalated status\"\n );\n // get the mod's owner wallet address\n address modAWallet = moderatorContract.getModOwner(disputeBook[orderId].modAId);\n address modBWallet = moderatorContract.getModOwner(disputeBook[orderId].modBId);\n // if modA's owner equal to modB's owner and they are msg sender\n if (\n modAWallet == modBWallet &&\n modAWallet == _msgSender()\n ) {\n // set modAResolution/modBResolution to voted\n orderModAResolution[orderId] = uint8(1);\n orderModBResolution[orderId] = uint8(1);\n resolvedFinally(orderId, true);\n emit Resolve(\n _msgSender(),\n true,\n orderId,\n orderBook[orderId].appId,\n uint8(0)\n );\n }\n // if voter is app owner , and modA/modB not agree with each other.\n else if (\n appOwner[orderBook[orderId].appId] == _msgSender() &&\n ((orderModAResolution[orderId] == uint8(1) &&\n orderModBResolution[orderId] == uint8(2)) ||\n (orderModAResolution[orderId] == uint8(2) &&\n orderModBResolution[orderId] == uint8(1)))\n ) {\n resolvedFinally(orderId, true);\n emit Resolve(\n _msgSender(),\n true,\n orderId,\n orderBook[orderId].appId,\n uint8(3)\n );\n }\n // if voter is modA, and modA not vote yet, and modB not vote or vote disagree\n else if (\n modAWallet == _msgSender() &&\n orderModAResolution[orderId] == uint8(0) &&\n (orderModBResolution[orderId] == uint8(0) ||\n orderModBResolution[orderId] == uint8(2))\n ) {\n // set modAResolution to voted\n orderModAResolution[orderId] = uint8(1);\n emit Resolve(\n _msgSender(),\n true,\n orderId,\n orderBook[orderId].appId,\n uint8(1)\n );\n }\n // if voter is modA, and modA not vote yet, and modB vote agree\n else if (\n modAWallet == _msgSender() &&\n orderModAResolution[orderId] == uint8(0) &&\n orderModBResolution[orderId] == uint8(1)\n ) {\n // set modAResolution to voted\n orderModAResolution[orderId] = uint8(1);\n resolvedFinally(orderId, true);\n emit Resolve(\n _msgSender(),\n true,\n orderId,\n orderBook[orderId].appId,\n uint8(1)\n );\n }\n // if voter is modB, and modB not vote yet, and modA not vote or vote disagree\n else if (\n modBWallet == _msgSender() &&\n orderModBResolution[orderId] == uint8(0) &&\n (orderModAResolution[orderId] == uint8(0) ||\n orderModAResolution[orderId] == uint8(2))\n ) {\n // set modBResolution to voted\n orderModBResolution[orderId] = uint8(1);\n emit Resolve(\n _msgSender(),\n true,\n orderId,\n orderBook[orderId].appId,\n uint8(2)\n );\n }\n // if voter is modB, and modB not vote yet, and modA vote agree\n else if (\n modBWallet == _msgSender() &&\n orderModBResolution[orderId] == uint8(0) &&\n orderModAResolution[orderId] == uint8(1)\n ) {\n // set modBResolution to voted\n orderModBResolution[orderId] = uint8(1);\n resolvedFinally(orderId, true);\n emit Resolve(\n _msgSender(),\n true,\n orderId,\n orderBook[orderId].appId,\n uint8(2)\n );\n }\n // in other case , revert\n else {\n revert(\"Escrow: sender can not vote!\");\n }\n }\n } \n\n // the _msgSender() does not agree the refund\n \n function disagreeRefund(uint256 orderId) public {\n require(\n orderBook[orderId].status == uint8(5),\n \"Escrow: mod can only vote on dispute escalated status\"\n );\n // get the mod's owner wallet address\n address modAWallet = moderatorContract.getModOwner(disputeBook[orderId].modAId);\n address modBWallet = moderatorContract.getModOwner(disputeBook[orderId].modBId);\n // if modA's owner equal to modB's owner and they are msg sender\n if (\n modAWallet == modBWallet && \n modAWallet == _msgSender()\n ) {\n // set modAResolution/modBResolution to voted\n orderModAResolution[orderId] = uint8(2);\n orderModBResolution[orderId] = uint8(2);\n resolvedFinally(orderId, false);\n emit Resolve(\n _msgSender(), \n false, \n orderId, \n orderBook[orderId].appId,\n uint8(0)\n );\n }\n // if voter is app owner , and modA/modB not agree with each other.\n else if (\n appOwner[orderBook[orderId].appId] == _msgSender() &&\n ((orderModAResolution[orderId] == uint8(2) &&\n orderModBResolution[orderId] == uint8(1)) ||\n (orderModAResolution[orderId] == uint8(1) &&\n orderModBResolution[orderId] == uint8(2)))\n ) {\n resolvedFinally(orderId, false);\n emit Resolve(\n _msgSender(), \n false, \n orderId, \n orderBook[orderId].appId,\n uint8(3)\n );\n }\n // if voter is modA, and modA not vote yet, and modB not vote or vote agree\n else if (\n modAWallet == _msgSender() &&\n orderModAResolution[orderId] == uint8(0) &&\n (orderModBResolution[orderId] == uint8(0) ||\n orderModBResolution[orderId] == uint8(1))\n ) {\n // set modAResolution to voted\n orderModAResolution[orderId] = uint8(2);\n emit Resolve(\n _msgSender(), \n false, \n orderId, \n orderBook[orderId].appId,\n uint8(1)\n );\n }\n // if voter is modA, and modA not vote yet, and modB vote disagree\n else if (\n modAWallet == _msgSender() &&\n orderModAResolution[orderId] == uint8(0) &&\n orderModBResolution[orderId] == uint8(2)\n ) {\n // set modAResolution to voted\n orderModAResolution[orderId] = uint8(2);\n resolvedFinally(orderId, false);\n emit Resolve(\n _msgSender(), \n false, \n orderId, \n orderBook[orderId].appId,\n uint8(1)\n );\n }\n // if voter is modB, and modB not vote yet, and modA not vote or vote agree\n else if (\n modBWallet == _msgSender() &&\n orderModBResolution[orderId] == uint8(0) &&\n (orderModAResolution[orderId] == uint8(0) ||\n orderModAResolution[orderId] == uint8(1))\n ) {\n // set modBResolution to voted\n orderModBResolution[orderId] = uint8(2);\n emit Resolve(\n _msgSender(), \n false, \n orderId, \n orderBook[orderId].appId,\n uint8(2)\n );\n }\n // if voter is modB, and modB not vote yet, and modA vote disagree\n else if (\n modBWallet == _msgSender() &&\n orderModBResolution[orderId] == uint8(0) &&\n orderModAResolution[orderId] == uint8(2)\n ) {\n // set modBResolution to voted\n orderModBResolution[orderId] = uint8(2);\n resolvedFinally(orderId, false);\n emit Resolve(\n _msgSender(), \n false, \n orderId, \n orderBook[orderId].appId,\n uint8(2)\n );\n }\n // in other case , revert\n else {\n revert(\"Escrow: sender can not vote!\");\n }\n } \n\n // if seller agreed refund, then refund immediately\n \n function sellerAgree(uint256 orderId) internal {\n require(_msgSender() == orderBook[orderId].seller);\n // update order status to finish\n orderBook[orderId].status = uint8(3);\n // final commission is the app owner commission\n uint8 finalCommission = appOwnerCommission[orderBook[orderId].appId];\n // add app ownner commission fee\n userBalance[appOwner[orderBook[orderId].appId]][orderBook[orderId].coinAddress] =\n userBalance[appOwner[orderBook[orderId].appId]][orderBook[orderId].coinAddress].add(\n orderBook[orderId].amount.mul(finalCommission).div(100));\n emit UserBalanceChanged(\n appOwner[orderBook[orderId].appId],\n true,\n orderBook[orderId].amount.mul(finalCommission).div(100),\n orderBook[orderId].coinAddress,\n orderBook[orderId].appId,\n orderId\n );\n // as the refund is approved, refund to buyer\n userBalance[orderBook[orderId].buyer][\n orderBook[orderId].coinAddress\n ] = userBalance[orderBook[orderId].buyer][\n orderBook[orderId].coinAddress\n ].add(disputeBook[orderId].refund.mul(100 - finalCommission).div(100));\n emit UserBalanceChanged(\n orderBook[orderId].buyer,\n true,\n disputeBook[orderId].refund.mul(100 - finalCommission).div(100),\n orderBook[orderId].coinAddress,\n orderBook[orderId].appId,\n orderId\n );\n // if there is amount left, then send left amount to seller\n if (orderBook[orderId].amount > disputeBook[orderId].refund) {\n userBalance[orderBook[orderId].seller][\n orderBook[orderId].coinAddress\n ] = userBalance[orderBook[orderId].seller][\n orderBook[orderId].coinAddress\n ].add(\n (orderBook[orderId].amount.sub(disputeBook[orderId].refund))\n .mul(100 - finalCommission)\n .div(100)\n );\n emit UserBalanceChanged(\n orderBook[orderId].seller,\n true,\n (orderBook[orderId].amount.sub(disputeBook[orderId].refund))\n .mul(100 - finalCommission)\n .div(100),\n orderBook[orderId].coinAddress,\n orderBook[orderId].appId,\n orderId\n );\n }\n emit ResolvedFinally(orderBook[orderId].appId, orderId, uint8(1));\n } \n \n function resolvedFinally(uint256 orderId, bool result) internal {\n // update order status to finish\n orderBook[orderId].status = uint8(3);\n\n // the mod who judge right decision will increase 1 score, as well as adding the mod commission\n uint8 modNum = 1;\n uint8 winResolve = result ? 1 : 2;\n // get the mod's owner wallet address\n address modAWallet = moderatorContract.getModOwner(disputeBook[orderId].modAId);\n address modBWallet = moderatorContract.getModOwner(disputeBook[orderId].modBId);\n // if modA's owner equal to modB's owner, then just increase 1 success score for the owner\n // and add the mod commission\n if (\n modAWallet == modBWallet\n ) {\n rewardMod(\n orderId,\n disputeBook[orderId].modAId,\n modAWallet\n );\n }\n // else if modA does not agree with modB\n else if (orderModAResolution[orderId] != orderModBResolution[orderId]) {\n modNum = 2;\n // anyway app owner will get the mod commission\n userBalance[appOwner[orderBook[orderId].appId]][\n orderBook[orderId].coinAddress\n ] = userBalance[appOwner[orderBook[orderId].appId]][\n orderBook[orderId].coinAddress\n ].add(\n orderBook[orderId]\n .amount\n .mul(appModCommission[orderBook[orderId].appId])\n .div(100)\n );\n // the mod who vote the same as final result will give award\n if (orderModAResolution[orderId] == winResolve) {\n rewardMod(\n orderId,\n disputeBook[orderId].modAId,\n modAWallet\n );\n moderatorContract.updateModScore(disputeBook[orderId].modBId,false);\n } else {\n rewardMod(\n orderId,\n disputeBook[orderId].modBId,\n modBWallet\n );\n moderatorContract.updateModScore(disputeBook[orderId].modAId,false);\n }\n }\n // else if modA agree with modB\n else {\n // give both mods reward\n modNum = 2;\n rewardMod(\n orderId,\n disputeBook[orderId].modAId,\n modAWallet\n );\n rewardMod(\n orderId,\n disputeBook[orderId].modBId,\n modBWallet\n );\n }\n // caculate the commission fee\n uint8 finalCommission = appOwnerCommission[orderBook[orderId].appId] +\n (modNum * appModCommission[orderBook[orderId].appId]);\n // send app owner commission fee\n userBalance[appOwner[orderBook[orderId].appId]][orderBook[orderId].coinAddress] =\n userBalance[appOwner[orderBook[orderId].appId]][orderBook[orderId].coinAddress].add(\n orderBook[orderId].amount.mul(appOwnerCommission[orderBook[orderId].appId]).div(100));\n emit UserBalanceChanged(\n appOwner[orderBook[orderId].appId],\n true,\n orderBook[orderId].amount.mul(appOwnerCommission[orderBook[orderId].appId]).div(100),\n orderBook[orderId].coinAddress,\n orderBook[orderId].appId,\n orderId\n );\n //if result is to refund, then refund to buyer, the left will be sent to seller\n //else all paid to the seller\n\n if (result == true) {\n // as the refund is approved, refund to buyer\n userBalance[orderBook[orderId].buyer][orderBook[orderId].coinAddress] = \n userBalance[orderBook[orderId].buyer][orderBook[orderId].coinAddress].add(\n disputeBook[orderId].refund.mul(100 - finalCommission).div(100));\n emit UserBalanceChanged(\n orderBook[orderId].buyer,\n true,\n disputeBook[orderId].refund.mul(100 - finalCommission).div(100),\n orderBook[orderId].coinAddress,\n orderBook[orderId].appId,\n orderId\n );\n // if there is amount left, then send left amount to seller\n if (orderBook[orderId].amount > disputeBook[orderId].refund) {\n userBalance[orderBook[orderId].seller][\n orderBook[orderId].coinAddress\n ] = userBalance[orderBook[orderId].seller][\n orderBook[orderId].coinAddress\n ].add(\n (\n orderBook[orderId].amount.sub(\n disputeBook[orderId].refund\n )\n ).mul(100 - finalCommission).div(100)\n );\n emit UserBalanceChanged(\n orderBook[orderId].seller,\n true,\n (orderBook[orderId].amount.sub(disputeBook[orderId].refund))\n .mul(100 - finalCommission)\n .div(100),\n orderBook[orderId].coinAddress,\n orderBook[orderId].appId,\n orderId\n );\n }\n // update product sold\n productContract.updateProdScore(orderBook[orderId].productId, false);\n emit ResolvedFinally(orderBook[orderId].appId, orderId, uint8(1));\n } else {\n // send all the amount to the seller\n userBalance[orderBook[orderId].seller][\n orderBook[orderId].coinAddress\n ] = userBalance[orderBook[orderId].seller][\n orderBook[orderId].coinAddress\n ].add(\n orderBook[orderId].amount.mul(100 - finalCommission).div(\n 100\n )\n );\n emit UserBalanceChanged(\n orderBook[orderId].seller,\n true,\n orderBook[orderId].amount.mul(100 - finalCommission).div(100),\n orderBook[orderId].coinAddress,\n orderBook[orderId].appId,\n orderId\n );\n // update product sold\n productContract.updateProdScore(orderBook[orderId].productId, true);\n emit ResolvedFinally(orderBook[orderId].appId, orderId, uint8(0));\n }\n }\n\n // reward mod\n // adding mod commission as well as increasing mod score\n \n function rewardMod(uint256 orderId, uint256 modId, address mod) private {\n moderatorContract.updateModScore(modId, true);\n userBalance[mod][orderBook[orderId].coinAddress] = \n userBalance[mod][orderBook[orderId].coinAddress].add(\n orderBook[orderId].amount.mul(appModCommission[orderBook[orderId].appId]).div(100));\n emit UserBalanceChanged(\n mod,\n true,\n orderBook[orderId].amount.mul(appModCommission[orderBook[orderId].appId]).div(100),\n orderBook[orderId].coinAddress,\n orderBook[orderId].appId,\n orderId\n );\n } \n\n //seller want to claim money from order to balance\n //or\n //buyer want to claim money after seller either not to refuse dispute or agree dispute\n \n function claim(uint256 orderId) public {\n // final commission is the app owner commission\n uint8 finalCommission = appOwnerCommission[orderBook[orderId].appId];\n // add app ownner commission fee\n userBalance[appOwner[orderBook[orderId].appId]][orderBook[orderId].coinAddress] =\n userBalance[appOwner[orderBook[orderId].appId]][orderBook[orderId].coinAddress].add(\n orderBook[orderId].amount.mul(finalCommission).div(100));\n emit UserBalanceChanged(\n appOwner[orderBook[orderId].appId],\n true,\n orderBook[orderId].amount.mul(finalCommission).div(100),\n orderBook[orderId].coinAddress,\n orderBook[orderId].appId,\n orderId\n );\n //seller claim\n if (_msgSender() == orderBook[orderId].seller) {\n require(\n orderBook[orderId].status == uint8(1),\n \"Escrow: order status must be equal to 1 \"\n );\n\n require(\n block.timestamp > orderBook[orderId].claimTime,\n \"Escrow: currently seller can not claim, need to wait\"\n );\n // send all the amount to the seller\n userBalance[orderBook[orderId].seller][\n orderBook[orderId].coinAddress\n ] = userBalance[orderBook[orderId].seller][\n orderBook[orderId].coinAddress\n ].add(\n orderBook[orderId].amount.mul(100 - finalCommission).div(\n 100\n )\n );\n // update product sold record\n productContract.updateProdScore(orderBook[orderId].productId, true);\n emit UserBalanceChanged(\n orderBook[orderId].seller,\n true,\n orderBook[orderId].amount.mul(100 - finalCommission).div(100),\n orderBook[orderId].coinAddress,\n orderBook[orderId].appId,\n orderId\n );\n \n } else if (_msgSender() == orderBook[orderId].buyer) {\n // buyer claim\n\n require(\n orderBook[orderId].status == uint8(2),\n \"Escrow: order status must be equal to 2 \"\n );\n\n require(\n block.timestamp > disputeBook[orderId].refuseExpired,\n \"Escrow: currently buyer can not claim, need to wait\"\n );\n // refund to buyer\n userBalance[orderBook[orderId].buyer][orderBook[orderId].coinAddress] = \n userBalance[orderBook[orderId].buyer][orderBook[orderId].coinAddress].add(\n disputeBook[orderId].refund.mul(100 - finalCommission).div(100));\n // update product sold record\n productContract.updateProdScore(orderBook[orderId].productId, false);\n emit UserBalanceChanged(\n orderBook[orderId].buyer,\n true,\n disputeBook[orderId].refund.mul(100 - finalCommission).div(100),\n orderBook[orderId].coinAddress,\n orderBook[orderId].appId,\n orderId\n );\n // if there is amount left, then send left amount to seller\n if (orderBook[orderId].amount > disputeBook[orderId].refund) {\n userBalance[orderBook[orderId].seller][\n orderBook[orderId].coinAddress\n ] = userBalance[orderBook[orderId].seller][\n orderBook[orderId].coinAddress\n ].add(\n (\n orderBook[orderId].amount.sub(\n disputeBook[orderId].refund\n )\n ).mul(100 - finalCommission).div(100)\n );\n \n emit UserBalanceChanged(\n orderBook[orderId].seller,\n true,\n (orderBook[orderId].amount.sub(disputeBook[orderId].refund))\n .mul(100 - finalCommission)\n .div(100),\n orderBook[orderId].coinAddress,\n orderBook[orderId].appId,\n orderId\n );\n }\n \n } else {\n revert(\"Escrow: only seller or buyer can claim\");\n }\n\n orderBook[orderId].status = 3;\n emit Claim(_msgSender(), orderBook[orderId].appId, orderId);\n } \n\n //withdraw from user balance\n function withdraw(uint256 _amount, address _coinAddress) public {\n //get user balance\n uint256 _balance = userBalance[_msgSender()][_coinAddress];\n\n require(_balance >= _amount, \"Escrow: insufficient balance!\");\n\n //descrease user balance\n userBalance[_msgSender()][_coinAddress] = _balance.sub(_amount);\n\n //if the coin type is ETH\n if (_coinAddress == address(0)) {\n //check balance is enough\n require(address(this).balance > _amount, \"Escrow: insufficient balance\");\n\n payable(_msgSender()).transfer(_amount);\n } else {\n //if the coin type is ERC20\n\n IERC20 _token = IERC20(_coinAddress);\n\n _token.transfer(_msgSender(), _amount);\n }\n\n //emit withdraw event\n emit Withdraw(_msgSender(), _amount, _coinAddress);\n }\n}\n"
|