首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用来自addLiquidityETH的UniswapV2?

如何使用来自addLiquidityETH的UniswapV2?
EN

Stack Overflow用户
提问于 2022-06-24 11:44:22
回答 1查看 403关注 0票数 0

我在使用Uniswap的"addLiquidityETH“函数时遇到了问题,它以前一直在工作,但现在总是说数量不够。

到目前为止,我确保有足够的令牌可供使用,批准用于同时使用令牌、池和池LP。我试图强制强制使用元问询将他们的事务数据发送到池中的硬编码数量,但是仍然失败,但是元问询没有。我用我在代码上发送的值(令牌数量和令牌路径)检查了来自元问询的事务数据输入,它们看起来是一样的,现在不知道还需要做什么检查。

我在查看是否有人知道使用这类函数的通常过程是什么,以及在使用它之前应该做的所有检查,或者如果我编写的使用"addLiquidityETH“的代码有什么问题,那么任何帮助都将不胜感激。

注意:我总是确保在使用"addLiquidityETH“时涉及到weth

UniswapRouter类

代码语言:javascript
复制
from datetime import datetime, timedelta
from decimal import Decimal
from math import trunc
from web3 import Web3
from service.uniswap.constants.uniswap_token_router_abi import UNISWAP_V2_ROUTER_02_ABI

# https://docs.uniswap.org/protocol/V2/reference/smart-contracts/router-02
# Uniswap Router ABI
# https://unpkg.com/@uniswap/v2-periphery@1.1.0-beta.0/build/IUniswapV2Router02.json

class UniswapRouter(object):
"""
Service class for using Uniswap Router.
"""

ROUTER_ABI_02 = UNISWAP_V2_ROUTER_02_ABI

def __init__(self, rpc_address:str, block_explorer:str, web3_options:dict) -> None:
    self.rpc_address = rpc_address
    self.block_explorer = block_explorer
    self.web3_options = web3_options

def getAmountsIn(self, contract_address:str, token_amount_out:Decimal, tokens_path:list, slippage:Decimal=Decimal(1.0)):
    """
    Used to retrive the minimum input of tokens.
    
    :param contract_address: 
        Uniswap router contract address.
    :param token_amount: 
        Amount of tokens
    :param tokens_path: 
        Route from tokens e.g: Token_A->Token_B or Token_A->Token_B->Token_C it can also be reversed.
    
    :returns :
    
    """
    
    try:
        # Setup account web3
        w3 = Web3(Web3.HTTPProvider(self.rpc_address))
        contract_address = Web3.toChecksumAddress(contract_address)
        contract = w3.eth.contract(contract_address, abi=UniswapRouter.ROUTER_ABI_02)
        
        amount_wei = Web3.toWei(token_amount_out,'ether')
        path_to = list()
        for address in tokens_path:
            path_to.append(Web3.toChecksumAddress(address))
        
        if len(path_to) < 2:
            raise Exception('Error: "tokens_path" should have at least 2 addresses!')
        
        result = contract.functions.getAmountsIn(amount_wei, path_to).call()
        
        amount_out = dict()
        amount_out["amountWei"] = result[1]
        amount_out["minInpWei"] = result[0]
        amount_out["amount"] = Web3.fromWei(result[1], 'ether')
        amount_out["minInp"] = Web3.fromWei(result[0], 'ether')
        
        # Slippage
        ca_slippage = (1-(slippage/100))
        ca_amount_out_slipppage = amount_out["minInp"] * ca_slippage
        ca_amount_out_decimals = trunc(Web3.toWei(ca_amount_out_slipppage, 'ether'))
        amount_out["minInpSlippageWei"] = ca_amount_out_decimals
        amount_out["minInpSlippage"] = Web3.fromWei(ca_amount_out_decimals, 'ether')
        
        return amount_out
    
    except BaseException as error:
        raise Exception(f"Error:{error}")


def add_liquidity_eth(self, account_pk:str, router_contract_address:str, token_address:str, token_amount:Decimal,
                    token_amount_min:Decimal, amount_weth_min:Decimal):
    """
    Should only be used with on ERC20/WETH pairs or when WETH is involved.
    """
    
    try:
        # Setup account web3
        w3 = Web3(Web3.HTTPProvider(self.rpc_address))
        account =  w3.eth.account.privateKeyToAccount(account_pk)
        w3.eth.default_account = account.address
        account_address = Web3.toChecksumAddress(account.address)
        token_address = Web3.toChecksumAddress(token_address)
        nonce = w3.eth.get_transaction_count(Web3.toChecksumAddress(account_address),"latest")
        transaction_args = {'gasPrice': w3.toWei(self.web3_options["gas_price"], 'gwei'), 'nonce': nonce}
        
        # Setup contract with web3
        contract_address = Web3.toChecksumAddress(router_contract_address)
        contract = w3.eth.contract(contract_address, abi=UniswapRouter.ROUTER_ABI_02)
        
        if token_amount_min <= 0 or token_amount <= 0 or amount_weth_min <= 0:
            raise Exception(f"All token and weth amounts should be greater than 0!")
        
        if token_amount_min >= token_amount:
            raise Exception(f"token_amount:{token_amount} should be greater than token_amount_min:{token_amount_min}!")
        
        token_amount = Web3.toWei(token_amount,'ether')

        token_amount_min = Web3.toWei(token_amount_min,'ether')
        amount_weth_min = Web3.toWei(amount_weth_min,'ether')
                   
        # Deadline for transaction
        deadline = datetime.now() + timedelta(days=0,seconds=self.web3_options["transaction_deadline"])
        timestamp_deadline = trunc(deadline.timestamp()*100)
        tx = contract.functions.addLiquidityETH(token_address, token_amount, token_amount_min, amount_weth_min,
                                                account_address, timestamp_deadline).buildTransaction(transaction_args)
        
        # Sign transaction
        signed_tx = w3.eth.account.sign_transaction(tx, private_key=account_pk)
        encoded_sent_transaction = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
        
        print(f"Waiting for transaction: {signed_tx.hash.hex()} to be mined....")
        tx_receipt = w3.eth.wait_for_transaction_receipt(transaction_hash=signed_tx.hash, timeout=self.web3_options["transaction_timeout"],
                                                        poll_latency=self.web3_options["poll_latency"])
        print(f"Transaction mined! Block explorer: {self.block_explorer}{signed_tx.hash.hex()}")
        
        # Check if transaction was made with success
        tx_receipt_status = tx_receipt["status"]
        if tx_receipt_status == 0:
            raise Exception(f"Transaction failed, receipt status:{tx_receipt_status}! Block explorer:{self.block_explorer}{signed_tx.hash.hex()}")
            
    except BaseException as error:
        raise Exception(f"Error: {error}")
EN

回答 1

Stack Overflow用户

发布于 2022-08-10 13:19:01

我也有同样的问题。它正在工作,现在说的是"web3.exceptions.ContractLogicError: execution reverted: UniswapV2Library: INSUFFICIENT_AMOUNT“。我已经试过所有我知道的东西来解决这个问题,但是

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72743584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档