0

im new to Embark, following my first tutorial here at https://hackernoon.com/how-to-create-a-token-factory-with-ethereum-part-1-85e84d1f38fc

When I run the following in the console

Token._supply().toNumber()

I get the error

Invalid JSON RPC response: "400 Bad Request".

What did I do wrong?

So far, here's what i did

  1. embark new TokenFactory && cd TokenFactory
  2. embark blockchain
  3. embark run
  4. Added solidity contract to app/contracts/token.sol

Then in the embark console, I ran the command Token._supply().toNumber().

enter image description here

token.sol

pragma solidity ^0.4.8;
contract Token {

  event Transfer(address indexed from, address indexed to, uint value);
  event Approval( address indexed owner, address indexed spender, uint value);

  mapping( address => uint ) _balances;
  mapping( address => mapping( address => uint ) ) _approvals;
  uint public _supply;
  function Token( uint initial_balance ) {
    _balances[msg.sender] = initial_balance;
    _supply = initial_balance;
  }
  function totalSupply() constant returns (uint supply) {
    return _supply;
  }
  function balanceOf( address who ) constant returns (uint value) {
    return _balances[who];
  }
  function transfer( address to, uint value) returns (bool ok) {
    if( _balances[msg.sender] < value ) {
      throw;
    }
    if( !safeToAdd(_balances[to], value) ) {
      throw;
    }
    _balances[msg.sender] -= value;
    _balances[to] += value;
    Transfer( msg.sender, to, value );
    return true;
  }
  function transferFrom( address from, address to, uint value) returns (bool ok) {
    // if you don't have enough balance, throw
    if( _balances[from] < value ) {
      throw;
    }
    // if you don't have approval, throw
    if( _approvals[from][msg.sender] < value ) {
      throw;
    }
    if( !safeToAdd(_balances[to], value) ) {
      throw;
    }
    // transfer and return true
    _approvals[from][msg.sender] -= value;
    _balances[from] -= value;
    _balances[to] += value;
    Transfer( from, to, value );
    return true;
  }
  function approve(address spender, uint value) returns (bool ok) {
    // TODO: should increase instead
    _approvals[msg.sender][spender] = value;
    Approval( msg.sender, spender, value );
    return true;
  }
  function allowance(address owner, address spender) constant returns (uint _allowance) {
    return _approvals[owner][spender];
  }
  function safeToAdd(uint a, uint b) internal returns (bool) {
    return (a + b >= a);
  }
}

Logs

========================
Welcome to Embark 2.6.0
========================
Building Assets
loading solc compiler..
compiling contracts...
token.sol:10:3: Warning: No visibility specified. Defaulting to "public".
  function Token( uint initial_balance ) {
  ^
Spanning multiple lines.

token.sol:14:3: Warning: No visibility specified. Defaulting to "public".
  function totalSupply() constant returns (uint supply) {
  ^
Spanning multiple lines.

token.sol:17:3: Warning: No visibility specified. Defaulting to "public".
  function balanceOf( address who ) constant returns (uint value) {
  ^
Spanning multiple lines.

token.sol:20:3: Warning: No visibility specified. Defaulting to "public".
  function transfer( address to, uint value) returns (bool ok) {
  ^
Spanning multiple lines.

token.sol:22:7: Warning: "throw" is deprecated in favour of "revert()", "require()" and "assert()".
      throw;
      ^---^

token.sol:25:7: Warning: "throw" is deprecated in favour of "revert()", "require()" and "assert()".
      throw;
      ^---^

token.sol:32:3: Warning: No visibility specified. Defaulting to "public".
  function transferFrom( address from, address to, uint value) returns (bool ok) {
  ^
Spanning multiple lines.

token.sol:35:7: Warning: "throw" is deprecated in favour of "revert()", "require()" and "assert()".
      throw;
      ^---^

token.sol:39:7: Warning: "throw" is deprecated in favour of "revert()", "require()" and "assert()".
      throw;
      ^---^

token.sol:42:7: Warning: "throw" is deprecated in favour of "revert()", "require()" and "assert()".
      throw;
      ^---^

token.sol:51:3: Warning: No visibility specified. Defaulting to "public".
  function approve(address spender, uint value) returns (bool ok) {
  ^
Spanning multiple lines.

token.sol:57:3: Warning: No visibility specified. Defaulting to "public".
  function allowance(address owner, address spender) constant returns (uint _allowance) {
  ^
Spanning multiple lines.

token.sol:60:3: Warning: Function state mutability can be restricted to pure
  function safeToAdd(uint a, uint b) internal returns (bool) {
  ^
Spanning multiple lines.

deploying contracts
Token already deployed at 0x4dbe9239321ddf4eae6eb32f83c1a50d3c426510
finished deploying contracts
writing file dist/css/app.css
writing file dist/js/app.js
writing file dist/index.html
Watching for changes
ready to watch file changes
1

That tutorial is for version 2.5.2 of embark and is not compatible with version 2.6.0; to make it work with 2.6.0 you need to convert the js calls to web3.js 1.0 and update the contract code to be compatible with the latest solc. The tutorial will be updated at some point to reflect the changes made in 2.6.0.

  • I wonder do you know any good guides to porting web3.js beta to version 1.0? – Tomachi Apr 18 '18 at 0:28

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.