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:
- newContract(receiver, hashlock, timelock) - a sender calls this to create a new HTLC and gets back a 32 byte contract id
- withdraw(contractId, preimage) - once the receiver knows the preimage of the hashlock hash they can claim the ETH with this function
- 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.
Name | Type | Description |
---|---|---|
receiver | address payable | Receiver of the ETH. |
hashLock | bytes32 | A keccak256 hash hashlock. |
timeLock | uint32 | UNIX epoch seconds time that the lock expires at. Refunds can be made after this time. |
Name | Type | Description |
---|---|---|
contractId | bytes32 | Id 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.
Name | Type | Description |
---|---|---|
contractId | bytes32 | Id of the HTLC. |
preimage | bytes32 | keccak256(_preimage) should equal the contract hashlock. |
Name | Type | Description |
---|---|---|
[0] | bool | bool 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.
Name | Type | Description |
---|---|---|
contractId | bytes32 | Id of HTLC to refund from. |
Name | Type | Description |
---|---|---|
[0] | bool | bool true on success |
getContract
function getContract(bytes32 contractId) external view returns (uint256, bytes32, bytes32, uint32, address, address, bool)
Get contract details.
Name | Type | Description |
---|---|---|
contractId | bytes32 | HTLC contract id |
haveContract
function haveContract(bytes32 contractId) internal view returns (bool)
Is there a contract with id _contractId.
Name | Type | Description |
---|---|---|
contractId | bytes32 | Id 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
Name | Type | Description |
---|---|---|
feeReceiverAddress | address | - address of new fee collector |
updateOwner
function updateOwner(address ownerAddress) external
This method will update the owner address on this smart contract
Name | Type | Description |
---|---|---|
ownerAddress | address | - 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
Name | Type | Description |
---|---|---|
updatedFeeNumerator | uint32 | - new fee numerator |
updatedFeeDenominator | uint32 | - new fee denominator |