Skip to content

Commit

Permalink
Improve uniswap scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
andresaiello committed Sep 22, 2023
1 parent c80a37f commit 20bf86d
Show file tree
Hide file tree
Showing 5 changed files with 377 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,17 @@ import {
SystemContract__factory,
UniswapV2Router02__factory
} from "../../typechain-types";
import { getNow, printReserves, sortPair } from "./uniswap.helpers";

const SYSTEM_CONTRACT = getSystemContractAddress();

const BTC_TO_ADD = parseUnits("0", 8);
const ETH_TO_ADD = parseUnits("1500");
const MATIC_TO_ADD = parseUnits("1500");
const BNB_TO_ADD = parseUnits("100");
const ETH_TO_ADD = parseUnits("0");
const MATIC_TO_ADD = parseUnits("0");
const BNB_TO_ADD = parseUnits("0");

const ZETA_TO_ADD = parseUnits("0");

interface Pair {
TokenA: string;
TokenB: string;
}

export const getNow = async () => {
const block = await ethers.provider.getBlock("latest");
return block.timestamp;
};

export const sortPair = (token1: string, token2: string): Pair => {
if (token1 < token2) {
return { TokenA: token1, TokenB: token2 };
}
return { TokenA: token2, TokenB: token1 };
};

const addTokenEthLiquidity = async (
tokenContract: ERC20,
tokenAmountToAdd: BigNumber,
Expand All @@ -54,6 +38,8 @@ const addTokenEthLiquidity = async (
const tx1 = await tokenContract.approve(uniswapRouter.address, MaxUint256);
await tx1.wait();

console.log("Uniswap approved to consume token...");

const tx2 = await uniswapRouter.addLiquidityETH(
tokenContract.address,
tokenAmountToAdd,
Expand Down Expand Up @@ -89,13 +75,6 @@ const estimateZetaForToken = async (

const ZETAValue = reservesZETA.mul(tokenAmountToAdd).div(reservesToken);

const tokenDecimals = await tokenContract.decimals();
console.log(
`Zeta/${getGasSymbolByNetwork(network)} reserves ${formatUnits(reservesZETA)}/${formatUnits(
reservesToken,
tokenDecimals
)}`
);
return ZETAValue;
};

Expand All @@ -116,7 +95,6 @@ async function addLiquidity(

const tokenAddress = await systemContract.gasCoinZRC20ByChainId(getChainId(network));
const tokenContract = ERC20__factory.connect(tokenAddress, deployer);
const tokenDecimals = await tokenContract.decimals();

const zetaToAdd = initLiquidityPool
? ZETA_TO_ADD
Expand All @@ -129,14 +107,9 @@ async function addLiquidity(
deployer
);

console.log(
`Zeta/${getGasSymbolByNetwork(network)} to add ${formatUnits(zetaToAdd)}/${formatUnits(
tokenAmountToAdd,
tokenDecimals
)}`
);

await printReserves(tokenContract, WZETAAddress, uniswapFactoryAddress, deployer);
// await addTokenEthLiquidity(tokenContract, tokenAmountToAdd, zetaToAdd, uniswapRouter, deployer);
await printReserves(tokenContract, WZETAAddress, uniswapFactoryAddress, deployer);
}
async function main() {
const WZETA_ADDRESS = getAddress({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { MaxUint256 } from "@ethersproject/constants";
import { formatUnits } from "@ethersproject/units";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { getChainId } from "@zetachain/addresses";
import { NetworkName } from "@zetachain/addresses";
import { getAddress } from "@zetachain/addresses";
import { getSystemContractAddress } from "@zetachain/addresses-tools";
import { BigNumber } from "ethers";
import { ethers } from "hardhat";

import {
ERC20,
ERC20__factory,
IUniswapV2Factory__factory,
IUniswapV2Pair,
IUniswapV2Pair__factory,
IUniswapV2Router02,
SystemContract__factory,
UniswapV2Router02__factory
} from "../../typechain-types";
import { getNow, printReserves, sortPair } from "./uniswap.helpers";

const SYSTEM_CONTRACT = getSystemContractAddress();

const removeTokenEthLiquidity = async (
tokenContract: ERC20,
LPContract: IUniswapV2Pair,
LPAmountToRemove: BigNumber,
uniswapRouter: IUniswapV2Router02,
deployer: SignerWithAddress
) => {
const tx1 = await LPContract.approve(uniswapRouter.address, MaxUint256);
await tx1.wait();

console.log("Uniswap approved to consume LP...");

const tx2 = await uniswapRouter.removeLiquidityETH(
tokenContract.address,
LPAmountToRemove,
0,
0,
deployer.address,
(await getNow()) + 360,
{ gasLimit: 10_000_000 }
);
await tx2.wait();
};

async function removeLiquidity(
network: NetworkName,
WZETAAddress: string,
uniswapFactoryAddress: string,
uniswapRouterAddress: string
) {
console.log(`Removing liquidity for: ${network}`);

const [deployer] = await ethers.getSigners();

const systemContract = await SystemContract__factory.connect(SYSTEM_CONTRACT, deployer);
const uniswapV2Factory = IUniswapV2Factory__factory.connect(uniswapFactoryAddress, deployer);
const uniswapRouter = await UniswapV2Router02__factory.connect(uniswapRouterAddress, deployer);

const tokenAddress = await systemContract.gasCoinZRC20ByChainId(getChainId(network));
const tokenContract = ERC20__factory.connect(tokenAddress, deployer);

const pair = sortPair(tokenAddress, WZETAAddress);

const poolAddress = await uniswapV2Factory.getPair(pair.TokenA, pair.TokenB);

const pool = IUniswapV2Pair__factory.connect(poolAddress, deployer);

const LPBalance = await pool.balanceOf(deployer.address);

console.log(`LP Balance: ${formatUnits(LPBalance, 18)} for ${poolAddress}`);

await printReserves(tokenContract, WZETAAddress, uniswapFactoryAddress, deployer);
// await removeTokenEthLiquidity(tokenContract, pool, LPBalance, uniswapRouter, deployer);
await printReserves(tokenContract, WZETAAddress, uniswapFactoryAddress, deployer);
}
async function main() {
const WZETA_ADDRESS = getAddress({
address: "weth9",
networkName: "athens",
zetaNetwork: "athens"
});

const UNISWAP_FACTORY_ADDRESS = getAddress({
address: "uniswapV2Factory",
networkName: "athens",
zetaNetwork: "athens"
});

const UNISWAP_ROUTER_ADDRESS = getAddress({
address: "uniswapV2Router02",
networkName: "athens",
zetaNetwork: "athens"
});

await removeLiquidity("goerli", WZETA_ADDRESS, UNISWAP_FACTORY_ADDRESS, UNISWAP_ROUTER_ADDRESS);
await removeLiquidity("polygon-mumbai", WZETA_ADDRESS, UNISWAP_FACTORY_ADDRESS, UNISWAP_ROUTER_ADDRESS);
await removeLiquidity("bsc-testnet", WZETA_ADDRESS, UNISWAP_FACTORY_ADDRESS, UNISWAP_ROUTER_ADDRESS);
await removeLiquidity("bitcoin-test", WZETA_ADDRESS, UNISWAP_FACTORY_ADDRESS, UNISWAP_ROUTER_ADDRESS);
}

main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
111 changes: 111 additions & 0 deletions packages/zevm-app-contracts/scripts/uniswap/sell-token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { MaxUint256 } from "@ethersproject/constants";
import { formatUnits, parseUnits } from "@ethersproject/units";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { getChainId } from "@zetachain/addresses";
import { NetworkName } from "@zetachain/addresses";
import { getAddress } from "@zetachain/addresses";
import { getSystemContractAddress } from "@zetachain/addresses-tools";
import { BigNumber } from "ethers";
import { ethers } from "hardhat";

import {
ERC20,
ERC20__factory,
IUniswapV2Router02,
SystemContract__factory,
UniswapV2Router02__factory
} from "../../typechain-types";
import { getNow, printReserves } from "./uniswap.helpers";

const SYSTEM_CONTRACT = getSystemContractAddress();

const BTC_TO_SELL = parseUnits("0", 8);
const ETH_TO_SELL = parseUnits("0");
const MATIC_TO_SELL = parseUnits("0");
const BNB_TO_SELL = parseUnits("0");

const swapZeta = async (
tokenContract: ERC20,
WZETAAddress: string,
amountIn: BigNumber,
uniswapRouter: IUniswapV2Router02,
deployer: SignerWithAddress
) => {
const tx1 = await tokenContract.approve(uniswapRouter.address, MaxUint256);
await tx1.wait();

console.log("Uniswap approved to consume token...");

const tx2 = await uniswapRouter.swapExactTokensForETH(
amountIn,
0,
[tokenContract.address, WZETAAddress],
deployer.address,
(await getNow()) + 360,
{ gasLimit: 10_000_000 }
);
await tx2.wait();

console.log(`Tx hash: ${tx2.hash}`);
};

async function sellToken(
network: NetworkName,
tokenAmountToSell: BigNumber,
WZETAAddress: string,
uniswapFactoryAddress: string,
uniswapRouterAddress: string
) {
console.log(`Selling token on: ${network}`);

const [deployer] = await ethers.getSigners();

const systemContract = await SystemContract__factory.connect(SYSTEM_CONTRACT, deployer);
const uniswapRouter = await UniswapV2Router02__factory.connect(uniswapRouterAddress, deployer);

const tokenAddress = await systemContract.gasCoinZRC20ByChainId(getChainId(network));
const tokenContract = ERC20__factory.connect(tokenAddress, deployer);

await printReserves(tokenContract, WZETAAddress, uniswapFactoryAddress, deployer);
// await swapZeta(tokenContract, WZETAAddress, tokenAmountToSell, uniswapRouter, deployer);
await printReserves(tokenContract, WZETAAddress, uniswapFactoryAddress, deployer);
}
async function main() {
const WZETA_ADDRESS = getAddress({
address: "weth9",
networkName: "athens",
zetaNetwork: "athens"
});

const UNISWAP_FACTORY_ADDRESS = getAddress({
address: "uniswapV2Factory",
networkName: "athens",
zetaNetwork: "athens"
});

const UNISWAP_ROUTER_ADDRESS = getAddress({
address: "uniswapV2Router02",
networkName: "athens",
zetaNetwork: "athens"
});

if (!ETH_TO_SELL.isZero()) {
await sellToken("goerli", ETH_TO_SELL, WZETA_ADDRESS, UNISWAP_FACTORY_ADDRESS, UNISWAP_ROUTER_ADDRESS);
}
if (!MATIC_TO_SELL.isZero()) {
await sellToken("polygon-mumbai", MATIC_TO_SELL, WZETA_ADDRESS, UNISWAP_FACTORY_ADDRESS, UNISWAP_ROUTER_ADDRESS);
}
if (!BNB_TO_SELL.isZero()) {
await sellToken("bsc-testnet", BNB_TO_SELL, WZETA_ADDRESS, UNISWAP_FACTORY_ADDRESS, UNISWAP_ROUTER_ADDRESS);
}
if (!BTC_TO_SELL.isZero()) {
await sellToken("bitcoin-test", BTC_TO_SELL, WZETA_ADDRESS, UNISWAP_FACTORY_ADDRESS, UNISWAP_ROUTER_ADDRESS);
}
}

main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
Loading

0 comments on commit 20bf86d

Please sign in to comment.