Deploy Your First Smart Contract

Are you new to blockchains, smart contracts, and Ethereum? Want to understand and engage with the exciting world of Web3? Then you're in the right place. This beginner tutorial will show you how to deploy your first smart contract and run it in your browser. No prior experience necessary. 

Chainlink Products:

General

Product Versions:

General

Required Time:

10 minutes

Requires:

Wallet with gas token & ERC-677 LINK

What to Expect

This beginner-friendly tutorial will guide you through two key Web3 skills:

  1. Deploy your first smart contract.
    We'll start with the basics. Our experts will walk you through the steps to create and deploy your first smart contract on Ethereum.
  2. Get the price of ETH.
    Next, we'll show you how to use Chainlink Price Feeds to fetch the real-time price of ether.

Steps to implement

Step 1: Install and fund your MetaMask wallet

Deploying smart contracts on-chain requires a wallet and ETH. The ETH pays for the work required by the Ethereum network to add the contract to the blockchain and store the variables. The wallet holds the ETH that you need to pay for the transaction. Install MetaMask, configure it to use the Sepolia testnet, and fund your wallet with free testnet ETH.

  1. Install and configure the MetaMask extension in your browser.
  1. After you install the extension, open your browser extension list and click MetaMask to open MetaMask.
  1. Follow the instructions in MetaMask to create a new MetaMask wallet. The new wallet includes a 12-word mnemonic phrase. This phrase is the key to your wallet. Copy that phrase down in a very secure location that only you can access. You can use this phrase to retrieve your wallet later or add it to another browser.
  1. Set MetaMask to use the Sepolia test network.
  1. Choose a Sepolia ETH faucet and follow the steps to send testnet ETH to your MetaMask wallet address. You can copy your wallet address by clicking your account name in MetaMask. After the faucet completes the transaction, you should have testnet ETH in your MetaMask wallet on the Sepolia testnet.

Now that you configured your wallet and funded it with testnet ETH, you can write, compile, and deploy your contract.

Step 2: Write, compile, and deploy your first smart contract

Your first contract is a simple HelloWorld.sol example. This example shows you how to set and retrieve variables in a smart contract on-chain.

[hyperlinks can be found here]

  1. Open the example contract in the Remix IDE. Remix opens and shows the contents of the smart contract. You can modify the code in this editor when you write your own contract.
  2. Because the code is already written, you can start the compile step. On the left side of Remix, click the Solidity Compiler tab to view the compiler settings.
  1. For this contract, use the default compiler settings. Click the Compile HelloWorld.sol button to compile the contract. This converts the contract from Solidity into bytecode that the Ethereum Virtual Machine can understand. Remix automatically detects the correct compiler version depending on the pragma that you specify in the contract.
  1. After Remix compiles the contract, deploy it. On the left side of Remix, click the Deploy and Run tab to view the deployment settings.
  1. In the deployment settings, select the Injected Provider environment. This tells Remix that you want to deploy your contract to the blockchain that you configured in MetaMask. You could optionally use one of the Javascript VM options, but they run in a virtual environment with no connection to an actual blockchain or Chainlink oracles.
  1. Next to the Deploy button, enter a message that you want to send with the smart contract when you deploy it. This contract has a constructor that sets an initial message when you deploy the contract.
  1. Click the Deploy button to deploy the contract and its initial message to the blockchain network. MetaMask opens and asks you to confirm payment to deploy the contract. Make sure MetaMask is set to the Sepolia network before you accept the transaction. Because these transactions are on the blockchain, they are not reversible.
  1. In the MetaMask prompt, click Confirm to approve the transaction and spend your testnet ETH required to deploy the contract.
  1. After a few seconds, the transaction completes and your contract appears under the Deployed Contracts list in Remix. Click the contract dropdown to view its variables and functions.
  1. Click the message variable. Remix retrieves and prints the initial message that you set.

The contract has an address just like your wallet address. If you save this address, you can return to your deployed contract at any time to retrieve variables or execute functions. To see details about your deployed contract, copy the contract address from the list in Remix and search for it in the Etherscan Sepolia Testnet Explorer.

Step 3: Run functions in your contract

Because you deployed the contract to an actual blockchain, several nodes on the test network confirmed your payment for the smart contract. The contract, its variables, and its functions remain in the blockchain permanently. To change the message variable that is stored with your contract, run the updateMessage function.

  1. In your deployed contract, enter a new message next to the updateMessage function.
  1. Click the updateMessage button to set the new message in the contract data. MetaMask opens and asks you to confirm payment to update the state of your contract.
  1. In the new MetaMask prompt, click Confirm to approve the transaction.
  1. Click the message variable again to see the updated value. It might take a few seconds before the transaction updates the variable.

Congratulations! Now you know how to deploy example contracts to a test network and run the functions in those contracts. You can write your own contracts and test them using this same process.

Next, learn how to connect your smart contracts to Chainlink Data Feeds and retrieve on-chain data that your smart contracts can act on.

Step 4: Examine the sample contract

Let’s learn how to pull the price of ether. Before we begin, let’s example the sample contract. This example contract obtains the latest price answer from the BTC / USD feed on the Sepolia testnet, but you can modify it to read any of the different Types of Data Feeds.

[Code snippet can be found here]

The contract has the following components:

  • The import line imports an interface named AggregatorV3Interface. Interfaces define functions without their implementation, which leaves inheriting contracts to define the actual implementation themselves. In this case, AggregatorV3Interface defines that all v3 Aggregators have the function latestRoundData. You can see the complete code for the AggregatorV3Interface on GitHub.
  • The constructor() {} initializes an interface object named dataFeed that uses AggregatorV3Interface and connects specifically to a proxy aggregator contract that is already deployed at 0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43. The interface allows your contract to run functions on that deployed aggregator contract.
  • The getChainlinkDataFeedLatestAnswer() function calls your dataFeed object and runs the latestRoundData() function. When you deploy the contract, it initializes the dataFeed object to point to the aggregator at 0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43, which is the proxy address for the Sepolia BTC / USD data feed. Your contract connects to that address and executes the function. The aggregator connects with several oracle nodes and aggregates the pricing data from those nodes. The response from the aggregator includes several variables, but getChainlinkDataFeedLatestAnswer() returns only the answer variable.

Step 5: Compile, deploy, and run the contract

Deploy the DataConsumerV3 smart contract on the Sepolia testnet.

  1. Open the example contract in Remix. Remix opens and shows the contents of the smart contract.
    [Open the contract in Remix]
  2. Because the code is already written, you can start the compile step. On the left side of Remix, click the Solidity Compiler tab to view the compiler settings.
  1. Use the default compiler settings. Click the Compile DataConsumerV3.sol button to compile the contract. Remix automatically detects the correct compiler version depending on the pragma that you specify in the contract. You can ignore warnings about unused local variables in this example.
  1. On the Deploy tab, select the Injected Provider environment. This contract specifically requires Web3 because it connects with another contract on the blockchain. Running in a JavaScript VM will not work.
  1. Because the example contract has several imports, Remix might select another contract to deploy by default. In the Contract section, select the DataConsumerV3 contract to make sure that Remix deploys the correct contract.
  1. Click Deploy to deploy the contract to the Sepolia testnet. MetaMask opens and asks you to confirm payment for deploying the contract. Make sure MetaMask is set to the Sepolia network before you accept the transaction. Because these transactions are on the blockchain, they are not reversible.
  1. In the MetaMask prompt, click Confirm to approve the transaction and spend your testnet ETH required to deploy the contract.
  1. After a few seconds, the transaction completes and your contract appears under the Deployed Contracts list in Remix. Click the contract dropdown to view its variables and functions.
  1. Click getChainlinkDataFeedLatestAnswer to show the latest answer from the aggregator contract. In this example, the answer is the latest price, which appears just below the button. The returned answer is an integer, so it is missing its decimal point. You can find the correct number of decimal places for this answer on the Price Feed addresses page by clicking the Show more details checkbox. The answer on the BTC / USD feed uses 8 decimal places, so an answer of 3030914000000 indicates a BTC / USD price of 30309.14. Each feed uses a different number of decimal places for answers.

Close icon

Signup form

Please fill this form to continue to next step

By submitting, I agree to receive email communications from Chainlink.

Please view the video here. We’ve also sent you a copy in your inbox.
Something went wrong. Please try again.