I am trying to create transaction local blockchain using ganache on When i confirm the transaction i get the error as "{"id":4818399857527,"jsonrpc":"2.0","params":..", please refer screenshot for more detail.


I also ran my tests all are successful but not able to create transation.

Below is my code, please help

App = {
web3Provider: null,
contracts: {},
account: '0x0', 
loading: false,
tokensAvilable : 750000,
init : function() {
    console.log('app started');
    return App.intiWeb3();

intiWeb3 : function() {
    if (typeof web3 !== 'undefined') {
        //console.log('web3  defined');
        // if there is already instance of web3 is present e.g. from metamask
        App.web3Provider = web3.currentProvider;
        web3 = new Web3(web3.currentProvider);
    } else {
        // Specify default instance if no web3 instance is provided
        //console.log('web3 not defined')
        App.web3Provider = new Web3.providers.httpProvider('http://localhost:7545');
        web3 = new Web3(App.web3Provider);

    return App.initContracts();


initContracts: function() {
    $.getJSON("AjTokenSale.json", function(ajtokensale) {
        App.contracts.AjTokenSale = TruffleContract(ajtokensale);
            //return ajtokensale.tokenPrice()
            //console.dir('dir is' + ajtokensale.tokenPrice());
            console.log('AJTOKEN sale address is ' + ajtokensale.address);
    }).done(function() {
      $.getJSON("AjToken.json", function(ajtoken) {
        App.contracts.AjToken = TruffleContract(ajtoken);
            console.log('AJ token  address ' + ajtoken.address);
        return App.render();


render: function() {
    if(App.loading) {
    App.loading = true;

    var loader = $('#loader');
    var content = $('#content');


    // load account data
    //var accounts = web3.eth.getallAccounts

    web3.eth.getCoinbase(function(err, account) {
        if(account) {
            App.account = account;
            $('#accountAddress').html("Your account: " + account);
        } else {
            console.log('Address is null, err is '+err)

    //$('#accountAddress').html("Your account: " + accounts[0]);

    App.contracts.AjTokenSale.deployed().then(function(instance) {
        ajtokenSaleInstance = instance;
        return ajtokenSaleInstance.tokenPrice();
    }).then(function(tokenPrice) {
        App.tokenPrice = tokenPrice.toNumber();
        //console.log('tokenPrice ' + tokenPrice.toNumber());
        $('.token-price').html(web3.fromWei(App.tokenPrice, 'ether'));
        return ajtokenSaleInstance.tokensSold();            
    }).then(function(tokensSold) {
        //App.tokensSold = tokensSold.toNumber(); 
        App.tokensSold = parseInt(tokensSold).toNumber(); 
        //App.tokensSold = 680000;

        var progressPercent = (Math.ceil(App.tokensSold) / App.tokensAvilable)* 100;
        $('#progress').css('width', progressPercent+'%');

        // load token contract 

        App.contracts.AjToken.deployed().then(function(instance) {
            ajTokenInstance = instance;
            return ajTokenInstance.balanceOf(App.account);
        }).then(function(balance) {
            App.loading = false;

    //.done(function() {



buyTokens:  function() {

    var numberOfToken = $('#numberOfToken').val();
    App.contracts.AjTokenSale.deployed(function(instance) {
        return instance.buyTokens(numberOfToken, {
            from: App.account, 
            value: numberOfToken * App.tokenPrice,
    }).then(function(result) {
        console.log("Tokens bought..");

$(function() {
$(window).load(function() {

Below is my AjTokenSale code

 pragma solidity ^0.4.2;
 import './AjToken.sol';

contract AjTokenSale {
address admin;
AjToken public tokenContract;
uint256 public tokenPrice;
uint256 public tokensSold;

event Sell(address _buyer, uint256 _amount);

function AjTokenSale(AjToken _tokenContract, uint256 _tokenPrice) public {
    //Assign an Admin
    admin = msg.sender;
    //Token contract
    tokenContract = _tokenContract;
    //Set the Token Price
    tokenPrice = _tokenPrice;


function multiply(uint x, uint y) internal pure returns(uint z) {

    require (y == 0 || (z = x * y)/y == x);


//Buy toekns
function buyTokens(uint256 _numberOfTokens) public payable {
    //Value is equal to token Price

    require(msg.value == multiply(_numberOfTokens , tokenPrice));

    //Require that contract has the enough toekns

    require(tokenContract.balanceOf(this) >= _numberOfTokens);

    //Require that a transfer is successful
    require(tokenContract.transfer(msg.sender, _numberOfTokens));
    //Keep track of tokensSold
    tokensSold += _numberOfTokens;
    //Trigger sell event
    Sell(msg.sender, _numberOfTokens); 

//End token sale 

function endSale() public {
    //only Admin can end the sale
    require(msg.sender == admin);       
    // Transfer remaining tokens back to admin
    require(tokenContract.transfer(admin, tokenContract.balanceOf(this))); 



below is AjToken code

pragma solidity ^0.4.2;

contract AjToken {
string  public name = "AJ Token";
string  public symbol = "AJ";
string  public standard = "AJ Token V1.0 standard";
uint256 public totalSupply;

event Transfer(
    address indexed _from,
    address indexed _to,
    uint256 _value

event Approval(
    address indexed _owner,
    address indexed _spender,
    uint256 _value

mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;

function AjToken (uint256 _initialSupply) public {
    balanceOf[msg.sender] = _initialSupply;
    totalSupply = _initialSupply;

function transfer(address _to, uint256 _value) public returns (bool success) {
    require(balanceOf[msg.sender] >= _value);

    balanceOf[msg.sender] -= _value;
    balanceOf[_to] += _value;

    Transfer(msg.sender, _to, _value);

    return true;

function approve(address _spender, uint256 _value) public returns (bool success) {
    allowance[msg.sender][_spender] = _value;

    Approval(msg.sender, _spender, _value);

    return true;

function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
    require(_value <= balanceOf[_from]);
    require(_value <= allowance[_from][msg.sender]);

    balanceOf[_from] -= _value;
    balanceOf[_to] += _value;

    allowance[_from][msg.sender] -= _value;

    //call transfer event 

    //finally, return boolean

    Transfer(_from, _to, _value);

    return true;

Kindly suggest.

  • @Richard Horrocks May be you can help.. Commented Dec 6, 2018 at 18:37
  • Could you add more details? What transaction are you trying to do? Does it work in Remix? tks Commented Dec 6, 2018 at 21:31
  • @LuizSoares I am creating buy transaction using buytokens method. I tried in Remix it gives same error "Transact to AjSaleToken.buyTokens errored: VM error: revert " Commented Dec 7, 2018 at 14:16

2 Answers 2


I found your problem, but first, here is an advice. Don't use pragma ^0.4.2 . It will open your contract to bugs of 0.4.5 and so on. Use the latest one.

Well, the problem is that your contract is reverting even before transfer. require(tokenContract.balanceOf(this) >= _numberOfTokens);

It is checking balance of the Sale. And it has zero tokens. So, you have to transfer tokens first to Sale. After I transfered some tokens to the Sale, it worked fine using Remix.

And it seems your tests are not 100% correct. Also check them.

I hope it helps.

  • ..Hey Thanks a lot man..i got the issue..yeah definetly i was missing that step...one more query...can we just check the balance of the sale contract require(balanceOf(this) >= _numberOfTokens); this way? Commented Dec 8, 2018 at 16:24

Follow the step to reconnecting host on chrome Metamask pluginenter image description here

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.