def token(dbsession, web3, eth_service, eth_network_id) -> Callable: # signer, multisig, cap extra_arguments = [ web3.eth.coinbase, "0x8f480474b014ea63d4fe5e52478e833fb9e8f938", # Mikko's testnet address to_wei(6000, "ether"), 2, # 2 share per each ETH 10**18, # 1 ETH 10**18 wei ] token = Token.create_token(web3, name="Toycrowd", supply=0, symbol="TOYCROWD", owner=web3.eth.coinbase, extra_arguments=extra_arguments, contract_name="CrowdfundToken") print("Token deployed") return token
def __init__(self, value: Union[int, float, str], denomination: str): # Lookup Conversion try: wrapped_denomination = self.__denominations[denomination] except KeyError: raise self.InvalidDenomination(f'"{denomination}"') # Convert or Raise try: self.__value = currency.to_wei(number=value, unit=wrapped_denomination) except ValueError as e: raise NU.InvalidAmount( f"{value} is an invalid amount of tokens: {str(e)}")
async def get_transaction_dict(self, priv, addr_to, amount, nonce, data, subtract_fee): if data: # Pull this out. gas = self.MAX_GAS else: get_code = await self.post('eth_getCode', addr_to, 'latest') gas = 50000 if len(get_code) > 3 else self.MIN_GAS gas_price = await self.get_gas_price() amount = to_wei(amount, 'ether') if subtract_fee: fee = gas * gas_price amount -= fee return { 'from': Account.privateKeyToAccount(priv).address, 'to': addr_to, 'value': amount, 'gas': gas, 'gasPrice': gas_price, 'data': data, 'chainId': int(settings.ETH_CHAIN_ID), 'nonce': nonce }
async def get_gas_price_mock(self): return to_wei(10, 'gwei')
def test_buy_crowdfund_with_gas(dbsession: Session, eth_network_id: UUID, web3: Web3, eth_service: EthereumService, token, toycrowd, rich_withdraw_address): """Perform a crowdfunnd buy operation without giving enough gas for the transaction.""" with transaction.manager: # Create withdraw operation caccount = dbsession.query(CryptoAddressAccount).one() # Use 4 as the heurestics for block account that doesn't happen right away, but still sensible to wait for it soonish asset = toycrowd() op = caccount.withdraw(Decimal(3), asset.external_id, "Buying Toycrowd", required_confirmation_count=1) op.other_data["gas"] = 2500333 # Limit should be ~100k success_op_count, failed_op_count = eth_service.run_waiting_operations() assert failed_op_count == 0 assert success_op_count == 1 with transaction.manager: ops = list(dbsession.query(CryptoOperation).all()) assert len(ops) == 3 # Create + deposit + withdraw op = ops[-1] txid = bin_to_txid(op.txid) # This should make the tx to included in a block confirm_transaction(web3, txid) # Set op.block eth_service.run_confirmation_updates() # Grab block number where out tx is with transaction.manager: ops = list(dbsession.query(CryptoOperation).all()) op = ops[-1] block_num = op.block wallet = get_wallet(web3, rich_withdraw_address) token_events = token.get_all_events() wallet_events = wallet.get_all_events() # Confirm we got it all right receipt = web3.eth.getTransactionReceipt(txid) logs = receipt["logs"] assert logs[0]["topics"][0] == token_events["Buy"] assert logs[1]["topics"][0] == token_events["Transfer"] assert logs[2]["topics"][0] == wallet_events["Withdraw"] data = get_crowdsale_data(token) assert data["wei_raised"] == to_wei("3", "ether") # Give tx time to confirm, so all confirmations will be there for db update run required_conf = 3 wait_for_block_number(web3, block_num + required_conf + 1, timeout=60) # This should trigger incoming notification eth_service.run_listener_operations() updates, failures = eth_service.run_confirmation_updates() assert failures == 0 assert updates == 2 # 1 eth withdraw, 1 token deposit # Check our db is updated with transaction.manager: # There is now new operation to deposit tokens ops = list(dbsession.query(CryptoOperation).all()) assert len(ops) == 4 op = ops[-1] # type: CryptoOperation assert op.operation_type == CryptoOperationType.deposit assert op.state == CryptoOperationState.success assert op.amount == 6 asset = toycrowd() crypto_address = dbsession.query(CryptoAddress).one() # type: CryptoAddress caccount = crypto_address.get_account(asset) assert caccount.account.get_balance() == 6
async def get_gas_price(self) -> int: if not settings.ETH_FEE: fee_station = FeeStation('eth') transaction_fee = await fee_station.get_fee() return min(self.MAX_FEE, transaction_fee) return to_wei(int(settings.ETH_FEE), 'gwei')
"""Test token transfers between initial issuer and hosted wallet. """ import pytest # How many ETH we move for test transactiosn from decimal import Decimal from web3.contract import Contract from eth_utils.currency import to_wei from websauna.wallet.ethereum.contract import confirm_transaction, TransactionConfirmationError from websauna.wallet.ethereum.token import Token from websauna.wallet.ethereum.utils import wei_to_eth from websauna.wallet.tests.eth.utils import create_contract_listener, send_balance_to_contract CAP = to_wei(6000, "ether") MULTISIG = "0x5589C14FbC92A73809fBCfF33Ab40eFc7E8E8467" @pytest.fixture(scope="module") def token(web3, coinbase) -> Contract: """Deploy a token contract in the blockchain.""" # signer, multisig, cap extra_arguments = [ coinbase, MULTISIG, CAP, 2, # 2 share per each ETH 10**18, # 1 ETH 10**18 wei ] return Token.create_token(web3, name="Mootoken",
async def get_eth_fee(self): resp_dict = await self.get(self.ETH_GAS_PRICE_URL) average = int(resp_dict['average'] / 10) return to_wei(average, 'gwei')