Skip to main content

Evm Extension

Overview

Discover how the EVM Extension for the Credenza Web SDK enhances blockchain interactions by providing streamlined management of the Ethereum Virtual Machine environment. This extension is pivotal for developers aiming to integrate Ethereum-based operations efficiently within their applications.

Installation

npm i @credenza3/core-web-evm-ext
typescript
import { EvmExtension } from '@credenza3/core-web-evm-ext'

Create SDK Instance

typescript
const sdk = new CredenzaSDK({
  extensions: [
    new EvmExtension({
      chainConfig,
      provider
    }),
  ],
});

Provider

Optional property Provider allows you to pass your own, custom provider (metamask window.ethereum for example) to the EvmExtension configuration. This is basically needed for loginWithSignature method

Define Chain Configuration

To define a chain configuration object for Credenza's EVM Extension, use the following details. The chainConfig object includes several properties, each specifying important parameters for the chain connection.

  • chainId: The unique identifier for the blockchain network you’re connecting to. Example: '0x13881' for Polygon's Mumbai Testnet.

  • rpcUrl: The endpoint URL to communicate with the blockchain. Example: 'https://polygon-mumbai-bor.publicnode.com'.

  • displayName: A user-friendly name for the chain. Example: 'Mumbai'.

  • blockExplorer: URL for the block explorer where you can view transactions and blocks. Example: 'https://mumbai.polygonscan.com/'.

  • nativeCurrency: An object describing the native currency. It includes properties such as name, symbol, and decimals. Example: { name: 'MATIC', symbol: 'MATIC', decimals: 18 }.

Here's an example of how you can define a chain configuration object:

typescript
const chainConfig = {
  chainId: '0x13881',
  rpcUrl: 'https://polygon-mumbai-bor.publicnode.com',
  displayName: 'Mumbai',
  blockExplorer: 'https://mumbai.polygonscan.com/',
  nativeCurrency: {
    name: 'MATIC',
    symbol: 'MATIC',
    decimals: 18, // defaults to 18
  }
}

Methods and Properties

Evm Login

sdk.evm.loginWithSignature(): Promise<void>

If custom provider was set when initing the extension, you can use sdk.evm.loginWithSignature to authenticate through your provider.

Switch Chain

sdk.evm.switchChain(chainConfig): Promise<void>

To switch chains, use the sdk.evm.switchChain method. This method's implementation depends on the login provider (e.g., Metamask or OAuth).

  • Initialize the SDK instance with the required extensions

  • Use await sdk.evm.switchChain(chainConfig) to switch the chain

  • The method adapts based on the login provider in use

Get Current Chain Config

sdk.evm.getChainConfig(): Promise<object>

To retrieve the current chain configuration, you can use the sdk.evm.getChainConfig method. This method returns the configuration details of the chain being used.

Here is a code snippet demonstrating how to get the current chain configuration:

typescript
const config = await sdk.evm.getChainConfig();
console.log(config);

Get Provider

sdk.evm.getEthersProvider(): Promise<ethers.providers.Provider>

To interact with the blockchain, you need to get the EVM provider or the ethers provider using the EvmExtension. These providers facilitate communication with the chain and the ethers.js library for more complex actions.

Obtaining the EVM provider

sdk.evm.getProvider(): Promise<object>

The above steps provide you with the necessary providers for blockchain interaction and utilizing the ethers.js functionalities. The EvmExtension methods getProvider and getEthersProvider simplify this process.

Complete the wrapping to use ethers.js for performing further operations efficiently.

typescript
const provider = await sdk.evm.getEthersProvider();

EVM provider lets you interact directly with the blockchain.

Wrapping the EVM provider with ethers.BrowserProvider:

typescript
const evmProvider = await sdk.evm.getProvider();

Enhance with Ethers.js

To boost functionality and flexibility, import Ethers.js directly from the EVM Extension. This allows easy integration and enhanced management of blockchain interactions within your application.