0

I'm trying to test my token buying function but I encounter an error, o facilitate understanding I will show the code

"Error: Returned error: VM Exception while processing transaction: revert"

my purchase function, In the remix it works perfectly:

uint public buyprice = 50000000000000 wei;
    
function buy() public payable {
            require(msg.value >= buyprice);
            require(_balances[address(this)] >= msg.value.div(buyprice) * 10 ** uint(decimals));
            
            amount_eth += msg.value;
            uint tokens = msg.value.div(buyprice) * 10 ** uint(decimals);
            _balances[msg.sender] += tokens;
            
            
            emit Transfer(address(this), msg.sender, tokens);
        }

I'm trying to test it that way:

it("buy tokens", async () => {
        let instance = this.StudyCoin;
        const weiAmount = new BN(5000000000000000);

        let buyTokens = await instance.buy();
        let balanceOfDeployer = await instance.balanceOf(deployerAccount);

        expect(instance.sendTransaction({from: deployerAccount, value: web3.utils.toWei(0.005, "ether")})).to.be.fulfilled;
        balanceOfDeployer = balanceOfDeployer.add(new BN(100000000000000000000));
        console.log(balanceOfDeployer);
        return expect(instance.balanceOf(deployerAccount)).to.eventually.be.a.bignumber.equal(balanceOfDeployer);

    });

I tried several searches and alternatives but without success. I'm using pragma 0.7.4, can someone give me some idea how to do this test, will open doors for several other tests thanks

1 Answer 1

0

Since buy is expecting ethers you should send it in the same transaction.

it("buy tokens", async () => {
    let instance = this.StudyCoin;
    const weiAmount = new BN(5000000000000000);

    let buyTokens = await instance.buy({
        from: deployerAccount,
        value: web3.utils.toWei(0.005, "ether")
    });

    ..
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.