I'm trying to create a Default Emission crowdsale. After writing my tests.& trying to call the buyTokens function.... I keep getting this error:
VM Exception while processing transaction: reverted with reason string 'SafeERC20: low-level call failed'
I've checked previous questions asked by others, and it seems I'm doing everything right.
The crowdsale contract owns half of the total tokens.
My rate seems to be set correctly.
Any ideas of what could be my issue?
Below are my files.
Thanks
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;
import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol";
contract Lukocoin is ERC20, ERC20Detailed {
constructor() public ERC20Detailed("Lukocoin", "LKC", 18) {
_mint(msg.sender, 50_000_000_000);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;
import "hardhat/console.sol";
import "@openzeppelin/contracts/crowdsale/Crowdsale.sol";
import "@openzeppelin/contracts/crowdsale/emission/AllowanceCrowdsale.sol";
import "@openzeppelin/contracts/ownership/Ownable.sol";
// import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
contract LukoCrowdsale is Crowdsale, Ownable {
/*
uint256 rate, - 1BNB equals x token
address payable wallet, - wallet to store funds sent (vaultAddress)
IERC20 token, - token address (tokenAddress)
address tokenWallet - address that own the token (clientAddress)
*/
address private vaultAddress;
constructor(
uint256 rate,
address payable _vaultAddress,
IERC20 token
) public Crowdsale(rate, _vaultAddress, token) {
vaultAddress = _vaultAddress;
}
/////////////////////////////////
///// ADMIN FUNCTIONS //////
/////////////////////////////////
function setRate(uint256 _newRate) public onlyOwner returns (bool) {
_rate = _newRate;
return true;
}
function setVault(address _vaultAddress) public onlyOwner returns (bool) {
vaultAddress = _vaultAddress;
return true;
}
}
const { expect } = require("chai");
const { BigNumber, utils } = require("ethers");
const { ethers } = require("hardhat");
describe("Lukocoin", function () {
before(async () => {
[deployer, client, user1, user2, user3] = await ethers.getSigners();
halfOfTotalSupply = BigNumber.from("25000000000"); // 25_000_000_000
rate = 3583; // 3_583
Lukocoin = await ethers.getContractFactory("Lukocoin");
lukocoin = await Lukocoin.deploy();
await lukocoin.deployed();
//
Vault = await ethers.getContractFactory("Vault");
vault = await Vault.deploy();
await vault.deployed();
//
LukoCrowdsale = await ethers.getContractFactory("LukoCrowdsale");
lukoCrowdsale = await LukoCrowdsale.deploy(
rate,
vault.address,
lukocoin.address
);
lukocoin.transfer(client.address, halfOfTotalSupply);
lukocoin.transfer(lukoCrowdsale.address, halfOfTotalSupply);
lukoCrowdsale.transferOwnership(client.address);
vault.transferOwnership(client.address);
});
it("should pass the smoke test", async function () {
expect(await lukocoin.name()).to.equal("Lukocoin");
expect(await lukocoin.symbol()).to.equal("LKC");
expect(await lukoCrowdsale.rate()).to.equal(3583);
expect(await lukoCrowdsale.token()).to.equal(lukocoin.address);
expect(await lukocoin.balanceOf(lukoCrowdsale.address)).to.equal(
halfOfTotalSupply
);
});
it("should sell user 358300LKC tokens for 100BNB at rate of 3583", async function () {
let bnbAmountSent = utils.parseEther("100");
console.log({ bnbAmountSent });
console.log({
lukoCrowdsaleBalance: await lukocoin.balanceOf(lukoCrowdsale.address),
});
let expectedTokenAmount = "358300";
await lukoCrowdsale.connect(user1).buyTokens(user1.address, {
value: bnbAmountSent,
});
});
});
In my case, the crowdsale contract didn't have enough tokens to process the purchase.