I am creating DEX I have been getting error while testing my smart contracts
My Smart contract
pragma solidity 0.8.6;
import "./Saubaan_2.sol";
contract MySwap {
string public name = "MySwap Ultimate Exchange";
Saubaan_2 public token;
uint public rate = 100;
event TokenPurchased(
address account,
address token,
uint amount,
uint rate
);
function buyTokens(address) public payable {
// Calculate the number of tokens to buy
uint tokenAmount = msg.value*rate;
// Require that myswap has enough tokens
require(token.balanceOf(address(this)) >= tokenAmount);
// Transfer tokens to the user
token.transfer(msg.sender, tokenAmount );
// Emit an event
emit TokenPurchased(msg.sender, address(token), tokenAmount, rate);
}
My Test.js file
const { assert } = require('chai');
const Saubaan_2= artifacts.require("Saubaan_2");
const MySwap = artifacts.require("MySwap");
require('chai')
.use(require('chai-as-promised'))
.should()
function tokens(n){
return web3.utils.toWei(n, 'ether');
}
contract('MySwap', (accounts) => {
let token, myswap
before(async() =>{
token = await Saubaan_2.new()
myswap = await MySwap.new()
await token.transfer(myswap.address, tokens('1000000') )
})
describe('Saubaan_2 deployment', async() => {
it('contract has a name', async() => {
const name = await token.name()
assert.equal(name , 'Saubaan 2')
})
})
describe('MySwap deployment', async() => {
it('contract has a name', async() => {
const name = await myswap.name()
assert.equal(name , 'MySwap Ultimate Exchange')
})
it('contract has tokens', async() => {
let balance = await token.balanceOf(myswap.address)
assert.equal(balance.toString(), tokens('1000000'))
})
})
describe ('buyTokens()', async() => {
let result
before( async() => {
// pUrchase tokens before each example
result = await myswap.buyTokens({from: accounts[1], value: web3.utils.toWei('1', 'ether') })
})
it('Allows user to instantly buy tokens from myswap for a fixed price ',async() => {
let investorBalance = await token.balanceOf(investor)
assert.equal(investorBalance.toString(), tokens('100'))
// Check myswap balance after purchase
let myswapBalance
myswapBalance = await token.balanceOf(myswap.address)
assert.equal(myswapBalance.toString(), tokens('99900'))
myswapbalance = await web3.eth.getBalance(myswap.address)
assert.equal(myswapBalance.toString(), web3.utils.toWei('1', 'ether'))
})
})
})
Now the error is occuring after this line
result = await myswap.buyTokens({from: accounts[1], value: web3.utils.toWei('1', 'ether') })
If I comment out the above line my test is running fine.
Error shown after truffle test
1) Contract: MySwap
buyTokens()
"before all" hook for "Allows user to instantly buy tokens from myswap for a fixed price ":
Error: Returned error: VM Exception while processing transaction: revert
at Context.<anonymous> (test\MySwap.test.js:44:34)
at processImmediate (internal/timers.js:464:21)
please help me I Want to become a good blockchain developer
Thanks in advance