Beispiel #1
0
    def _swap_close(self, context, signer_pubkey, swap_close_payload):
        """
        Bob or Alice closes the swap by providing the secret key which matches secret lock.
        Requires "is_approved = True"
        Requires hash of secret key to match secret lock

        """
        swap_info = self.get_swap_info_from_swap_id(context,
                                                    swap_close_payload.swap_id)

        if not swap_info.secret_lock:
            raise InvalidTransaction(
                'Secret lock is required to close the swap!')

        if web3_hash(swap_close_payload.secret_key) != swap_info.secret_lock:
            raise InvalidTransaction(
                'Secret key doesn\'t match specified secret lock!')

        if swap_info.is_initiator and swap_info.state != AtomicSwapInfo.APPROVED:
            raise InvalidTransaction(
                'Transaction cannot be closed before it\'s approved.')

        transfer_payload = AccountClient.get_transfer_payload(
            swap_info.receiver_address, swap_info.amount)
        token_updated_state = AccountHandler()._transfer_from_address(
            context, ZERO_ADDRESS, transfer_payload)
        swap_info.state = AtomicSwapInfo.CLOSED

        return {**self.get_state_update(swap_info), **token_updated_state}
Beispiel #2
0
    def _swap_close(self, context, signer_pubkey, swap_close_payload):
        """
        Close atomic swap.

        Any party (Bob or Alice) can close atomic swap by providing secret key. If hash from secret key matches
        secret lock, secret key is valid. Closing atomic swap means participant (not initiator)
        get REMchain tokens instead ERC20 tokens.

        Closing requires atomic swap to be approved.
        """
        swap_identifier = swap_close_payload.swap_id

        address_swap_info_is_stored_by = self.make_address_from_data(swap_identifier)
        swap_information = get_data(context, AtomicSwapInfo, address_swap_info_is_stored_by)

        if not swap_information:
            raise InvalidTransaction(f'Atomic swap was not initiated for identifier {swap_identifier}!')

        if swap_information.state in NOT_PERMITTED_TO_CHANGE_SWAP_STATUSES:
            raise InvalidTransaction(
                f'No operations can be done upon the swap: {swap_identifier} as it is already closed or expired.',
            )

        if not swap_information.secret_lock:
            raise InvalidTransaction('Secret lock is required to close the swap.')

        if web3_hash(swap_close_payload.secret_key) != swap_information.secret_lock:
            raise InvalidTransaction('Secret key doesn\'t match specified secret lock.')

        if swap_information.is_initiator and swap_information.state != AtomicSwapInfo.APPROVED:
            raise InvalidTransaction('Transaction cannot be closed before it\'s approved.')

        account = get_data(context, Account, swap_information.receiver_address)
        if account is None:
            account = Account()
        account.balance += swap_information.amount

        swap_information.secret_key = swap_close_payload.secret_key
        swap_information.state = AtomicSwapInfo.CLOSED

        return {
            address_swap_info_is_stored_by: swap_information,
            swap_information.receiver_address: account,
        }
Beispiel #3
0
    SETTINGS_KEY_ZERO_ADDRESS_OWNERS,
    ZERO_ADDRESS,
)

TOKENS_AMOUNT_TO_SWAP = 200

BOT_ADDRESS = '112007b9433e1da5c624ff926477141abedfd57585a36590b0a8edc4104ef28093ee30'
BOT_PRIVATE_KEY = '1cb15ecfe1b3dc02df0003ac396037f85b98cf9f99b0beae000dc5e9e8b6dab4'
BOT_PUBLIC_KEY = '03ecc5cb4094eb05319be6c7a63ebf17133d4ffaea48cdcfd1d5fc79dac7db7b6b'

ALICE_PRIVATE_KEY = '8c87d914a6cfeaf027413760ad359b5a56bfe0eda504d879b21872c7dc5b911c'
ALICE_PUBLIC_KEY = '02feb988591c78e58e57cdce5a314bd04798971227fcc2316907355392a2c99c25'
ALICE_ADDRESS = '112007db8a00c010402e2e3a7d03491323e761e0ea612481c518605648ceeb5ed454f7'

SECRET_KEY = '3e0b064c97247732a3b345ce7b2a835d928623cb2871c26db4c2539a38e61a16'
SECRET_LOCK = web3_hash(SECRET_KEY)

SWAP_ID = '033102e41346242476b15a3a7966eb5249271025fc7fb0b37ed3fdb4bcce3884'

ADDRESS_TO_GET_GENESIS_MEMBERS_AS_STRING_BY = _make_settings_key(
    SETTINGS_KEY_ZERO_ADDRESS_OWNERS)
ADDRESS_TO_STORE_SWAP_INFO_BY = BasicHandler(
    name=AtomicSwapHandler().family_name,
    versions=AtomicSwapHandler()._family_versions[0]).make_address_from_data(
        data=SWAP_ID)

TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS = {
    'family_name': AtomicSwapHandler().family_name,
    'family_version': AtomicSwapHandler()._family_versions[0],
}