|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.4; |
| 3 | + |
| 4 | +import "@openzeppelin/contracts/access/Ownable.sol"; |
| 5 | +import "./Controllable.sol"; |
| 6 | + |
| 7 | +contract RegistrationProxy is Ownable, Controllable { |
| 8 | + enum Status { |
| 9 | + NON_EXISTENT, |
| 10 | + PENDING, |
| 11 | + SUCCESS, |
| 12 | + FAILURE |
| 13 | + } |
| 14 | + |
| 15 | + event InitiateRequest( |
| 16 | + uint256 indexed id, |
| 17 | + string name, |
| 18 | + string recipient, |
| 19 | + uint8 yearsToRegister, |
| 20 | + uint256 value, |
| 21 | + uint256 ttl |
| 22 | + ); |
| 23 | + |
| 24 | + event ResultInfo( |
| 25 | + uint256 indexed id, |
| 26 | + bool success, |
| 27 | + uint256 refundAmt |
| 28 | + ); |
| 29 | + |
| 30 | + struct Record { |
| 31 | + // string name; |
| 32 | + address initiator; |
| 33 | + uint256 value; |
| 34 | + uint256 ttl; |
| 35 | + Status status; |
| 36 | + } |
| 37 | + |
| 38 | + uint256 public id; |
| 39 | + uint256 public holdPeriod; |
| 40 | + mapping(uint256 => Record) public idToRecord; |
| 41 | + |
| 42 | + constructor(uint256 _holdPeriod) Ownable() { |
| 43 | + holdPeriod = _holdPeriod; |
| 44 | + } |
| 45 | + |
| 46 | + function setHoldPeriod(uint256 _holdPeriod) external onlyOwner { |
| 47 | + holdPeriod = _holdPeriod; |
| 48 | + } |
| 49 | + |
| 50 | + function register( |
| 51 | + string calldata name, |
| 52 | + string calldata recipient, |
| 53 | + uint8 yearsToRegister |
| 54 | + ) external payable { |
| 55 | + uint256 ttl = block.timestamp + holdPeriod; |
| 56 | + uint256 _id = id++; |
| 57 | + |
| 58 | + idToRecord[_id] = Record(msg.sender, msg.value, ttl, Status.PENDING); |
| 59 | + |
| 60 | + emit InitiateRequest( |
| 61 | + _id, |
| 62 | + name, |
| 63 | + recipient, |
| 64 | + yearsToRegister, |
| 65 | + msg.value, |
| 66 | + ttl |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + function success(uint256 _id, uint256 refundAmt) external onlyController { |
| 71 | + Record memory record = idToRecord[_id]; |
| 72 | + require(record.status == Status.PENDING, "Invalid state"); |
| 73 | + require(refundAmt <= record.value, "Refund exceeds received value"); |
| 74 | + |
| 75 | + record.status = Status.SUCCESS; |
| 76 | + idToRecord[_id] = record; |
| 77 | + payable(record.initiator).transfer(refundAmt); |
| 78 | + |
| 79 | + emit ResultInfo(_id, true, refundAmt); |
| 80 | + } |
| 81 | + |
| 82 | + function failure(uint256 _id) external { |
| 83 | + Record memory record = idToRecord[_id]; |
| 84 | + require(record.status == Status.PENDING, "Invalid state"); |
| 85 | + require(controllers[msg.sender] || record.ttl < block.timestamp, "Only controller can respond till TTL"); |
| 86 | + |
| 87 | + record.status = Status.FAILURE; |
| 88 | + idToRecord[_id] = record; |
| 89 | + payable(record.initiator).transfer(record.value); |
| 90 | + |
| 91 | + emit ResultInfo(_id, false, record.value); |
| 92 | + } |
| 93 | +} |
0 commit comments