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.
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 is0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc
. This can be confirmed by the value of itstoken0
andtoken1
. The result of functionget_token_pair_details
from question_1.js shows that:
-
The address for
token0
is0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
. This is known asUSDC
on the Ethereum mainnet. -
The address for
token1
is0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2
. This is known asWETH
on the Ethereum mainnet. insert here
- In Etherscan's Contract tab for the
USDC/WETH
pool, it can be seen that the contractUniswapV2Pair
inherits from interfaceIUniswapV2Pair
.
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 functionswap()
inside contractUniswapV2Pair
will emitSwap
event upon a successful swap.
- Inside question_1.js, function
get_hex_signature
with the canonical text signature ofSwap(address,uint256,uint256,uint256,uint256,address)
as input results in0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822
. This is the hex signature forSwap
event.
- To further confirm that the hex signature for
Swap
event is0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822
, a search at the Ethereum Signature Database's event signature page can be performed.
- As expected, in Ethereum Signature Database's event signature page, searching for an event signature with hex
0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822
results in ID 75 of the following text signature:
Swap(address,uint256,uint256,uint256,uint256,address)
- In Etherscan's Events tab for the
USDC/WETH
pool, it can be seen that theevent Swap(address,uint256,uint256,uint256,uint256,address)
has0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822
as its event signature. Thetopic0
of every event is its event signature in hex.
- In Etherscan's Events tab for the
USDC/WETH
pool, filtering the logs by0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822
results in a list of recent swaps.
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.
- As expected, in the Transaction Receipt Event Logs tab on Etherscan, the transaction has 2
Swap
event logs.
-
In the Transaction Receipt Event Logs tab on Etherscan, the log with index 15 is a Swap event from USDC/WETH Uniswap V2 pool. It can be seen that the pool swaps
token1
withtoken0
. -
The symbol for
token0
isUSDC
on the Ethereum mainnet. -
The symbol for
token1
isWETH
on the Ethereum mainnet.
- 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 oneSwap
event that swaps 1.15481 WETH to 3184.35 USDC.
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.
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.