Esempio n. 1
0
    def _is_valid(self, submission_data: Dict[str, any]) -> bool:
        # lookup the tx hash in secret20, and validate it.
        self.logger.info(f"Testing validity of {submission_data}")
        nonce = submission_data['nonce']
        token = submission_data['token']

        # todo: validate fee

        try:
            if token == '0x0000000000000000000000000000000000000000':
                self.logger.info("Testing secret-ETH to ETH swap")
                swap = query_scrt_swap(nonce, self.config["scrt_swap_address"],
                                       self.token_map['native'].address)
            else:
                self.logger.info(
                    f"Testing {self.token_map[token].address} to {token} swap")
                swap = query_scrt_swap(nonce, self.config["scrt_swap_address"],
                                       self.token_map[token].address)
        except subprocess.CalledProcessError as e:
            self.logger.error(f'Error querying transaction: {e}')
            raise RuntimeError from None

        try:
            swap_data = swap_query_res(swap)
            self.logger.debug(f'Parsing swap info: {swap_data}')
        except (AttributeError, JSONDecodeError) as e:
            raise ValueError from e
        if self._validate_tx_data(swap_data, submission_data):
            self.logger.info('Validated successfully')
            return True
        self.logger.info('Failed to validate')
        return False
Esempio n. 2
0
    def _handle_swap(self, swap_data: str, src_token: str, dst_token: str, retry=False):
        swap_json = swap_query_res(swap_data)
        # this is an id, and not the TX hash since we don't actually know where the TX happened, only the id of the
        # swap reported by the contract
        swap_id = get_swap_id(swap_json)
        dest_address = swap_json['destination']
        self.logger.debug(f'{swap_json}')
        amount = int(swap_json['amount'])

        swap_failed = False
        fee = 0
        data = b''
        nonce = int(swap_json['nonce'])
        swap = None

        try:
            if dst_token == 'native':
                data, tx_dest, tx_amount, tx_token, fee = self._tx_native_params(amount, dest_address, retry)
            else:
                self.erc20.address = dst_token
                data, tx_dest, tx_amount, tx_token, fee = self._tx_erc20_params(amount, dest_address, dst_token, retry)

            if retry:

                original_nonce = nonce
                nonce = int(self.multisig_wallet.get_token_nonce(swap_retry_address) + 1)  # + 1 to advance the counter

                swap = Swap.objects.get(src_tx_hash=swap_id)
                swap.status = Status.SWAP_FAILED

                self.update_retry_db(f"{original_nonce}|{tx_token}", f"{nonce}|{swap_retry_address.lower()}")

                tx_token = w3.toChecksumAddress(swap_retry_address)

            msg = message.Submit(w3.toChecksumAddress(tx_dest),
                                 tx_amount,  # if we are swapping token, no ether should be rewarded
                                 nonce,
                                 tx_token,
                                 fee,
                                 data)

        except ValueError as e:
            self.logger.error(f"Error: {e}")
            swap_failed = True

        # this could have already been set by retry
        if not swap:
            swap = Swap(src_network="Secret", src_tx_hash=swap_id, unsigned_tx=data, src_coin=src_token,
                        dst_coin=dst_token, dst_address=dest_address, amount=str(amount), dst_network="Ethereum",
                        status=Status.SWAP_FAILED)

        if swap_failed or not self._validate_fee(amount, fee):
            self._save_failed_swap(swap, swap_id)
        else:
            self._broadcast_and_save(msg, swap, swap_id)
Esempio n. 3
0
    def _is_valid(self, submission_data: Dict[str, any]) -> bool:
        # lookup the tx hash in secret20, and validate it.
        nonce = submission_data['nonce']
        swap = query_scrt_swap(nonce, self.config['secret_swap_contract_address'])

        try:
            swap_data = swap_query_res(swap)
        except (AttributeError, JSONDecodeError) as e:
            raise ValueError from e
        if self._validate_tx_data(swap_data, submission_data):
            return True
        return False
Esempio n. 4
0
    def _handle_swap(self, swap_data: str):
        # Note: This operation costs Ethr
        # disabling this for now till I get a feeling for what can fail
        # try:
        swap_json = swap_query_res(swap_data)

        data = b""

        dest_address = base64.b64decode(swap_json['destination']).decode()

        msg = message.Submit(dest_address, int(swap_json['amount']), int(swap_json['nonce']), data)

        self._broadcast_transaction(msg)
Esempio n. 5
0
    def _handle_swap(self, swap_data: str):
        # Note: This operation costs Ether
        # disabling this for now till I get a feeling for what can fail
        # try:
        swap_json = swap_query_res(swap_data)

        # Note: if token is swapped, the destination function name HAS to have the following signature
        # transfer(address to, uint256 value)
        data = self.token_contract.transaction_raw_bytes('transfer',
                                                         swap_json['destination'],
                                                         int(swap_json['amount']),
                                                         b"")
        msg = message.Submit(self.token_contract.address,
                             0,  # if we are swapping token, no ether should be rewarded
                             int(swap_json['nonce']),
                             data)

        self._broadcast_transaction(msg)
Esempio n. 6
0
    def _is_valid(self, submission_data: Dict[str, any]) -> bool:
        # lookup the tx hash in secret20, and validate it.
        self.logger.info(f"Testing validity of {submission_data}")
        nonce = submission_data['nonce']
        token = submission_data['token']

        try:
            swap = query_scrt_swap(nonce, self.config.scrt_swap_address,
                                   self._eth_to_scrt(token))
        except subprocess.CalledProcessError as e:
            self.logger.error(f'Error querying transaction: {e}')
            raise RuntimeError from None

        try:
            swap_data = swap_query_res(swap)
            self.logger.debug(f'Parsing swap info: {swap_data}')
        except (AttributeError, JSONDecodeError) as e:
            raise ValueError from e
        if self._validate_tx_data(swap_data, submission_data):
            self.logger.info('Validated successfully')
            return True
        self.logger.info('Failed to validate')
        return False
Esempio n. 7
0
    def _handle_swap(self, swap_data: str, src_token: str, dst_token: str):
        swap_json = swap_query_res(swap_data)
        # this is an id, and not the TX hash since we don't actually know where the TX happened, only the id of the
        # swap reported by the contract
        swap_id = get_swap_id(swap_json)
        dest_address = swap_json['destination']
        self.logger.info(f'{swap_json}')
        amount = int(swap_json['amount'])

        if dst_token == 'native':
            data, tx_dest, tx_amount, tx_token, fee = self._tx_native_params(
                amount, dest_address)
        else:
            self.erc20.address = dst_token
            data, tx_dest, tx_amount, tx_token, fee = self._tx_erc20_params(
                amount, dest_address, dst_token)

        if not self._validate_fee(amount, fee):
            self.logger.error("Tried to swap an amount too low to cover fee")
            swap = Swap(src_network="Secret",
                        src_tx_hash=swap_id,
                        unsigned_tx=data,
                        src_coin=src_token,
                        dst_coin=dst_token,
                        dst_address=dest_address,
                        amount=str(amount),
                        dst_network="Ethereum",
                        status=Status.SWAP_FAILED)
            try:
                swap.save()
            except (DuplicateKeyError, NotUniqueError):
                pass
            return

        msg = message.Submit(
            tx_dest,
            tx_amount,  # if we are swapping token, no ether should be rewarded
            int(swap_json['nonce']),
            tx_token,
            fee,
            data)
        # todo: check we have enough ETH
        swap = Swap(src_network="Secret",
                    src_tx_hash=swap_id,
                    unsigned_tx=data,
                    src_coin=src_token,
                    dst_coin=dst_token,
                    dst_address=dest_address,
                    amount=str(amount),
                    dst_network="Ethereum",
                    status=Status.SWAP_FAILED)
        try:
            tx_hash = self._broadcast_transaction(msg)
            swap.dst_tx_hash = tx_hash
            swap.status = Status.SWAP_SUBMITTED
        except (ValueError, TransactionNotFound) as e:
            self.logger.critical(
                f"Failed to broadcast transaction for msg {repr(msg)}: {e}")
        finally:
            try:
                swap.save()
            except (DuplicateKeyError, NotUniqueError):
                pass