Atomic Call-Success Settlement

atomic-call-success-settlement · conditional-settlement · 519 B runtime · funds-movement

scores

source

pragma solidity ^0.8.13;

contract AtomicCallSuccessSettlement {
    address private immutable target;
    address payable private immutable agent;
    address payable private immutable client;
    bytes private targetCalldata;
    bool private settled;

    constructor(address target_, bytes memory targetCalldata_, address payable agent_, address payable client_) payable {
        target = target_;
        agent = agent_;
        client = client_;
        targetCalldata = targetCalldata_;
    }

    function execute() external {
        require(!settled);
        settled = true;
        (bool ok, ) = target.call(targetCalldata);
        if (ok) {
            selfdestruct(agent);
        } else {
            selfdestruct(client);
        }
    }
}