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 8f8565d commit a227f3a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
5 changes: 2 additions & 3 deletions tasks/core/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import type {
UniswapV2Pair,
} from "../../typechain-types";
import { createClient } from "../util/client";
import {calculateOutputAmount} from "../util/math";
import {deployNilContract} from "../util/deploy";
import { deployNilContract } from "../util/deploy";
import { calculateOutputAmount } from "../util/math";

task("demo", "Run demo for Uniswap Pairs and Factory").setAction(
async (taskArgs, hre) => {
Expand Down Expand Up @@ -345,4 +345,3 @@ task("demo", "Run demo for Uniswap Pairs and Factory").setAction(
);
},
);

33 changes: 16 additions & 17 deletions tasks/util/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import type {HardhatRuntimeEnvironment} from "hardhat/types";
import assert from "node:assert";

import type { HardhatRuntimeEnvironment } from "hardhat/types";

export async function deployNilContract(
hre: HardhatRuntimeEnvironment,
name: string,
args: string[] = [],
hre: HardhatRuntimeEnvironment,
name: string,
args: string[] = [],
) {
const factory = await hre.ethers.getContractFactory(name);
assert.ok(factory.runner);
assert.ok(factory.runner.sendTransaction);
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();
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");
}
if (!txReceipt || !txReceipt.contractAddress) {
throw new Error("Contract deployment failed");
}

const deployedContract = factory.attach(txReceipt.contractAddress);
return { deployedContract, contractAddress: txReceipt.contractAddress };
}
const deployedContract = factory.attach(txReceipt.contractAddress);
return { deployedContract, contractAddress: txReceipt.contractAddress };
}
17 changes: 8 additions & 9 deletions tasks/util/math.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

export function calculateOutputAmount(
amountIn: bigint,
reserveIn: bigint,
reserveOut: bigint,
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;
}
const amountInWithFee = amountIn * BigInt(997);
const numerator = amountInWithFee * reserveOut;
const denominator = reserveIn * BigInt(1000) + amountInWithFee;
return numerator / denominator;
}

0 comments on commit a227f3a

Please sign in to comment.