🧩 Functional Roles of ethers.js in Dialect AI
🔐 1. Wallet Integration
Dialect AI uses ethers.js to facilitate user wallet connections in the browser via:
MetaMask (browser extension)
WalletConnect (mobile wallets via QR)
Injected Web3 Providers (e.g., Brave, Coinbase Wallet)
Workflow:
User types a command:
“Deploy a token called StarCoin.”
Bot responds:
“Please connect your wallet to proceed with deployment.”
Using
ethers.providers.Web3Provider(window.ethereum), the bot:Requests wallet connection via
eth_requestAccountsRetrieves the user’s Ethereum address
Detects the active chain ID (e.g., Goerli, Mainnet)
Instantiates a signer via
provider.getSigner()
The signer is stored in memory for secure transaction signing during the session.
🏗️ 2. Smart Contract Deployment
When a Solidity contract is compiled (via solc-js), ethers.js is used to deploy it using the signer’s wallet.
Process:
Artifacts Required:
ABIBytecode
Create Factory:
tsCopyEditconst factory = new ethers.ContractFactory(abi, bytecode, signer);Deploy:
tsCopyEditconst contract = await factory.deploy(...constructorArgs); await contract.deployed();Track Result:
contract.addresstransaction.hashNetwork and gas used
Bot response to user:
✅ “Your contract has been deployed at: 0xAbc...DEF Transaction Hash: 0x123...789”
📖 3. Calling View / Pure Functions
For read-only operations like balance checks, token metadata, or event queries, the bot uses ethers.Contract with a connected provider (no signing required).
Examples:
tsCopyEditconst contract = new ethers.Contract(tokenAddress, abi, provider);
const balance = await contract.balanceOf(userAddress);
const name = await contract.name();
const symbol = await contract.symbol();These calls:
Are instantaneous (from RPC)
Require no gas
Do not prompt the user to sign
Example Commands:
“What’s my balance of ECO tokens?” → Calls
balanceOf(address)and returnsECO: 10,000
💸 4. Sending Transactions (Write Functions)
For state-changing operations like transfer(), mint(), or DAO vote() commands, ethers.js allows Dialect AI to send signed transactions via the connected wallet.
Steps:
Create contract with signer:
tsCopyEditconst contract = new ethers.Contract(contractAddress, abi, signer);Prepare and send transaction:
tsCopyEditconst tx = await contract.transfer(recipient, amount); await tx.wait(); // Wait for block confirmationHandle result:
Return
tx.hash, block numberReport success/failure to user
Supported Actions:
Transfer tokens
transfer(address,uint256)
Mint tokens
mint(address,uint256) (if enabled)
Approve spending
approve(spender, amount)
Send ETH
signer.sendTransaction({...})
Interact with dApps
Custom function calls to other contracts
Last updated