Update script for zksync deployment

This commit is contained in:
Antonio Antonino 2025-04-24 15:25:32 +02:00
parent 4c4ff5a4e1
commit a18938282b
No known key found for this signature in database
GPG Key ID: 70CC1DF6BCF7E76D
2 changed files with 21 additions and 156 deletions

View File

@ -2,14 +2,13 @@ const Web3 = require("web3");
const Factory = require("./node_modules/@uniswap/v2-core/build/UniswapV2Factory.json");
const Router = require("./node_modules/@uniswap/v2-periphery/build/UniswapV2Router02.json");
const ERC20 = require("./node_modules/@openzeppelin/contracts/build/contracts/ERC20PresetFixedSupply.json");
const Pair = require("./node_modules/@uniswap/v2-core/build/UniswapV2Pair.json");
const WETH = require("./node_modules/canonical-weth/build/contracts/WETH9.json");
const RPC = "http://localhost:8545";
const prvKey = "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
const RPC = "http://127.0.0.1:3050";
const prvKey = "7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110";
const GasPrice = 0.000005;
const GasLimit = 6000000;
const gasPrice = 0.000005;
const gasLimit = 60000000;
// deploy Weth
async function deployWeth(web3, sender) {
@ -17,7 +16,7 @@ async function deployWeth(web3, sender) {
let weth = new web3.eth.Contract(WETH.abi);
weth = await weth
.deploy({ data: WETH.bytecode })
.send({ from: sender, gas: GasLimit, gasprice: GasPrice })
.send({ from: sender, gas: gasLimit, gasprice: gasPrice })
console.log("Weth address:", weth.options.address);
@ -32,7 +31,7 @@ async function deployWeth(web3, sender) {
async function deployTokens(web3, sender) {
try {
let tokenMem = new web3.eth.Contract(ERC20.abi);
let tokenNet = new web3.eth.Contract(ERC20.abi);
let tokenNmo = new web3.eth.Contract(ERC20.abi);
tokenMem = await tokenMem
.deploy({
@ -41,30 +40,30 @@ async function deployTokens(web3, sender) {
"Mehmet",
"MEM",
// 18,
web3.utils.toWei("9999999999999999999", "ether"),
web3.utils.toWei("1000000", "ether"),
sender,
],
})
.send({ from: sender, gas: GasLimit, gasprice: GasPrice });
.send({ from: sender, gas: gasLimit, gasprice: gasPrice });
console.log("MEM Token address:", tokenMem.options.address);
tokenNet = await tokenNet
tokenNmo = await tokenNmo
.deploy({
data: ERC20.bytecode,
arguments: [
"New Ether",
"NET",
"Nomos",
"NMO",
// 18,
web3.utils.toWei("9999999999999999999", "ether"),
web3.utils.toWei("1000000", "ether"),
sender,
],
})
.send({ from: sender, gas: GasLimit, gasprice: GasPrice });
.send({ from: sender, gas: gasLimit, gasprice: gasPrice });
console.log("NET Token address:", tokenNet.options.address);
console.log("NMO Token address:", tokenNmo.options.address);
return [tokenMem.options.address, tokenNet.options.address];
return [tokenMem.options.address, tokenNmo.options.address];
} catch (error) {
console.log('ERC20 deployment went wrong! Lets see what happened...')
console.log(error)
@ -78,7 +77,7 @@ async function deployRouter(web3, factoryAddress, wethAddress, sender) {
let router = new web3.eth.Contract(Router.abi);
router = await router
.deploy({ data: Router.bytecode, arguments: [factoryAddress, wethAddress] })
.send({ from: sender, gas: GasLimit, gasprice: GasPrice });
.send({ from: sender, gas: gasLimit, gasprice: gasPrice });
console.log("Router address:", router.options.address);
@ -96,7 +95,7 @@ async function deployFactory(web3, feeToSetter, sender) {
let factory = new web3.eth.Contract(Factory.abi);
factory = await factory
.deploy({ data: Factory.bytecode, arguments: [feeToSetter] })
.send({ from: sender, gas: GasLimit, gasprice: GasPrice });
.send({ from: sender, gas: gasLimit, gasprice: gasPrice });
console.log("Factory address:", factory.options.address);
@ -108,60 +107,6 @@ async function deployFactory(web3, feeToSetter, sender) {
}
async function approve(tokenContract, spender, amount, sender) {
try {
await tokenContract.methods
.approve(spender, amount)
.send({ from: sender, gas: GasLimit, gasprice: GasPrice })
.on("transactionHash", function (hash) {
console.log("transaction hash", hash);
})
.on("receipt", function (receipt) {
console.log("receipt", receipt);
});
} catch (err) {
console.log("the approve transaction reverted! Lets see why...");
await tokenContract.methods
.approve(spender, amount)
.call({ from: sender, gas: GasLimit, gasprice: GasPrice });
}
}
// check some stuff on a deployed uniswapV2Pair
async function checkPair(
web3,
factoryContract,
tokenMemAddress,
tokenNetAddress,
sender,
routerAddress
) {
try {
console.log("tokenMemAddress: ", tokenMemAddress);
console.log("tokenNetAddress: ", tokenNetAddress);
const pairAddress = await factoryContract.methods
.getPair(tokenMemAddress, tokenNetAddress)
.call();
console.log("tokenMem Address", tokenMemAddress);
console.log("tokenNet Address", tokenNetAddress);
console.log("pairAddress", pairAddress);
console.log("router address", routerAddress);
const pair = new web3.eth.Contract(Pair.abi, pairAddress);
const reserves = await pair.methods.getReserves().call();
console.log("reserves for tokenMem", web3.utils.fromWei(reserves._reserve0));
console.log("reserves for tokenNet", web3.utils.fromWei(reserves._reserve1));
} catch (err) {
console.log("the check pair reverted! Lets see why...");
console.log(err);
}
}
async function deployUniswap() {
const web3 = new Web3(RPC);
const account = web3.eth.accounts.wallet.add(prvKey);
@ -181,92 +126,12 @@ async function deployUniswap() {
);
const router = new web3.eth.Contract(Router.abi, routerAddress);
// const multicallAddress = await deployMulticall(web3, myAddress);
// const multicall = new web3.eth.Contract(Multicall.abi, multicallAddress);
const [tokenMemAddress, tokenNetAddress] = await deployTokens(web3, myAddress);
const [tokenMemAddress, tokenNmoAddress] = await deployTokens(web3, myAddress);
const tokenMem = new web3.eth.Contract(ERC20.abi, tokenMemAddress);
const tokenNet = new web3.eth.Contract(ERC20.abi, tokenNetAddress);
const tokenNmo = new web3.eth.Contract(ERC20.abi, tokenNmoAddress);
return (tokenMem, tokenMemAddress, tokenNet, tokenNetAddress, myAddress, web3, router, routerAddress, factory, weth, wethAddress)
}
async function addLiquidity(tokenA, tokenAAddress, tokenB, tokenBAddress, myAddress, web3, router, routerAddress, factory, weth, wethAddress) {
// liquidity
const amountADesired = web3.utils.toWei("10000000", "ether");
const amountBDesired = web3.utils.toWei("10000000", "ether");
const amountAMin = web3.utils.toWei("0", "ether");
const amountBMin = web3.utils.toWei("0", "ether");
// deadline
var BN = web3.utils.BN;
const time = Math.floor(Date.now() / 1000) + 200000;
const deadline = new BN(time);
// before calling addLiquidity we need to approve the router
// we need to approve atleast amountADesired and amountBDesired
const spender = router.options.address;
const amountA = amountADesired;
const amountB = amountBDesired;
await approve(tokenA, spender, amountA, myAddress);
await approve(tokenB, spender, amountB, myAddress);
await approve(weth, wethAddress, amountA, myAddress);
await approve(weth, spender, amountA, myAddress);
// try to add liquidity to a non-existen pair contract
try {
await router.methods
.addLiquidity(
tokenAAddress,
tokenBAddress,
amountADesired,
amountBDesired,
amountAMin,
amountBMin,
myAddress,
deadline
)
.send({
from: myAddress,
gas: GasLimit,
gasprice: GasPrice,
})
.on("transactionHash", function (hash) {
console.log("transaction hash", hash);
})
.on("receipt", function (receipt) {
console.log("receipt", receipt);
});
} catch (err) {
console.log("the addLiquidity transaction reverted! Lets see why...");
await router.methods
.addLiquidity(
tokenAAddress,
tokenBAddress,
amountADesired,
amountBDesired,
amountAMin,
amountBMin,
myAddress,
deadline
)
.call({
from: myAddress,
gas: GasLimit,
gasprice: GasPrice,
});
}
await checkPair(
web3,
factory,
tokenAAddress,
tokenBAddress,
myAddress,
routerAddress
);
return (tokenMem, tokenMemAddress, tokenNmo, tokenNmoAddress, myAddress, web3, router, routerAddress, factory, weth, wethAddress)
}
deployUniswap();

View File

@ -14,7 +14,7 @@ echo -e "${BLUE}[Step]${NC} ${YELLOW}Installing dependencies${NC}"
npm install
echo ""
echo -e "${BLUE}[Step]${NC} ${YELLOW}Deploying contracts to http://localhost:8545${NC}"
echo -e "${BLUE}[Step]${NC} ${YELLOW}Deploying contracts to http://127.0.0.1:3050${NC}"
node deploy-contracts.js
if [ $? -eq 0 ]; then