I. Overview
In the above article has explained the WEB3 and Geth interaction, the following will use WEB3 to visit our already deployed token contract
second, the application of Practice 1. New File
Create a new Contract.js file under the Nodejs project
2. Create a contract instance
WEB3 = require ("web3") var web3 = new Web3 (web3.givenprovider| | '
http://127.0.0.1:8545 ');
Web3.setprovider (' http://127.0.0.1:8545 ');
var mycontract = new Web3.eth.Contract ([{"Constant": true, ' inputs ': [{' name ': '] ', ' type ': ' Address '
}], "name": "Balanceof", "outputs": [{"Name": "," type ":" uint256 "}]," payable ": false, ' statemutability ': ' View ', ' type ': ' function '}, {' constant ': false, ' inputs ': [{' Name ': ' _to ', ' Typ E ":" Address "}, {" Name ":" _value "," type ":" uint256 "}]," name ":" Transfer "," outputs ": []," p
Ayable ": false," statemutability ":" Nonpayable "," type ":" Function "}, {" Constant ": false," inputs ": [{ ' Name ': ' Addr ', ' type ': ' Address '} ', ' name ': ' getbalance ', ' outputs ': [{' Name ': ' ', ' type ': '
Uint256 "}]," payable ": false," statemutability ":" Nonpayable "," type ":" Function "}, {" Inputs ": [{ "Name": "InItialsupply ', ' type ': ' uint256 '}], ' payable ': false, ' statemutability ': ' nonpayable ', ' type ': ' Constructo R "}], ' 0x7b05b7bca697f821e94de75f62005c13b66f4575 ', {from: ' 0xa5d4725d9dc3f7e73818936abe151602ad6d26fa ',///Defaul T from address gasprice: ' 20000000000 '//default gas price in Wei, Gwei in this case});
The code above uses the ABI and the contract address to build a contract-only instance
which
0x7b05b7bca697f821e94de75f62005c13b66f4575
Is the address of the contract, in the deployment contract can see 3, call the contract Balanceof method view account balance
MyContract.methods.balanceOf (' 0xa5d4725d9dc3f7e73818936abe151602ad6d26fa '). Call (). Then (function (r) {
Console.log (' Show the Custom account Balance: ');
Console.log (R)
})
4, the use of intelligent contracts to initiate transactions
MyContract.methods.transfer (' 0xa5d4725d9dc3f7e73818936abe151602ad6d26fa ', m). Send (). Then (function (r) {
Console.log (' Use of the contract to send coin: ');
Console.log (R)
})
5. Check the balance of the two parties ' contracts again
Code slightly