ethers/lib/utils#formatEther TypeScript Examples

The following examples show how to use ethers/lib/utils#formatEther. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: plantUmlStreamer.ts    From tx2uml with MIT License 6 votes vote down vote up
writeTransactionDetails = (
    plantUmlStream: Readable,
    transaction: TransactionDetails,
    options: PumlGenerationOptions = {}
): void => {
    if (options.noTxDetails) {
        return
    }
    plantUmlStream.push(`\nnote over ${participantId(transaction.from)}`)
    if (transaction.error) {
        plantUmlStream.push(
            ` ${FailureFillColor}\nError: ${transaction.error} \n`
        )
    } else {
        // no error so will use default colour of tx details note
        plantUmlStream.push("\n")
    }
    plantUmlStream.push(`Nonce: ${transaction.nonce.toLocaleString()}\n`)
    plantUmlStream.push(
        `Gas Price: ${formatUnits(transaction.gasPrice, "gwei")} Gwei\n`
    )
    plantUmlStream.push(
        `Gas Limit: ${formatNumber(transaction.gasLimit.toString())}\n`
    )
    plantUmlStream.push(
        `Gas Used: ${formatNumber(transaction.gasUsed.toString())}\n`
    )
    const txFeeInWei = transaction.gasUsed.mul(transaction.gasPrice)
    const txFeeInEther = formatEther(txFeeInWei)
    const tFeeInEtherFormatted = Number(txFeeInEther).toLocaleString()
    plantUmlStream.push(`Tx Fee: ${tFeeInEtherFormatted} ETH\n`)
    plantUmlStream.push("end note\n")
}
Example #2
Source File: plantUmlStreamer.ts    From tx2uml with MIT License 6 votes vote down vote up
genEtherValue = (trace: Trace, noEtherValue: boolean = false): string => {
    if (noEtherValue || trace.value.eq(0)) {
        return ""
    }
    // Convert wei value to Ether
    const ether = formatEther(trace.value)
    // Add thousand commas. Can't use formatNumber for this as it doesn't handle decimal numbers.
    // Assuming the amount of ether is not great than JS number limit.
    const etherFormatted = Number(ether).toLocaleString()
    return `\\n${etherFormatted} ETH`
}
Example #3
Source File: bidder.ts    From hubble-contracts with MIT License 6 votes vote down vote up
maybeBid = async (blockNumber: number) => {
        const slot = this.burnAuction.currentSlot(blockNumber);
        if (slot <= this.currentSlot) return;
        this.currentSlot = slot;
        console.info("New slot", slot);
        const bid = await this.burnAuction.getAuctioningSlotBid(blockNumber);
        console.info(
            "Auctioning slot",
            "coordinator",
            bid.coordinator,
            formatEther(bid.amount),
            "ETH"
        );
        if (bid.coordinator == this.burnAuction.myAddress) {
            console.log("We are already the auction winner, no bid");
            return;
        }
        if (bid.amount >= this.willingnessToBid) {
            console.log("Amount > our willingness to bid, no bid!");
            return;
        }
        console.log("Bid", formatEther(this.willingnessToBid), "ETH");
        const l1Txn = await this.burnAuction.bid(this.willingnessToBid);
        console.log("Bid L1 txn", l1Txn.hash);
        await l1Txn.wait(1);
        console.log("Bid mined", "L1 txn", l1Txn.hash);
    };
Example #4
Source File: hypervisor.ts    From hypervisor with The Unlicense 5 votes vote down vote up
task('deploy-hypervisor-factory', 'Deploy Hypervisor contract')
  .setAction(async (cliArgs, { ethers, run, network }) => {

    const args = {
      uniswapFactory: "0x1f98431c8ad98523631ae4a59f267346ea31f984",
    };

    console.log('Network')
    console.log('  ', network.name)
    console.log('Task Args')
    console.log(args)

    // compile

    await run('compile')

    // get signer

    const signer = (await ethers.getSigners())[0]
    console.log('Signer')
    console.log('  at', signer.address)
    console.log('  ETH', formatEther(await signer.getBalance()))

    // deploy contracts

    const hypervisorFactoryFactory = await ethers.getContractFactory('HypervisorFactory')

    const hypervisorFactory = await deployContract(
      'HypervisorFactory',
      await ethers.getContractFactory('HypervisorFactory'),
      signer,
      [args.uniswapFactory]
    )

    await hypervisorFactory.deployTransaction.wait(5)
    await run('verify:verify', {
      address: hypervisorFactory.address,
      constructorArguments: [args.uniswapFactory],
    })
})
Example #5
Source File: hypervisor.ts    From hypervisor with The Unlicense 5 votes vote down vote up
task('deploy-hypervisor-orphan', 'Deploy Hypervisor contract without factory')
  .addParam('pool', 'the uniswap pool address')
  .addParam('name', 'erc20 name')
  .addParam('symbol', 'erc2 symbol')
  .setAction(async (cliArgs, { ethers, run, network }) => {

    // compile

    await run('compile')

    // get signer

    const signer = (await ethers.getSigners())[0]
    console.log('Signer')
    console.log('  at', signer.address)
    console.log('  ETH', formatEther(await signer.getBalance()))

    const args = {
      pool: cliArgs.pool,
      owner: signer.address,
      name: cliArgs.name,
      symbol: cliArgs.symbol 
    }

    console.log('Network')
    console.log('  ', network.name)
    console.log('Task Args')
    console.log(args)

    const hypervisor = await deployContract(
      'Hypervisor',
      await ethers.getContractFactory('Hypervisor'),
      signer,
      [args.pool, args.owner, args.name, args.symbol]
    )

    await hypervisor.deployTransaction.wait(5)
    await run('verify:verify', {
      address: hypervisor.address,
      constructorArguments: [args.pool, args.owner, args.name, args.symbol],
    })

  });
Example #6
Source File: hypervisor.ts    From hypervisor with The Unlicense 5 votes vote down vote up
task('deploy-hypervisor', 'Deploy Hypervisor contract via the factory')
  .addParam('factory', 'address of hypervisor factory')
  .addParam('token0', 'token0 of pair')
  .addParam('token1', 'token1 of pair')
  .addParam('fee', 'LOW, MEDIUM, or HIGH')
  .addParam('name', 'erc20 name')
  .addParam('symbol', 'erc2 symbol')
  .setAction(async (cliArgs, { ethers, run, network }) => {

    await run('compile')

    // get signer

    const signer = (await ethers.getSigners())[0]
    console.log('Signer')
    console.log('  at', signer.address)
    console.log('  ETH', formatEther(await signer.getBalance()))
    
    const args = {
      factory: cliArgs.factory,  
      token0: cliArgs.token0,
      token1: cliArgs.token1,
      fee: FeeAmount[cliArgs.fee],
      name: cliArgs.name,
      symbol: cliArgs.symbol 
    };

    console.log('Network')
    console.log('  ', network.name)
    console.log('Task Args')
    console.log(args)


    const hypervisorFactory = await ethers.getContractAt(
      'HypervisorFactory',
      args.factory,
      signer,
    )

    const hypervisor = await hypervisorFactory.createHypervisor(
      args.token0, args.token1, args.fee, args.name, args.symbol) 

  })
Example #7
Source File: hypervisor.ts    From hypervisor with The Unlicense 5 votes vote down vote up
task('verify-hypervisor', 'Verify Hypervisor contract')
  .addParam('hypervisor', 'the hypervisor to verify')
  .addParam('pool', 'the uniswap pool address')
  .addParam('name', 'erc20 name')
  .addParam('symbol', 'erc2 symbol')
  .setAction(async (cliArgs, { ethers, run, network }) => {

    console.log('Network')
    console.log('  ', network.name)

    await run('compile')

    // get signer

    const signer = (await ethers.getSigners())[0]
    console.log('Signer')
    console.log('  at', signer.address)
    console.log('  ETH', formatEther(await signer.getBalance()))

    const args = {
      pool: cliArgs.pool,
      owner: signer.address,
      name: cliArgs.name,
      symbol: cliArgs.symbol 
    }

    console.log('Task Args')
    console.log(args)

    const hypervisor = await ethers.getContractAt(
      'Hypervisor',
      cliArgs.hypervisor,
      signer,
    )
    await run('verify:verify', {
      address: hypervisor.address,
      constructorArguments: Object.values(args),
    })

  });
Example #8
Source File: swap.ts    From hypervisor with The Unlicense 5 votes vote down vote up
task('deploy-swap', 'Deploy Swap contract')
  .addParam('owner', "your address")
  .addParam('token', "visr address")
  .setAction(async (cliArgs, { ethers, run, network }) => {
    // compile

    await run('compile')

    // get signer

    const signer = (await ethers.getSigners())[0]
    console.log('Signer')
    console.log('  at', signer.address)
    console.log('  ETH', formatEther(await signer.getBalance()))

    const _owner = ethers.utils.getAddress(cliArgs.owner);
    const _VISR = ethers.utils.getAddress(cliArgs.token);

    // TODO cli args
    // goerli
    const args = {
      _owner,
      _router: "0xE592427A0AEce92De3Edee1F18E0157C05861564",
      _VISR,
    };

    console.log('Network')
    console.log('  ', network.name)
    console.log('Task Args')
    console.log(args)

    const swap = await deployContract(
      'Swap',
      await ethers.getContractFactory('Swap'),
      signer,
      Object.values(args)
    )

    await swap.deployTransaction.wait(5)
    await run('verify:verify', {
      address: swap.address,
      constructorArguments: Object.values(args),
    })

  });
Example #9
Source File: worker.ts    From noether with Apache License 2.0 5 votes vote down vote up
retire = async (
    workerManager: WorkerManager,
    gasPriceProvider: GasPriceProvider,
    address: string,
    user: string
): Promise<boolean> => {
    const retired = await workerManager.isRetired(address);
    if (retired) {
        const provider = workerManager.provider;
        const signer = workerManager.signer;

        // get node owner (user or owner of pool)
        let owner = user;
        if (await isPool(workerManager, user)) {
            // get owner of the pool
            const pool = await createStakingPool(user, workerManager.signer);
            owner = await pool.owner();
        }

        // get this node remaining balance
        const balance = await provider.getBalance(address);

        if (balance.isZero()) {
            log.info(`node retired, zero balance`);
            return true;
        }

        // estimate gas of transaction
        const gas = await provider.estimateGas({
            to: owner,
            value: balance,
        });

        // get gas price from provider
        const gasPrice = await gasPriceProvider.getGasPrice();

        // calculate the fees
        const fee = gasPrice.mul(gas);

        // send transaction returning all remaining balance to owner
        const value = balance.sub(fee);
        log.info(
            `node retired, returning ${formatEther(value)} ETH to user ${owner}`
        );
        const tx = await signer.sendTransaction({
            to: owner,
            value,
            gasLimit: gas,
            gasPrice,
        });
        log.info(`transaction ${tx.hash}, waiting for confirmation...`);
        const receipt = await tx.wait(CONFIRMATIONS);
        log.debug(`gas used: ${receipt.gasUsed}`);

        return true;
    }
    return false;
}