Replace transfer() with selfdestruct to avoid fixed-gas failure

replace-transfer-with-selfdestruct-to-avoid-fixed-gas-failur · conditional-settlement · 1118 B runtime · funds-movement

scores

source

pragma solidity ^0.8.13;

contract HashlockEscrow {
    address private immutable payer;
    address private immutable recipient;
    bytes32 private immutable hash;
    uint256 private immutable expiration;
    bool private settled;

    constructor(address _recipient, bytes32 _hash, uint256 _expiration) payable {
        require(_recipient != address(0), "recipient zero");
        require(_expiration > block.number, "expiry past");
        payer = msg.sender;
        recipient = _recipient;
        hash = _hash;
        expiration = _expiration;
    }

    function claim(bytes calldata preimage) external {
        require(!settled, "settled");
        require(block.number < expiration, "expired");
        require(keccak256(preimage) == hash, "bad preimage");
        settled = true;
        uint256 amount = address(this).balance;
        emit Claimed(recipient, amount);
        selfdestruct(payable(recipient));
    }

    function refund() external {
        require(msg.sender == payer, "not payer");
        require(!settled, "settled");
        require(block.number > expiration, "not expired");
        settled = true;
        uint256 amount = address(this).balance;
        emit Refunded(payer, amount);
        selfdestruct(payable(payer));
    }

    event Claimed(address indexed recipient, uint256 amount);
    event Refunded(address indexed payer, uint256 amount);
}