I create my contract with the following code:
pragma solidity ^0.5.0;
contract Coin {
function sayHello() public view returns(bytes32) {
return "Hello";
}
}
truffle-config:
module.exports = {
rpc: {
host: "localhost",
port: 8543
},
networks: {
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 8543, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
from: "0x3242587c258109ed8722d667501d2116f08e91d1",
gas: 2100000
},
},
mocha: {
},
compilers: {
solc: {
}
}
}
And I migrate it with truffle migrate
on my private network and it outputs:
=====================
Replacing 'Coin'
----------------
> transaction hash: 0x0c9476750b030a7e8ee35678643573e3f941292d3909bbb33a5248185c307774
> Blocks: 0 Seconds: 0
> contract address: 0x71A258BE260db4397d5AA32aa7F1BcbB7f03C071
> block number: 10230
> block timestamp: 1561343003
> account: 0x3242587C258109eD8722d667501d2116F08e91D1
> balance: 9.87546732
> gas used: 502220
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.0100444 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.0100444 ETH
Summary
=======
> Total deployments: 2
> Final cost: 0.01527226 ETH
So I deployed it successfully, right?
Then I tried to interact with the contract with the following code
const Web3 = require('web3');
const Web3Accounts = require('web3-eth-accounts');
const web3 = new Web3(Web3.givenProvider || 'http://localhost:8543', null, {});
const coinbase = "0x7bd59507b55d46eb874e6a2f8ee4befa123e414e";
const owner = "0x3242587c258109ed8722d667501d2116f08e91d1";
const abi = /*abi*/
var coin = new web3.eth.Contract(abi, "0x71A258BE260db4397d5AA32aa7F1BcbB7f03C071",
{defaultAccount: owner, defaultGasPrice: '20000000'}); // Contract object
const main = async () => {
try {
let reply = await coin.methods.sayHello().call({from: owner});
console.log(reply);
}
catch(error) {
console.error(`error: ${error}`);
}
};
main()
.then(() => { console.log("done"); })
.catch(err => { console.log("Failed with error: " + err); });
It outputted:
null
done
my envs:
"dependencies": {
"web3": "^1.0.0-beta.55",
"web3-eth-accounts": "^1.0.0-beta.55"
}
I started my private network with geth --port 3000 --networkid 58343 --nodiscover --datadir=./blkchain --maxpeers=0 --rpc --rpcport 8543 --rpcaddr 127.0.0.1 --rpccorsdomain "*" --rpcapi "eth,net,web3,personal,miner"
I just start in the blockchain world. Please forgive me if this is a dumb question.
coin
variable.