How to use eth-connect - 10 common examples

To help you get started, we’ve selected a few eth-connect examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github decentraland / explorer / kernel / packages / shared / ethereum / EthereumService.ts View on Github external
export async function requirePayment(toAddress: string, amount: number, currency: string): Promise {
  try {
    let fromAddress = await getUserAccount()
    if (!fromAddress) {
      throw new Error(`Not a web3 game session`)
    }

    let result: Promise

    if (currency === 'ETH') {
      result = requestManager.eth_sendTransaction({
        to: toAddress,
        from: fromAddress,
        // TODO: fix this in eth-connect
        value: toWei(amount as any, 'ether') as any,
        data: null as any
      })
    } else {
      const supportedTokens: Record = {} // a TODO: getNetworkConfigurations(network).paymentTokens

      if (currency in supportedTokens === false) {
        // tslint:disable:no-console
        console.warn(`WARNING! Using a non-supported coin. Skipping operation! Please use one of the next coins 'ETH'`)
        return false
      }

      const contractInstance = await getERC20(requestManager, supportedTokens[currency])

      // Check this account has enough tokens for the transaction
      const balance = await getERC20Balance(fromAddress, contractInstance.address)
      if (balance.lt(amount)) {
github decentraland / explorer / kernel / packages / shared / ethereum / provider.ts View on Github external
import { ethereumConfigurations, ETHEREUM_NETWORK } from 'config'
import { defaultLogger } from 'shared/logger'
import { Account } from 'web3x/account'
import { getUserProfile } from 'shared/comms/peers'
import { getTLD } from '../../config/index'
import { removeUserProfile } from '../comms/peers'
import { Eth } from 'web3x/eth'

declare var window: Window & {
  ethereum: any
  web3: any
}

export const providerFuture = future()
export const requestManager = new RequestManager(null)

let providerRequested = false

function processLoginAttempt(response: IFuture<{ successful: boolean; provider: any; localIdentity?: Account }>) {
  return async () => {
    // TODO - look for user id matching account - moliva - 18/02/2020
    let userData = getUserProfile()

    // Modern dapp browsers...
    if (window['ethereum']) {
      if (!isSessionExpired(userData)) {
        response.resolve({ successful: true, provider: window.ethereum })
      } else {
        showEthConnectAdvice(false)
        let result
        try {
github decentraland / explorer / kernel / packages / shared / ethereum / EthereumService.ts View on Github external
export async function getERC20Balance(
  address: string,
  tokenAddress: string,
  inWei: boolean = false
): Promise {
  const contract = await getERC20(requestManager, tokenAddress)

  const balance = await contract.balanceOf(address)

  if (inWei) {
    return balance
  }

  return fromWei(balance as any, 'ether') as any
}
github decentraland / explorer / kernel / packages / shared / ethereum / provider.ts View on Github external
function createProvider() {
  const network = getTLD() === 'zone' ? ETHEREUM_NETWORK.ROPSTEN : ETHEREUM_NETWORK.MAINNET
  return new WebSocketProvider(ethereumConfigurations[network].wss)
}
github decentraland / explorer / kernel / packages / shared / ethereum / ERC20.ts View on Github external
export async function getERC20(requestManager: RequestManager, address: Address): Promise {
  return (await new ContractFactory(requestManager, ERC20Abi).at(address)) as IERC20
}
github decentraland / explorer / kernel / packages / shared / ethereum / ERC721.ts View on Github external
export async function getERC721(requestManager: RequestManager, address: Address): Promise {
  return (await new ContractFactory(requestManager, ERC721Abi).at(address)) as IERC721
}

eth-connect

Ethereum TypeScript API, middleware to talk to a ethereum node using an async provider

LGPL-3.0
Latest version published 11 months ago

Package Health Score

62 / 100
Full package analysis