0

I am new to DApp development. I have deployed a TokenFactory contract and that Factory contract has a function "Create New Token" that generates a new token contract. The function "Create New Token" is working fine in remix IDE but I need to attach a GUI with this. All other functions are working fine with GUI but when i click on the "Create New Token" but it is giving me error. Below is the Factory Contract Code :

    pragma solidity ^0.4.18;

    import "./TestToken.sol";
    //import "./NewCrowdsale.sol";

    contract FactoryToken {

      // index of created contracts

      address[] public contracts;

       mapping (address => bool) created;
      //Crowdsale public theObj;
      address newContract;


      string public  name ;
      string public  symbol ;
      uint256 public  decimals ;

      // useful to know the row count in contracts index

      function getContractCount() public constant returns(uint contractCount)
      {
        return contracts.length;
      }

       function setToken(string _Name, string _Symbol, uint256 _Decimal) public {
          name = _Name ;
          symbol = _Symbol;
          decimals= _Decimal;
       }

       function getToken() public constant returns (string,string, uint256) {
           return (name, symbol,decimals);
       }


      // deploy a new contract

       function newToken() public returns(address objAddress)
  {
       newContract = new WILDToken(name,symbol,decimals);
   contracts.push(newContract);


        return newContract;



  }

       function verify() public constant returns (address) {
            return newContract;
        }
      //function GetAddress() public view returns (address) {

       // return (c);
      //}
    }

And here is the code that i have written for testrpc and web3

I have a button, and by clicking on that button it calls the newToken function to create a new token contract which is now giving me error.

 $("#create").click(function () {

           Coursetro.newToken(3,{from:web3.eth.accounts[0],gas: 470000});
        });

Any Help will be appreciated. Thanks

  • Can you share also the error that is displayed in order to allow us understand where and why it fails ? Thanks – Asone Jan 25 '18 at 9:18
  • with this code, I am getting the error "Invalid number of arguments to Solidity function". Also plz explain this line as well Coursetro.newToken(3,{from:web3.eth.accounts[0],gas: 470000}); I am calling newToken function but don't know what is number 3 that i am passing in arguments. I have copied this line of code from some where – usman mehmood Jan 25 '18 at 9:24
0

Note that you are passing a parameter in your instruction :

Coursetro.newToken(3,{from:web3.eth.accounts[0],gas: 470000})

when your smart-contract method does not declare any :

function newToken() public returns(address objAddress){ newContract = new WILDToken(name,symbol,decimals); contracts.push(newContract); return newContract; }

Also note that you are calling WILDToken which is not declared anywhere in the code you provided.

In order to find easily errors in your code you can use remix IDE that is helpful in notifying errors and warnings when writing smart-contract

EDIT :

Instead of having a method to set parameters of your token you'd better implement variables to pass in your method that creates the tokens, this way you'll only have to call one method to generate it instead of calling two.

It should give something like this :

function newToken(string _name, string _symbol, uint _decimals) public returns(address objAddress){

    /* Below WILDToken will have to be replaced with the Contract that has to be instanciated. 
    /* Note that in order to have clean code you'd better name your files with the Contract name. 

    newContract = new WILDToken(_name,_symbol,_decimals);

    contracts.push(newContract);

    /* Note that in Web3, the returned value by Method is not accessible. You can delete it if you don't intend to have another contract calling this method. If you do so, don't forget to delete the returns declaration also. */ 

    return newContract;
}

on the front-end, imagining you your instance of the contract would be CourseTro you would have something like this :

var a = web3.eth.accounts[0];
var n = 'MyTokenName';
var s = 'TknSbl';
var d = 18;
Coursetro.newToken(n,s,d,{from : a, gas : '1000000' }).then(function(r){
    console.log('Transaction succeed, see returned info below :'); 
    console.log(r);
}).catch(function(e){
    console.log('Transaction failed, see logs below :');
    console.log(e);
});
  • WildToken is declared in TestToken.sol which is inherited at the top and newToken function is working fine in remix IDE as i have mentioned earlier but i need to call this function from my DApp (Web GUI). Can you please explain or provide me the code for calling "newToken" function form web GUI ? – usman mehmood Jan 25 '18 at 10:48
  • added as requested – Asone Jan 25 '18 at 11:12
  • I have tried your code, but that code is giving me error "Coursetro.newToken(...).then is not a function". – usman mehmood Jan 25 '18 at 11:45
  • it was an example. The 'then' and catch instructions relies on ES6 ( i've been doing most of my front-end in typescript which allow to use easily then/catch ). You'll probably have to adapt the code to be handled by your scripts, getting rid of the then/catch and rewrite it in a proper way. just know that the then instruction is usually triggered when you get a response from the request and the catch is triggered when request fails. – Asone Jan 25 '18 at 12:10
  • Promises in javascript : developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… – Asone Jan 25 '18 at 12:13

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.