In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you an overview of the Uniswap Python development package and how to deploy it, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
The Uniswap.py development kit is suitable for rapidly increasing the support for Uniswap protocols for Python applications. It supports both application scenarios with self-deployed ethernet block chain nodes and lightweight deployment scenarios with third-party nodes. Official download address: Uniswap.py development package
1. Overview of the development package
The Uniswap.py development package mainly includes the following features:
Deploy Uniswap protocol with one click to facilitate rapid development and testing
Supports all interfaces of the Uniswap protocol and provides developer-friendly API
Supports liquidity addition, removal and exchange transactions for various trading pairs such as ERC20/ERC20, ETH/ERC20, etc.
Support automatic market-making price calculation and slip point calculation
Support automatic estimation and manual setting of gas consumption and gas price in eTaifang transaction
Support EIP712 signature authorization, complete liquidity maintenance within a single transaction
The Uniswap.py package runs in Python 3.6 + environment, and the current version 1.0.0. The main classes / interfaces and relationships are shown in the following figure:
A list of the main code files of the Uniswap.py development package can be found in the official description: http://sc.hubwiz.com/codebag/uniswap-py/
2. Compile the contract with sample code 2.1
Enter the project directory at the terminal and execute the following command to compile Uniswap and the test Token contract provided by the development package:
~ $cd ~ / uniswap.py~/uniswap.py$ bin/build-contracts
The implementation results are as follows:
2.2 deployment contract
First, start the development private chain on another terminal:
~ $ganache-cli-d
Then go to the project demo directory and execute the following command to deploy the contract:
~ $cd ~ / uniswap.py/demo~/uniswap.py/demo$ python deploy_contracts.py
The implementation results are as follows:
Note: if you need to test the chain in Ethernet Square, such as Ropsten, Rinkeby, etc., or deploy the above contract in the main chain, or start ganache-cli with other parameters, you need to modify the account configuration in demo/config.py.
2.3 Token Authorization
The code token_approve.py demonstrates how to authorize the Uniswap routing contract agent to operate on the HAPY token and USDT token of the current account.
Enter the project demo directory at the terminal and execute the following command:
~ $cd ~ / uniswap.py/demo~/uniswap.py/demo$ python token_approve.py
The implementation results are as follows:
2.4 ERC20/ERC20 trading pairs add liquidity
The code add_liquidity.py demonstrates how to add liquidity to an ERC20/ERC20 trading pair.
Enter the project demo directory at the terminal and execute the following command:
~ $cd ~ / uniswap.py/demo~/uniswap.py/demo$ python add_liquidity.py
The implementation results are as follows:
The Uniswap agreement stipulates that trading pairs will be automatically created the first time liquidity is added, and when liquidity is added again, the demo code will first display the current position information. For example, execute the following command again:
~ / uniswap.py/demo$ python add_liquidity.py
The implementation results are as follows:
2.5 ERC20/ERC20 trading pair removes liquidity
The code remove_liquidity.py demonstrates how to remove liquidity from a specified ERC20/ERC20 trading pair.
Enter the project demo directory at the terminal and execute the following command:
~ $cd ~ / uniswap.py/demo~/uniswap.py/demo$ python remove_liquidity.py
The implementation results are as follows:
2.6 ERC20/ERC20 exchange: based on the quantity of input token
The code swap_exact_tokens_for_tokens.py demonstrates how to implement ERC20/ERC20 conversion based on the number of input token.
Enter the project demo directory at the terminal and execute the following command:
~ $cd ~ / uniswap.py/demo~/uniswap.py/demo$ python swap_exact_tokens_for_tokens.py
The implementation results are as follows:
2.7ERC20/ERC20 exchange: based on the quantity of exported token
The code swap_tokens_for_exact_tokens.py demonstrates how to implement ERC20/ERC20 conversion based on the number of output token.
Enter the project demo directory at the terminal and execute the following command:
~ $cd ~ / uniswap.py/demo~/uniswap.py/demo$ python swap_tokens_for_exact_tokens.py
The implementation results are as follows:
2.8 ETH/ERC20 trading pairs add liquidity
The code add_liquidty_eth.py demonstrates how to add liquidity to an ETH/ERC20 or ERC20/ETH trading pair.
Enter the project demo directory at the terminal and execute the following command:
~ $cd ~ / uniswap.py/demo~/uniswap.py/demo$ python add_liquidity_eth.py
The implementation results are as follows:
Adding liquidity for the first time will automatically create a trading pair, and then adding again will first display the current position information. For example, execute the following command again:
~ / uniswap.py/demo$ python add_liquidity_eth.py
The implementation results are as follows:
2.9 ETH/ERC20 trading pair removes liquidity
The code remove_liquidity_eth.py demonstrates how to remove liquidity from an ETH/ERC20 or ERC20/ETH trading pair.
Enter the project demo directory at the terminal and execute the following command:
~ $cd ~ / uniswap.py/demo~/uniswap.py/demo$ python remove_liquidity_eth.py
The implementation results are as follows:
2.10 ETH/ERC20 exchange: based on the input quantity of ETH
The code swap_exact_eth_for_tokens.py demonstrates and how to convert a specified amount of ETH into ERC20 token.
Enter the project demo directory at the terminal and execute the following command:
~ $cd ~ / uniswap.py/demo~/uniswap.py/demo$ python swap_exact_eth_for_tokens.py
The implementation results are as follows:
2.11 ETH/ERC20 exchange: based on the quantity of exported token
The code swap_eth_for_exact_tokens.py demonstrates how to convert ETH into a specified number of ERC20 token.
Enter the project demo directory at the terminal and execute the following command:
~ $cd ~ / uniswap.py/demo~/uniswap.py/demo$ python swap_eth_for_exact_tokens.py
The implementation results are as follows:
2.12 ERC20/ETH exchange: based on the input quantity of token
The code swap_exact_tokens_for_eth.py demonstrates how to convert a specified amount of ERC20 token into ETH.
Enter the project demo directory at the terminal and execute the following command:
~ $cd ~ / uniswap.py/demo~/uniswap.py/demo$ python swap_exact_tokens_for_eth.py
The implementation results are as follows:
2.13 ERC20/ETH exchange: based on the quantity of exported ETH
The code swap_tokens_for_exact_eth.py demonstrates how to convert ERC20token into a specified number of ETH.
Enter the project demo directory at the terminal and execute the following command:
~ $cd ~ / uniswap.py/demo~/uniswap.py/demo$ python swap_tokens_for_exact_eth.py
The implementation results are as follows:
3. Use the uniswap.py development package
SwapKit is the entrance to the development package, and using this class, you can quickly implement the following functions:
Liquidity addition and removal: support ERC20/ERC20, ERC20/ETH, ETH/ERC20 and other trading pairs
Creation and execution of exchange transactions: supports various trading pairs such as ERC20/ERC20, ERC20/ETH, ETH/ERC20, etc., and supports input or output prices.
User position query: query the position of the specified account in the specified trading pair, such as the number of positions, proportion, and so on.
3.1 instantiate SwapKit
Three parameters are passed in to instantiate SwapKit:
Tai Tai Fong Node URL
Uniswap routing contract address
The private key of the Ethernet Square account used to perform the contract interaction.
For example, the following code creates a SwapKit instance that accesses the Ethernet main network Uniswap protocol:
From uniswap import SwapKitkit = SwapKit ('http://localhost:8545', # Ethernet Fong node URL' 0x7a250d5630B4.. b4c659F2488Dreply, # Uniswap routing contract address' 0x4f3e.. 3b1d' # default execution account, specify private key); 3.2 use ProtocolMeta (optional)
In order to avoid confusing the Uniswap routing addresses of each network, uniswap.py provides a static method getPresetAddress () of the ProtocolMeta class to obtain the Uniswap protocol contract addresses officially deployed by Uniswap in the Ethernet main network and the test network. For example, obtain the routing contract of the main network:
From uniswap import ProtocolMetarouterAddr = ProtocolMeta.getPresetAddress (SwapKit.MAINNET, # ethernet main SwapKit.ROUTER # Uniswap routing contract); print ('router address:% s'% routerAddr) # 0x7a250d5630B4.
For the ethernet network and logo currently supported by ProtocolMeta, please see the official website description: http://sc.hubwiz.com/codebag/uniswap-py/
3.3 generate liquidity add / remove order
Because the transaction slip point processing and price precalculation are involved in the Uniswap protocol, uniswap.py provides a special class OrderBuilder for generating liquidity add / remove orders. The implementation of OrderBuilder has built-in automatic price calculation and slip point calculation, so callers only need to specify basic data to generate liquidity add / remove proxy orders that can be submitted to Uniswap contracts.
Use the orderBuilder wake-up of the SwapKit object to get the pre-created OrderBuilder object, and call the build () method of OrderBuilder to generate the delegate. For example:
Order = (kit.orderBuilder () # returns the OrderBuilder object.... # Parameter setting .build () # generate and return order)
The OrderBuilder object provides the following methods for adjusting the behavior of the generator:
OrderType (type): sets the type of delegation order. Currently, two types are supported:
Order.LIQUIDITY_ADD-liquidity add order
Order.LIQUIDITY_REMOVE-liquidity removal order
TokenA (addr) / tokenB (addr): the two Token addresses of the trading pair are denoted by SwapKit.ETH_TOKEN in etheric currency
AmountA (bn) / amountB (bn): the number of two token added, which needs to be set only when adding flows
Liquidity (bn): number of LP token, which needs to be set only when removing liquidity
Slippage (bn): set the transaction slip point. The default value is 5, which indicates a 0.5% slip point tolerance range.
To (addr): address for receiving the result of execution of the entrustment order
For example, the following code generates a liquidity add delegation order:
From uniswap import SwapKit Orderorder = (kit.orderBuilder # get delegated order generator object .orderType (Order.LIQUIDITY_ADD) # add liquidity .tokenA ('0x.') # transaction pair TokenA address .tokenB (' 0x.') # transaction pair TokenB address .amount A (10000000000000000000000) # automatically calculate the number of TokenA at market price based on the quantity of TokenB. Slippage (10) # slippage tolerance range 1%. To (kit.credential.address) # set the receive address of LP Token .build () # generate proxy) print ('amountA = >% d'% order.amountA) # TokenA set quantity print (' amountB = >% d'% order.amountB) # TokenB calculate quantity print ('amountAMin = >% d'% order.amountAMin) # minimum acceptable quantity of TokenA after sliding point processing print (' amountBMin = >% d'% order) .amount AMin) # minimum acceptable quantity of TokenB after sliding point processing
Note:
Use a special SwapKit.ETH_TOKEN to represent the ETH in the transaction pair. The value is: 0x00000000000000000000000000000000000eEEEE.
The quantity of token is specified in the smallest unit.
Similarly, the following code generates a liquidity removal order:
Order = (kit.orderBuilder # get delegated order generator object .orderType (Order.LIQUIDITY_REMOVE) # remove liquidity. TokenA ('0x.) # transaction pair TokenA address .tokenB ('0x.') # transaction to TokenB address .transactions (10000000000000000000) # amount of liquidity to be removed .slippage (10) # slippage tolerance range 1%. To (kit.credential.address) # token receive address .build () # Generate order) print ('amountAMin = >% d'% order.amountAMin) # minimum number of TokenA to be received after sliding point processing print (' amountBMin = >% d'% order.amountAMin) # minimum number of TokenB to be received after sliding point processing
The following values provided by the Order object help to provide better feedback to the user before executing the delegate:
AmountA/amountB: the ideal value of tokenA or tokenB calculated at market price
AmountAMin/amountAMax: minimum / maximum value of tokenA after slip point processing
AmountBMin/amountBMax: minimum / maximum value of tokenB after slip point processing
3.4 execution of liquidity order
The generated liquidity order can be executed using the executeOrder () method of SwapKit, and the liquidity add / remove transaction is finally completed. For example:
From uniswap import to_hextxid = kit.executeOrder (trade) # execute order print ('txid = >% s'% to_hex (txid)) # display transaction ID
By default, the executeOrder () method automatically estimates the gas limit and gas price required by the transaction, but you can pass in additional parameters to set these two values manually.
For example, the following code executes the liquidity maintenance order with the specified gas parameter:
From uniswap import to_hexopts = {'gas': 4000000, # 4 million' gasPrice': 200000000000 # 200 gwei} txid = kit.executeOrder (order, opts) # execute proxy print ('txid = >% s'% to_hex (txid)) # display transaction ID3.5 query position information
Use the getLiquidityInfo () method of SwapKit to query the position information of a specified account in a specified trading pair. For example, query the position of an address:
Info = kit.getLiquidityInfo (# query position information '0x90F8.. c9C1', # address to be queried '0x9956.. F4B8' # transaction pair tokenA address' 0x94dF.. .2Df7'# transaction pair tokenB address) print ('total = >% d'% info ['totalSupply']) # Total LP Token print (' balance = >% d'% info ['balance']) # number of positions print (' share = > % .4f'% info ['share']) # position ratio 3.6 generate exchange transaction object
Similar to the addition and removal of liquidity, the transaction exchange in Uniswap also has the problem of automatic price calculation and slip point processing. To this end, the uniswap.py development package provides a special class TradeBuilder to simplify this operation. TradeBuilder has built-in automatic price calculation and slip point processing, so callers only need to set basic parameters.
Use the tradeBuilder property of the SwapKit object to get the pre-created TradeBuilder object, for example:
Trade = (kit.tradeBuilder # get the pre-created exchange transaction generator. # Parameter settings .build () # generate exchange transaction object)
TradeBuilder provides the following methods to adjust the behavior of the generator:
TradeType (type): exchange type. Available values are:
Trade.EXACT_INPUT-based on the number of tokenIn
Trade.EXACT_OUTPUT-based on the number of tokenOut
TokenIn (addr)-enter the address of the Token
TokenOut (addr)-address of the output Token
AmountIn (bn)-enter the quantity, which needs to be set only if the exchange type is EXACT_INPUT
AmountOut (bn)-output quantity, which needs to be set only if the exchange type is EXACT_OUTPUT
Slippage (bn)-transaction slip point setting. The default value is 5, which means 0.5% slip point is allowed.
To (addr)-output token receive address
For example, the following code generates an exchange transaction object based on the number of token entered:
From uniswap import Tradetrade = (kit.tradeBuilder # get exchange transaction generator .tradeType (Trade.EXACT_INPUT) # based on the number of token entered. Tokenin ('0x..') # enter the token address .tokenOut ('0x..') # output token address. Amount in (10000000000000000000) # enter the number of token. Slippage (10) # slippage tolerance range 1%. To (kit.credential.address) # output token receive address .build () # generate exchange Transaction object) print ('amountIn = >% d'% trade.amountIn) # enter the set number of token print (' amountOut = >% d'% trade.amountOut) # the number of output token obtained by the automatic market-making algorithm print ('amountOutMin = >% d'% trade.amountOutMin) # the minimum number of output token that should be received after the slide point processing 3.7 execute the exchange transaction
Executes the specified exchange transaction object using the executeTrade () method of the SwapKit object. For example:
From uniswap import to_hextxid = kit.executeTrade (trade) # execute exchange transaction print ('txid = >% s'% to_hex (txid)) # display transaction ID
By default, the executeTrade () method automatically estimates the gas limit and gas price required by the transaction, but you can pass in additional parameters to set these two values manually.
For example, the following code performs an exchange transaction using the set gas parameter:
Opts = {'gas': 4000000, # 4 million' gasPrice': 200000000000 # 200gwei} txid = kit.executeTrade (trade, opts) # execute the transaction the above is an overview of the Uniswap Python development package and how to deploy it. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.