Skip to content

Commit

Permalink
complete test for mint of LP
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalum committed Oct 13, 2023
1 parent 452822a commit 3abf4ae
Showing 1 changed file with 85 additions and 39 deletions.
124 changes: 85 additions & 39 deletions test/test_mint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,18 @@
import subprocess
from web3 import Web3

from src.utils import get_contract, get_account, get_provider, get_env_variable
from src.utils import get_contract, get_account, get_provider, get_env_variable, real_reservers_to_virtal_reserves
from src.position import Position
from src.provider import Provider

WETH_ADDRESS = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"

class TestMint(unittest.TestCase):

def setUp(self):

if os.getenv('GITHUB_ACTIONS') == 'true':
print('Running within GitHub Actions')
API_KEY = os.getenv('INFURA_KEY')
else:
print('Not running within GitHub Actions')
API_KEY = get_env_variable("MAINNET_PROVIDER")
API_KEY = get_env_variable("INFURA_KEY")


# spin up Ethereum node
self.node_process = subprocess.Popen(["npx", "hardhat", "node", "--fork", API_KEY])

Expand All @@ -33,35 +29,19 @@ def setUp(self):

time.sleep(10)

if os.getenv('GITHUB_ACTIONS') == 'true':

self.w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:8545"))
self.account = self.w3.eth.account.from_key(os.getenv('TEST_ACCOUNT_PRIVATE_KEY'))
self.w3 = get_provider(test=True)
self.account = get_account(test=True)

self.pool_contract = get_contract("USDC_ETH_POOL", address="0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640", test=True)
self.router_contract = get_contract("UNISWAP_ROUTER", address="0xE592427A0AEce92De3Edee1F18E0157C05861564", test=True)
self.nft_contract = get_contract("NFT_POSITION_MANAGER", address="0xC36442b4a4522E871399CD717aBDD847Ab11FE88", test=True)
self.pool_contract = get_contract("USDC_ETH_POOL", address="0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640", test=True)
self.router_contract = get_contract("UNISWAP_ROUTER", address="0xE592427A0AEce92De3Edee1F18E0157C05861564", test=True)
self.nft_contract = get_contract("NFT_POSITION_MANAGER", address="0xC36442b4a4522E871399CD717aBDD847Ab11FE88", test=True)

self.token0_address = self.pool_contract.functions.token0().call()
self.token1_address = self.pool_contract.functions.token1().call()
self.fee = self.pool_contract.functions.fee().call()

self.token0_contract = get_contract("token0", self.token0_address, test=True)
self.token1_contract = get_contract("token1", self.token1_address, test=True)
else:
self.w3 = get_provider(test=True)
self.account = get_account(test=True)
self.token0_address = self.pool_contract.functions.token0().call()
self.token1_address = self.pool_contract.functions.token1().call()
self.fee = self.pool_contract.functions.fee().call()

self.pool_contract = get_contract("USDC_ETH_POOL", test=True)
self.router_contract = get_contract("UNISWAP_ROUTER", test=True)
self.nft_contract = get_contract("NFT_POSITION_MANAGER", test=True)

self.token0_address = self.pool_contract.functions.token0().call()
self.token1_address = self.pool_contract.functions.token1().call()
self.fee = self.pool_contract.functions.fee().call()

self.token0_contract = get_contract("token0", self.token0_address, test=True)
self.token1_contract = get_contract("token1", self.token1_address, test=True)
self.token0_contract = get_contract("token0", self.token0_address, test=True)
self.token1_contract = get_contract("token1", self.token1_address, test=True)

self.token0_decimals = self.token0_contract.functions.decimals().call()
self.token1_decimals = self.token1_contract.functions.decimals().call()
Expand Down Expand Up @@ -91,16 +71,49 @@ def testMintPositionOnlyToken0(self):
self.assertEqual(balance_token1, 0)

if self.token0_address == WETH_ADDRESS:
self._wrap_token(self.token0_contract, 10**18)
self._wrap_token(self.token0_contract, 100 * 10**18)
else:
self._swap(WETH_ADDRESS, self.token0_address, 10**18, True)
self._swap(WETH_ADDRESS, self.token0_address, 100 * 10**18, True)

balance_token0 = self.token0_contract.functions.balanceOf(self.account.address).call()
balance_token1 = self.token1_contract.functions.balanceOf(self.account.address).call()

self.assertEqual(balance_token1, 0)
self.assertGreater(balance_token0, 0)

provider = Provider(test=True)

current_block = provider.get_current_block()
current_tick = provider.get_current_tick(current_block)
current_sqrt_price = provider.get_current_sqrt_price(current_block)

lower_tick = int(current_tick // 10 * 10 - 100)
upper_tick = int(current_tick // 10 * 10 + 100)

y_real = 1 * 10**self.token1_decimals
x_virt, y_virt, x_real = real_reservers_to_virtal_reserves(lower_tick, upper_tick, current_tick, current_sqrt_price, y_real=y_real)
liquidity = math.sqrt(x_virt * y_virt)

position = Position(current_tick, lower_tick, upper_tick, liquidity, None, None)

expected_amount_token0 = position.amount_x(current_tick, current_sqrt_price)
expected_amount_token1 = position.amount_y(current_tick, current_sqrt_price)

_, txn_receipt = provider.mint_position(position, current_tick, current_sqrt_price)

token_id = int.from_bytes(txn_receipt["logs"][3]["topics"][-1], byteorder="big")

actual_amount_token0 = int.from_bytes(txn_receipt["logs"][0]["data"][-32:])
actual_amount_token1 = int.from_bytes(txn_receipt["logs"][1]["data"][-32:])

actual_liquidity = int.from_bytes(txn_receipt["logs"][4]["data"][:32])

# TODO: fix: delta should be way smaller
self.assertAlmostEqual(expected_amount_token0, actual_amount_token0, delta=10**self.token0_decimals)
self.assertAlmostEqual(expected_amount_token1, actual_amount_token1, delta=0.001*10**self.token1_decimals)

self.assertAlmostEqual(liquidity, actual_liquidity, delta=0.001*10**self.token1_decimals)

def testMintPositionOnlyToken1(self):

balance_token0 = self.token0_contract.functions.balanceOf(self.account.address).call()
Expand All @@ -110,16 +123,49 @@ def testMintPositionOnlyToken1(self):
self.assertEqual(balance_token1, 0)

if self.token1_address == WETH_ADDRESS:
self._wrap_token(self.token1_contract, 10**18)
self._wrap_token(self.token1_contract, 100 * 10**18)
else:
self._swap(WETH_ADDRESS, self.token1_address, 10**18, True)
self._swap(WETH_ADDRESS, self.token1_address, 100 * 10**18, True)

balance_token0 = self.token0_contract.functions.balanceOf(self.account.address).call()
balance_token1 = self.token1_contract.functions.balanceOf(self.account.address).call()

self.assertEqual(balance_token0, 0)
self.assertGreater(balance_token1, 0)


provider = Provider(test=True)

current_block = provider.get_current_block()
current_tick = provider.get_current_tick(current_block)
current_sqrt_price = provider.get_current_sqrt_price(current_block)

lower_tick = int(current_tick // 10 * 10 - 100)
upper_tick = int(current_tick // 10 * 10 + 100)

y_real = 1 * 10**self.token1_decimals
x_virt, y_virt, x_real = real_reservers_to_virtal_reserves(lower_tick, upper_tick, current_tick, current_sqrt_price, y_real=y_real)
liquidity = math.sqrt(x_virt * y_virt)

position = Position(current_tick, lower_tick, upper_tick, liquidity, None, None)

expected_amount_token0 = position.amount_x(current_tick, current_sqrt_price)
expected_amount_token1 = position.amount_y(current_tick, current_sqrt_price)

_, txn_receipt = provider.mint_position(position, current_tick, current_sqrt_price)

token_id = int.from_bytes(txn_receipt["logs"][3]["topics"][-1], byteorder="big")

actual_amount_token0 = int.from_bytes(txn_receipt["logs"][0]["data"][-32:])
actual_amount_token1 = int.from_bytes(txn_receipt["logs"][1]["data"][-32:])

actual_liquidity = int.from_bytes(txn_receipt["logs"][4]["data"][:32])

# TODO: fix: delta should be way smaller
self.assertAlmostEqual(expected_amount_token0, actual_amount_token0, delta=10**self.token0_decimals)
self.assertAlmostEqual(expected_amount_token1, actual_amount_token1, delta=0.001*10**self.token1_decimals)

self.assertAlmostEqual(liquidity, actual_liquidity, delta=0.001*10**self.token1_decimals)


def _swap(self, token_in, token_out, swap_token_amount, eth) -> None:

Expand Down

0 comments on commit 3abf4ae

Please sign in to comment.