0

I am a beginner for solidity 👶

My question about deploy using with web3js.

I have two smart contract and in main contract I have a createContract function which is create a new subtoken contract and return me a contact address.

after this I deploy SubToken contact at this address. and I can check my symbol, name etc. ✅

I can do all of things on Remix but the problem is when I decided to use web3js I feel little bit confused 😐 🤷‍♂️

My createContract function doesn't work and I don't know why. I saw a error like this:

(node:34806) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:34806) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Anyone can help me please 😊🙏

A few screenshots for what I try to do with web3js:

createContract function : https://eksiup.com/p/ir92373iheta

I deploy subToken same address : https://eksiup.com/p/ro923749zyrn

Here's my smart contract :

pragma solidity ^0.5.0 ;  
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol'; 

contract SubToken{  
    MainContract mc; 

    constructor(address _address) public payable {   
       mc= MainContract(_address); 
    }   

    function name() view public returns(string memory){ 
        return mc.name(address(this)); 
    } 
    function symbol() view public returns(string memory){ 
        return mc.symbol(address(this)); 
    } 
    function decimal() view public returns(uint){ 
        return mc.decimal(address(this)); 
    } 
    function totalSupply() view public returns(uint){ 
        return mc.totalSupply(address(this)); 
    } 

    function balanceOf(address account) view public returns(uint){ 
        return mc.balanceOf(address(this), account); //msg.sender
    } 

    function transfer(address recipient , uint amount) public returns (bool){ 
        mc.transfer(address(this), msg.sender, recipient, amount);
        emit Transfer(msg.sender, recipient, amount);
        return true; 
    } 

    event Transfer(address indexed from, address indexed to, uint tokens);  
    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);  

}  
contract MainContract { 

    using SafeMath for uint; 

    struct token{  
        string  name;  
        string   symbol;  
        uint8  decimals;  
        uint  totalSupply;  
        mapping (address => uint)  _balances;
        mapping (address => mapping (address => uint256))  _allowances;
    }

    mapping(address => token) public tokens; 

    function createNewContract(string memory _name,string memory _symbol,uint8 _decimals,uint _totalSupply) public returns(address youraddress){  
        SubToken st = new SubToken(address(this));
        tokens[address(st)] = token(_name,_symbol,_decimals,_totalSupply);
        tokens[address(st)]._balances[msg.sender]=_totalSupply;
        return address(st);
    }  

    function name(address _concractAddress) public view returns(string memory){ 
        return tokens[_concractAddress].name; 
    } 
    function symbol(address _concractAddress) public view returns(string memory){ 
        return tokens[_concractAddress].symbol; 
    } 
    function decimal(address _concractAddress) public view returns(uint8){ 
        return tokens[_concractAddress].decimals; 
    } 
    function totalSupply(address _concractAddress) public view returns(uint){ 
        return tokens[_concractAddress].totalSupply; 
    } 
    function balanceOf(address _concractAddress, address account) public view returns(uint){
        return tokens[_concractAddress]._balances[account];
    }

    function transfer(address _concractAddress, address sender, address recipient, uint amount) public { 
       require(sender != address(0));
       require(recipient != address(0));

       tokens[_concractAddress]._balances[sender] =  tokens[_concractAddress]._balances[sender].sub(amount);
       tokens[_concractAddress]._balances[recipient] =  tokens[_concractAddress]._balances[recipient].add(amount);

       emit Transfer(sender, recipient, amount); 
    } 


    event Transfer(address indexed _from, address indexed _to, uint256 _value); 
    event Approval(address indexed _owner, address indexed _spender, uint256 _value); 

} 

Here's the web3js

var createNewContractFunction = function(cb,_jsonFile = JSONFile, _privateKey=privateKey, _account=account){
    var abi = JSONFile.abi;

    //contract = new web3.eth.Contract(abi);



    web3.eth.getTransactionCount(account, (err,txCount) => {

        var createNewContractData = contract.methods.createNewContract("FurkanToken", "FT", 18, 99).encodeABI();
        if(err){
            cb(err);
            return;
        }
        txObject ={
            //to:_contractAddress,
            nonce: web3.utils.toHex(txCount), //value : 0x0
            value: 0x0,//3 ether
            gasLimit: web3.utils.toHex('2100000'),
            gasPrice: web3.utils.toHex(web3.utils.toWei('70','gwei')),
            data: createNewContractData

        }
        //console.log(txObject);
        //2 sign transaction with my private key

        tx = new Tx(txObject,{chain :'ropsten'});
        tx.sign(privateKey);

        serializedTransaction =tx.serialize();
        raw = '0x'+ serializedTransaction.toString('hex');

        //3 broadcast the transaction to ropsten
        web3.eth.sendSignedTransaction(raw, (err,txHash) => {
            cb(err,txHash);
            // if(err){
            //     console.log('your err is : ',err);
            //     return;
            // }

        })

    }) 
}
6
  • 1
    Your web3.js script is all wrong. To begin with, your call to contract.methods.createNewContract returns a promise which you need to resolve. You can either await for this promise to complete, or you can pass a callback function to handle the promise. – goodvibration Aug 26 '19 at 10:04
  • Thank you but I dont know how can I use awit and promise.Can you give me example or source where I can learn ? Can you write correct answer on my code if you do this for me I will be grateful :) thanks again 😊 – Furkan Demirel Aug 27 '19 at 9:05
  • 1
    Currently, you have commented out to part from your transaction. Try using contract.methods.createNewContract("FurkanToken", "FT", 18, 99).send() instead of sendinig raw transaction. – Igor Sobolev Aug 27 '19 at 14:17
  • eksiup.com/p/cy93623xszoc @IgorSobolev hey thanks for comment I modified this but still have problem with async can you help me answer my question with code not comment please :)🙏 – Furkan Demirel Aug 27 '19 at 16:10
  • @goodvibration when I try catch or then I saw an error says then is not a function 😯 Can you help me please 🙏 – Furkan Demirel Aug 27 '19 at 16:13

Your Answer

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

Browse other questions tagged or ask your own question.