Connessione a un provider
RPC e wallet
Connessione a un provider è una lezione Web3 & DApp Development Fundamentals gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Web3 & DApp Development Fundamentals, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Web3 & DApp Development Fundamentals include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Talking to a Blockchain
To interact with Ethereum from JavaScript you need a provider — an object that connects to a node and lets you read chain data and send transactions.
Libraries like ethers.js and web3.js wrap the JSON-RPC protocol nodes speak.
Providers vs Signers
Two key abstractions:
- Provider — read-only connection to the chain (balances, blocks, view calls).
- Signer — holds a private key and can sign and send transactions.
You read with a provider and write with a signer.
A JSON-RPC Provider
The most common provider connects to an RPC endpoint such as Infura, Alchemy, or a local node:
const { ethers } = require("ethers");
const provider = new ethers.JsonRpcProvider(
"https://mainnet.example-rpc.io/KEY"
);With it you can immediately query public chain data.
const { ethers } = require("ethers");
const provider = new ethers.JsonRpcProvider(
"https://mainnet.example-rpc.io/KEY"
);Reading Network Info
Once connected, query basic network state:
const network = await provider.getNetwork();
console.log(network.chainId);
const block = await provider.getBlockNumber();
console.log("Latest block:", block);The chainId identifies which network you are on (1 = mainnet).
const network = await provider.getNetwork();
console.log(network.chainId);
const block = await provider.getBlockNumber();
console.log("Latest block:", block);Checking a Balance
Get an account's ETH balance through the provider:
const balance = await provider.getBalance("0xAbc...");
console.log(ethers.formatEther(balance), "ETH");The raw value is in wei (a BigInt); formatEther converts it to a human-readable ETH string.
const balance = await provider.getBalance("0xAbc...");
console.log(ethers.formatEther(balance), "ETH");Browser Wallets
In a browser dApp, the wallet (like MetaMask) injects a provider at window.ethereum. Wrap it with a BrowserProvider:
const provider = new ethers.BrowserProvider(window.ethereum);
await provider.send("eth_requestAccounts", []);The eth_requestAccounts call prompts the user to connect.
const provider = new ethers.BrowserProvider(window.ethereum);
await provider.send("eth_requestAccounts", []);Getting a Signer
From a browser provider, obtain the connected account's signer:
const signer = await provider.getSigner();
console.log("Connected as:", await signer.getAddress());This signer can now send transactions approved by the user in their wallet.
const signer = await provider.getSigner();
console.log("Connected as:", await signer.getAddress());Wallet from a Private Key
On a backend you can create a signer from a private key. Connect it to a provider so it can broadcast:
const wallet = new ethers.Wallet(
process.env.PRIVATE_KEY,
provider
);Never hardcode private keys — load them from environment variables.
const wallet = new ethers.Wallet(
process.env.PRIVATE_KEY,
provider
);web3.js Comparison
web3.js is the older alternative. Its connection looks like:
const { Web3 } = require("web3");
const web3 = new Web3("https://mainnet.example-rpc.io/KEY");
const block = await web3.eth.getBlockNumber();Concepts map closely, but ethers.js separates providers and signers more cleanly.
const { Web3 } = require("web3");
const web3 = new Web3("https://mainnet.example-rpc.io/KEY");
const block = await web3.eth.getBlockNumber();Keeping Secrets Safe
RPC keys and private keys are sensitive:
- Store them in
.env, never in source control. - Use read-only providers in front-end code.
- Private keys belong only on secure backends or in the user's own wallet.
Default and Fallback Providers
ethers can combine several backends for reliability. A fallback provider queries multiple endpoints and agrees on a result:
const provider = ethers.getDefaultProvider("mainnet", {
alchemy: process.env.ALCHEMY_KEY,
infura: process.env.INFURA_KEY,
});If one backend is down, others keep your app working.
const provider = ethers.getDefaultProvider("mainnet", {
alchemy: process.env.ALCHEMY_KEY,
infura: process.env.INFURA_KEY,
});Quick Check
Test your understanding of providers and signers.
Recap
You learned to connect to a blockchain.
- A provider reads chain data; a signer sends transactions.
JsonRpcProviderconnects to an RPC URL;BrowserProviderwraps wallet injections.getSignerreturns the connected account;new Wallet(key, provider)signs on a backend.- Values are in wei; use
formatEtherto display them. - Keep RPC and private keys in
.env, never in code.
Domande Frequenti
La lezione «Connessione a un provider» è gratuita?
Sì — il testo completo di «Connessione a un provider» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Web3 & DApp Development Fundamentals, passa a CoddyKit PRO. Il corso Web3 & DApp Development Fundamentals include 4 lezioni in totale.
Cosa imparerò in «Connessione a un provider»?
RPC e wallet Eserciti Web3 & DApp Development Fundamentals con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Web3 & DApp Development Fundamentals?
Non è richiesta alcuna esperienza precedente. Web3 & DApp Development Fundamentals su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.
Quanto tempo richiede la lezione «Connessione a un provider»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Web3 & DApp Development Fundamentals?
Sì. Ogni lezione Web3 & DApp Development Fundamentals include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Connessione a un provider
- Lettura dei dati dei contratti
- Invio delle transazioni
- Ascolto degli eventi