0

I am following this tutorial on youtube "Code Your Own Cryptocurrency on Ethereum (Full)" i stuck at one of the part where i am trying to return the value of TokensSold but i keep on getting error as

"TypeError: ajtokenSaleInstance.tokensSold is not a function"

The same is working for the instructor is the refrence link https://youtu.be/XdKv5uwEk5A?t=22108

Below is my code, please help

App = {
web3Provider: null,
contracts: {},
account: '0x0', 
loading: false,
tokensSold:0,
tokenPrice:1000000000000000,    
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;
        //console.dir(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);
        App.contracts.AjTokenSale.setProvider(App.web3Provider);
        App.contracts.AjTokenSale.deployed().then(function(ajtokensale){
            //return ajtokensale.tokenPrice()
            //console.dir('dir is' + ajtokensale.tokenPrice());
            console.log('AJTOKEN sale address is ' + ajtokensale.address);
            //console.dir(ajtokensale);
        }); 
    }).done(function() {
      $.getJSON("AjToken.json", function(ajtoken) {
        App.contracts.AjToken = TruffleContract(ajtoken);
        App.contracts.AjToken.setProvider(App.web3Provider);
        App.contracts.AjToken.deployed().then(function(ajtoken){
            console.log('AJ token  address ' + ajtoken.address);
        });
        return App.render();
      });
     });

},

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

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

    loader.show();
    content.hide();

    // 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;
        //console.log(ajtokenSaleInstance);
        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;
        $('.tokens-sold').html(App.tokensSold);
        $('.token-available').html(App.tokensAvilable);

        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) {
            $('.ajtoken-balance').html(balance.toNumber());
            App.loading = false;
            loader.hide();
            content.show();
        })
    });

    //.done(function() {


    //});

},

buyTokens:  function() {
    $('#content').hide();
    $('#loader').show();

    var numberOfToken = $('#numberOfToken').val();
    App.contracts.AjTokenSale.deployed(function(instance) {
        return instance.buyTokens(numberOfToken, {
            from: App.account, 
            value: numberOfToken * App.tokenPrice,
            gas:500000
        });
    }).then(function(result) {
        console.log("Tokens bought..");
        $('form').trigger('reset');
        $('#loader').hide();
        $('#content').show();
    })
}}


$(function() {
$(window).load(function() {
    App.init();
});
}); 

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 tokenSold;

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;
}    

//Multiply

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 tokenSold
    tokenSold += _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))); 
    admin.transfer(address(this).balance);

}

}

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;
}
}
  • 1
    Can you please attach your contract code too? – Aniket Dec 3 '18 at 7:49
  • i have added the code..please have a look. – Ajas Bakran Dec 3 '18 at 18:29
  • Hi @user1576448 - you've attached the Javascript test code. We'll need the actual Solidity code. (i.e. .sol files.) – Richard Horrocks Dec 3 '18 at 18:44
  • @RichardHorrocks my mistake, sorry for that. I have updated the code – Ajas Bakran Dec 3 '18 at 18:56
0

Looks like a typo.

Error (extra "s"):

"TypeError: ajtokenSaleInstance.tokensSold is not a function"

Contract:

uint256 public tokenSold;

  • ..Thanks a lot man..it worked..:)...unfortunately cant upvote answer due to reputation constraint.. – Ajas Bakran Dec 3 '18 at 19:14
  • No problem - glad it worked :-) – Richard Horrocks Dec 3 '18 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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