DEV Community

Swatantra goswami
Swatantra goswami

Posted on

Ethereum chain Integration

const { ethers } = require('ethers')
const bip39 = require('bip39')


export class Ethereum {
    constructor(rpcUrl) {

    }

    // provider
    getProvider() {
        const provider = new ethers.providers.JsonRpcProvider(`https://rpc-amoy.polygon.technology`);
        return provider;
    }

    // generate address
    generateAddress() {
        const mnemonic = bip39.generateMnemonic(96);
        const eightWords = mnemonic.split(' ').slice(0, 8).join(' ');
        const seed = bip39.mnemonicToSeedSync(eightWords);
        const hdNode = ethers.utils.HDNode.fromSeed(seed);
        const { address, privateKey } = hdNode.derivePath("m/44'/60'/0'/0/0");
        return { address, privateKey }
    }
    // calculate gas fees
    async calculateGas(toAddress, from) {
        const txData = {
            to: toAddress,
            value: ethers.parseEther('0.01'),
            from: '0xYourWalletAddressHere'
        };
        const gasLimit = await provider.estimateGas(txData);
    }
    // get balance
    async getBalance(address) {
        const provider = this.getProvider();
        const balance = await provider.getBalance(address) // returns the BigNumber object
        const balanceEther = ethers.utils.formatEther(balance) // return balance in ether
        const balanceWei = ethers.utils.parseEther(balanceEther)
        return {
            balance: balance,
            balanceEther: balanceEther,
            balanceWei: balanceWei
        }
    }
    // validate address
    validateAddress(address) {
        return ethers.isAddress(address);
    }
    // do transaction
    async transfer(sendersPk, amount, toAddress) {
        const provider = this.getProvider();
        const wallet = new ethers.Wallet(sendersPk, provider);
        const tx = await wallet.sendTransaction({
            to: toAddress,
            value: ethers.utils.parseEther(amount)
        })
        await tx.wait();
    }

    // token transfer
    async tokenTransfer(tokenAddress, toAddress, amount) {
        const provider = this.getProvider();
        const ERC20_ABI = [
            "function name() view returns (string)",
            "function symbol() view returns (string)",
            "function decimals() view returns (uint8)",
            "function balanceOf(address a) view returns (uint)",
            "function transfer(address to, uint amount) returns (bool)",
        ];
        const contract = new ethers.Contract(tokenAddress, ERC20_ABI, provider);
        const res = await contract.transfer(toAddress, amount)
        return res.hash;
    }

    // get transaction details
    transactionDetails() {

    }

    // get token details or import token
    async getTokenDetails(accountAddress, tokenAddress) {
        const provider = this.getProvider();
        const ERC20_ABI = [
            "function name() view returns (string)",
            "function symbol() view returns (string)",
            "function decimals() view returns (uint8)",
            "function balanceOf(address a) view returns (uint)",
            "function transfer(address to, uint amount) returns (bool)",
        ];
        const contract = new ethers.Contract(tokenAddress, ERC20_ABI, provider);
        const name = (await contract.name());
        const symbol = (await contract.symbol());
        const decimals = (await contract.decimals());
        const balance = await contract.balanceOf(accountAddress)
        return {
            name,
            symbol,
            decimals,
            balance
        }
    }

}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)