الاتصال بموفّر
RPC والمحافظ
الاتصال بموفّر درس مجاني في Web3 & DApp Development Fundamentals على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Web3 & DApp Development Fundamentals، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Web3 & DApp Development Fundamentals 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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.
الأسئلة الشائعة
هل درس «الاتصال بموفّر» مجاني؟
نعم — نص درس «الاتصال بموفّر» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Web3 & DApp Development Fundamentals، انتقل إلى CoddyKit PRO. تتضمن دورة Web3 & DApp Development Fundamentals 4 دروس في المجموع.
ماذا ستتعلم في «الاتصال بموفّر»؟
RPC والمحافظ تتمرن على Web3 & DApp Development Fundamentals مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Web3 & DApp Development Fundamentals؟
لا تُشترط خبرة سابقة. Web3 & DApp Development Fundamentals على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «الاتصال بموفّر»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Web3 & DApp Development Fundamentals هذا؟
نعم. كل درس في Web3 & DApp Development Fundamentals يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- الاتصال بموفّر
- قراءة بيانات العقد
- إرسال المعاملات
- الاستماع إلى الأحداث