Skip to main content

Deploy Your First Smart Contract

Welcome to the Deploy Your First Smart Contract tutorial! In this guide, you'll learn how to deploy a simple smart contract on the Ethereum network using Blacksun.

Prerequisites

  1. Blacksun account: Make sure you have a Blacksun account. If you don't, sign up here.
  2. API Key: You need an API Key to interact with Ethereum. Learn how to create one in our Create API Key guide.
  3. Ethereum wallet: You'll need an Ethereum wallet with some ETH for gas fees.
  4. Smart contract: A simple Solidity smart contract to deploy. You can use a sample contract like the one provided below.

Sample Smart Contract

Here's a simple Solidity smart contract you can deploy for this tutorial:

// Declare the Solidity version the code is compatible with
pragma solidity ^0.8.0;

// Define a new contract called SimpleStorage
contract SimpleStorage {
// Declare a private unsigned integer variable called storedData
uint256 private storedData;

// Define a public function called set that accepts an unsigned integer argument called x
function set(uint256 x) public {
// Assign the value of x to the storedData variable
storedData = x;
}

// Define a public view function called get that returns an unsigned integer
function get() public view returns (uint256) {
// Return the value stored in the storedData variable
return storedData;
}
}

Deploy Your First Smart Contract

  1. Compile your smart contract using a tool like Remix IDE. Download the compiled ABI and Bytecode files.

  2. Replace [BLACSKUN_RPC_ENDPOINT] with your Blacksun RPC endpoint URL.

  3. Use one of the code snippets below in your preferred language to deploy the smart contract:

// Import the required libraries
const Web3 = require('web3');
const fs = require('fs');

// Connect to Blacksun RPC endpoint using Web3
const web3 = new Web3(new Web3.providers.HttpProvider([BLACSKUN_RPC_ENDPOINT]));

// Replace 'YOUR_PRIVATE_KEY' with your private key
const privateKey = 'YOUR_PRIVATE_KEY';
// Create an account object from the private key
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
// Add the account to the wallet
web3.eth.accounts.wallet.add(account);

// Read the ABI and bytecode files
const abi = JSON.parse(fs.readFileSync('path/to/abi.json', 'utf8'));
const bytecode = fs.readFileSync('path/to/bytecode.txt', 'utf8');

// Create a new contract instance with the ABI
const myContract = new web3.eth.Contract(abi);

// Define an async function to deploy the contract
const deploy = async () => {
// Get the current gas price
const gasPrice = await web3.eth.getGasPrice();
// Estimate the gas required to deploy the contract
const gasEstimate = await myContract.deploy({ data: bytecode }).estimateGas();

// Deploy the contract
myContract
.deploy({ data: bytecode })
.send({ from: account.address, gas: gasEstimate, gasPrice: gasPrice })
// Log the transaction hash
.on('transactionHash', (hash) => console.log('Transaction Hash:', hash))
// Log the confirmation number
.on('confirmation', (confirmationNumber, receipt) =>
console.log('Confirmation:', confirmationNumber)
)
// Log any errors
.on('error', (error) => console.error('Error:', error))
// Log the deployed contract address
.then((newContractInstance) =>
console.log('Contract Address:', newContractInstance.options.address)
);
};

// Call the deploy function
deploy();

Congratulations! You've successfully deployed your first smart contract using Blacksun. Now, you can interact with your deployed contract using the contract address.