What are Smart Contracts?

Smart contracts are self-executing contracts with the terms of the agreement directly written into code. They run on the Ethereum blockchain, allowing for trustless and automated transactions between parties. Once deployed, smart contracts cannot be altered, ensuring that the terms are immutable and transparent.

Smart contracts can perform a variety of functions, such as:

  • Transferring tokens or Ether between addresses.
  • Executing complex logic based on conditions.
  • Interacting with other smart contracts.
  • Storing and managing data securely.

How Ethers.js Interacts with Smart Contracts

Ethers.js is a library that allows developers to interact with the Ethereum blockchain and smart contracts. It provides a simple and intuitive API for:

  • Deploying smart contracts.
  • Calling functions on deployed contracts.
  • Listening for events emitted by contracts.

Interacting with a Smart Contract

Below is an example of how to interact with a deployed smart contract using Ethers.js. For this example, we'll assume you have a simple smart contract with a function to get and set a stored value.

Sample Smart Contract (Solidity)

        
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SimpleStorage {
uint256 private storedData;

function set(uint256 x) public {
storedData = x;
}

function get() public view returns (uint256) {
return storedData;
}
}

Interacting with the Smart Contract Using Ethers.js


<div class="example">
<label for="contractAddress">Contract Address:</label>
<input type="text" id="contractAddress" placeholder="0x...">
<label for="privateKey">Your Private Key:</label>
<input type="text" id="privateKey" placeholder="Your private key">
<label for="valueToSet">Value to Set:</label>
<input type="text" id="valueToSet" placeholder="Value">
<button id="setValue">Set Value</button>
<button id="getValue">Get Value</button>
<h3>Stored Value: <span id="storedValue">N/A</span></h3>
</div>

<script>
const provider = new ethers.providers.getDefaultProvider('homestead');

document.getElementById('setValue').onclick = async () => {
const contractAddress = document.getElementById('contractAddress').value;
const privateKey = document.getElementById('privateKey').value;
const valueToSet = document.getElementById('valueToSet').value;

const wallet = new ethers.Wallet(privateKey, provider);
const contract = new ethers.Contract(contractAddress, [
"function set(uint256 x) public",
"function get() public view returns (uint256)"
], wallet);

try {
const txResponse = await contract.set(valueToSet);
await txResponse.wait(); // Wait for the transaction to be mined
alert('Value set successfully!');
} catch (error) {
console.error(error);
alert('Failed to set value');
}
};

document.getElementById('getValue').onclick = async () => {
const contractAddress = document.getElementById('contractAddress').value;

const contract = new ethers.Contract(contractAddress, [
"function get() public view returns (uint256)"
], provider);

try {
const value = await contract.get();
document.getElementById('storedValue').innerText = value.toString();
} catch (error) {
console.error(error);
alert('Failed to get value');
}
};
</script>

Conclusion

Smart contracts are a powerful feature of the Ethereum blockchain, enabling automated and trustless transactions. Ethers.js provides a robust framework for interacting with these contracts, allowing developers to easily deploy and call functions on them. Understanding how to work with smart contracts is essential for building decentralized applications on Ethereum.