6

I am newbie in solidity. I don't understand what steps I must do for deploy contract in the Ropsten test-net.

This is my contract:

pragma solidity ^0.4.15;

import './ERC20.sol';
import './SafeMath.sol';

contract StepanToken2 is ERC20{
    using SafeMath for uint256;

    address public ownerFirst;
    address public ownerSecond;

    string public constant name = "Stepan Token";
    string public constant symbol = "SPT";
    uint8 public constant decimals = 8; 
    uint totalTokens = 100000000000000;


    //balances for accaunts
    mapping(address => uint) balances;

    //Owner of account approves the transfer of an amount to another account
    mapping(address => mapping(address => uint)) allowed;


    function StepanToken2 (address _ownerFirst, address _ownerSecond){
       ownerFirst = _ownerFirst;
       ownerSecond = _ownerSecond;

       balances[ownerFirst] = SafeMath.div(totalTokens, 3);
       balances[ownerSecond] = SafeMath.sub(totalTokens, balances[ownerFirst]);
    }

    function totalSupply() constant returns (uint256 totalSupply){
         return totalTokens;
    }

    function balanceOf(address _owner) constant returns (uint256 balance){
         return balances[_owner];
    }


    function transfer(address _to, uint _value)  returns (bool success){
            require(balances[msg.sender] >= _value && _value > 0 && SafeMath.add(balances[_to], _value) > balances[_to]);
                balances[msg.sender] = SafeMath.sub(balances[msg.sender],_value);
                balances[_to] = SafeMath.add(balances[_to], _value);
                Transfer(msg.sender,  _to, _value);
                return true;     
    }

    function transferFrom(address _from, address _to, uint _value) returns (bool success){
        require(balances[_from] >= _value && allowed[_from][msg.sender] >= _value && _value > 0 && SafeMath.add(balances[_to], _value) > 0);
            balances[_from] = SafeMath.sub(balances[_from], _value);
            allowed[_from][msg.sender] = SafeMath.sub(allowed[_from][msg.sender], _value);
            balances[_to] = SafeMath.add(balances[_to], _value);
            Transfer(_from, _to, _value);
            return true;
    }


    function approve(address _spender, uint _value)  returns (bool success){
        allowed[msg.sender][_spender] = _value;
        Approval(msg.sender, _spender,  _value);
        return true;
    }

    function allowance(address _owner, address _spender) constant returns (uint256 remaining){
        return allowed[_owner][_spender];
    }

}

And it compiles and migrates normal. When contract will compiling in ropsten I should set parameters in constructor contract. I have created an HTML file with JS for this contract:

var Web3 = require('web3'); var web3 = new Web3();

    function initConnectWeb3() {

            web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/[infura_api_key]"));


        console.log(web3);
    }

    initConnectWeb3();

    primaryAddress = web3.eth.accounts[0];
    var MyContract = web3.eth.contract([{
            "constant": true,
            "inputs": [],
            "name": "name",
            "outputs": [{
                "name": "",
                "type": "string"
            }],
            "payable": false,
            "type": "function"
        },
        {
            "constant": false,
            "inputs": [{
                    "name": "_spender",
                    "type": "address"
                },
                {
                    "name": "_value",
                    "type": "uint256"
                }
            ],
            "name": "approve",
            "outputs": [{
                "name": "success",
                "type": "bool"
            }],
            "payable": false,
            "type": "function"
        },
        {
            "constant": true,
            "inputs": [],
            "name": "totalSupply",
            "outputs": [{
                "name": "totalSupply",
                "type": "uint256"
            }],
            "payable": false,
            "type": "function"
        },
        {
            "constant": false,
            "inputs": [{
                    "name": "_from",
                    "type": "address"
                },
                {
                    "name": "_to",
                    "type": "address"
                },
                {
                    "name": "_value",
                    "type": "uint256"
                }
            ],
            "name": "transferFrom",
            "outputs": [{
                "name": "success",
                "type": "bool"
            }],
            "payable": false,
            "type": "function"
        },
        {
            "constant": true,
            "inputs": [],
            "name": "decimals",
            "outputs": [{
                "name": "",
                "type": "uint8"
            }],
            "payable": false,
            "type": "function"
        },
        {
            "constant": true,
            "inputs": [],
            "name": "ownerFirst",
            "outputs": [{
                "name": "",
                "type": "address"
            }],
            "payable": false,
            "type": "function"
        },
        {
            "constant": true,
            "inputs": [{
                "name": "_owner",
                "type": "address"
            }],
            "name": "balanceOf",
            "outputs": [{
                "name": "balance",
                "type": "uint256"
            }],
            "payable": false,
            "type": "function"
        },
        {
            "constant": true,
            "inputs": [],
            "name": "symbol",
            "outputs": [{
                "name": "",
                "type": "string"
            }],
            "payable": false,
            "type": "function"
        },
        {
            "constant": false,
            "inputs": [{
                    "name": "_to",
                    "type": "address"
                },
                {
                    "name": "_value",
                    "type": "uint256"
                }
            ],
            "name": "transfer",
            "outputs": [{
                "name": "success",
                "type": "bool"
            }],
            "payable": false,
            "type": "function"
        },
        {
            "constant": true,
            "inputs": [{
                    "name": "_owner",
                    "type": "address"
                },
                {
                    "name": "_spender",
                    "type": "address"
                }
            ],
            "name": "allowance",
            "outputs": [{
                "name": "remaining",
                "type": "uint256"
            }],
            "payable": false,
            "type": "function"
        },
        {
            "constant": true,
            "inputs": [],
            "name": "ownerSecond",
            "outputs": [{
                "name": "",
                "type": "address"
            }],
            "payable": false,
            "type": "function"
        },
        {
            "inputs": [{
                    "name": "_ownerFirst",
                    "type": "address"
                },
                {
                    "name": "_ownerSecond",
                    "type": "address"
                }
            ],
            "payable": false,
            "type": "constructor"
        },
        {
            "anonymous": false,
            "inputs": [{
                    "indexed": true,
                    "name": "_from",
                    "type": "address"
                },
                {
                    "indexed": true,
                    "name": "_to",
                    "type": "address"
                },
                {
                    "indexed": false,
                    "name": "_value",
                    "type": "uint256"
                }
            ],
            "name": "Transfer",
            "type": "event"
        },
        {
            "anonymous": false,
            "inputs": [{
                    "indexed": true,
                    "name": "_owner",
                    "type": "address"
                },
                {
                    "indexed": true,
                    "name": "_spender",
                    "type": "address"
                },
                {
                    "indexed": false,
                    "name": "_value",
                    "type": "uint256"
                }
            ],
            "name": "Approval",
            "type": "event"
        }
    ]);

    // contact = MyContract.new(0xC0b4ec83028307053Fbe8d00ba4372384fe4b52B, 0x4E90a36B45879F5baE71B57Ad525e817aFA54890, {from: primaryAddress,  gas: 50000});
    contract = MyContract.new("0xC0b4ec83028307053Fbe8d00ba4372384fe4b52B", "0x4E90a36B45879F5baE71B57Ad525e817aFA54890", {
        from: primaryAddress,
        gas: 50000
        // data: evmCode
    }, function (err, contract) {
        if (!err && contract.address)
            console.log(contract.address);
    });

I have added changes to my script

And now I have problem with: enter image description here

2

5 Answers 5

7

You can try this NodeJS solution:

module.paths.push('/usr/lib/node_modules');

var fs = require('fs');
const contract_data = JSON.parse(
    fs.readFileSync('path to your JSON file')
);


var openKey = "open Key (address)";
var privateKey = "privateKey";


var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/ "));

web3.eth.accounts.wallet.add("0x" + privateKey);

var contract = new web3.eth.Contract(contract_data.abi);

contract.deploy({
    data: contract_data.unlinked_binary,
    arguments: []
}).send({
    from: openKey,
    gas: 1500000,
    gasPrice: '80000000'
}, function (error, transactionHash) {

}).on('error', function (error) {
    console.log('error', error);
}).on('transactionHash', function (transactionHash) {
    console.log('transactionHash', transactionHash);
}).on('receipt', function (receipt) {
    console.log('receipt', receipt.contractAddress);
}).on('confirmation', function (confirmationNumber, receipt) {
    console.log('confirmation', confirmationNumber);
});
1
  • On web3 0.20.1, I am getting error TypeError: Cannot read property 'add' of undefined on the line web3.eth.accounts.wallet.add(...)... Is it different for 0.20.1?
    – Nyxynyx
    Nov 6, 2017 at 3:08
5

You can deploy this contract in another way:

var Web3 = require('web3');
var util = require('ethereumjs-util');
var tx = require('ethereumjs-tx');
var web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/...'));
var address = '...';
var key ='...';
var bytecode = '...';
var interface = [...];

function sendRaw(rawTx) {
    var privateKey = new Buffer(key, 'hex');
    var transaction = new tx(rawTx);
    transaction.sign(privateKey);
    var serializedTx = transaction.serialize().toString('hex');
    web3.eth.sendRawTransaction(
    '0x' + serializedTx, function(err, result) {
        if(err) {
            console.log(err);
        } else {
            console.log(result);
        }
    });
}

var rawTx = {
    nonce: web3.toHex(web3.eth.getTransactionCount(address)),
    gasLimit: web3.toHex(1500000),
    gasPrice: web3.toHex(80000000),
    data: bytecode 
};
sendRaw(rawTx);
3

at first glance, i think your mistake is using addresses without quotes "" or ' in this line :

MyContract.new([0xC0b4ec83028307053Fbe8d00ba4372384fe4b52B, 0x4E90a36B45879F5baE71B57Ad525e817aFA54890], {}...

besides you have to provide two addresses not an array of addresses

MyContract.new('0xC0b4ec83028307053Fbe8d00ba4372384fe4b52B', '0x4E90a36B45879F5baE71B57Ad525e817aFA54890', {}...

as the constructor :

 function StepanToken2 (address _ownerFirst, address _ownerSecond)

accepts 2 arguments not an array.

3
  • I changed my code and I have same problem which I had on picture. Oct 24, 2017 at 11:15
  • try MyContract.new('0xC0b4ec83028307053Fbe8d00ba4372384fe4b52B', '0x4E90a36B45879F5baE71B57Ad525e817aFA54890', {}... without array brackets
    – Badr Bellaj
    Oct 24, 2017 at 12:17
  • THanks I do it the same as you. But in result I have error "Invalid address" My code contract = MyContract.new('0xC0b4ec83028307053Fbe8d00ba4372384fe4b52B', '0x4E90a36B45879F5baE71B57Ad525e817aFA54890', { from: primaryAddress, gas: 50000 // data: evmCode }, function (err, contract) { if (!err && contract.address) console.log(contract.address); }); Oct 25, 2017 at 8:12
2

You have to set your default address. Use the following command to set that web3.eth.defaultAccount = web3.eth.accounts[0];. And also make sure your account is unlocked using web3.personal.unlockAccount().

1

I've put together a suite of scripts for interacting with smart contracts (the Solidity variant) at https://github.com/fixone/ethdemo. You can check the steps in https://github.com/fixone/ethdemo/blob/master/compile-sign-deploy.js and please note that it assumes that the solc binary is invoked (in the 'exec' function to compile the contract)

1
  • Thanks for adding the scripts, but also consider answering the question by pointing what should be modified in order for the question to be answered. (I am aware that the question is also lacking vital information) Oct 23, 2017 at 19:33

Your Answer

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

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