-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(contracts): add solidity proxy contract for registration
- Loading branch information
1 parent
118c9d5
commit a115848
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.4; | ||
|
||
import "@openzeppelin/contracts/access/Ownable.sol"; | ||
|
||
abstract contract Controllable is Ownable { | ||
mapping(address => bool) public controllers; | ||
|
||
modifier onlyController { | ||
require(controllers[msg.sender]); | ||
_; | ||
} | ||
|
||
function setController(address controller, bool status) external onlyOwner { | ||
controllers[controller] = status; | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
packages/contracts/contracts/relayer/evm/RegistrationProxy.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.4; | ||
|
||
import "@openzeppelin/contracts/access/Ownable.sol"; | ||
import "./Controllable.sol"; | ||
|
||
contract RegistrationProxy is Ownable, Controllable { | ||
enum Status { | ||
NON_EXISTENT, | ||
PENDING, | ||
SUCCESS, | ||
FAILURE | ||
} | ||
|
||
event InitiateRequest( | ||
uint256 indexed id, | ||
string name, | ||
string recipient, | ||
uint8 yearsToRegister, | ||
uint256 value, | ||
uint256 ttl | ||
); | ||
|
||
event ResultInfo( | ||
uint256 indexed id, | ||
bool success, | ||
uint256 refundAmt | ||
); | ||
|
||
struct Record { | ||
// string name; | ||
address initiator; | ||
uint256 value; | ||
uint256 ttl; | ||
Status status; | ||
} | ||
|
||
uint256 public id; | ||
uint256 public holdPeriod; | ||
mapping(uint256 => Record) public idToRecord; | ||
|
||
constructor(uint256 _holdPeriod) Ownable() { | ||
holdPeriod = _holdPeriod; | ||
} | ||
|
||
function setHoldPeriod(uint256 _holdPeriod) external onlyOwner { | ||
holdPeriod = _holdPeriod; | ||
} | ||
|
||
function register( | ||
string calldata name, | ||
string calldata recipient, | ||
uint8 yearsToRegister | ||
) external payable { | ||
uint256 ttl = block.timestamp + holdPeriod; | ||
uint256 _id = id++; | ||
|
||
idToRecord[_id] = Record(msg.sender, msg.value, ttl, Status.PENDING); | ||
|
||
emit InitiateRequest( | ||
_id, | ||
name, | ||
recipient, | ||
yearsToRegister, | ||
msg.value, | ||
ttl | ||
); | ||
} | ||
|
||
function success(uint256 _id, uint256 refundAmt) external onlyController { | ||
Record memory record = idToRecord[_id]; | ||
require(record.status == Status.PENDING, "Invalid state"); | ||
require(refundAmt <= record.value, "Refund exceeds received value"); | ||
|
||
record.status = Status.SUCCESS; | ||
idToRecord[_id] = record; | ||
payable(record.initiator).transfer(refundAmt); | ||
|
||
emit ResultInfo(_id, true, refundAmt); | ||
} | ||
|
||
function failure(uint256 _id) external { | ||
Record memory record = idToRecord[_id]; | ||
require(record.status == Status.PENDING, "Invalid state"); | ||
require(controllers[msg.sender] || record.ttl < block.timestamp, "Only controller can respond till TTL"); | ||
|
||
record.status = Status.FAILURE; | ||
idToRecord[_id] = record; | ||
payable(record.initiator).transfer(record.value); | ||
|
||
emit ResultInfo(_id, false, record.value); | ||
} | ||
} |