Пример #1
0
def deploy_erc20(w3: Web3,
                 name: str,
                 symbol: str,
                 owner: str,
                 amount: int,
                 decimals: int = 18,
                 deployer: str = None,
                 account: LocalAccount = None) -> Contract:
    if account:
        erc20_contract = get_example_erc20_contract(w3)
        tx = erc20_contract.constructor(
            name, symbol, decimals, owner, amount).buildTransaction({
                'nonce':
                w3.eth.get_transaction_count(account.address,
                                             block_identifier='pending')
            })
        signed_tx = account.sign_transaction(tx)
        tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
    else:
        deployer = deployer or w3.eth.accounts[0]
        erc20_contract = get_example_erc20_contract(w3)
        tx_hash = erc20_contract.constructor(
            name, symbol, decimals, owner, amount).transact({'from': deployer})

    tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
    erc20_address = tx_receipt.contractAddress
    deployed_erc20 = get_example_erc20_contract(w3, erc20_address)
    assert deployed_erc20.functions.balanceOf(owner).call() == amount
    return deployed_erc20
Пример #2
0
def deploy_example_erc20(w3,
                         amount: int,
                         owner: str,
                         deployer: str = None,
                         account: LocalAccount = None):
    if account:
        erc20_contract = get_example_erc20_contract(w3)
        tx = erc20_contract.constructor(amount, owner).buildTransaction()
        if 'nonce' not in tx:
            tx['nonce'] = w3.eth.getTransactionCount(
                account.address, block_identifier='pending')
        signed_tx = account.sign_transaction(tx)
        tx_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
        tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
        erc20_address = tx_receipt.contractAddress
        deployed_erc20 = get_example_erc20_contract(w3, erc20_address)
        assert deployed_erc20.functions.balanceOf(owner).call() == amount
        return deployed_erc20

    deployer = deployer or w3.eth.accounts[0]
    erc20_contract = get_example_erc20_contract(w3)
    tx_hash = erc20_contract.constructor(amount,
                                         owner).transact({'from': deployer})
    tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
    erc20_address = tx_receipt.contractAddress
    deployed_erc20 = get_example_erc20_contract(w3, erc20_address)
    assert deployed_erc20.functions.balanceOf(owner).call() == amount
    return deployed_erc20
Пример #3
0
def send_tx(w3: Web3, tx, account: LocalAccount) -> bytes:
    tx['from'] = account.address
    if 'nonce' not in tx:
        tx['nonce'] = w3.eth.get_transaction_count(account.address,
                                                   block_identifier='pending')

    if 'gasPrice' not in tx:
        tx['gasPrice'] = w3.eth.gas_price

    if 'gas' not in tx:
        tx['gas'] = w3.eth.estimateGas(tx)
    else:
        tx['gas'] *= 2

    signed_tx = account.sign_transaction(tx)
    tx_hash = w3.eth.send_raw_transaction(bytes(signed_tx.rawTransaction))
    tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
    assert tx_receipt.status == 1, 'Error with tx %s - %s' % (tx_hash.hex(),
                                                              tx)
    return tx_hash
Пример #4
0
def send_tx(w3: Web3, tx: TxParams, account: LocalAccount) -> bytes:
    tx["from"] = account.address
    if "nonce" not in tx:
        tx["nonce"] = w3.eth.get_transaction_count(account.address,
                                                   block_identifier="pending")

    if "gasPrice" not in tx and "maxFeePerGas" not in tx:
        tx["gasPrice"] = w3.eth.gas_price

    if "gas" not in tx:
        tx["gas"] = w3.eth.estimate_gas(tx)
    else:
        tx["gas"] *= 2

    signed_tx = account.sign_transaction(tx)
    tx_hash = w3.eth.send_raw_transaction(bytes(signed_tx.rawTransaction))
    tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
    assert tx_receipt.status == 1, "Error with tx %s - %s" % (tx_hash.hex(),
                                                              tx)
    return tx_hash