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:
- 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
- withdraw(contractId, preimage) - once the receiver knows the preimage of the hashlock hash they can claim the tokens with this function
- 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.
Name | Type | Description |
---|---|---|
receiver | address | Receiver of the tokens. |
hashLock | bytes32 | A keccak256 hash hashlock. |
timeLock | uint32 | UNIX epoch seconds time that the lock expires at. Refunds can be made after this time. |
tokenContract | address | ERC20 Token contract address. |
amount | uint256 | Amount of the token to lock up. |
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 ownership of the locked tokens 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 restore ownership of the tokens to the sender.
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 amount, bytes32 hashlock, bytes32 preimage, uint32 timelock, address sender, address receiver, address tokenContract, bool collected)
Get contract details.
Name | Type | Description |
---|---|---|
contractId | bytes32 | HTLC contract id |
haveContract
function haveContract(bytes32 contractId) internal view returns (bool exists)
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.
updateFeeReceiver
function updateFeeReceiver(address feeReceiverAddress) external
This method will update the feeTo 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 |