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
- Blacksun account: Make sure you have a Blacksun account. If you don't, sign up here.
- API Key: You need an API Key to interact with Ethereum. Learn how to create one in our Create API Key guide.
- Ethereum wallet: You'll need an Ethereum wallet with some ETH for gas fees.
- 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
Compile your smart contract using a tool like Remix IDE. Download the compiled ABI and Bytecode files.
Replace [BLACSKUN_RPC_ENDPOINT] with your Blacksun RPC endpoint URL.
Use one of the code snippets below in your preferred language to deploy the smart contract:
- JavaScript
- Python
// 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();
from web3 import Web3
import json
# Connect to the Blacksun RPC endpoint
w3 = Web3(Web3.HTTPProvider([BLACSKUN_RPC_ENDPOINT]))
# Set your private key
private_key = 'YOUR_PRIVATE_KEY'
# Create an account object using the private key
account = w3.eth.account.privateKeyToAccount(private_key)
# Load ABI (Application Binary Interface) from JSON file
with open('path/to/abi.json') as f:
abi = json.load(f)
# Load the bytecode from a text file
with open('path/to/bytecode.txt') as f:
bytecode = f.read()
# Create a contract object using the ABI and bytecode
MyContract = w3.eth.contract(abi=abi, bytecode=bytecode)
# Estimate gas for deploying the smart contract
gas_estimate = MyContract.constructor().estimateGas()
# Get the current gas price
gas_price = w3.eth.gasPrice
# Prepare the transaction object
transaction = {
'from': account.address,
'gas': gas_estimate,
'gasPrice': gas_price,
'nonce': w3.eth.getTransactionCount(account.address),
}
# Sign the transaction using the private key
transaction_signed = w3.eth.account.signTransaction(transaction, private_key)
# Send the signed transaction
transaction_hash = w3.eth.sendRawTransaction(transaction_signed.rawTransaction)
# Print the transaction hash
print(f'Transaction Hash: {transaction_hash.hex()}')
# Wait for the transaction receipt
transaction_receipt = w3.eth.waitForTransactionReceipt(transaction_hash)
# Get the deployed contract address from the receipt
contract_address = transaction_receipt['contractAddress']
# Print the contract address
print(f'Contract Address: {contract_address}')
Congratulations! You've successfully deployed your first smart contract using Blacksun. Now, you can interact with your deployed contract using the contract address.