0% found this document useful (0 votes)
7 views7 pages

Blockchain Function Interaction Guide

The document outlines the implementation of function interaction in blockchain systems, focusing on user interaction with smart contracts through wallets like MetaMask and Trust Wallet. It details the steps for connecting these wallets, accessing contract addresses, using blockchain explorers, and performing read and write operations on smart contracts. Additionally, it emphasizes the importance of gas fees for write operations that alter the blockchain state.

Uploaded by

dociw26398
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Blockchain Function Interaction Guide

The document outlines the implementation of function interaction in blockchain systems, focusing on user interaction with smart contracts through wallets like MetaMask and Trust Wallet. It details the steps for connecting these wallets, accessing contract addresses, using blockchain explorers, and performing read and write operations on smart contracts. Additionally, it emphasizes the importance of gas fees for write operations that alter the blockchain state.

Uploaded by

dociw26398
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

2.

3 Implementing function Interaction

Function interaction is a critical concept in blockchain-based systems. It encompasses two main


contexts:

1. User Interaction with Smart Contracts:


This refers to how users interact with deployed smart contracts via wallets like
MetaMask or Trust Wallet. Users connect their wallets, access contract functions, and
perform operations on the blockchain, such as:
o Read operations: Fetching data from the blockchain (e.g., querying a token
balance).
o Write operations: Modifying the blockchain state (e.g., transferring tokens).
2. Function Interaction in Solidity:
In Solidity, function interaction refers to the process where one smart contract calls a
function in another contract. This allows contracts to communicate, exchange data, and
execute logic by sending messages between their respective blockchain addresses.

 Connect to wallet

Metamask Wallet

MetaMask is a type of Ethereum wallet that bridges the gap between the user interfaces for
Ethereum (e.g. Mist browsers, DApps) and the regular web (e.g. Chrome, Firefox, websites).

Its function is to inject a JavaScript library called [Link] into the namespace of each page your
browser loads. [Link] is written by the Ethereum core team. MetaMask is mainly used as a
plugin in chrome.

After adding MetaMask as an extension in chrome and creating an account, set up your account
as follows

Steps to Connect MetaMask

1. Install MetaMask:
MetaMask can be installed as:
o A browser extension for Chrome, Firefox, Brave, or Edge.
o A mobile app for iOS and Android devices.
2. Create or Import a Wallet:
o New Wallet: Generate a 12-word or 24-word seed phrase for wallet recovery.
o Import Wallet: Use an existing seed phrase to restore a wallet.
3. Add Networks:
By default, MetaMask connects to the Ethereum Mainnet, but you can add testnets or
custom networks:
o Testnets: Sepolia, Goerli (Rinkeby has been deprecated).
o Custom Networks: Configure settings for Layer 2 solutions (e.g., Polygon) or
other blockchains.
4. Interact with DApps:
Use JavaScript libraries like [Link] or the ethereum API to connect MetaMask to a
DApp.
Example Code Snippet for Connection:

if (typeof [Link] !== "undefined") {


try {
const accounts = await [Link]({ method: "eth_requestAccounts" });
[Link]("Connected accounts:", accounts);
} catch (error) {
[Link]("User rejected the connection request:", error);
}
} else {
[Link]("MetaMask is not installed.");
}
Trust wallet

Trust Wallet is a mobile cryptocurrency wallet that supports multiple blockchains, including
Ethereum, Binance Smart Chain (BSC), and many more. It allows users to store, send, receive,
and manage digital assets. Trust Wallet also supports DApps and NFTs through its built-in
browser.

Steps to Connect Trust Wallet:

1. Install Trust Wallet: Available on Android and iOS.

2. Link to DApp:

o Use WalletConnect to connect Trust Wallet to decentralized applications


(DApps).
o Example integration:

import WalletConnectProvider from "@walletconnect/web3-provider";

// Initialize WalletConnect provider


const walletConnectProvider = new WalletConnectProvider({
infuraId: "YOUR_INFURA_PROJECT_ID", // Replace with your Infura Project ID
});

// Enable the provider to establish a connection


await [Link]();

// Now you can interact with the connected wallet


const web3 = new Web3(walletConnectProvider);
[Link]("Wallet connected:", [Link]);

 Access the Contract Address

A contract address is a unique identifier for a deployed smart [Link] access it:

1. Use a blockchain explorer (egEtherscan, BscScan, or PolygonScan) search for the specific
token you want,.
2. Once located, the contract address will be displayed on the contract's page.
3. Copy the contract address for use in your dApp or wallet.

 Use a Blockchain Explorer

Blockchain explorers, such as Etherscan, allow users to interact with contracts, monitor
transactions, and view contract details. These tools provide transparency and access to the
blockchain's data

Steps to Use a Blockchain Explorer:

1. Search for the Contract Address:


o Paste the contract address into the search bar of the blockchain explorer (e.g.,
Etherscan for Ethereum, BscScan for Binance Smart Chain).
2. View Contract Details:
o Once the contract is located, you can view important details, such as:
 Contract Source Code (if verified)
 ABI (Application Binary Interface): The interface that allows interaction
with the contract.
 Token Holders and Transaction History
3. Interact with the Contract:
o If the contract allows, you can read or write data directly from the explorer’s
interface, including:
 Calling Read Functions: View data or token balances.
 Executing Write Functions: Send transactions or interact with contract
methods (e.g., transferring tokens).

 Perform function operations

When interacting with smart contracts on the blockchain, there are two primary types of
function operations: read-only (free) and write (gas-consuming) operations.

Read only operations

These operations allow you to retrieve data from the contract without altering the blockchain
state. They are called "view" or "pure" functions in Solidity and do not require gas unless they
are executed within a transaction. These functions are free to call since they do not make
changes to the blockchain.

Examples:

1. Check Contract Balance:

A function that retrieves the contract's balance.

Solidity Example:

function getBalance() public view returns (uint) {

return address(this).balance;

JavaScript Interaction ([Link]):

const balance = await [Link]().call();


[Link]("Contract Balance:", balance);

2. Fetch User Information:

A function that returns information (e.g., balance) about a user.

Solidity Example:

mapping(address => User) public users;

function getUser(address user) public view returns (uint balance) {

return users[user].balance;

JavaScript Interaction ([Link]):

const userBalance = await [Link](userAddress).call();

[Link]("User Balance:", userBalance);

Write operation

Write operations involve modifying the blockchain state, such as changing variables or
executing transactions. These operations cost gas because they require a transaction to be
mined and validated by the network. Write operations are typically called "state-changing"
functions in Solidity.

Examples of Write Operations:

1. Transfer Tokens:

A common write operation is transferring tokens from one account to another.

Solidity Example:

function transfer(address to, uint amount) public {

require(balanceOf[[Link]] >= amount, "Insufficient balance");

balanceOf[[Link]] -= amount;
balanceOf[to] += amount;

emit Transfer([Link], to, amount);

JavaScript Interaction ([Link]):

const accounts = await [Link]();

await [Link](toAddress, amount).send({ from: accounts[0] });

2. Update User Information:

Writing data to the contract’s state, such as modifying a user’s balance or other variables.

Solidity Example:

mapping(address => uint) public balances;

function updateBalance(address user, uint newBalance) public {

balances[user] = newBalance;

JavaScript Interaction ([Link]):

const accounts = await [Link]();

await [Link](userAddress, newBalance).send({ from: accounts[0] });

How to Perform Write Operations:

Via [Link] or [Link]: Use .send() for write operations, which triggers a transaction and
incurs gas costs.

Example:

await [Link](arg1, arg2).send({ from: userAddress });

 The .send() method sends a transaction to the Ethereum network. You'll need to
confirm the transaction in your connected wallet (e.g., MetaMask), and the transaction
will be processed after the required gas fee is paid.
 Gas Fees:
Write operations modify the blockchain state, so they require gas. The user must have
enough funds to cover the transaction costs.

2.4 Optimizing Gas Costs

You might also like