Skip to content

Commit

Permalink
tests + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Rolaman committed Oct 9, 2024
1 parent 7a25279 commit 593ec8a
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 106 deletions.
35 changes: 20 additions & 15 deletions tasks/core/demo-router-sync.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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
Expand Down
24 changes: 15 additions & 9 deletions tasks/core/demo-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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}`);
Expand Down Expand Up @@ -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(
Expand Down
28 changes: 16 additions & 12 deletions tasks/util/currency-init.ts
Original file line number Diff line number Diff line change
@@ -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<CurrencyResult> {
const walletAddress = process.env.WALLET_ADDR;
Expand All @@ -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(
Expand All @@ -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({
Expand All @@ -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;
}
13 changes: 8 additions & 5 deletions tasks/util/dex-deployment.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -27,8 +30,8 @@ export async function deployDex(
factory,
factoryAddress,
router,
routerAddress
}
routerAddress,
};
}

export interface DexDeploymentResult {
Expand Down
12 changes: 8 additions & 4 deletions tasks/util/pair-init.ts
Original file line number Diff line number Diff line change
@@ -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<PairInitResult> {
const walletAddress = process.env.WALLET_ADDR;
if (!walletAddress) {
Expand Down Expand Up @@ -41,7 +45,7 @@ export async function initPair(
return {
pair,
address: pairAddress,
}
};
}

export interface PairInitResult {
Expand Down
Loading

0 comments on commit 593ec8a

Please sign in to comment.