Skip to content

Commit

Permalink
demo-task: completed for the demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Rolaman committed Sep 24, 2024
1 parent 5064273 commit 8f8565d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 105 deletions.
33 changes: 2 additions & 31 deletions tasks/core/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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;
}
74 changes: 0 additions & 74 deletions tasks/router/router_1.ts

This file was deleted.

24 changes: 24 additions & 0 deletions tasks/util/deploy.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
11 changes: 11 additions & 0 deletions tasks/util/math.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 8f8565d

Please sign in to comment.