1

So I'm learning Solidity in creating a new token. I successfully created a token and implemented custom tax and burn functionalities. Now I'm trying to understand the Uniswap liquidity features. The thing I don't understand is the address(this) function. In many example code's I see liquidity being added FROM this address and the pair tokens are stored there aswell.

However, how do I access this address through metamask? How are the tokens stored there? Does a simple contract where totalSupply is transferd to owner() or (msg.sender) also have tokens in address(this)? Address(this) totally confuses my mind.

So this piece of code:

uniswapV2Router.addLiquidityETH{value: ethAmount}(
        address(this), //What is this address, shouldn't this be the owner address?
        tokenAmount, //Custom token amount
        0, // slippage is unavoidable
        0, // slippage is unavoidable
        address(this), //Liquidity tokens are stored here and are inaccesible?
        block.timestamp 
    );

So in this code example above. The first parameter and the second last parameter is address(token). The first parameter should be according to the docs "The contract address of the desired token." Which is my own token of course. Now the second param is the amount of this token. How does address(this) contain tokens to add to the liq pool? The second last param is according the docs "Recipient of the liquidity tokens.", are they inaccessible?

3 Answers 3

1

The

address(this)

is the address of the deployed smart contract itself.

In Uniswap context, guessing without you quoting any source code, it is the address of the pair contract.

0
0
address(this)

refers to the contract address you're working on in which you're writing code.

In this case, Both of the addresses should not be using address(this), because the two address parameters in the .addLiquidityETH are address token & address to which means you're transferring your tokens back to this contract. to is the recipient of the liquidity tokens

This function will create or add liquidity to a token/weth pool.

  1. One address should be the token address of the pool
  2. second address should be the recipient address
0

Every smart contract that has ever been and will be deployed, will itself have an address on the blockchain, just like your wallet address does. For smart contracts, this address will be a deterministic hash of the deployer address and a nonce, referenced by address(this).

Many (some) tokens have an auto-liquidity feature where they take a percentage of tokens on transfers and deposit them into the address of the smart contract itself.

The reason the tokens are sent to the smart contract itself is due to how UniswapV2 works, as it uses msg.sender instead of a custom Context _msgSender() meaning that meta-transactions to the Uniswap Router will always return the most recent caller i.e. your contract.

Then once a certain number of tokens are reached, the contract will sell half those tokens for an equal amount of ETH and then deposit a half token, half-ETH combined pool into the Uniswap V2 Pool.

In this case of the LP recipient also being address(this), this would mean that the liquidity provider tokens would be sent to your smart contract itself, and the only way to manipulate them would be programmatically from said contract, provided that the relevant code was written within the contract in order to be able to do so.

In theory this is best practice as the contract can not be changed after deployment, and what happens to the LP tokens is set in stone, however in practice most contracts choose to send LP tokens to a particular owner address, as programmatically manipulating liquidity pools for stuff like time-locks, migrating to V3/V4 pools, or burning is not something one tends to want to do purely via code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.