使用hardhat自动化部署合约的时候,在创建合约时出现SafeERC20: approve from non-zero to non-zero allowance问题:
部署脚本如下:
import { ethers, network } from "hardhat";
import { Signer, BigNumber, utils } from "ethers";
const MaxUint256 = BigNumber.from(2).pow(256).sub(1);
async function main() {
let gov: Signer;
[gov] = await ethers.getSigners();
console.log(`gov address: ${await gov.getAddress()}`);
let mdexRouter;
let hecoPool;
// mocks
if (network.name == 'heco_mainnet') {
// todo
} else if (network.name == 'bsc_testnet') {
// tokens
const Token = await ethers.getContractFactory("TestToken");
const bnb = await Token.attach('0xc2fd8B9D7eb1bc91d68803a08691FfE2ed8c3ddc');
const cake = await Token.attach('0x24b7b032e73B939ff94F0225b0908a38a9B2130B');
const wbnb = await Token.attach('0x5F68D16D2ee5A40f4294185536d103EfEfD8f934');
const MdexRouterMock = await ethers.getContractFactory("MdexRouterMock");
mdexRouter = await MdexRouterMock.attach('0x56f390BB3feB639C274e1cd9F4EEFf0F2e969C01');
const HecoPoolMock = await ethers.getContractFactory("HecoPoolMock");
hecoPool = await HecoPoolMock.deploy(cake.address);
await hecoPool.add('0x12F2Cf3CAf5F17F8645A10fD5bF014fA7138D32C');
const pid = await hecoPool.LpOfPid('0x12F2Cf3CAf5F17F8645A10fD5bF014fA7138D32C');
await cake.connect(gov).transfer(hecoPool.address, utils.parseEther('10000000'));
// vault
const Vault = await ethers.getContractFactory("VolcanoVault");
const vault = await Vault.deploy();
const rewards = await gov.getAddress();
await vault.initialize('0x12F2Cf3CAf5F17F8645A10fD5bF014fA7138D32C', await gov.getAddress(), rewards, await gov.getAddress(), await gov.getAddress());
await vault.connect(gov).setDepositLimit(MaxUint256);
const StrategyPancakeCAKELP = await ethers.getContractFactory("StrategyPancakeCAKELP");
const strategyPancakeCAKELP = await StrategyPancakeCAKELP.deploy(vault.address, pid, hecoPool.address, mdexRouter.address, wbnb.address, cake.address, {gasLimit:10000000});
await vault.addStrategy(strategyPancakeCAKELP.address, 10000, 0, MaxUint256, 1000);
console.log(`Contract Deployed Summary\n=========================`);
console.log(`| BNB | ${bnb.address} |`);
console.log(`| CAKE | ${cake.address} |`);
console.log(`| WBNB | ${wbnb.address} |`);
console.log(`| MdexRouter | ${mdexRouter.address} |`);
console.log(`| HecoPool | ${hecoPool.address} |`);
console.log(`| Vault | ${vault.address} |`);
console.log(`| StrategyPancakeCAKELP | ${strategyPancakeCAKELP.address} |`);
}
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});