Ethernet Square Intelligent Contract Coin application Development (5)-WEB3 call Smart token contract __ Block chain

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.