pragma solidity ^0.8.20;
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant NOT_ENTERED = 1;
uint256 private constant ENTERED = 2;
uint256 private _status;
/**
* @dev Unauthorized reentrant call.
*/
error ReentrancyGuardReentrantCall();
constructor() {
_status = NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be NOT_ENTERED
if (_status == ENTERED) {
revert ReentrancyGuardReentrantCall();
}
// Any calls to nonReentrant after this point will fail
_status = ENTERED;
}
function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == ENTERED;
}
}
// File: ITimeBasedExperience.sol
pragma solidity ^0.8.20;
interface ITimeBasedExperience {
/**
* @notice Redeem shares (minutes) for an experience. You need to redeem at least 15 shares
* @param creatorAddress Address of the experience creator
* @param shares Amount of minutes
*/
function redeemSharesAllowed(address creatorAddress, uint256 shares, address from) external;
/**
* @notice Transfer shares (minutes) to another address
* @param creatorAddress Cretor of the experience
* @param from Owner of the shares
* @param to New owner of the shares
* @param shares Amount of shares (minutes) to transfer
* @dev Used only by internal contracts, not meant for public use
*/
function transferShare(address creatorAddress, address from, address to, uint256 shares) external;
function experienceExists(address creatorAddress) external view returns (bool);
}
// File: directmessages.sol
pragma solidity ^0.8.20;
contract DirectMessages is ReentrancyGuard {
ITimeBasedExperience public immutable timeBasedExperience;
address private immutable _owner;
bool public isPaused;
struct Question {
bool answered;
uint256 createdAtTimestamp;
address asker;
}
event NewQuestion(address indexed to, address indexed from, uint256 nonce, uint256 askedAt);
event AnsweredQuestion(address indexed to, address indexed from, uint256 nonce, uint256 answeredAt);
event RefundedQuestion(address indexed to, address indexed from, uint256 nonce, uint256 refundedAt);
// Creator -> Current nonce
mapping(address => uint256) public creatorQuestionsNonce;
// Creator -> Nonce -> Question
mapping(address => mapping(uint256 => Question)) public question;
constructor(address _timeBasedExperience) {
timeBasedExperience = ITimeBasedExperience(_timeBasedExperience);
_owner = msg.sender;
isPaused = false;
}
function askCreator(address _creatorAddress) external nonReentrant whenNotPaused {
require(msg.sender == tx.origin, "DirectMessages: Only EOA can ask question");
// Check if experince exists
require(timeBasedExperience.experienceExists(_creatorAddress), "DirectMessages: Experience does not exist");
uint256 nonce = creatorQuestionsNonce[_creatorAddress];
// Gets ownership of 1 share from the asker
timeBasedExperience.transferShare(_creatorAddress, msg.sender, address(this), 1);
// Create the question data structure storing the asker and the timestamp
question[_creatorAddress][nonce] =
Question({answered: false, createdAtTimestamp: block.timestamp, asker: msg.sender});
// Increment the questions nonce for the given creator
creatorQuestionsNonce[_creatorAddress] = nonce + 1;
emit NewQuestion(_creatorAddress, msg.sender, nonce, block.timestamp);
}
function answerQuestion(uint256 _nonce) external nonReentrant {
// Can only answer a question once
require(
question[msg.sender][_nonce].answered == false, "DirectMessages: You have already answered this question"
);
require(question[msg.sender][_nonce].asker != address(0), "DirectMessages: Question does not exist");
// Mark the question as answered
question[msg.sender][_nonce].answered = true;
// Redeems share that was put in escrow by the asker
timeBasedExperience.redeemSharesAllowed(msg.sender, 1, question[msg.sender][_nonce].asker);
emit AnsweredQuestion(msg.sender, question[msg.sender][_nonce].asker, _nonce, block.timestamp);
}
function refund(address _creatorAddress, uint256 _nonce) external nonReentrant {
// Require msg.sender is asker
require(question[_creatorAddress][_nonce].asker == msg.sender, "DirectMessages: You are not the asker");
// Require question has not been answered
require(question[_creatorAddress][_nonce].answered == false, "DirectMessages: Question has been answered");
// Require 5 days passed since question was asked
require(
block.timestamp - question[_creatorAddress][_nonce].createdAtTimestamp >= 5 days,
"DirectMessages: You can only refund after 5 days"
);
// Transfer share back to asker
timeBasedExperience.transferShare(_creatorAddress, address(this), msg.sender, 1);
// Marks the question as answered
question[_creatorAddress][_nonce].answered = true;
emit RefundedQuestion(_creatorAddress, msg.sender, _nonce, block.timestamp);
}
modifier whenNotPaused() {
require(!isPaused, "DirectMessages: Contract is paused");
_;
}
function setPauseStatus(bool _isPaused) external {
require(msg.sender == _owner, "DirectMessages: Only owner can pause");
isPaused = _isPaused;
}
}
{
"compilationTarget": {
"DirectMessages.sol": "DirectMessages"
},
"evmVersion": "shanghai",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": false,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_timeBasedExperience","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"ReentrancyGuardReentrantCall","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":false,"internalType":"uint256","name":"nonce","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"answeredAt","type":"uint256"}],"name":"AnsweredQuestion","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":false,"internalType":"uint256","name":"nonce","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"askedAt","type":"uint256"}],"name":"NewQuestion","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":false,"internalType":"uint256","name":"nonce","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"refundedAt","type":"uint256"}],"name":"RefundedQuestion","type":"event"},{"inputs":[{"internalType":"uint256","name":"_nonce","type":"uint256"}],"name":"answerQuestion","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_creatorAddress","type":"address"}],"name":"askCreator","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"creatorQuestionsNonce","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"isPaused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"question","outputs":[{"internalType":"bool","name":"answered","type":"bool"},{"internalType":"uint256","name":"createdAtTimestamp","type":"uint256"},{"internalType":"address","name":"asker","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_creatorAddress","type":"address"},{"internalType":"uint256","name":"_nonce","type":"uint256"}],"name":"refund","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bool","name":"_isPaused","type":"bool"}],"name":"setPauseStatus","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"timeBasedExperience","outputs":[{"internalType":"contract ITimeBasedExperience","name":"","type":"address"}],"stateMutability":"view","type":"function"}]