Se connecter à un fournisseur
RPC et portefeuilles
Se connecter à un fournisseur est une leçon Web3 & DApp Development Fundamentals gratuite sur CoddyKit. Ceci est la leçon 1 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Web3 & DApp Development Fundamentals, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Web3 & DApp Development Fundamentals comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
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.
Questions Fréquemment Posées
La leçon « Se connecter à un fournisseur » est-elle gratuite ?
Oui — le texte complet de « Se connecter à un fournisseur » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Web3 & DApp Development Fundamentals, passe à CoddyKit PRO. Le cours Web3 & DApp Development Fundamentals comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Se connecter à un fournisseur » ?
RPC et portefeuilles Tu pratiques Web3 & DApp Development Fundamentals avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Web3 & DApp Development Fundamentals ?
Aucune expérience préalable n'est requise. Web3 & DApp Development Fundamentals sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 1 sur 4.
Combien de temps prend la leçon « Se connecter à un fournisseur » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Web3 & DApp Development Fundamentals ?
Oui. Chaque leçon Web3 & DApp Development Fundamentals inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Se connecter à un fournisseur
- Lire les données d’un contrat
- Envoyer des transactions
- Écouter les événements