Verified contractExactMIT
HashedTimelockETH
Address0x33bab46ccaa5bd95df8b0d6961c11dbd98a16e7a
- Balance
0.000555 ETH
- Transactions
18
- Compiler
solc 0.8.30
- Same code
Only this contract
Details
- Compiler
- solc 0.8.30+commit.73712a01
- Language
- solidity
- Optimization
- No
- EVM version
- prague
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 25,481,259
- Creation transaction
- 0x9a982f5eda…d3120bce23
src/contracts/HashedTimelockETH.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
/**
* @title Hashed Timelock Contracts (HTLCs) on Ethereum ETH.
* @author Chris Buckland (chatch) - Original implementation
* @dev Modernized for Solidity 0.8.x with gas optimizations and safety improvements
*
* This contract provides a way to create and keep HTLCs for native ETH.
*
* See HashedTimelockERC20.sol for a contract that provides the same functions
* for ERC20 tokens.
*
* Protocol:
*
* 1) newContract(receiver, hashlock, timelock) - a sender calls this to create
* a new HTLC with ETH value and gets back a 32 byte contract id
* 2) withdraw(contractId, preimage) - once the receiver knows the preimage of
* the hashlock hash they can claim the ETH with this function
* 3) refund() - after timelock has expired and if the receiver did not
* withdraw funds the sender / creator of the HTLC can get their ETH
* back with this function.
*
* Source: https://github.com/chatch/hashed-timelock-contract-ethereum
*/
contract HashedTimelockETH {
event HTLCETHNew(
bytes32 indexed contractId,
address indexed sender,
address indexed receiver,
uint256 amount,
bytes32 hashlock,
uint256 timelock
);
event HTLCETHWithdraw(bytes32 indexed contractId);
event HTLCETHRefund(bytes32 indexed contractId);
struct LockContract {
address payable sender;
address payable receiver;
uint256 amount;
bytes32 hashlock; // sha-256 hash
uint256 timelock; // UNIX timestamp seconds - locked UNTIL this time
bool withdrawn;
bool refunded;
bytes32 preimage;
}
mapping(bytes32 => LockContract) public contracts;
// Custom errors (gas efficient in 0.8.x)
error FundsAmountMustBePositive();
error TimelockMustBeInFuture();
error ContractDoesNotExist();
error HashlockMismatch();
error NotReceiver();
error AlreadyWithdrawn();
error TimelockNotYetPassed();
error NotSender();
error AlreadyRefunded();
error ContractAlreadyExists();
error TransferFailed();
modifier fundsSent() {
if (msg.value == 0) revert FundsAmountMustBePositive();
_;
}
modifier futureTimelock(uint256 _time) {
if (_time <= block.timestamp) revert TimelockMustBeInFuture();
_;
}
modifier contractExists(bytes32 _contractId) {
if (!haveContract(_contractId)) revert ContractDoesNotExist();
_;
}
modifier hashlockMatches(bytes32 _contractId, bytes32 _x) {
if (contracts[_contractId].hashlock != sha256(abi.encodePacked(_x))) {
revert HashlockMismatch();
}
_;
}
modifier withdrawable(bytes32 _contractId) {
LockContract storage c = contracts[_contractId];
if (c.receiver != msg.sender) revert NotReceiver();
if (c.withdrawn) revert AlreadyWithdrawn();
if (c.timelock <= block.timestamp) revert TimelockNotYetPassed();
_;
}
modifier refundable(bytes32 _contractId) {
LockContract storage c = contracts[_contractId];
if (c.sender != msg.sender) revert NotSender();
if (c.refunded) revert AlreadyRefunded();
if (c.withdrawn) revert AlreadyWithdrawn();
if (c.timelock > block.timestamp) revert TimelockNotYetPassed();
_;
}
/**
* @dev Sender sets up a new hash time lock contract depositing the ETH and
* providing the receiver lock terms.
*
* @param _receiver Receiver of the ETH.
* @param _hashlock A sha-256 hash hashlock.
* @param _timelock UNIX epoch seconds time that the lock expires at.
* Refunds can be made after this time.
* @return contractId Id of the new HTLC. This is needed for subsequent calls.
*/
function newContract(address payable _receiver, bytes32 _hashlock, uint256 _timelock)
external
payable
fundsSent
futureTimelock(_timelock)
returns (bytes32 contractId)
{
contractId = sha256(
abi.encodePacked(
msg.sender,
_receiver,
msg.value,
_hashlock,
_timelock
)
);
// Reject if a contract already exists with the same parameters
if (haveContract(contractId)) revert ContractAlreadyExists();
contracts[contractId] = LockContract(
payable(msg.sender),
_receiver,
msg.value,
_hashlock,
_timelock,
false,
false,
bytes32(0)
);
emit HTLCETHNew(
contractId,
msg.sender,
_receiver,
msg.value,
_hashlock,
_timelock
);
}
/**
* @dev Called by the receiver once they know the preimage of the hashlock.
* This will transfer the locked funds to their address.
*
* @param _contractId Id of the HTLC.
* @param _preimage sha256(_preimage) should equal the contract hashlock.
* @return bool true on success
*/
function withdraw(bytes32 _contractId, bytes32 _preimage)
external
contractExists(_contractId)
hashlockMatches(_contractId, _preimage)
withdrawable(_contractId)
returns (bool)
{
LockContract storage c = contracts[_contractId];
c.preimage = _preimage;
c.withdrawn = true;
(bool success, ) = c.receiver.call{value: c.amount}("");
if (!success) revert TransferFailed();
emit HTLCETHWithdraw(_contractId);
return true;
}
/**
* @dev Called by the sender if there was no withdraw AND the time lock has
* expired. This will refund the contract amount.
*
* @param _contractId Id of HTLC to refund from.
* @return bool true on success
*/
function refund(bytes32 _contractId)
external
contractExists(_contractId)
refundable(_contractId)
returns (bool)
{
LockContract storage c = contracts[_contractId];
c.refunded = true;
(bool success, ) = c.sender.call{value: c.amount}("");
if (!success) revert TransferFailed();
emit HTLCETHRefund(_contractId);
return true;
}
/**
* @dev Get contract details.
* @param _contractId HTLC contract id
* @return sender Address of the sender
* @return receiver Address of the receiver
* @return amount Amount of ETH locked
* @return hashlock Hash lock
* @return timelock Time lock (Unix timestamp)
* @return withdrawn Whether funds have been withdrawn
* @return refunded Whether funds have been refunded
* @return preimage Preimage of the hash lock
*/
function getContract(bytes32 _contractId)
public
view
returns (
address sender,
address receiver,
uint256 amount,
bytes32 hashlock,
uint256 timelock,
bool withdrawn,
bool refunded,
bytes32 preimage
)
{
if (!haveContract(_contractId)) revert ContractDoesNotExist();
LockContract storage c = contracts[_contractId];
return (
c.sender,
c.receiver,
c.amount,
c.hashlock,
c.timelock,
c.withdrawn,
c.refunded,
c.preimage
);
}
/**
* @dev Check if there is a contract with a given id.
* @param _contractId Id into contracts mapping.
*/
function haveContract(bytes32 _contractId)
internal
view
returns (bool exists)
{
exists = (contracts[_contractId].sender != address(0));
}
}
Compiler settingsShow
{
"viaIR": false,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"optimizer": {
"runs": 200,
"enabled": false
},
"evmVersion": "prague",
"remappings": [
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
"forge-std/=lib/forge-std/src/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.