Skip to content

ariessa/blockchain-dex-events

Repository files navigation

🦎 Blockchain DEX Events

Contract Calls Knowledge

Question

Using https://polygon-rpc.com/ RPC node as a service, write the code and RPC call to obtain totalSupply of the MANA token issued on the Polygon (MATIC) blockchain.

You may consider using the ERC-20 ABI for your solution.

You may approach the above natively or with a library such as Ethereum.rb, web3js, or ethers.


Solution

The solution can be viewed at contract_calls_knowledge.js.



DEX Event Logs

Question 1

Using the Etherscan block explorer, find a list of recent swaps for the following USDC/ETH pool on Uniswap V2.

Provide a screenshot for your response.


Solution

  • The contract address for USDC/WETH pool on Uniswap V2 is 0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc. This can be confirmed by the value of its token0 and token1. The result of function get_token_pair_details from question_1.js shows that:


  • The address for token0 is 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48. This is known as USDC on the Ethereum mainnet.

  • The address for token1 is 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2. This is known as WETH on the Ethereum mainnet. insert here


  • In Etherscan's Contract tab for the USDC/WETH pool, it can be seen that the contract UniswapV2Pair inherits from interface IUniswapV2Pair.


This makes the contract UniswapV2Pair has the following Swap event:

event Swap(
    address indexed sender,
    uint amount0In,
    uint amount1In,
    uint amount0Out,
    uint amount1Out,
    address indexed to
);

  • In Etherscan's Contract tab for the USDC/WETH pool, it can be seen that the function swap() inside contract UniswapV2Pair will emit Swap event upon a successful swap.


  • Inside question_1.js, function get_hex_signature with the canonical text signature of Swap(address,uint256,uint256,uint256,uint256,address) as input results in 0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822. This is the hex signature for Swap event.



Swap(address,uint256,uint256,uint256,uint256,address)


  • In Etherscan's Events tab for the USDC/WETH pool, it can be seen that the event Swap(address,uint256,uint256,uint256,uint256,address) has 0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822 as its event signature. The topic0 of every event is its event signature in hex.


  • In Etherscan's Events tab for the USDC/WETH pool, filtering the logs by 0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822 results in a list of recent swaps.


Question 2

https://etherscan.io/tx/0x5e555836bacad83ac3989dc1ec9600800c7796d19d706f007844dfc45e9703ac/ is a swap transaction on a Uniswap V2 pool. One of the associated swaps here is a trade from 1.15481 ETH to $3,184.35. Determine in the block explorer where that raw number is coming from and how it is being derived.

You may use screenshot to show your answers.


Solution

  • In the Transaction Details page on Etherscan, it can be seen that the transaction involves 2 token swaps:

    • 25,000 DOMI for 1.154811757668969125 ETH
    • 1.154811757668969125 ETH for 3,184.355095 USDC

This means that the transaction has 2 Swap event logs.





  • The data section shows that WETH tokens were swapped for USDC tokens. The amounts are in BigNumber for precision purposes and because Solidity does not support floating numbers.
amount0In: 0
amount1In: 1154811757668969125
amount0Out: 3184355095
amount1Out: 0

In order to get a human readable number, the amounts need to be divided by 10 to the power of its decimals.

For example:

amount0_decimals = (10 ** 18)
amount0 = 1154811757668969125 / amount0_decimals

  • Using function get_event_log_data from question_2.js, it can be seen that there is one Swap event that swaps 1.15481 WETH to 3184.35 USDC.


Question 3

Quickswap, a DEX on Polygon (MATIC) allows users to swap two assets as a trade.

For every swap transaction that is recorded on the blockchain, a swap event is emitted and stored in the network with this hash ID 0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822.

Write the RPC API call to get all the swap events that were emitted for the block #26444465.

Use https://polygon-rpc.com/ RPC node as a service.


Solution

The solution can be viewed at question_3.js.



Question 4

When using the Quickswap DEX, we noticed that the price impact is -42.09% when we increase the size of the trade. What does price impact mean, why is it important, the math behind the price impact. Include as many details as you can to support your explanation.


Solution

In the context of decentralised exchange (DEX), price impact refers to the change in the market price of an asset caused by the execution of a trade. It is usually presented in the form of percentage or decimal value.

There are two kinds of price impact:

  • Positive price impact - increase in price for buys, decrease for sells
  • Negative price impact - decrease in price for buys, increase for sells

Price impact is important for the following reasons:

  • It helps traders to trade efficiently without causing significant price changes
  • It indicates the impact of a trade to the market.
  • It helps traders to manage risks associated with large trades that may impact market prices unfavorably.

It is calculated as follows:

p_before = price of the asset before trade
p_after = price of the asset after trade

price impact (%) = (p_after - p_before) / p_before * 100;

Decentralised exchanges like Quickswap provides an estimated or calculated price impact to users before they confirm a swap transaction.



About

Technical test for a full stack developer role.

Topics

Resources

Stars

Watchers

Forks