def test_wallet_balance_selection(self, patched_fee): ETH = ETHFactory() fee_amount = EthereumTokenAmount(amount=Decimal("0.001"), currency=ETH) patched_fee.return_value = fee_amount token = Erc20TokenFactory() token_amount = EthereumTokenAmount(amount=Decimal("10"), currency=token) add_eth_to_wallet(self.wallet, fee_amount) add_token_to_wallet(self.wallet, token_amount) self.assertIsNotNone(Wallet.select_for_transfer(token_amount)) self.assertIsNone(Wallet.select_for_transfer(2 * fee_amount))
def select_for_transfer( cls, transfer_amount: EthereumTokenAmount) -> Optional[Wallet_T]: max_fee_amount: EthereumTokenAmount = get_max_fee() assert max_fee_amount.is_ETH ETH = max_fee_amount.currency eth_required = max_fee_amount token_required = EthereumTokenAmount(amount=transfer_amount.amount, currency=transfer_amount.currency) wallets = cls.objects.all() if transfer_amount.is_ETH: token_required += eth_required funded_wallets = [ w for w in wallets if w.get_balance(ETH) >= token_required ] else: funded_wallets = [ wallet for wallet in wallets if wallet.get_balance(token_required.currency) >= token_required and wallet.get_balance(ETH) >= eth_required ] try: return random.choice(funded_wallets) except IndexError: return None
def on_transaction_mined_check_for_deposit(sender, **kw): tx = kw["instance"] if kw["created"]: accounts = EthereumAccount.objects.all() accounts_by_address = { account.address: account for account in accounts } if tx.to_address in accounts_by_address.keys(): ETH = EthereumToken.ETH(tx.block.chain) eth_amount = EthereumTokenAmount(amount=from_wei( tx.value, "ether"), currency=ETH) account_deposit_received.send( sender=Transaction, account=accounts_by_address[tx.to_address], transaction=tx, amount=eth_amount, )
def get_balance(self, currency: EthereumToken) -> EthereumTokenAmount: return EthereumTokenAmount.aggregated(self.user.balance_entries.all(), currency=currency)
def as_token_amount(self) -> EthereumTokenAmount: return EthereumTokenAmount(amount=self.amount, currency=self.token)
def balance_amount(self) -> EthereumTokenAmount: return EthereumTokenAmount(amount=self.balance, currency=self.token)
def setUp(self): super().setUp() self.ETH = ETHFactory() self.fee_amount = EthereumTokenAmount(amount=Decimal("0.001"), currency=self.ETH)