Background
OKC did not support the use of deploying and calling contracts using hardhad and ethers.js. This caused development difficulties for many developers familiar with using hardhat and ethers.js.
Reason
The underlying logic for OKC not supporting hardhad and ethers.js is that ethers.js calculates the transaction hash locally when deploying and invoking contracts, and then compares it with the transaction hash obtained from the chain, and reports an error if the transaction hash is inconsistent. etherjs.js calculates the transaction hash locally in the same way as ethereum, while OEC The algorithm of calculating transaction hash is not the same as that of ethereum, so it causes the error of inconsistent transaction hash when deploying and calling contracts on OEC using ethers.js.
Since hardhat is dependent on ethers.js, the same problem exists when deploying and invoking contracts using hardhat.
Current status
OKC team has now upgraded OKC's transaction hash algorithm to be consistent with ethereum, and it is now online on OKC main and test networks. Developers can now use Hardhad and ethers.js to deploy and call contracts on the OKC main and test networks.
Test
A code script and test results of deploying uniswap with hardhat are posted below
const { ethers } = require("hardhat");
const FEE_TO_SETTER = "0x0000000000000000000000000000000000000000"
const WOKT_ADDRESS = "0x2219845942d28716c0F7C605765fABDcA1a7d9E0"
async function addLiquidity() {
let owner
[owner] = await ethers.getSigners();
console.log("deploying mock token ...")
const MockToken = await ethers.getContractFactory("mockERC20")
const tokenA = await MockToken.deploy()
console.log("tokenA deployed at : " + tokenA.address)
const tokenB = await MockToken.deploy()
console.log("tokenB deployed at : " + tokenB.address)
console.log("minting mock token ...")
await tokenA.mint(owner.address, ethers.utils.parseEther('10000'))
await tokenB.mint(owner.address, ethers.utils.parseEther('10000'))
console.log("tokenA balance: " + await tokenA.balanceOf(owner.address))
console.log("tokenB balance: " + await tokenB.balanceOf(owner.address))
console.log("deploying swap ...")
const PairFactory = await ethers.getContractFactory("UniswapV2Factory")
const Router = await ethers.getContractFactory("UniswapV2Router02")
const pairFactory = await PairFactory.deploy(FEE_TO_SETTER)
const hash = await pairFactory.getHash()
const router = await Router.deploy(pairFactory.address, WOKT_ADDRESS, hash)
console.log("pair code hash: " + hash)
console.log("router factory deployed at : " + router.address)
console.log("pair factory deployed at : " + pairFactory.address)
console.log("approving token ...")
await tokenA.approve(router.address, ethers.utils.parseEther('10000'))
await tokenB.approve(router.address, ethers.utils.parseEther('10000'))
console.log("adding liquidity ...")
await router.addLiquidity(
tokenA.address,
tokenB.address,
ethers.utils.parseEther('10000'),
ethers.utils.parseEther('10000'),
0,
0,
owner.address,
Date.now() + 86400
)
const pairAddress = await pairFactory.getPair(tokenA.address, tokenB.address)
const Pair = await ethers.getContractFactory("UniswapV2Pair")
const pair = await Pair.attach(pairAddress)
const LPBalance = await pair.balanceOf(owner.address)
console.log("LP balance: " + LPBalance)
}
addLiquidity()
Execute the command:
hardhat run scripts/addLiquidityTest.js --network OECTestnet
The test results:
deploying mock token ...
tokenA deployed at : 0xaf5E2955D3b3c0f9Ddee0Eb3cEBAaB0557790BA4
tokenB deployed at : 0x5a22Eb62C851ee54248E953E3AF681974A3b52e7
minting mock token ...
tokenA balance: 10000000000000000000000
tokenB balance: 10000000000000000000000
deploying swap ...
pair code hash: 0x49018e33b4fcecd6638ed4ea6338be0821c5bb2fb8d1224da42df2a3960c873f
router factory deployed at : 0xC78678e91812B010D13F807408934560F0C935d1
pair factory deployed at : 0x6644BE9e283cACA2Ddbf8FBE55943E012395099D
approving token ...
adding liquidity ...
LP balance: 9999999999999999999000
The test results show that contracts can be successfully deployed and invoked on the OKC test network using hardhat, and are faster and have lower gas fees than on ethereum.