From 8f8565da6afbfe5e7038645913bd499a4474680f Mon Sep 17 00:00:00 2001 From: rolaman Date: Tue, 24 Sep 2024 17:32:15 +0300 Subject: [PATCH] demo-task: completed for the demo --- tasks/core/demo.ts | 33 ++---------------- tasks/router/router_1.ts | 74 ---------------------------------------- tasks/util/deploy.ts | 24 +++++++++++++ tasks/util/math.ts | 11 ++++++ 4 files changed, 37 insertions(+), 105 deletions(-) delete mode 100644 tasks/router/router_1.ts create mode 100644 tasks/util/deploy.ts create mode 100644 tasks/util/math.ts diff --git a/tasks/core/demo.ts b/tasks/core/demo.ts index a9f1b53..9d682de 100644 --- a/tasks/core/demo.ts +++ b/tasks/core/demo.ts @@ -9,6 +9,8 @@ import type { UniswapV2Pair, } from "../../typechain-types"; import { createClient } from "../util/client"; +import {calculateOutputAmount} from "../util/math"; +import {deployNilContract} from "../util/deploy"; task("demo", "Run demo for Uniswap Pairs and Factory").setAction( async (taskArgs, hre) => { @@ -344,34 +346,3 @@ task("demo", "Run demo for Uniswap Pairs and Factory").setAction( }, ); -export async function deployNilContract( - hre: HardhatRuntimeEnvironment, - name: string, - args: string[] = [], -) { - const factory = await hre.ethers.getContractFactory(name); - assert.ok(factory.runner); - assert.ok(factory.runner.sendTransaction); - - const deployTx = await factory.getDeployTransaction(...args); - const sentTx = await factory.runner.sendTransaction(deployTx); - const txReceipt = await sentTx.wait(); - - if (!txReceipt || !txReceipt.contractAddress) { - throw new Error("Contract deployment failed"); - } - - const deployedContract = factory.attach(txReceipt.contractAddress); - return { deployedContract, contractAddress: txReceipt.contractAddress }; -} - -function calculateOutputAmount( - amountIn: bigint, - reserveIn: bigint, - reserveOut: bigint, -): bigint { - const amountInWithFee = amountIn * BigInt(997); - const numerator = amountInWithFee * reserveOut; - const denominator = reserveIn * BigInt(1000) + amountInWithFee; - return numerator / denominator; -} diff --git a/tasks/router/router_1.ts b/tasks/router/router_1.ts deleted file mode 100644 index de4e23c..0000000 --- a/tasks/router/router_1.ts +++ /dev/null @@ -1,74 +0,0 @@ -import { - Faucet, - HttpTransport, - LocalECDSAKeySigner, - PublicClient, - WalletV1, - generateRandomPrivateKey, -} from "@nilfoundation/niljs"; -import { task } from "hardhat/config"; -import { encodeFunctionData } from "viem"; -import { - Currency, - UniswapV2Factory, - UniswapV2Pair, - UniswapV2Router01, -} from "../../typechain-types"; - -task("router_1", "Router: init and add liquidity") - .addParam("token0") - .addParam("token1") - .addParam("router") - .addParam("factory") - .setAction(async (taskArgs, hre) => { - const walletAddress = process.env.WALLET_ADDR; - - if (!walletAddress) { - throw new Error("WALLET_ADDR is not set in environment variables"); - } - - const client = new PublicClient({ - transport: new HttpTransport({ - endpoint: "http://127.0.0.1:8529", - }), - shardId: 1, - }); - - const faucet = new Faucet(client); - - const signer = new LocalECDSAKeySigner({ - privateKey: `0x${process.env.PRIVATE_KEY}`, - }); - - const pubkey = await signer.getPublicKey(); - - const wallet = new WalletV1({ - pubkey: pubkey, - salt: BigInt(Math.round(Math.random() * 10000)), - shardId: 1, - client, - signer, - }); - - const token0Address = taskArgs.token0.toLowerCase(); - const token1Address = taskArgs.token1.toLowerCase(); - const factoryAddress = taskArgs.factory.toLowerCase(); - const routerAddress = taskArgs.router.toLowerCase(); - - const UniswapV2Router = - await hre.ethers.getContractFactory("UniswapV2Router01"); - const router = UniswapV2Router.attach(factoryAddress) as UniswapV2Router01; - - wallet.sendMessage({ - to: walletAddress, - feeCredit: 1_000_000n * 10n, - value: 0n, - data: encodeFunctionData({ - abi: UniswapV2Router01.abi, - functionName: "setCurrencyName", - args: ["MY_TOKEN"], - }), - }); - - // TODO - }); diff --git a/tasks/util/deploy.ts b/tasks/util/deploy.ts new file mode 100644 index 0000000..cbd4dc7 --- /dev/null +++ b/tasks/util/deploy.ts @@ -0,0 +1,24 @@ +import type {HardhatRuntimeEnvironment} from "hardhat/types"; +import assert from "node:assert"; + + +export async function deployNilContract( + hre: HardhatRuntimeEnvironment, + name: string, + args: string[] = [], +) { + const factory = await hre.ethers.getContractFactory(name); + assert.ok(factory.runner); + assert.ok(factory.runner.sendTransaction); + + const deployTx = await factory.getDeployTransaction(...args); + const sentTx = await factory.runner.sendTransaction(deployTx); + const txReceipt = await sentTx.wait(); + + if (!txReceipt || !txReceipt.contractAddress) { + throw new Error("Contract deployment failed"); + } + + const deployedContract = factory.attach(txReceipt.contractAddress); + return { deployedContract, contractAddress: txReceipt.contractAddress }; +} \ No newline at end of file diff --git a/tasks/util/math.ts b/tasks/util/math.ts new file mode 100644 index 0000000..655507e --- /dev/null +++ b/tasks/util/math.ts @@ -0,0 +1,11 @@ + +export function calculateOutputAmount( + amountIn: bigint, + reserveIn: bigint, + reserveOut: bigint, +): bigint { + const amountInWithFee = amountIn * BigInt(997); + const numerator = amountInWithFee * reserveOut; + const denominator = reserveIn * BigInt(1000) + amountInWithFee; + return numerator / denominator; +} \ No newline at end of file