Exemplo n.º 1
0
    async def test_get_transactions(self):
        """
        Test for get transactions
        """
        self.wallet.create_wallet()
        tx = Transaction(
            hash="0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060",
            date_time=datetime(2015, 8, 7, 3, 30, 33),
            from_=self.wallet.get_address().result(),
            to="0x5df9b87991262f6ba471f09758cde1c0fc1de734",
            gas=5,
            gas_price=5,
            nonce=0,
            is_pending=False,
            value=31337,
            block_number=46147
        )
        mock_provider = MockObject()
        mock_provider.get_latest_blocknr = lambda *_: 46147
        mock_provider.get_transactions = lambda *_, **x: [tx]
        self.wallet.provider = mock_provider

        transactions = await self.wallet.get_transactions()
        tx_dict = {
            'id': tx.hash,
            'outgoing': True,
            'from': self.wallet.get_address().result(),
            'to': "0x5df9b87991262f6ba471f09758cde1c0fc1de734",
            'amount': 31337,
            'fee_amount': 25,
            'currency': self.identifier,
            'timestamp': time.mktime(datetime(2015, 8, 7, 3, 30, 33).timetuple()),
            'description': f'Confirmations: {1}'
        }
        self.assertEqual([tx_dict], transactions)
Exemplo n.º 2
0
    async def transfer(self, amount, address) -> str:
        """
        Transfer Ethereum to another wallet.
        If the amount exceeds the available balance, an `InsufficientFunds` exception is raised.

        :param amount: the transfer amount
        :param address: the receiver address
        :return: transfer hash
        """
        balance = await self.get_balance()

        if balance['available'] < int(amount):
            raise InsufficientFunds('Insufficient funds')

        self._logger.info(
            'Creating Ethereum payment with amount %f to address %s', amount,
            address)

        transaction = {
            'from':
            self.get_address().result(),
            'to':
            Web3.toChecksumAddress(
                address),  # addresses should be checksumaddresses to work
            'value':
            int(amount),
            'nonce':
            self.database.get_transaction_count(self.get_address().result()),
            'gasPrice':
            self.provider.get_gas_price(),
            'chainId':
            self.chain_id
        }

        transaction['gas'] = self.provider.estimate_gas()
        # submit to blockchain
        signed = self.account.sign_transaction(transaction)
        self.provider.submit_transaction(signed['rawTransaction'].hex())

        # add transaction to database
        self.database.add(
            Transaction(from_=transaction['from'],
                        to=transaction['to'],
                        value=transaction['value'],
                        gas=transaction['gas'],
                        nonce=transaction['nonce'],
                        gas_price=transaction['gasPrice'],
                        hash=signed['hash'].hex(),
                        is_pending=True,
                        token_identifier=self.get_identifier()))
        return signed['hash'].hex()
Exemplo n.º 3
0
 def test_get_transactions(self):
     # 4 txs because get_transactions calls get_transactions_received and they both request confirmed transactions
     # and unconfirmed transactions
     txs = [
         Transaction(
             hash=
             '0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060',
             date_time=datetime(2015, 8, 7, 3, 30, 33),
             from_='0xa1e4380a3b1f749673e270229993ee55f35663b4',
             to='0x5df9b87991262f6ba471f09758cde1c0fc1de734',
             gas=21000,
             gas_price=50000000000000,
             nonce=0,
             is_pending=False,
             value=31337,
             block_number=46147),
         Transaction(
             hash=
             '0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060',
             date_time=datetime(2015, 8, 7, 3, 30, 33),
             from_='0xa1e4380a3b1f749673e270229993ee55f35663b4',
             to='0x5df9b87991262f6ba471f09758cde1c0fc1de734',
             gas=21000,
             gas_price=50000000000000,
             nonce=0,
             is_pending=False,
             value=31337,
             block_number=46147),
     ]
     responses.add(responses.GET,
                   f'{self.bcp.base_url}/transactions',
                   json=self.sample_transactions_response)
     self.assertEqual(
         txs,
         self.bcp.get_transactions(
             '0xa1e4380a3b1f749673e270229993ee55f35663b4'))
Exemplo n.º 4
0
    def _normalize_transaction(self, tx) -> Transaction:
        """
        Turns the tx from blockchair into the tx format of the wallet.
        :param tx: Tx from blockchair
        :return: Transaction object
        """

        return Transaction(block_number=tx['block_id'],
                           hash=tx['hash'],
                           date_time=datetime.fromisoformat(tx['time']),
                           to=tx['recipient'],
                           from_=tx['sender'],
                           value=tx['value'],
                           gas_price=tx['gas_price'],
                           gas=tx['gas_used'],
                           nonce=tx['nonce'],
                           is_pending=tx['block_id'] is None)
Exemplo n.º 5
0
    def _normalize_transaction(self, tx) -> Transaction:
        """
        Turns the tx from etherscan into the tx format of the wallet.
        :param tx: Tx from etherscan
        :return: Transaction object
        """

        return Transaction(
            block_number=tx['blockNumber'],
            hash=tx['hash'],
            date_time=datetime.utcfromtimestamp(int(tx['timeStamp'])),
            to=tx['to'],
            from_=tx['from'],
            value=tx['value'],
            gas_price=tx['gasPrice'],
            gas=tx['gasUsed'],
            nonce=tx['nonce'],
            is_pending=False  # etherscan transactions are allways confirmed
        )
Exemplo n.º 6
0
 def test_get_transactions(self):
     """
     Test for get transactions
     """
     correct_tx = [
         Transaction(
             block_number=10116177,
             hash=
             '0x7688616385aeca4362e30eea9a46ab10ec18259793d1db969c527ada84c73a88',
             from_='0x4d52d092b1c54f29c33ff2a2290e51849c3cf020',
             to='0x1120987bb7e25816ac01c74e70f643b28adf341c',
             value=988000000000000000,
             gas=21000,
             gas_price=40000000000,
             is_pending=False,
             date_time=datetime.utcfromtimestamp(1590157996))
     ]
     responses.add(responses.GET,
                   f'{self.esp.base_url}',
                   json=self.sample_transactions_response)
     tx = self.esp.get_transactions(
         '0x1120987bb7e25816aC01c74e70f643b28AdF341C')
     self.assertEqual(correct_tx, tx)
Exemplo n.º 7
0
    async def transfer(self, amount, address):
        balance = await self.get_balance()
        if balance['available'] < int(amount):
            raise InsufficientFunds('Insufficient funds')

        self._logger.info(
            'Creating Ethereum Token (%s) payment with amount %f to address %s',
            self.get_name(), amount, address)
        tx = self.contract.functions.transfer(
            Web3.toChecksumAddress(address), amount).buildTransaction({
                'gas':
                self.provider.estimate_gas(),
                'gasPrice':
                self.provider.get_gas_price(),
                'chainId':
                self.chain_id
            })
        tx.update({
            'nonce':
            self.database.get_transaction_count(self.get_address().result())
        })
        s_tx = self.account.sign_transaction(tx)

        # add transaction to database
        self.database.add(
            Transaction(from_=self.get_address().result(),
                        to=address,
                        value=amount,
                        gas=tx['gas'],
                        nonce=tx['nonce'],
                        gas_price=tx['gasPrice'],
                        hash=s_tx['hash'].hex(),
                        is_pending=True,
                        token_identifier=self.get_identifier()))

        return self.provider.submit_transaction(s_tx['rawTransaction'].hex())