HTLC

HTLC ERC20

HTLC (Hashed Timelock Contracts) on Ethereum Virtual Machine (EVM) ERC20 tokens.

This contract provides a way to create and keep HTLCs for ERC20 tokens.

See HashedTimelock.sol for a contract that provides the same functions for the native ETH token.

Protocol:

  1. newContract(receiver, hashlock, timelock, tokenContract, amount) - a sender calls this to create a new HTLC on a given token (tokenContract) for a given amount. A 32 byte contract id is returned
  2. withdraw(contractId, preimage) - once the receiver knows the preimage of the hashlock hash they can claim the tokens with this function
  3. refund() - after timelock has expired and if the receiver did not withdraw the tokens the sender / creator of the HTLC can get their tokens back with this function.

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

Functions

newContract

function newContract(address receiver, bytes32 hashLock, uint32 timeLock, address tokenContract, uint256 amount) external returns (bytes32 contractId)

Sender / Payer sets up a new hash time lock contract depositing the funds and providing the reciever and terms.

NOTE: _receiver must first call approve() on the token contract. See allowance check in tokensTransferable modifier.

NameTypeDescription
receiveraddressReceiver of the tokens.
hashLockbytes32A keccak256 hash hashlock.
timeLockuint32UNIX epoch seconds time that the lock expires at. Refunds can be made after this time.
tokenContractaddressERC20 Token contract address.
amountuint256Amount of the token to lock up.
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 ownership of the locked tokens 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 restore ownership of the tokens to the sender.

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

getContract

function getContract(bytes32 contractId) external view returns (uint256 amount, bytes32 hashlock, bytes32 preimage, uint32 timelock, address sender, address receiver, address tokenContract, bool collected)

Get contract details.

NameTypeDescription
contractIdbytes32HTLC contract id

haveContract

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

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.

updateFeeReceiver

function updateFeeReceiver(address feeReceiverAddress) external

This method will update the feeTo 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