Skip to content

Commit

Permalink
feat(contracts): add solidity proxy contract for registration
Browse files Browse the repository at this point in the history
  • Loading branch information
realnimish committed Sep 19, 2024
1 parent 118c9d5 commit a115848
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/contracts/contracts/relayer/evm/Controllable.sol
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 packages/contracts/contracts/relayer/evm/RegistrationProxy.sol
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);
}
}

0 comments on commit a115848

Please sign in to comment.