🧩 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:

  1. User types a command:

    “Deploy a token called StarCoin.”

  2. Bot responds:

    “Please connect your wallet to proceed with deployment.”

  3. Using ethers.providers.Web3Provider(window.ethereum), the bot:

    • Requests wallet connection via eth_requestAccounts

    • Retrieves the user’s Ethereum address

    • Detects the active chain ID (e.g., Goerli, Mainnet)

    • Instantiates a signer via provider.getSigner()

  4. 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:

  1. Artifacts Required:

    • ABI

    • Bytecode

  2. Create Factory:

    tsCopyEditconst factory = new ethers.ContractFactory(abi, bytecode, signer);
  3. Deploy:

    tsCopyEditconst contract = await factory.deploy(...constructorArgs);
    await contract.deployed();
  4. Track Result:

    • contract.address

    • transaction.hash

    • Network and gas used

  5. 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 returns ECO: 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:

  1. Create contract with signer:

    tsCopyEditconst contract = new ethers.Contract(contractAddress, abi, signer);
  2. Prepare and send transaction:

    tsCopyEditconst tx = await contract.transfer(recipient, amount);
    await tx.wait(); // Wait for block confirmation
  3. Handle result:

    • Return tx.hash, block number

    • Report success/failure to user

Supported Actions:

Action
Function Called

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