Call The Contract

$ vim call.js
const Web3 = require('web3');
const fs = require('fs');
const solc = require('solc');

// connect to chain
const ethereumUri = 'http://localhost:8545';
const accountAddress = '2a7a93ca16e95a6e4478bf67e33ce3a9c20bf66d';
const accountPassword = 'node1';
const contractAddress = '0xce5b2a924367a7c7f638b1c269518d6064bd0e93';
const contractABI = [ {
    "constant": false,
    "inputs": [
        {
            "name": "a",
            "type": "uint256"
        }, {
            "name": "b",
            "type": "uint256"
        }
    ],
    "name": "add",
    "outputs": [ {
        "name": "",
        "type": "uint256"
    } ],
    "payable": false,
    "type": "function"
    }, {
    "constant": true,
    "inputs": [],
    "name": "getCount",
    "outputs": [ {
        "name": "",
        "type": "uint256"
    } ],
    "payable": false,
    "type": "function"
} ];

const web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider(ethereumUri));

if(!web3.isConnected()) {
    throw new Error(`unable to connect to ethereum node at ${ethereumUri}`);
} else {
    console.log(`connected to ehterum node at ${ethereumUri}`);
    const coinbase = web3.eth.coinbase;
    console.log(`coinbase: ${coinbase}`);
    const balance = web3.eth.getBalance(coinbase);
    console.log(`balance: ${web3.fromWei(balance, 'ether')} ETH`);
    console.log(web3.eth.accounts); 

    if (web3.personal.unlockAccount(accountAddress, accountPassword)) {
        console.log(`${accountAddress} is unlocaked`);
    } else {
        console.log(`unlock failed, ${accountAddress}`);
    }
}

const MyContract = web3.eth.contract(contractABI);
const contractInstance = MyContract.at(contractAddress);
const addition = contractInstance.add.sendTransaction(1, 2, {
    from: accountAddress
});
console.log(`addition ${addition}`);
console.log("after contract deploy, call:" + contractInstance.getCount.call());
$ node call.js
connected to ehterum node at http://localhost:8545
coinbase: 0x2a7a93ca16e95a6e4478bf67e33ce3a9c20bf66d
balance: 9.04625697166532776746648320380374280103671755200316906558258575081821325312e+56 ETH
[ '0x2a7a93ca16e95a6e4478bf67e33ce3a9c20bf66d' ]
2a7a93ca16e95a6e4478bf67e33ce3a9c20bf66d is unlocaked
addition 0xb8373824ef5c5d6cdcf139b415b33e072d2fcbc7b9977a57b5fa75203b0f7b00
after contract deploy, call:2

使用Node.js部署智能合約(Smart Contract)

results matching ""

    No results matching ""