def approve(w3, account, allowed_address, allowance: Wei, token: Erc20Token):
    token_proxy = _make_token_proxy(w3=w3, token=token)
    old_allowance = token_proxy.functions.allowance(account.address,
                                                    allowed_address).call()

    if old_allowance > 0:
        send_raw_transaction(
            w3,
            account,
            token_proxy.functions.approve,
            allowed_address,
            0,
            gas=GAS_REQUIRED_FOR_APPROVE,
        )

    tx_hash = send_raw_transaction(
        w3,
        account,
        token_proxy.functions.approve,
        allowed_address,
        allowance,
        gas=GAS_REQUIRED_FOR_APPROVE,
    )

    wait_for_transaction(w3, tx_hash)
def approve(w3, account, allowed_address, allowance: Wei, token: Erc20Token):
    token_proxy = _make_token_proxy(w3=w3, token=token)
    old_allowance = token_proxy.functions.allowance(account.address,
                                                    allowed_address).call()
    nonce = w3.eth.getTransactionCount(account.address)

    if old_allowance > 0:
        send_raw_transaction(
            w3,
            account,
            token_proxy.functions.approve,
            allowed_address,
            0,
            gas=GAS_REQUIRED_FOR_APPROVE,
            nonce=nonce,
        )
        nonce += 1

    transaction_receipt = send_raw_transaction(
        w3,
        account,
        token_proxy.functions.approve,
        allowed_address,
        allowance,
        gas=GAS_REQUIRED_FOR_APPROVE,
        nonce=nonce,
    )

    wait_for_transaction(w3, transaction_receipt)
def deposit_service_tokens(w3: Web3, account: Account, token: Erc20Token,
                           amount: Wei):
    token_proxy = _make_token_proxy(w3=w3, token=token)
    deposit_proxy = _make_deposit_proxy(w3=w3, token=token)

    current_deposit_amount = TokenAmount(
        Wei(deposit_proxy.functions.total_deposit(account.address).call()),
        token)

    new_deposit_amount = TokenAmount(amount, token)

    total_deposit = current_deposit_amount + new_deposit_amount

    send_raw_transaction(
        w3,
        account,
        token_proxy.functions.approve,
        deposit_proxy.address,
        total_deposit.as_wei,
        gas=GAS_REQUIRED_FOR_APPROVE,
    )

    return send_raw_transaction(
        w3,
        account,
        deposit_proxy.functions.deposit,
        account.address,
        total_deposit.as_wei,
        gas=GAS_REQUIRED_FOR_DEPOSIT,
    )
    def buy_tokens(self, account: Account, token_amount: TokenAmount):
        costs = self.calculate_transaction_costs(token_amount, account)

        if costs is None:
            raise ExchangeError("Failed to get transaction costs")

        exchange_proxy = self._get_exchange_proxy(token_amount.ticker)
        latest_block = self.w3.eth.getBlock("latest")
        deadline = latest_block.timestamp + self.EXCHANGE_TIMEOUT
        gas = costs["gas"]
        gas_price = costs["gas_price"]
        transaction_params = {
            "from": account.address,
            "value": costs["total"].as_wei,
            "gas": 2 * gas,  # estimated gas sometimes is not enough
            "gas_price": gas_price.as_wei,
        }

        return send_raw_transaction(
            self.w3,
            account,
            exchange_proxy.functions.ethToTokenSwapOutput,
            token_amount.as_wei,
            deadline,
            **transaction_params,
        )
    def buy_tokens(self, account: Account, token_amount: TokenAmount):
        if self.network.name not in self.SUPPORTED_NETWORKS:
            raise ExchangeError(
                f"{self.name} does not list {token_amount.ticker} on {self.network.name}"
            )

        transaction_costs = self.calculate_transaction_costs(token_amount, account)
        if transaction_costs is None:
            raise ExchangeError("Failed to get transactions costs")

        eth_to_sell = transaction_costs["eth_sold"]
        exchange_rate = transaction_costs["exchange_rate"]
        gas_price = transaction_costs["gas_price"]
        gas = transaction_costs["gas"]

        transaction_params = {
            "from": account.address,
            "gas_price": gas_price.as_wei,
            "gas": gas,
            "value": eth_to_sell.as_wei,
        }

        return send_raw_transaction(
            self.w3,
            account,
            self.network_contract_proxy.functions.swapEtherToToken,
            self.get_token_network_address(token_amount.ticker),
            exchange_rate.as_wei,
            **transaction_params,
        )
Beispiel #6
0
def removeLiquidity(w3, account, router_proxy):
    pair_address = to_canonical_address(get_pair_address(w3, router_proxy))
    liquidity_token = Erc20Token(
        ticker="UNI-V2",
        wei_ticker="UNI-V2 WEI",
        address=pair_address
    )
    amount = get_token_balance(w3, account, liquidity_token)

    approve(w3, account, router_proxy.address, amount.as_wei, liquidity_token)

    min_eth = 1
    min_tokens = 1
    deadline = generateDeadline(w3)
    transaction_params = {
        "from": account.address,
        "gas_price": w3.eth.generateGasPrice(),
        "gas": GAS_LIMIT,
    }

    tx_hash = send_raw_transaction(
        w3,
        account,
        router_proxy.functions.removeLiquidityETH,
        WIZ_TOKEN.address,
        amount.as_wei,
        min_tokens,
        min_eth,
        account.address,
        deadline,
        **transaction_params,
    )
    wait_for_transaction(w3, tx_hash)
Beispiel #7
0
def addLiquidity(w3, account, router_proxy, current_rate):
    max_tokens = get_token_balance(w3, account, WIZ_TOKEN)

    approve(w3, account, router_proxy.address, max_tokens.as_wei, WIZ_TOKEN)

    deadline = generateDeadline(w3)
    eth_amount = EthereumAmount(0.001)
    token_amount_desired = TokenAmount(eth_amount.value / current_rate.value, WIZ_TOKEN)
    transaction_params = {
        "from": account.address,
        "value": eth_amount.as_wei,
        "gas_price": w3.eth.generateGasPrice(),
        "gas": GAS_LIMIT,
    }

    tx_hash = send_raw_transaction(
        w3,
        account,
        router_proxy.functions.addLiquidityETH,
        WIZ_TOKEN.address,
        token_amount_desired.as_wei,
        int(token_amount_desired.as_wei * 0.8),
        int(eth_amount.as_wei * 0.8),
        account.address,
        deadline,
        **transaction_params,
    )
    wait_for_transaction(w3, tx_hash)
Beispiel #8
0
def deposit_service_tokens(w3: Web3, account: Account, token: Erc20Token,
                           amount: Wei):
    token_proxy = _make_token_proxy(w3=w3, token=token)
    deposit_proxy = _make_deposit_proxy(w3=w3, token=token)

    current_deposit_amount = TokenAmount(
        Wei(deposit_proxy.functions.total_deposit(account.address).call()),
        token)

    gas_price = Wei(int(w3.eth.generateGasPrice() * GAS_PRICE_MARGIN))
    new_deposit_amount = TokenAmount(amount, token)
    total_deposit = current_deposit_amount + new_deposit_amount
    nonce = w3.eth.getTransactionCount(account.address)

    send_raw_transaction(
        w3,
        account,
        token_proxy.functions.approve,
        deposit_proxy.address,
        0,
        gas=GAS_REQUIRED_FOR_APPROVE,
        gas_price=gas_price,
        nonce=nonce,
    )

    send_raw_transaction(
        w3,
        account,
        token_proxy.functions.approve,
        deposit_proxy.address,
        total_deposit.as_wei,
        gas=GAS_REQUIRED_FOR_APPROVE,
        gas_price=gas_price,
        nonce=nonce + 1,
    )

    return send_raw_transaction(
        w3,
        account,
        deposit_proxy.functions.deposit,
        account.address,
        total_deposit.as_wei,
        gas=GAS_REQUIRED_FOR_DEPOSIT,
        gas_price=gas_price,
        nonce=nonce + 2,
    )
Beispiel #9
0
def mint_tokens(w3: Web3, account: Account, token: Erc20Token):
    contract_manager = ContractManager(contracts_precompiled_path())
    token_proxy = w3.eth.contract(
        address=to_checksum_address(token.address),
        abi=contract_manager.get_contract_abi(CONTRACT_CUSTOM_TOKEN),
    )

    return send_raw_transaction(w3,
                                account,
                                token_proxy.functions.mint,
                                token.supply,
                                gas=GAS_REQUIRED_FOR_MINT)
Beispiel #10
0
    def buy_tokens(self,
                   account: Account,
                   token_amount: TokenAmount,
                   transaction_costs=None):
        if transaction_costs is None:
            transaction_costs = dict()
        if self.network.name not in self.SUPPORTED_NETWORKS:
            raise ExchangeError(
                f"{self.name} does not list {token_amount.ticker} on {self.network.name}"
            )

        if not transaction_costs:
            transaction_costs = self.calculate_transaction_costs(
                token_amount, account)
        if transaction_costs is None:
            raise ExchangeError("Failed to get transactions costs")

        eth_to_sell = transaction_costs["eth_sold"]
        exchange_rate = transaction_costs["exchange_rate"]
        gas_price = transaction_costs["gas_price"]
        gas = transaction_costs["gas"]

        eth_address = to_checksum_address(
            kyber_tokens.get_token_network_address(self.chain_id,
                                                   TokenTicker("ETH")))

        transaction_params = {
            "from": account.address,
            "gas_price": gas_price.as_wei,
            "gas": gas,
            "value": eth_to_sell.as_wei,
        }
        return send_raw_transaction(
            self.w3,
            account,
            self.network_contract_proxy.functions.trade,
            eth_address,
            eth_to_sell.as_wei,
            self.get_token_network_address(token_amount.ticker),
            account.address,
            token_amount.as_wei,
            exchange_rate.as_wei,
            account.address,
            **transaction_params,
        )
    def _send_buy_transaction(self, account: Account, transaction_costs: dict,
                              contract_function, *args):
        eth_to_sell = transaction_costs["eth_sold"]
        gas = transaction_costs["gas"]
        gas_price = transaction_costs["gas_price"]
        transaction_params = {
            "from": account.address,
            "value": eth_to_sell.as_wei,
            "gas": gas,
            "gas_price": gas_price.as_wei,
        }

        return send_raw_transaction(
            self.w3,
            account,
            contract_function,
            *args,
            **transaction_params,
        )