HTLC

HTLC ERC

HTCL (Hashed Timelock Contracts) on Ethereum Virtual Machine (EVM) ERC tokens.

This contract provides a way to create and keep HTLCs for 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 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.

You can find the full contract verified & published on ether.

Functions

newContract

function newContract(address payable receiver, bytes32 hashLock, uint32 timeLock) external payable returns (bytes32 contractId)

Sender sets up a new hash time lock contract depositing the ETH and providing the receiver lock terms.

NameTypeDescription
receiveraddress payableReceiver of the ETH.
hashLockbytes32A keccak256 hash hashlock.
timeLockuint32UNIX epoch seconds time that the lock expires at. Refunds can be made after this time.
NameTypeDescription
contractIdbytes32Id of the new HTLC. This is needed for subsequent calls.

withdraw

function withdraw(bytes32 contractId, bytes32 preimage) external returns (bool)

Called by the receiver once they know the preimage of the hashlock. This will transfer the locked funds to their address.

NameTypeDescription
contractIdbytes32Id of the HTLC.
preimagebytes32keccak256(_preimage) should equal the contract hashlock.
NameTypeDescription
[0]boolbool true on success

refund

function refund(bytes32 contractId) external returns (bool)

Called by the sender if there was no withdraw AND the time lock has expired. This will refund the contract amount.

NameTypeDescription
contractIdbytes32Id of HTLC to refund from.
NameTypeDescription
[0]boolbool true on success

getContract

function getContract(bytes32 contractId) external view returns (uint256, bytes32, bytes32, uint32, address, address, bool)

Get contract details.

NameTypeDescription
contractIdbytes32HTLC contract id

haveContract

function haveContract(bytes32 contractId) internal view returns (bool)

Is there a contract with id _contractId.

NameTypeDescription
contractIdbytes32Id into contracts mapping.

getEmptyContractHash

function getEmptyContractHash() private pure returns (bytes32)

This method return a keccak256 hash of an empty Lock contract.

getCollectedFee

function getCollectedFee() external view returns (uint256)

Returns the total fee collected on this smart contract

withdrawCollectedFee

function withdrawCollectedFee() external

This method will help user to withdraw the fee collected on this smart contract, it will only let the user to withdraw any fee not the balance of this smart contract as the balance might have some amount from the contract which has been locked.

updateFeeReceiver

function updateFeeReceiver(address feeReceiverAddress) external

This method will update the feeReceiver address who can withdraw the fee collected on this smart contract

NameTypeDescription
feeReceiverAddressaddress- address of new fee collector

updateOwner

function updateOwner(address ownerAddress) external

This method will update the owner address on this smart contract

NameTypeDescription
ownerAddressaddress- address of new owner

updateFee

function updateFee(uint32 updatedFeeNumerator, uint32 updatedFeeDenominator) external

This method will allow owner to update the fee charged upon successful withdrawal

NameTypeDescription
updatedFeeNumeratoruint32- new fee numerator
updatedFeeDenominatoruint32- new fee denominator