contract AbstractRampSwaps is Ownable, WithStatus, WithOracles, AssetAdapter {

    /// @dev contract version, defined in semver
    string public constant VERSION = "0.3.1";

    /// @dev used as a special swap endTime value, to denote a yet unclaimed swap
    uint32 internal constant SWAP_UNCLAIMED = 1;
    uint32 internal constant MIN_ACTUAL_TIMESTAMP = 1000000000;

    /// @notice how long are sender's funds locked from a claim until he can cancel the swap
    uint32 internal constant SWAP_LOCK_TIME_S = 3600 * 24 * 7;

    event Created(bytes32 indexed swapHash);
    event BuyerSet(bytes32 indexed oldSwapHash, bytes32 indexed newSwapHash);
    event Claimed(bytes32 indexed oldSwapHash, bytes32 indexed newSwapHash);
    event Released(bytes32 indexed swapHash);
    event SenderReleased(bytes32 indexed swapHash);
    event Returned(bytes32 indexed swapHash);
    event SenderReturned(bytes32 indexed swapHash);

    /**
     * @notice Mapping from swap details hash to its end time (as a unix timestamp).
     * After the end time the swap can be cancelled, and the funds will be returned to the sender.
     * Value `(SWAP_UNCLAIMED)` is used to denote that a swap exists, but has not yet been claimed
     * by any receiver, and can also be cancelled until that.
     */
    mapping (bytes32 => uint32) internal swaps;

    /**
     * @dev EIP712 type hash for the struct:
     * EIP712Domain {
     *   name: string;
     *   version: string;
     *   chainId: uint256;
     *   verifyingContract: address;
     * }
     */
    bytes32 internal constant EIP712_DOMAIN_TYPEHASH = 0x8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f;
    bytes32 internal EIP712_DOMAIN_HASH;

    constructor(uint256 _chainId) internal {
        EIP712_DOMAIN_HASH = keccak256(
            abi.encode(
                EIP712_DOMAIN_TYPEHASH,
                keccak256(bytes("RampSwaps")),
                keccak256(bytes(VERSION)),
                _chainId,
                address(this)
            )
        );
    }

    /**
     * Swap creation, called by the crypto sender. Checks swap parameters and ensures the crypto
     * asset is locked on this contract.
     * Additionally to the swap details, this function takes params v, r, s, which is checked to be
     * an ECDSA signature of the swap hash made by the oracle -- to prevent users from creating
     * swaps outside Ramp Network.
     *
     * Emits a `Created` event with the swap hash.
     */
    function create(
        address _oracle,
        bytes calldata _assetData,
        bytes32 _paymentDetailsHash,
        uint8 v,
        bytes32 r,
        bytes32 s
    )
        external
        payable
        statusAtLeast(Status.ACTIVE)
        isOracle(_oracle)
        checkAssetType(_assetData)
        returns
        (bool success)
    {
        bytes32 swapHash = getSwapHash(
            msg.sender, address(0), _oracle, keccak256(_assetData), _paymentDetailsHash
        );
        requireSwapNotExists(swapHash);
        require(ecrecover(swapHash, v, r, s) == _oracle, "invalid swap oracle signature");
        // Set up swap status before transfer, to avoid reentrancy attacks.
        // Even if a malicious token is somehow passed to this function (despite the oracle
        // signature of its details), the state of this contract is already fully updated,
        // so it will behave correctly (as it would be a separate call).
        swaps[swapHash] = SWAP_UNCLAIMED;
        require(
            lockAssetFrom(_assetData, msg.sender),
            "failed to lock asset on escrow"
        );
        emit Created(swapHash);
        return true;
    }

    /**
     * Swap claim, called by the swap's oracle on behalf of the receiver, to confirm his interest
     * in buying the crypto asset.
     * Additional v, r, s parameters are checked to be the receiver's EIP712 typed data signature
     * of the swap's details and a 'claim this swap' action -- which verifies the receiver's address
     * and the authenthicity of his claim request. See `getClaimTypedHash` for description of the
     * signed swap struct.
     *
     * Emits a `Claimed` event with the current swap hash and the new swap hash, updated with
     * receiver's address. The current swap hash equal to the hash emitted in `create`, unless
     * `setBuyer` was called in the meantime -- then the current swap hash is equal to the new
     * swap hash, because the receiver's address was already set.
     */
    function claim(
        address _sender,
        address _receiver,
        address _oracle,
        bytes calldata _assetData,
        bytes32 _paymentDetailsHash,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external statusAtLeast(Status.ACTIVE) onlyOracle(_oracle) {
        // Verify claim signature
        bytes32 claimTypedHash = getClaimTypedHash(
            _sender,
            _receiver,
            _assetData,
            _paymentDetailsHash
        );
        require(ecrecover(claimTypedHash, v, r, s) == _receiver, "invalid claim receiver signature");
        // Verify swap hashes
        bytes32 oldSwapHash = getSwapHash(
            _sender, address(0), _oracle, keccak256(_assetData), _paymentDetailsHash
        );
        bytes32 newSwapHash = getSwapHash(
            _sender, _receiver, _oracle, keccak256(_assetData), _paymentDetailsHash
        );
        bytes32 claimFromHash;
        // We want this function to be universal, regardless of whether `setBuyer` was called before.
        // If it was, the hash is already changed
        if (swaps[oldSwapHash] == 0) {
            claimFromHash = newSwapHash;
            requireSwapUnclaimed(newSwapHash);
        } else {
            claimFromHash = oldSwapHash;
            requireSwapUnclaimed(oldSwapHash);
            requireSwapNotExists(newSwapHash);
            swaps[oldSwapHash] = 0;
        }
        // any overflow security warnings can be safely ignored -- SWAP_LOCK_TIME_S is a small
        // constant, so this won't overflow an uint32 until year 2106
        // solium-disable-next-line security/no-block-members
        swaps[newSwapHash] = uint32(block.timestamp) + SWAP_LOCK_TIME_S;
        emit Claimed(claimFromHash, newSwapHash);
    }

    /**
     * Swap release, which transfers the crypto asset to the receiver and removes the swap from
     * the active swap mapping. Normally called by the swap's oracle after it confirms a matching
     * wire transfer on sender's bank account. Can be also called by the sender, for example in case
     * of a dispute, when the parties reach an agreement off-chain.
     *
     * Emits a `Released` event with the swap's hash.
     */
    function release(
        address _sender,
        address payable _receiver,
        address _oracle,
        bytes calldata _assetData,
        bytes32 _paymentDetailsHash
    ) external statusAtLeast(Status.FINALIZE_ONLY) onlyOracleOrSender(_sender, _oracle) {
        bytes32 swapHash = getSwapHash(
            _sender, _receiver, _oracle, keccak256(_assetData), _paymentDetailsHash
        );
        requireSwapClaimed(swapHash);
        // Delete the swap status before transfer, to avoid reentrancy attacks.
        swaps[swapHash] = 0;
        require(
            sendAssetTo(_assetData, _receiver),
            "failed to send asset to receiver"
        );
        if (msg.sender == _sender) {
            emit SenderReleased(swapHash);
        } else {
            emit Released(swapHash);
        }
    }

    /**
     * Swap return, which transfers the crypto asset back to the sender and removes the swap from
     * the active swap mapping. Can be called by the sender or the swap's oracle, but only if the
     * swap is not claimed, or was claimed but the escrow lock time expired.
     *
     * Emits a `Returned` event with the swap's hash.
     */
    function returnFunds(
        address payable _sender,
        address _receiver,
        address _oracle,
        bytes calldata _assetData,
        bytes32 _paymentDetailsHash
    ) external statusAtLeast(Status.RETURN_ONLY) onlyOracleOrSender(_sender, _oracle) {
        bytes32 swapHash = getSwapHash(
            _sender, _receiver, _oracle, keccak256(_assetData), _paymentDetailsHash
        );
        requireSwapUnclaimedOrExpired(swapHash);
        // Delete the swap status before transfer, to avoid reentrancy attacks.
        swaps[swapHash] = 0;
        require(
            sendAssetTo(_assetData, _sender),
            "failed to send asset to sender"
        );
        if (msg.sender == _sender) {
            emit SenderReturned(swapHash);
        } else {
            emit Returned(swapHash);
        }
    }

    /**
     * After the sender creates a swap, he can optionally call this function to restrict the swap
     * to a particular receiver address. The swap can't then be claimed by any other receiver.
     *
     * Emits a `BuyerSet` event with the created swap hash and new swap hash, updated with
     * receiver's address.
     */
    function setBuyer(
        address _sender,
        address _receiver,
        address _oracle,
        bytes calldata _assetData,
        bytes32 _paymentDetailsHash
    ) external statusAtLeast(Status.ACTIVE) onlySender(_sender) {
        bytes32 assetHash = keccak256(_assetData);
        bytes32 oldSwapHash = getSwapHash(
            _sender, address(0), _oracle, assetHash, _paymentDetailsHash
        );
        requireSwapUnclaimed(oldSwapHash);
        bytes32 newSwapHash = getSwapHash(
            _sender, _receiver, _oracle, assetHash, _paymentDetailsHash
        );
        requireSwapNotExists(newSwapHash);
        swaps[oldSwapHash] = 0;
        swaps[newSwapHash] = SWAP_UNCLAIMED;
        emit BuyerSet(oldSwapHash, newSwapHash);
    }

    /**
     * Given all valid swap details, returns its status. To check a swap with unset buyer,
     * use `0x0` as the `_receiver` address. The return can be:
     * 0: the swap details are invalid, swap doesn't exist, or was already released/returned.
     * 1: the swap was created, and is not claimed yet.
     * >1: the swap was claimed, and the value is a timestamp indicating end of its lock time.
     */
    function getSwapStatus(
        address _sender,
        address _receiver,
        address _oracle,
        bytes calldata _assetData,
        bytes32 _paymentDetailsHash
    ) external view returns (uint32 status) {
        bytes32 swapHash = getSwapHash(
            _sender, _receiver, _oracle, keccak256(_assetData), _paymentDetailsHash
        );
        return swaps[swapHash];
    }

    /**
     * Calculates the swap hash used to reference the swap in this contract's storage.
     */
    function getSwapHash(
        address _sender,
        address _receiver,
        address _oracle,
        bytes32 assetHash,
        bytes32 _paymentDetailsHash
    ) internal pure returns (bytes32 hash) {
        return keccak256(
            abi.encodePacked(
                _sender, _receiver, _oracle, assetHash, _paymentDetailsHash
            )
        );
    }

    /**
     * Returns the EIP712 typed hash for the struct:
     * EIP712<Type>Swap {
     *   action: bytes32;
     *   sender: address;
     *   receiver: address;
     *   asset: asset data struct, see `getAssetTypedHash` in specific AssetAdapter contracts
     *   paymentDetailsHash: bytes32;
     * }
     */
    function getClaimTypedHash(
        address _sender,
        address _receiver,
        bytes memory _assetData,
        bytes32 _paymentDetailsHash
    ) internal view returns(bytes32 msgHash) {
        bytes32 dataHash = keccak256(
            abi.encode(
                EIP712_SWAP_TYPEHASH,
                bytes32("claim this swap"),
                _sender,
                _receiver,
                getAssetTypedHash(_assetData),
                _paymentDetailsHash
            )
        );
        return keccak256(abi.encodePacked(bytes2(0x1901), EIP712_DOMAIN_HASH, dataHash));
    }

    function requireSwapNotExists(bytes32 swapHash) internal view {
        require(swaps[swapHash] == 0, "swap already exists");
    }

    function requireSwapUnclaimed(bytes32 swapHash) internal view {
        require(swaps[swapHash] == SWAP_UNCLAIMED, "swap already claimed or invalid");
    }

    function requireSwapClaimed(bytes32 swapHash) internal view {
        require(swaps[swapHash] > MIN_ACTUAL_TIMESTAMP, "swap unclaimed or invalid");
    }

    function requireSwapUnclaimedOrExpired(bytes32 swapHash) internal view {
        require(
            // solium-disable-next-line security/no-block-members
            (swaps[swapHash] > MIN_ACTUAL_TIMESTAMP && block.timestamp > swaps[swapHash]) ||
                swaps[swapHash] == SWAP_UNCLAIMED,
            "swap not expired or invalid"
        );
    }

}
/**
 * The main contract managing Ramp Swaps escrows lifecycle: create, claim, release and return.
 * Uses an abstract AssetAdapter to carry out the transfers and handle the particular asset data.
 * With a corresponding off-chain protocol allows for atomic-swap-like transfer between
 * fiat currencies and crypto assets.
 *
 * @dev an active swap is represented by a hash of its details, mapped to its escrow expiration
 * timestamp. When the swap is created, but not yet claimed, its end time is set to SWAP_UNCLAIMED.
 * The hashed swap details are:
 *  * address sender: the swap's creator, that sells the crypto asset;
 *  * address receiver: the user that buys the crypto asset, `0x0` until the swap is claimed;
 *  * address oracle: address of the oracle that handles this particular swap;
 *  * bytes assetData: description of the crypto asset, handled by an AssetAdapter;
 *  * bytes32 paymentDetailsHash: hash of the fiat payment details: account numbers, fiat value
 *    and currency, and the transfer reference (title), that can be verified off-chain.
 *
 * @author Ramp Network sp. z o.o.
 */
function getSwapHash(
    address _sender,
    address _receiver,
    address _oracle,
    bytes32 assetHash,
    bytes32 _paymentDetailsHash
) internal pure returns (bytes32 hash) {
    return keccak256(
        abi.encodePacked(
            _sender, _receiver, _oracle, assetHash, _paymentDetailsHash
        )
    );
}

/**
 * Calculates the swap hash used to reference the swap in this contract's storage.
 */