From 593ec8acfa8005efa88ab0541c13199f28b0d7c7 Mon Sep 17 00:00:00 2001 From: rolaman Date: Wed, 9 Oct 2024 17:20:52 +0300 Subject: [PATCH] tests + refactoring --- tasks/core/demo-router-sync.ts | 35 ++++---- tasks/core/demo-router.ts | 24 ++++-- tasks/util/currency-init.ts | 28 ++++--- tasks/util/dex-deployment.ts | 13 +-- tasks/util/pair-init.ts | 12 ++- test/demo-router-sync.test.ts | 147 +++++++++++++++++++-------------- 6 files changed, 153 insertions(+), 106 deletions(-) diff --git a/tasks/core/demo-router-sync.ts b/tasks/core/demo-router-sync.ts index 5a3db45..9bdc91d 100644 --- a/tasks/core/demo-router-sync.ts +++ b/tasks/core/demo-router-sync.ts @@ -1,13 +1,13 @@ -import {shardNumber} from "@nilfoundation/hardhat-plugin/dist/utils/conversion"; -import {waitTillCompleted} from "@nilfoundation/niljs"; -import {task} from "hardhat/config"; -import {encodeFunctionData} from "viem"; -import {createClient} from "../util/client"; -import {calculateOutputAmount} from "../util/math"; -import {deployDex} from "../util/dex-deployment"; -import {initCurrency} from "../util/currency-init"; -import {initPair} from "../util/pair-init"; -import {sleep} from "../util/currencyUtils"; +import { shardNumber } from "@nilfoundation/hardhat-plugin/dist/utils/conversion"; +import { waitTillCompleted } from "@nilfoundation/niljs"; +import { task } from "hardhat/config"; +import { encodeFunctionData } from "viem"; +import { createClient } from "../util/client"; +import { initCurrency } from "../util/currency-init"; +import { sleep } from "../util/currencyUtils"; +import { deployDex } from "../util/dex-deployment"; +import { calculateOutputAmount } from "../util/math"; +import { initPair } from "../util/pair-init"; task( "demo-router-sync", @@ -23,23 +23,28 @@ task( const mintCurrency1Amount = 10000; const swapAmount = 1000; - const {wallet, publicClient} = await createClient(); + const { wallet, publicClient } = await createClient(); - const {factory, routerAddress} = await deployDex(hre); + const { factory, routerAddress } = await deployDex(hre); console.log("Dex deployed, router - " + routerAddress); const { address: token0Address, currency: token0Contract, - id: token0Id + id: token0Id, } = await initCurrency("Token0", mintAmount, hre); const { address: token1Address, currency: token1Contract, - id: token1Id + id: token1Id, } = await initCurrency("Token1", mintAmount, hre); - const {address: pairAddress, pair} = await initPair(token0Address, token1Address, factory, hre); + const { address: pairAddress, pair } = await initPair( + token0Address, + token1Address, + factory, + hre, + ); console.log("Pair deployed " + pairAddress); // Verify the balance of the recipient wallet for both currencies diff --git a/tasks/core/demo-router.ts b/tasks/core/demo-router.ts index cb7cdfb..2a154c4 100644 --- a/tasks/core/demo-router.ts +++ b/tasks/core/demo-router.ts @@ -3,11 +3,11 @@ import { waitTillCompleted } from "@nilfoundation/niljs"; import { task } from "hardhat/config"; import { encodeFunctionData } from "viem"; import { createClient } from "../util/client"; +import { initCurrency } from "../util/currency-init"; import { mintAndSendCurrency } from "../util/currencyUtils"; +import { deployDex } from "../util/dex-deployment"; import { calculateOutputAmount } from "../util/math"; -import {deployDex} from "../util/dex-deployment"; -import {initCurrency} from "../util/currency-init"; -import {initPair} from "../util/pair-init"; +import { initPair } from "../util/pair-init"; task("demo-router", "Run demo with Uniswap Router").setAction( async (taskArgs, hre) => { @@ -21,23 +21,28 @@ task("demo-router", "Run demo with Uniswap Router").setAction( const mintCurrency1Amount = 10000; const swapAmount = 1000; - const {wallet, publicClient, signer} = await createClient(); + const { wallet, publicClient, signer } = await createClient(); - const {factory, routerAddress} = await deployDex(hre); + const { factory, routerAddress } = await deployDex(hre); console.log("Dex deployed, router - " + routerAddress); const { address: token0Address, currency: token0Contract, - id: token0Id + id: token0Id, } = await initCurrency("Token0", mintAmount, hre); const { address: token1Address, currency: token1Contract, - id: token1Id + id: token1Id, } = await initCurrency("Token1", mintAmount, hre); - const {address: pairAddress, pair} = await initPair(token0Address, token1Address, factory, hre); + const { address: pairAddress, pair } = await initPair( + token0Address, + token1Address, + factory, + hre, + ); console.log("Pair deployed " + pairAddress); console.log(`Pair initialized successfully at address: ${pairAddress}`); @@ -290,7 +295,8 @@ task("demo-router", "Run demo with Uniswap Router").setAction( balanceToken1.toString(), ); - userBalanceToken0 = await token0Contract.getCurrencyBalanceOf(walletAddress); + userBalanceToken0 = + await token0Contract.getCurrencyBalanceOf(walletAddress); userBalanceToken1 = await token1Contract.getCurrencyBalanceOf(walletAddress); console.log( diff --git a/tasks/util/currency-init.ts b/tasks/util/currency-init.ts index d0b16d8..77beb17 100644 --- a/tasks/util/currency-init.ts +++ b/tasks/util/currency-init.ts @@ -1,12 +1,12 @@ -import {deployNilContract} from "./deploy"; -import type {Currency} from "../../typechain-types"; -import {HardhatRuntimeEnvironment} from "hardhat/types"; -import {faucetWithdrawal, mintAndSendCurrency, sleep} from "./currencyUtils"; -import {createClient} from "./client"; +import type { HardhatRuntimeEnvironment } from "hardhat/types"; +import type { Currency } from "../../typechain-types"; +import { createClient } from "./client"; +import { faucetWithdrawal, mintAndSendCurrency, sleep } from "./currencyUtils"; +import { deployNilContract } from "./deploy"; export async function initCurrency( name: string, - mintAmount: any, + mintAmount: number, hre: HardhatRuntimeEnvironment, ): Promise { const walletAddress = process.env.WALLET_ADDR; @@ -25,12 +25,16 @@ export async function initCurrency( await signer.getPublicKey(), ]); - const contract = CurrencyContract.attach(currencyAddress.toLowerCase()) as Currency; + const contract = CurrencyContract.attach( + currencyAddress.toLowerCase(), + ) as Currency; const id = await contract.getCurrencyId(); - console.log(`Deployed token ${name}, address - ${currencyAddress}, id - ${id}`) + console.log( + `Deployed token ${name}, address - ${currencyAddress}, id - ${id}`, + ); - await sleep(2000) + await sleep(2000); // Prepare currencies await faucetWithdrawal( @@ -41,7 +45,7 @@ export async function initCurrency( publicClient, ); - await sleep(2000) + await sleep(2000); console.log(`Minting ${mintAmount} ${name} to wallet ${walletAddress}...`); await mintAndSendCurrency({ @@ -58,11 +62,11 @@ export async function initCurrency( address: currencyAddress.toLowerCase(), currency: contract, id: id, - } + }; } export interface CurrencyResult { address: string; currency: Currency; - id: bigint, + id: bigint; } diff --git a/tasks/util/dex-deployment.ts b/tasks/util/dex-deployment.ts index ca81c41..2e0a567 100644 --- a/tasks/util/dex-deployment.ts +++ b/tasks/util/dex-deployment.ts @@ -1,6 +1,9 @@ -import {deployNilContract} from "./deploy"; -import type {UniswapV2Factory, UniswapV2Router01} from "../../typechain-types"; -import {HardhatRuntimeEnvironment} from "hardhat/types"; +import type { HardhatRuntimeEnvironment } from "hardhat/types"; +import type { + UniswapV2Factory, + UniswapV2Router01, +} from "../../typechain-types"; +import { deployNilContract } from "./deploy"; export async function deployDex( hre: HardhatRuntimeEnvironment, @@ -27,8 +30,8 @@ export async function deployDex( factory, factoryAddress, router, - routerAddress - } + routerAddress, + }; } export interface DexDeploymentResult { diff --git a/tasks/util/pair-init.ts b/tasks/util/pair-init.ts index f471e18..a2bfbe7 100644 --- a/tasks/util/pair-init.ts +++ b/tasks/util/pair-init.ts @@ -1,12 +1,16 @@ -import type {Currency, UniswapV2Factory, UniswapV2Pair} from "../../typechain-types"; -import {HardhatRuntimeEnvironment} from "hardhat/types"; +import type { HardhatRuntimeEnvironment } from "hardhat/types"; +import type { + Currency, + UniswapV2Factory, + UniswapV2Pair, +} from "../../typechain-types"; export async function initPair( token0: string, token1: string, factory: UniswapV2Factory, hre: HardhatRuntimeEnvironment, - shardId: number = 1, + shardId = 1, ): Promise { const walletAddress = process.env.WALLET_ADDR; if (!walletAddress) { @@ -41,7 +45,7 @@ export async function initPair( return { pair, address: pairAddress, - } + }; } export interface PairInitResult { diff --git a/test/demo-router-sync.test.ts b/test/demo-router-sync.test.ts index 3fbae73..d179515 100644 --- a/test/demo-router-sync.test.ts +++ b/test/demo-router-sync.test.ts @@ -1,18 +1,23 @@ -import {createClient} from "../tasks/util/client"; -import {mintAndSendCurrency} from "../tasks/util/currencyUtils"; -import {encodeFunctionData} from "viem"; -import {waitTillCompleted} from "@nilfoundation/niljs"; -import {shardNumber} from "@nilfoundation/hardhat-plugin/dist/utils/conversion"; -import {calculateOutputAmount} from "../tasks/util/math"; -import {deployDex} from "../tasks/util/dex-deployment"; -import {initCurrency} from "../tasks/util/currency-init"; -import {initPair} from "../tasks/util/pair-init"; +import { shardNumber } from "@nilfoundation/hardhat-plugin/dist/utils/conversion"; +import { waitTillCompleted } from "@nilfoundation/niljs"; +import { encodeFunctionData } from "viem"; +import { createClient } from "../tasks/util/client"; +import { initCurrency } from "../tasks/util/currency-init"; +import { mintAndSendCurrency } from "../tasks/util/currencyUtils"; +import { deployDex } from "../tasks/util/dex-deployment"; +import { calculateOutputAmount } from "../tasks/util/math"; +import { initPair } from "../tasks/util/pair-init"; +import type { + Currency, + UniswapV2Factory, + UniswapV2Pair, + UniswapV2Router01, +} from "../typechain-types"; const hre = require("hardhat"); const { expect } = require("chai"); -describe("Uniswap with Router (Sync)", async function () { - +describe("Uniswap with Router (Sync)", async () => { const walletAddress = process.env.WALLET_ADDR; if (!walletAddress) { throw new Error("WALLET_ADDR is not set in environment variables"); @@ -24,18 +29,18 @@ describe("Uniswap with Router (Sync)", async function () { const swapAmount = 1000; let routerAddress: string; - let RouterContract: any; + let RouterContract: UniswapV2Router01; let pairAddress: string; - let token0Contract: any; - let token1Contract: any; + let token0Contract: Currency; + let token1Contract: Currency; let token0Id: bigint; let token1Id: bigint; - let token0Address: any; - let token1Address: any; - let pair: any; - let reserve0: any; - let reserve1: any; - let factory: any; + let token0Address: string; + let token1Address: string; + let pair: UniswapV2Pair; + let reserve0: bigint; + let reserve1: bigint; + let factory: UniswapV2Factory; // DEPLOYMENT const { wallet, publicClient, signer } = await createClient(); @@ -43,27 +48,34 @@ describe("Uniswap with Router (Sync)", async function () { before(async function () { this.timeout(120000); - const {wallet, publicClient} = await createClient(); + const { wallet, publicClient } = await createClient(); - ({factory, routerAddress} = await deployDex(hre)); + ({ factory, routerAddress } = await deployDex(hre)); console.log("Dex deployed, router - " + routerAddress); ({ address: token0Address, currency: token0Contract, - id: token0Id + id: token0Id, } = await initCurrency("Token0", mintAmount, hre)); ({ address: token1Address, currency: token1Contract, - id: token1Id + id: token1Id, } = await initCurrency("Token1", mintAmount, hre)); - ({ address: pairAddress, pair} = await initPair(token0Address, token1Address, factory, hre)); + ({ address: pairAddress, pair } = await initPair( + token0Address, + token1Address, + factory, + hre, + )); console.log("Pair deployed " + pairAddress); // 2. MINT CURRENCIES - console.log(`Minting ${mintAmount} Currency0 to wallet ${walletAddress}...`); + console.log( + `Minting ${mintAmount} Currency0 to wallet ${walletAddress}...`, + ); await mintAndSendCurrency({ publicClient, signer, @@ -75,7 +87,9 @@ describe("Uniswap with Router (Sync)", async function () { }); // Mint and send Currency1 - console.log(`Minting ${mintAmount} Currency1 to wallet ${walletAddress}...`); + console.log( + `Minting ${mintAmount} Currency1 to wallet ${walletAddress}...`, + ); await mintAndSendCurrency({ publicClient, signer, @@ -97,32 +111,39 @@ describe("Uniswap with Router (Sync)", async function () { ); }); - it("Add liquidity", async function () { + it("Add liquidity", async () => { // Mint liquidity - const routerArtifact = await hre.artifacts.readArtifact("UniswapV2Router01"); + const routerArtifact = + await hre.artifacts.readArtifact("UniswapV2Router01"); console.log("Adding liquidity..."); - console.log("DEBUG " + [ - pairAddress, - walletAddress, - mintCurrency0Amount, - mintCurrency1Amount, - 1, - 1, - ]); + console.log( + "DEBUG " + + [ + pairAddress, + walletAddress, + mintCurrency0Amount, + mintCurrency1Amount, + 1, + 1, + ], + ); console.log("DEBUG " + routerAddress + " " + wallet.address); - console.log("DEBUG " + [ - { - id: token0Id, - amount: BigInt(mintCurrency0Amount), - }, - { - id: token1Id, - amount: BigInt(mintCurrency1Amount), - }, - ]); + console.log( + "DEBUG " + + [ + { + id: token0Id, + amount: BigInt(mintCurrency0Amount), + }, + { + id: token1Id, + amount: BigInt(mintCurrency1Amount), + }, + ], + ); const hash = await wallet.sendMessage({ to: routerAddress, @@ -159,18 +180,19 @@ describe("Uniswap with Router (Sync)", async function () { // Retrieve and log reserves from the pair [reserve0, reserve1] = await pair.getReserves(); - expect(reserve0).eq(10000) - expect(reserve1).eq(10000) + expect(reserve0).eq(10000); + expect(reserve1).eq(10000); // Check and log liquidity provider balance const lpBalance = await pair.getCurrencyBalanceOf(walletAddress); - expect(lpBalance).eq(9000) + expect(lpBalance).eq(9000); const totalSupply = await pair.getCurrencyTotalSupply(); - expect(totalSupply).eq(18000) + expect(totalSupply).eq(18000); }); - it("Swap", async function () { - const routerArtifact = await hre.artifacts.readArtifact("UniswapV2Router01"); + it("Swap", async () => { + const routerArtifact = + await hre.artifacts.readArtifact("UniswapV2Router01"); const expectedOutputAmount = calculateOutputAmount( BigInt(swapAmount), reserve0, @@ -216,8 +238,9 @@ describe("Uniswap with Router (Sync)", async function () { expect(balanceCurrency1After).eq(190906); }); - it("Remove Liquidity", async function () { - const routerArtifact = await hre.artifacts.readArtifact("UniswapV2Router01"); + it("Remove Liquidity", async () => { + const routerArtifact = + await hre.artifacts.readArtifact("UniswapV2Router01"); const total = await pair.getCurrencyTotalSupply(); console.log("Total supply:", total.toString()); @@ -250,14 +273,16 @@ describe("Uniswap with Router (Sync)", async function () { console.log("Burn executed."); - const userBalanceToken0 = await token0Contract.getCurrencyBalanceOf(walletAddress); - const userBalanceToken1 = await token1Contract.getCurrencyBalanceOf(walletAddress); - expect(userBalanceToken0).eq(194500) - expect(userBalanceToken1).eq(195453) + const userBalanceToken0 = + await token0Contract.getCurrencyBalanceOf(walletAddress); + const userBalanceToken1 = + await token1Contract.getCurrencyBalanceOf(walletAddress); + expect(userBalanceToken0).eq(194500); + expect(userBalanceToken1).eq(195453); // Fetch and log reserves after burn const reserves = await pair.getReserves(); - expect(reserves[0]).eq(5500) - expect(reserves[1]).eq(4547) + expect(reserves[0]).eq(5500); + expect(reserves[1]).eq(4547); }); });