Reference¶
Uniswap V2¶
- class dexsnake.uniswap_v2.UniswapV2Factory(web3)¶
Initializes a new instance of the
UniswapV2Factoryclass.- Parameters:
web3 (
Web3) – AWeb3instance connected to a blockchain node.
- get_pair(token_a, token_b)¶
Returns the address of the pair for
token_aandtoken_bif it has been created, else returns the null address- Parameters:
token_a (str) – The address of the first token.
token_b (str) – The address of the second token.
- Returns:
The address of the pair.
- Return type:
str
- class dexsnake.uniswap_v2.UniswapV2Pair(web3, address)¶
Initializes a new instance of the
UniswapV2Pairclass.- Parameters:
web3 (
Web3) – AWeb3instance connected to a blockchain node.address (str) – The address of the pair contract.
- get_price()¶
Returns the current price of
token_0denominated intoken_1.- Returns:
The pair’s current price.
- Return type:
Decimal
- get_reserves()¶
Returns the current reserves of
token_0andtoken_1after taking into account the token decimals.- Returns:
A tuple containing the pair’s current reserves.
- Return type:
Tuple[
Decimal,Decimal]
- property token_0: ERC20Token¶
Returns the
ERC20Tokeninstance representing the first token in the pair.- Returns:
An
ERC20Tokeninstance representing the first token.- Return type:
ERC20Token
- property token_1: ERC20Token¶
Returns the
ERC20Tokeninstance representing the second token in the pair.- Returns:
An
ERC20Tokeninstance representing the second token.- Return type:
ERC20Token
- class dexsnake.uniswap_v2.UniswapV2Router(web3)¶
Initializes a new instance of the
UniswapV2Factoryclass.- Parameters:
web3 (
Web3) – AWeb3instance connected to a blockchain node.
- swap_exact_tokens_for_tokens(amount_in, amount_out_min, path, to, account, private_key, deadline=None, gas=None, gas_price=None)¶
Swaps an exact amount of input tokens for as many output tokens as possible, along the route determined by
path.- Parameters:
amount_in (
Decimal) – The amount of input tokens to send.amount_out_min (
Decimal) – The minimum amount of output tokens that must be received for the transaction not to revert.path (List[str]) – A list of token addresses. The length of
pathmust be >= 2 and Uniswap V2 pairs for each consecutive pair of addresses must exist and have liquidity.to (str) – The recipient of the output tokens.
account (str) – The account address from which the transaction will be sent.
private_key (str) – The private key of the account.
deadline (int, optional) – The Unix timestamp after which the transaction will revert. If not provided, it will be set to five minutes from the current time.
gas (int, optional) – The gas limit for the transaction. If not provided, it will be estimated automatically.
gas_price (int, optional) – The gas price for the transaction in wei (i.e., 1e-18 ETH). If not provided, the current network gas price will be used.
- Returns:
The transaction receipt.
- Return type:
TxReceipt
- swap_tokens_for_exact_tokens(amount_out, amount_in_max, path, to, account, private_key, deadline=None, gas=None, gas_price=None)¶
Swaps as few input tokens as possible for an exact amount of output tokens, along the route determined by
path.- Parameters:
amount_out (
Decimal) – The amount of input tokens to receive.amount_in_max (Decimal) – The maximum amount of input tokens that must be received for the transaction not to revert.
path (List[str]) – A list of token addresses. The length of
pathmust be >= 2 and Uniswap V2 pairs for each consecutive pair of addresses must exist and have liquidity.to (str) – The recipient of the output tokens.
account (str) – The account address from which the transaction will be sent.
private_key (str) – The private key of the account.
deadline (int, optional) – The Unix timestamp after which the transaction will revert. If not provided, it will be set to five minutes from the current time.
gas (int, optional) – The gas limit for the transaction. If not provided, it will be estimated automatically.
gas_price (int, optional) – The gas price for the transaction in wei (i.e., 1e-18 ETH). If not provided, the current network gas price will be used.
- Returns:
The transaction receipt.
- Return type:
TxReceipt
Uniswap V3¶
- class dexsnake.uniswap_v3.UniswapV3Factory(web3)¶
Initializes a new instance of the
UniswapV3Factoryclass.- Parameters:
web3 (
Web3) – AWeb3instance connected to a blockchain node.
- get_pool(token_a, token_b, fee)¶
Returns the address of the pool for
token_aandtoken_bwith a given fee if it has been created, else returns the null address- Parameters:
token_a (str) – The address of the first token.
token_b (str) – The address of the second token.
fee (int) – The pool’s fee denominated in hundredths of a basis point (i.e., 1e-6). Must be one of the following: 500, 3000, 10000.
- Returns:
The address of the pair.
- Return type:
str
- class dexsnake.uniswap_v3.UniswapV3Pool(web3, address)¶
Initializes a new instance of the
UniswapV3Poolclass.- Parameters:
web3 (
Web3) – AWeb3instance connected to a blockchain node.address (str) – The address of the pool contract.
- property fee: int¶
Returns the pool’s fee denominated in hundredths of a basis point (i.e., 1e-6).
- Returns:
The fee tier of the pool.
- Return type:
int
- get_price()¶
Returns the current price of
token_0denominated intoken_1in the pool.- Returns:
The current price in the pool.
- Return type:
float
- property token_0: ERC20Token¶
Returns the
ERC20Tokeninstance representing the first token in the pair.- Returns:
An
ERC20Tokeninstance representing the first token.- Return type:
ERC20Token
- property token_1: ERC20Token¶
Returns the
ERC20Tokeninstance representing the second token in the pair.- Returns:
An
ERC20Tokeninstance representing the second token.- Return type:
ERC20Token
- class dexsnake.uniswap_v3.UniswapV3Router(web3)¶
Initializes a new instance of the
UniswapV3Routerclass.- Parameters:
web3 (
Web3) – AWeb3instance connected to a blockchain node.
- exact_input_single(amount_in, amount_out_min, token_in, token_out, fee, recipient, account, private_key, deadline=None, gas=None, gas_price=None)¶
Swaps an exact amount of input tokens for as many output tokens as possible, in a single Uniswap V3 pool defined by the token pair and fee.
- Parameters:
amount_in (
Decimal) – The amount of input tokens to send.amount_out_min (
Decimal) – The minimum amount of output tokens that must be received for the transaction not to revert.token_in (str) – The address of the input token.
token_out (str) – The address of the output token.
fee (int) – The pool’s fee denominated in hundredths of a basis point (i.e., 1e-6). Must be one of the following: 500, 3000, 10000.
recipient (str) – The recipient of the output tokens.
account (str) – The account address from which the transaction will be sent.
private_key (str) – The private key of the account.
deadline (int, optional) – The Unix timestamp after which the transaction will revert. If not provided, it will be set to five minutes from the current time.
gas (int, optional) – The gas limit for the transaction. If not provided, it will be estimated automatically.
gas_price (int, optional) – The gas price for the transaction in wei. If not provided, the current network gas price will be used.
- Returns:
The transaction receipt of the swap operation.
- Return type:
TxReceipt
- exact_output_single(amount_out, amount_in_max, token_in, token_out, fee, recipient, account, private_key, deadline=None, gas=None, gas_price=None)¶
Swaps as few input tokens as possible for an exact amount of output tokens, in a single Uniswap V3 pool defined by the token pair and fee.
- Parameters:
amount_out (
Decimal) – The amount of output tokens to receive.amount_in_max (
Decimal) – The maximum amount of input tokens that can be sent.token_in (str) – The address of the input token.
token_out (str) – The address of the output token.
fee (int) – The pool’s fee denominated in hundredths of a basis point (i.e., 1e-6). Must be one of the following: 500, 3000, 10000.
recipient (str) – The recipient of the output tokens.
account (str) – The account address from which the transaction will be sent.
private_key (str) – The private key of the account.
deadline (int, optional) – The Unix timestamp after which the transaction will revert. If not provided, it will be set to five minutes from the current time.
gas (int, optional) – The gas limit for the transaction. If not provided, it will be estimated automatically.
gas_price (int, optional) – The gas price for the transaction in wei. If not provided, the current network gas price will be used.
- Returns:
The transaction receipt of the swap operation.
- Return type:
TxReceipt
Utils¶
- class dexsnake.utils.ERC20Token(web3, address)¶
Initializes a new instance of the
ERC20Tokenclass.For details about the ERC20 standard, see https://eips.ethereum.org/EIPS/eip-20.
- Parameters:
web3 (
Web3) – AWeb3instance connected to a blockchain node.address (str) – The address of the ERC20 token contract.
- allowance(owner, spender)¶
Returns the amount which
spenderis allowed to withdraw fromowner.- Parameters:
owner (str) – The address of the token owner.
spender (str) – The address of the spender.
- Returns:
The remaining allowance of tokens.
- Return type:
Decimal
- approve(spender, value, account, private_key, gas=None, gas_price=None)¶
Approves the specified address to spend the specified amount of tokens on behalf of the caller.
- Parameters:
spender (str) – The address to approve.
value (
Decimal) – The amount of tokens to approve.account (str) – The account address from which the transaction will be sent.
private_key (str) – The private key of the account.
gas (int, optional) – The gas limit for the transaction. If not provided, it will be estimated automatically.
gas_price (int, optional) – The gas price for the transaction in wei. If not provided, the current network gas price will be used.
- Returns:
The transaction receipt.
- Return type:
TxReceipt
- balance_of(account)¶
Returns the balance of the specified account.
- Parameters:
account (str) – The address of the account.
- Returns:
The balance of the account.
- Return type:
Decimal
- property decimals: int¶
Returns the number of decimals the token uses.
- Returns:
The number of decimals.
- property name: str¶
Returns the name of the token.
- Returns:
The token name.
- property symbol: str¶
Returns the symbol of the token.
- Returns:
The token symbol.
- total_supply()¶
Returns the total supply of the token.
- Returns:
Total token supply.
- Return type:
Decimal
- transfer(to, value, account, private_key, gas=None, gas_price=None)¶
Transfers tokens to the specified address.
- Parameters:
to (str) – The address to transfer tokens to.
value (float) – The amount of tokens to transfer.
account (str) – The account address from which the transaction will be sent.
private_key (str) – The private key of the account.
gas (int, optional) – The gas limit for the transaction. If not provided, it will be estimated automatically.
gas_price (int, optional) – The gas price for the transaction in wei. If not provided, the current network gas price will be used.
- Returns:
The transaction receipt.
- Return type:
TxReceipt