Solidity

無效的 JSON RPC 響應:Simple Embark Dapp 中的“400 Bad Request”

  • November 8, 2017

我是 Embark 新手,按照我在https://hackernoon.com/how-to-create-a-token-factory-with-ethereum-part-1-85e84d1f38fc的第一個教程

當我在控制台中執行以下

Token._supply().toNumber()

我得到錯誤

無效的 JSON RPC 響應:“400 錯誤請求”。

我做錯什麼了?

到目前為止,這就是我所做的

  1. embark new TokenFactory&&cd TokenFactory
  2. embark blockchain
  3. embark run
  4. 添加了solidity合約app/contracts/token.sol

然後在登船控制台中,我執行了命令Token._supply().toNumber()

在此處輸入圖像描述

令牌.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);
 }
}

日誌

========================
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

該教程適用於 2.5.2 版的登船,與 2.6.0 版不兼容;要使其與 2.6.0 一起使用,您需要將 js 呼叫轉換為 web3.js 1.0 並更新合約程式碼以與最新的 solc 兼容。本教程將在某個時候更新以反映 2.6.0 中所做的更改。

引用自:https://ethereum.stackexchange.com/questions/30043