Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy smart contracts to infura

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to deploy smart contracts into infura". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to deploy smart contracts into infura"!

In this particular example, we will migrate to the Ropsten test network. Let's assume you already have a dapp. If you need a test dapp, you can use Truffle's Pet Shop dapp.

Install HDWalletProvider

Infura HDWalletProvider is a standalone npm package installed as follows:

npm install truffle-hdwallet-provider

Note: If you install on Windows and encounter MSBUILD errors, you may need to install Windows Build Tools. From a console with administrator privileges, run npm install -g windows-build-tools and try the installation again.

Register Infura

Before you can use Infura, you need to register the Infura access token.

Fill out and submit the form and you will receive an access token. The relevant information will be displayed on the screen and sent to the email you provide. Need to record this access token and make sure it is not seen by others!

Configure Truffle Project

The next step is to edit your truffle.js file to enable HDWalletProvider and make the necessary configuration for deployment to Ropsten.

STEP 1: First, define the HDWalletProvider object in the configuration file. Add the following code to the top of the truffle.js file:

var HDWalletProvider = require("truffle-hdwallet-provider");

STEP 2: Next, provide mnemonic to generate your account.

var mnemonic = "orange apple banana ... ";

Warning: During this process, we strongly recommend storing the mnemonic in another (secret) file to reduce the risk of mnemonic leakage. If someone knows your mnemonic, they will have all your addresses and private keys!

STEP 3: Add a Ropsten network definition:

module.exports = { networks: { ropsten: { provider: function() { return new HDWalletProvider(mnemonic, "https://ropsten.infura.io/") }, network_id: 3 } } };

Notes:

Although this example defines only a single network, you can define multiple networks as usual.

The provider in the ropsten network definition will use the instantiated HDWalletProvider.

HDWalletProvider takes mnemonic and desired network as parameters. A list of Infura supported networks is available on the Infura home page.

Be sure to replace it with the Infura access token you got earlier.

The provider value is encapsulated in a function, which ensures that it is not initialized until it is needed. This is especially important if you are connected to multiple networks. For more information on this topic, see the Network Configuration section of the Truffle documentation.

By default, the first account generated by the mnemonic will be responsible for performing the contract migration task. But if desired, you can pass in parameters to specify which account to use. For example, to use a third account:

new HDWalletProvider(mnemonic, "https://ropsten.infura.io/", 2);

The account index is zero-based, so 2 represents the third address.

Use Faucet to get ether

Make sure your account has enough balance to deploy. Ether is available on the Ropsten network through a service called Faucet. Although there are multiple Faucet sites out there, one service we recommend is hosted on EthTools.

Navigate to EthTools for Ether Faucet.

Enter your mnemonic and choose how many ethers you want (up to 5).

Faucet will link to your first account. Click "Request Ether" to submit the request.

Soon, your account will receive the requested ether.

Note: ether can also be applied through MetaMask. Connect your account on Ropsten and click on the "Buy" button, which will provide a link to MetaMask's Ropsten test Faucet, which works similarly to the above.

We can now start deploying contracts to Ropsten!

deploy the contract

STEP 1: Compile the project:

truffle compile

STEP 2: Deploying to the Ropsten Network:

truffle migrate --network ropsten

If all goes well, you should see output similar to the following:

Using network 'ropsten'. Running migration: 1_initial_migration.js Deploying Migrations... ... 0xd79bc3c5a7d338a7f85db9f86febbee738ebdec9494f49bda8f9f4c90b649db7 Migrations: 0x0c6c4fc8831755595eda4b5724a61ff989e2f8b9 Saving successful migration to network... ... 0xc37320561d0004dc149ea42d839375c3fc53752bae5776e4e7543ad16c1b06f0 Saving artifacts... Running migration: 2_deploy_contracts.js Deploying MyContract... ... 0x7efbb3e4f028aa8834d0078293e0db7ff8aff88e72f33960fc806a618a6ce4d3 MyContract: 0xda05d7bfa5b6af7feab7bd156e812b4e564ef2b1 Saving successful migration to network... ... 0x6257dd237eb8b120c8038b066e257baee03b9c447c3ba43f843d1856de1fe132 Saving artifacts...

Please note that your transaction ID will be different than above.

Note: If you receive Error: Exceeds block gas limit, you may need to manually set the gas limit for the contract.

STEP 3: If you want to verify that the contract has been successfully deployed, check in the Ropsten section of Etherscan. In the Search field, enter the Deployment Deal ID. In the example above, the transaction ID is:

0x7efbb3e4f028aa8834d0078293e0db7ff8aff88e72f33960fc806a618a6ce4d3

You should be able to see details about the transaction, including the block number where the transaction is protected.

At this point, I believe everyone has a deeper understanding of "how to deploy smart contracts into infura". Let's do it in practice! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report