Nodejs How to deploy Smart contracts-WEB3 version 0.20

Source: Internet
Author: User
Tags etherscan

Reference: Https://www.jianshu.com/p/7e541cd67be2

There are many ways to deploy a smart contract, such as using the truffle framework, using remix-ide, and so on, where the deployment method is implemented using NODEJS step-up deployment:

Const WEB3 = require (' WEB3 '); Const WEB3=NewWEB3 ();//using WEB3 to make a call to the Blockchain interface//connecting the blockchain via RPC, where the local private chain is connectedWeb3.setprovider (NewWeb3.providers.HttpProvider (' http://127.0.0.1:7545 '));Const FS = require ("FS"); Const SOLC= Require ("Solc"); Let source= Fs.readfilesync ("Token.sol", ' UTF8 ');//Read the Sol Smart contract file//Compile the smart contract, the second parameter is set to 1 to activate the optimizer optimiserlet compiledcontract = Solc.compile (source,1); for ( Let Contractname in compiledcontract.contracts) {var bytecode = compiledcontract.contracts[contractname].bytecode;//won The bytecode var abi = Json.parse (compiledcontract.contracts[contractname].interface) of the post-compilation contract; Obtain the ABI of the post-compilation contract and write it in json form}let gasestimate = Web3.eth.estimateGas ({data: ' 0x ' +bytecode});//Get this contract to deploy probably the Gaslet needed Mycontract = Web3.eth.contract (ABI);

The above is the method of compiling the ABI, and then there are two ways to use it:
(1)
When never deployed, the gas example here is gas:gasestimate, but I find that sometimes it seems too small to be successful, so every time I make sure I can be absolutely successful, I set it to a very large number, which will come back anyway, and you can set it to gas: Gasestimate, but if there's something wrong in the back, it's probably the problem here.

    var instance = Mycontract. New (50,{from:user1,data: ' 0x ' +bytecode,gas:47000000},function(e,contract) {             if(  typeof contract.address!== ' undefined ') {                console.log (' contract mined! Address: ' + contract.address + ' Transactionhash: ' + contract.transactionhash);            }    });


You can then use instance to invoke the functions in the smart contract.

(2)

When already deployed in the private chain is obsolete, the inside address is the above method to get the contract.address

Let instance = mycontract.at (' 0x86757c9bdea10815e7d75a1577b6d9d2825dae0a '); // can be changed

But the problem with this approach is that when you publish your contract, your private key is networked,
In security policy, we need to try to avoid the private key in the networked state.

An Ethereum deployment contract is to send a signed transaction with a byte code to an empty address, where the sender is the owner of the contract.

So we just need to build the contract into a deal, we sign the deal without a network , and then send the signature to the Ethereum network. This can reduce the risk of our private key being compromised.
Here to use the method for contract deployment, refer to: https://my.oschina.net/u/3794778/blog/1801897

Web3.eth.sendRawTransaction (Signedtransactiondata [, callback]) parameter:    signedtransaciondata:string-  16 signed transaction data in the binary format.    callback:function- callback function, which is used to support asynchronous execution of 7. Return value:    string- 32 bytes of the 16 binary format of the transaction hash string. Example:  
varTx = require (' Ethereumjs-tx '//ETHEREUMJS-TX provides a method of converting private key to public key
Your own private key.varPrivatekey =NewBuffer (' e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109 ', ' hex ');//structuring the transaction is not required from , because the transaction is signed by the private key, and the signature generated by the private key can be restored out of the public key address, so the transaction itself does not need redundant storage sender information. varRAWTX = {
This field needs to take the counter of the transaction that your sending account initiates, can eth_getTransactionCount obtain the current nonce , direct use without adding 1 nonce:' 0x00 ', Gasprice:' 0x09184e72a000 ', Gaslimit:' 0x2710 ', to:' 0x0000000000000000000000000000000000000000 ',//to to empty address value:' 0x00 ',//You can omit data:' 0x7f7465737432000000000000000000000000000000000000000000000000000000600057 '//is the previously generated' 0x ' +bytecode}vartx =NewTx (RAWTX); tx.sign (Privatekey);varSERIALIZEDTX =tx.serialize ();
The value here is the required 16 binary format of the signed transaction data //Console.log (serializedtx.tostring (' hex '));//0xf889808609184e72a00082271094000000000000000000000000000000000000000080a47f74657374320000000000000000000000000000000 000000000000000000000006000571ca08a8bbf888cfa37bbf0bb965423625641fc956967b81d12e23709cead01446075a01ce999b56a8a88504be365 442ea61239198e23d1fce7d00fcfc5cd3b44b7215fweb3.eth.sendRawTransaction (serializedtx.tostring (' Hex '),function(err, hash) {if(!err) Console.log (hash); //"0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385" Here is the transaction hash of the successful return of the transaction.});

Finally, the generated bytecode can be directly to a third party, such as Https://etherscan.io/pushTx to broadcast your transaction, if the broadcast fails, you can directly see the error message

The following URL is a good explanation of the Etherscan tool, so you can see
Http://8btc.com/thread-75748-1-7.html



Nodejs How to deploy Smart contracts-WEB3 version 0.20

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.