@ethersproject/abstract-provider#Provider TypeScript Examples

The following examples show how to use @ethersproject/abstract-provider#Provider. 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: utils.ts    From ether-swr with MIT License 7 votes vote down vote up
export function getContract(
  address: string,
  abi: ContractInterface,
  provider: Provider | Web3Provider
): Contract {
  let contract = contracts.get(address)
  if (contract) {
    return contract
  }
  contract = new Contract(address, abi, provider as Provider)
  contracts.set(address, contract)
  return contract
}
Example #2
Source File: utils.ts    From ether-swr with MIT License 7 votes vote down vote up
call = (
  parameters: string[],
  provider: ethersProviders.Provider,
  ABIs
): Promise<any> => {
  const [address, method, ...otherParams] = parameters
  // it's a contract
  if (isAddress(address)) {
    if (!ABIs) throw new ABIError(`ABI repo not found`)
    if (!ABIs.get) throw new ABIError(`ABI repo isn't a Map`)
    const abi = ABIs.get(address)
    if (!abi) throw new ABINotFound(`ABI not found for ${address}`)
    const contract = new Contract(address, abi, provider)
    return contract[method](...otherParams)
  }
  const param2 = method
  const baseMethod = address // getBalance, getTransactionCount, etc
  return provider[baseMethod](param2, ...otherParams)
}
Example #3
Source File: kms-ethers-signer.ts    From cloud-cryptographic-wallet with MIT License 6 votes vote down vote up
constructor(
    private readonly config: KmsEthersSignerConfig,
    provider?: Provider
  ) {
    super();
    defineReadOnly(this, "provider", provider);

    this.kmsSigner = new KmsSigner(config.keyId, config.kmsClientConfig ?? {});

    this.adapter = new Adapter({ signer: this.kmsSigner, version }, provider);
  }
Example #4
Source File: resolveENSContentHash.ts    From interface-v2 with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Fetches and decodes the result of an ENS contenthash lookup on mainnet to a URI
 * @param ensName to resolve
 * @param provider provider to use to fetch the data
 */
export default async function resolveENSContentHash(
  ensName: string,
  provider: Provider,
): Promise<string> {
  const ensRegistrarContract = new Contract(
    REGISTRAR_ADDRESS,
    REGISTRAR_ABI,
    provider,
  );
  const hash = namehash(ensName);
  const resolverAddress = await ensRegistrarContract.resolver(hash);
  return resolverContract(resolverAddress, provider).contenthash(hash);
}
Example #5
Source File: resolveENSContentHash.ts    From limit-orders-lib with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Fetches and decodes the result of an ENS contenthash lookup on mainnet to a URI
 * @param ensName to resolve
 * @param provider provider to use to fetch the data
 */
export default async function resolveENSContentHash(
  ensName: string,
  provider: Provider
): Promise<string> {
  const ensRegistrarContract = new Contract(
    REGISTRAR_ADDRESS,
    REGISTRAR_ABI,
    provider
  );
  const hash = namehash(ensName);
  const resolverAddress = await ensRegistrarContract.resolver(hash);
  return resolverContract(resolverAddress, provider).contenthash(hash);
}
Example #6
Source File: resolveENSContentHash.ts    From sybil-interface with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Fetches and decodes the result of an ENS contenthash lookup on mainnet to a URI
 * @param ensName to resolve
 * @param provider provider to use to fetch the data
 */
export default async function resolveENSContentHash(ensName: string, provider: Provider): Promise<string> {
  const ensRegistrarContract = new Contract(REGISTRAR_ADDRESS, REGISTRAR_ABI, provider)
  const hash = namehash(ensName)
  const resolverAddress = await ensRegistrarContract.resolver(hash)
  return resolverContract(resolverAddress, provider).contenthash(hash)
}
Example #7
Source File: gas-price-provider.ts    From noether with Apache License 2.0 6 votes vote down vote up
createGasPriceProvider = async (
    provider: Provider,
    type: GasPriceProviderType,
    apiKey: string | undefined = undefined
): Promise<GasPriceProvider> => {
    const network = await provider.getNetwork();
    const providerGasPriceProvider = new ProviderGasPriceProvider(
        provider,
        GAS_PRICE_MULTIPLIER
    );

    if (
        type !== "eth-provider" &&
        network.chainId === GAS_STATION_API_CHAIN_ID
    ) {
        log.debug(
            `using gas price predictor from eth gas station with "${type}" profile`
        );
        const gasStationProvider = new GasStationGasPriceProvider({
            url: GAS_STATION_API_URL,
            key: apiKey,
            timeout: GAS_STATION_API_REQUEST_TIMEOUT_MS,
            profile: type,
        });
        return new ChainGasPriceProvider([
            gasStationProvider,
            providerGasPriceProvider,
        ]);
    } else {
        log.debug(`using gas price predictor from ethereum provider`);
        return providerGasPriceProvider;
    }
}
Example #8
Source File: resolveENSContentHash.ts    From cheeseswap-interface with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Fetches and decodes the result of an ENS contenthash lookup on mainnet to a URI
 * @param ensName to resolve
 * @param provider provider to use to fetch the data
 */
export default async function resolveENSContentHash(ensName: string, provider: Provider): Promise<string> {
  const ensRegistrarContract = new Contract(REGISTRAR_ADDRESS, REGISTRAR_ABI, provider)
  const hash = namehash(ensName)
  const resolverAddress = await ensRegistrarContract.resolver(hash)
  return resolverContract(resolverAddress, provider).contenthash(hash)
}
Example #9
Source File: provider-gas-price-provider.ts    From noether with Apache License 2.0 5 votes vote down vote up
constructor(provider: Provider, gasPriceMultiplier = 100) {
        this.provider = provider;
        this.gasPriceMultiplier = gasPriceMultiplier;
    }
Example #10
Source File: kms-ethers-signer.ts    From cloud-cryptographic-wallet with MIT License 5 votes vote down vote up
connect(provider: Provider): KmsEthersSigner {
    return new KmsEthersSigner(this.config, provider);
  }
Example #11
Source File: adapter.ts    From cloud-cryptographic-wallet with MIT License 5 votes vote down vote up
constructor(config: AdapterConfig, provider?: Provider) {
    super();

    this.signer = config.signer;
    defineReadOnly(this, "provider", provider);

    this.logger = new Logger(config.version);
  }
Example #12
Source File: adapter.ts    From cloud-cryptographic-wallet with MIT License 5 votes vote down vote up
connect(provider: Provider): Adapter {
    return new Adapter(
      { signer: this.signer, version: this.logger.version },
      provider
    );
  }
Example #13
Source File: transactionsUpdater.ts    From mStable-apps with GNU Lesser General Public License v3.0 5 votes vote down vote up
TransactionsUpdater = (): null => {
  const account = useAccount()
  const accountPrev = usePrevious(account)
  const provider = useSignerOrProvider()
  const blockNumber = useBlockNow()

  const state = useTransactionsState()
  const { check, finalize, reset } = useTransactionsDispatch()

  /**
   * Reset transactions state on account change
   */
  useEffect(() => {
    if (accountPrev !== account) {
      reset()
    }
  }, [account, accountPrev, reset])

  /**
   * Check pending transaction status on new blocks, and finalize if possible.
   */
  useEffect(
    (): (() => void) | void => {
      if (provider && blockNumber) {
        let stale = false
        Object.values(state)
          .filter(tx => STATUS_NEEDS_CHECK.includes(tx.status) && tx.hash && tx.blockNumber !== blockNumber)
          .forEach(tx => {
            ;(((provider as Signer).provider || provider) as Provider)
              .getTransactionReceipt(tx.hash as string)
              .then((receipt: TransactionReceipt) => {
                if (!stale) {
                  if (!receipt) {
                    if (tx?.manifest?.id) {
                      check(tx.manifest.id, blockNumber)
                    }
                  } else {
                    finalize(tx.manifest, receipt)
                  }
                }
              })
              .catch(() => {
                if (tx?.manifest?.id) {
                  check(tx.manifest.id, blockNumber)
                }
              })
          })

        return () => {
          stale = true
        }
      }
      return undefined
    },
    // `blockNumber` and `provider` should be the only deps; otherwise it will
    // check too often.
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [blockNumber, provider],
  )

  return null
}
Example #14
Source File: resolveENSContentHash.ts    From limit-orders-lib with GNU General Public License v3.0 5 votes vote down vote up
// cache the resolver contracts since most of them are the public resolver
function resolverContract(
  resolverAddress: string,
  provider: Provider
): Contract {
  return new Contract(resolverAddress, RESOLVER_ABI, provider);
}
Example #15
Source File: index.ts    From limit-orders-lib with GNU General Public License v3.0 5 votes vote down vote up
private _provider: Provider | undefined;
Example #16
Source File: index.ts    From limit-orders-lib with GNU General Public License v3.0 5 votes vote down vote up
constructor(
    chainId: ChainId,
    signerOrProvider?: Signer | Provider,
    handler?: Handler,
    isFlashbotsProtected = false
  ) {
    if (handler && !isValidChainIdAndHandler(chainId, handler)) {
      throw new Error("Invalid chainId and handler");
    } else if (
      isFlashbotsProtected &&
      (handler || !isFlashbotsCompatibleChainId(chainId))
    ) {
      throw new Error(
        "Invalid chainId or handler for Flashbots bundle submission. handler must be undefined, and chainId either 1 (mainnet) or 5 (goerli)"
      );
    }

    this._chainId = chainId;
    this._subgraphUrl = SUBGRAPH_URL[chainId];
    this._signer = Signer.isSigner(signerOrProvider)
      ? signerOrProvider
      : undefined;
    this._provider = Provider.isProvider(signerOrProvider)
      ? signerOrProvider
      : Signer.isSigner(signerOrProvider)
      ? signerOrProvider.provider
      : undefined;

    this._gelatoLimitOrders = this._signer
      ? GelatoLimitOrders__factory.connect(
          GELATO_LIMIT_ORDERS_ADDRESS[this._chainId],
          this._signer
        )
      : this._provider
      ? GelatoLimitOrders__factory.connect(
          GELATO_LIMIT_ORDERS_ADDRESS[this._chainId],
          this._provider
        )
      : (new Contract(
          GELATO_LIMIT_ORDERS_ADDRESS[this._chainId],
          GelatoLimitOrders__factory.createInterface()
        ) as GelatoLimitOrdersContract);
    this._moduleAddress = isFlashbotsProtected
      ? GELATO_LIMIT_ORDERS_MODULE_FLASHBOTS_ADDRESS[this._chainId]
      : GELATO_LIMIT_ORDERS_MODULE_ADDRESS[this._chainId];
    this._handler = handler;
    this._handlerAddress = handler
      ? HANDLERS_ADDRESSES[this._chainId][handler]?.toLowerCase()
      : undefined;
    this._isFlashbotsProtected = isFlashbotsProtected;

    this._abiEncoder = new utils.AbiCoder();

    this._erc20OrderRouter = this._signer
      ? ERC20OrderRouter__factory.connect(
          GELATO_LIMIT_ORDERS_ERC20_ORDER_ROUTER[this._chainId],
          this._signer
        )
      : this._provider
      ? ERC20OrderRouter__factory.connect(
          GELATO_LIMIT_ORDERS_ERC20_ORDER_ROUTER[this._chainId],
          this._provider
        )
      : (new Contract(
          GELATO_LIMIT_ORDERS_ERC20_ORDER_ROUTER[this._chainId],
          ERC20OrderRouter__factory.createInterface()
        ) as ERC20OrderRouter);
  }
Example #17
Source File: resolveENSContentHash.ts    From cheeseswap-interface with GNU General Public License v3.0 5 votes vote down vote up
// cache the resolver contracts since most of them are the public resolver
function resolverContract(resolverAddress: string, provider: Provider): Contract {
  return new Contract(resolverAddress, RESOLVER_ABI, provider)
}
Example #18
Source File: provider-gas-price-provider.ts    From noether with Apache License 2.0 5 votes vote down vote up
private readonly provider: Provider;
Example #19
Source File: base-provider.ts    From bodhi.js with Apache License 2.0 5 votes vote down vote up
static isProvider(value: any): value is Provider {
    return !!(value && value._isProvider);
  }
Example #20
Source File: resolveENSContentHash.ts    From sybil-interface with GNU General Public License v3.0 5 votes vote down vote up
// cache the resolver contracts since most of them are the public resolver
function resolverContract(resolverAddress: string, provider: Provider): Contract {
  return new Contract(resolverAddress, RESOLVER_ABI, provider)
}
Example #21
Source File: resolveENSContentHash.ts    From interface-v2 with GNU General Public License v3.0 5 votes vote down vote up
// cache the resolver contracts since most of them are the public resolver
function resolverContract(
  resolverAddress: string,
  provider: Provider,
): Contract {
  return new Contract(resolverAddress, RESOLVER_ABI, provider);
}
Example #22
Source File: base-provider.ts    From bodhi.js with Apache License 2.0 5 votes vote down vote up
removeAllListeners = (eventName?: EventType): Provider => throwNotImplemented('removeAllListeners');
Example #23
Source File: base-provider.ts    From bodhi.js with Apache License 2.0 5 votes vote down vote up
off = (eventName: EventType, listener?: Listener): Provider => throwNotImplemented('off');
Example #24
Source File: base-provider.ts    From bodhi.js with Apache License 2.0 5 votes vote down vote up
once = (eventName: EventType, listener: Listener): Provider => throwNotImplemented('once');
Example #25
Source File: base-provider.ts    From bodhi.js with Apache License 2.0 5 votes vote down vote up
on = (eventName: EventType, listener: Listener): Provider => throwNotImplemented('on');