1

//Contract build .json to js obj
import { abi } from './contract.js'

//Init web3
import { getWeb3 } from  './getWeb3'

// Import are working fine.

export const getTokenContract = () => (
  new Promise((resolve, reject) => {
    getWeb3.then(web3 => { //Get web3
      var contract = web3.eth.contract(abi) //Instance contract with json abi
      var instance = contract.at("0x918daf9d0bd5c927ca8f1c5776e7c0c200be0fc23994dbe34dd9c9669c6a900d") //Get adress of the contract
      console.log(contract)
      resolve(instance)
    })
  })
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.1/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.1/react-dom.min.js"></script>

我尝试访问实例内的智能合约功能。当我 console.log 时,abi 中的功能正确显示。

该地址没有可用的功能。举个例子:

实例.getFidelity...

getFidelity 没有定义,所以崩溃。

  • testrpc 启动。
  • 松露编译
  • 松露迁移(成功部署在 0x918...)

阿比json:

// Token Interface
export const Token = [
  {
    "contractName": "MetaCoin",
    "abi": [
      {
        "constant": false,
        "inputs": [
          {
            "name": "addr",
            "type": "address"
          }
        ],
        "name": "getFidelity",
        "outputs": [
          {
            "name": "",
            "type": "uint256"
          }
        ],
        "payable": false,
        "type": "function"
      },
      {
        "constant": true,
        "inputs": [],
        "name": "owner",
        "outputs": [
          {
            "name": "",
            "type": "address"
          }
        ],
        "payable": false,
        "type": "function"
      },
      {
        "constant": false,
        "inputs": [
          {
            "name": "receiver",
            "type": "address"
          },
          {
            "name": "amount",
            "type": "uint256"
          }
        ],
        "name": "sendCoin",
        "outputs": [
          {
            "name": "sufficient",
            "type": "bool"
          }
        ],
        "payable": false,
        "type": "function"
      },
      {
        "constant": false,
        "inputs": [],
        "name": "redeemFidelity",
        "outputs": [
          {
            "name": "sufficient",
            "type": "bool"
          }
        ],
        "payable": false,
        "type": "function"
      },
      {
        "constant": false,
        "inputs": [
          {
            "name": "newOwner",
            "type": "address"
          }
        ],
        "name": "transferOwnership",
        "outputs": [],
        "payable": false,
        "type": "function"
      },
      {
        "constant": false,
        "inputs": [
          {
            "name": "addr",
            "type": "address"
          }
        ],
        "name": "getBalance",
        "outputs": [
          {
            "name": "",
            "type": "uint256"
          }
        ],
        "payable": false,
        "type": "function"
      },
      {
        "inputs": [],
        "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": "_from",
            "type": "address"
          }
        ],
        "name": "Fidelity",
        "type": "event"
      },
      {
        "anonymous": false,
        "inputs": [
          {
            "indexed": true,
            "name": "previousOwner",
            "type": "address"
          },
          {
            "indexed": true,
            "name": "newOwner",
            "type": "address"
          }
        ],
        "name": "OwnershipTransferred",
        "type": "event"
      }
    ]
    }

  ];

ERC20 智能合约

pragma solidity ^0.4.4;

import "./Ownable.sol";

contract MetaCoin is Ownable {

	mapping (address => uint) balances;
  mapping (address => uint) fidelity;

	event Transfer(address indexed _from, address indexed _to, uint256 _value);
	event Fidelity(address indexed _from);

	function MetaCoin() {
		balances[tx.origin] = 10000;
    fidelity[tx.origin] = 100;
	}

	function sendCoin(address receiver, uint amount) returns(bool sufficient) {
		if (balances[msg.sender] < amount) return false;
		balances[msg.sender] -= amount;
		balances[receiver] += amount;
    if (receiver == owner && amount > 10) {
      fidelity[msg.sender] += 1;
    }
    Transfer(msg.sender, receiver, amount);
		return true;
	}

	function redeemFidelity() returns (bool sufficient) {
		if (fidelity[msg.sender] < 10) return false;
		fidelity[msg.sender] -= 10;
		Fidelity(msg.sender);
		return true;
	}

	function getBalance(address addr) returns(uint) {
		return balances[addr];
	}

  function getFidelity(address addr) returns(uint) {
		return fidelity[addr];
	}

}

4

0 回答 0