1.4.1 • Published 10 months ago

@sei-js/evm v1.4.1

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

@sei-js/evm

Typescript library containing helper functions for interacting with the EVM on Sei.

Installation

yarn add @sei-js/evm

Wallet Connection

This package provides exports for easily interacting with wagmi, viem, and ethers.js. You can interact with the Sei EVM using all the same hooks and helper functions these tools offer. Read the Wagmi, viem, and ethers v6 documentation for more information on how to use these tools.

Wallet network setup

Ensure that your EVM wallet has the Sei network enabled. Learn more on how to set up your wallet.

Connection with Wagmi

The WagmiProvider is a React context provider that allows you to easily interact with the EVM. It provides many useful hooks for reading and writing on Sei via the EVM JSON-RPC.

import { ReactNode } from 'react';
import { WagmiProvider } from 'wagmi';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ARCTIC_1_VIEM_CHAIN, createWagmiConfig } from '@sei-js/evm';

const queryClient = new QueryClient();

export const WalletProvider = ({ children }: { children: ReactNode }) => {
 return (
   <WagmiProvider config={createWagmiConfig([ARCTIC_1_VIEM_CHAIN])}>
     <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
   </WagmiProvider>
 );
};

Connection with ethers v6

The 'ethers' package is a popular library for interacting with the Ethereum blockchain. This package provides a helper function for creating an ethers provider that is connected to the Sei EVM.

import { ADDRESS_PRECOMPILE_ADDRESS } from '@sei-js/evm';
import { ethers } from 'ethers';

const provider = new ethers.BrowserProvider(window.ethereum); // or any other provider
const signer = await provider.getSigner();

const accounts = await provider.send('eth_requestAccounts', []);

const contract = getAddressPrecompileEthersV6Contract(ADDRESS_PRECOMPILE_ADDRESS, signer);

const cosmosAddress = await contract.getSeiAddr(accounts[0]);

Usage with viem

This package exports viem Chains and precompile ABI's for Sei. The ABI used in the ethers example above is a viem ABI instance and the ARCTIC_1_VIEM_CHAIN is a viem Chain instance.

Utils

This package also exports a set of utility functions for interacting with the EVM on Sei.

parseSei

Parses an amount of Sei to the smallest unit of Sei (uSei).

import { parseSei } from '@sei-js/evm';

const amount = parseSei('1000000');
console.log(amount); // 1000000000000000000

Interoperability with Cosmos

Sei v2 supports both EVM JSON-RPC and Cosmos RPC interfaces. In order to easily interact with certain Cosmos modules, Sei v2 has a set of precompiled contracts that can be called from the EVM.

PrecompileDescription
Address PrecompileEnables the retrieval of associated EVM addresses for given Cosmos addresses and vice versa.
Bank PrecompileProvides functionalities for managing balances, supply, symbols, and more.
Distribution PrecompileFacilitates operations related to rewards withdrawal and distribution.
Governance PrecompileSupports actions such as depositing funds into proposals and voting.
JSON PrecompileFacilitates interoperability between the EVM and Cosmos.
Staking PrecompileEnables staking functionalities like delegation and undelegation.
WASM PrecompileProvides functionalities for executing WebAssembly (WASM) code.

Interoperability using Wagmi, viem, and ethers

Each precompile has contract exports a typed 'ethers' contracts and provides the ABI and contract addresses for each precompile for usage with Wagmi and viem.

Address Precompile

The Address precompile contract enables the retrieval of associated EVM addresses for given Cosmos addresses and vice versa.

Functions

Function NameInput ParametersReturn ValueDescription
getEvmAddraddr: string{ response: string }Retrieves the associated EVM address for a given Cosmos address.
getSeiAddraddr: string{ response: string }Retrieves the associated Cosmos address for a given EVM address.
associatev: string, r: string, s: string, customMessage: string{ response: string }Associates an EVM address with it's corresponding Sei Native address on chain using a signature.
associatePubKeypubKeyHex: string{ response: string }Associates an EVM address with it's corresponding Cosmos address on chain using the Hex-Encoded compressed pubkey (excluding the '0x').

Precompile Address

0x0000000000000000000000000000000000001004

Bank Precompile

The Bank precompile contract provides functionalities for managing balances, supply, symbols, and more.

Functions

Function NameInput ParametersReturn ValueDescription
balanceacc: string, denom: string{ amount: string }Retrieves the balance of the specified account for the given denomination.
decimalsdenom: string{ response: string }Retrieves the number of decimal places for the specified denomination.
namedenom: string{ response: string }Retrieves the name of the specified denomination.
sendfromAddress: string, toAddress: string, denom: string, amount: number{ success: boolean }Sends tokens from one address to another.
supplydenom: string{ response: string }Retrieves the total supply of tokens for the specified denomination.
symboldenom: string{ response: string }Retrieves the symbol of the specified denomination.
sendNativetoNativeAddress: string, value: string{ success: boolean }Sends native tokens to a specified address.

Precompile Addresses

0x0000000000000000000000000000000000001001

Distribution Precompile

The Distribution precompile contract facilitates operations related to rewards withdrawal and distribution.

Functions

Function NameInput ParametersReturn ValueDescription
setWithdrawAddresswithdrawAddress: string{ success: boolean }Sets the withdrawal address for rewards.
withdrawDelegationRewardsvalidator: string{ success: boolean }Withdraws delegation rewards for a given validator.
withdrawMultipleDelegationRewardsvalidators: string[]{ success: boolean }Withdraws delegation rewards for given validators.
rewardsdelegatorAddress: address{ rewards: Rewards }Queries rewards available for a given delegator address. Rewards are usually returned as decimals. To calculate the actual amount, divide the amount by decimals.

Precompile Addresses

0x0000000000000000000000000000000000001007

Governance Precompile

The Governance precompile contract supports actions to deposit funds into proposals and vote on them.

Functions

Function NameInput ParametersReturn ValueDescription
depositproposalID: string{ success: boolean }Deposits funds into a governance proposal.
voteproposalID: string, option: string{ success: boolean }Votes on a governance proposal.

Precompile Addresses

0x0000000000000000000000000000000000001006

JSON Precompile

The JSON precompile contract facilitates interoperability between the EVM and Cosmos by providing functions to extract data in various formats.

Functions

Function NameInput ParametersReturn ValueDescription
extractAsBytesinput: string, key: string{ response: string }Extracts data as bytes from the input using the specified key.
extractAsBytesListinput: string, key: string{ response: string[] }Extracts data as a list of bytes from the input using the specified key.
extractAsUint256input: string, key: string{ response: string }Extracts data as a uint256 from the input using the specified key.

Precompile Addresses

Staking Precompile

The Staking precompile contract provides functions for delegation, re-delegation, and un-delegation staking operations.

Functions

Function NameInput ParametersReturn ValueDescription
delegatevalAddress: string{ success: boolean }Delegates tokens to the specified validator.
redelegatesrcAddress: string, dstAddress: string, amount: string{ success: boolean }Redelegates tokens from the source validator to the destination validator.
undelegatevalAddress: string, amount: string{ success: boolean }Undelegates tokens from the specified validator.
delegationdelegator: address, valAddress: string{ delegation: Delegation }Queries delegation by delegator and validator address. Shares in DelegationDetails are usually returned as decimals. To calculate the actual amount, divide the shares by decimals.

Precompile Addresses

0x0000000000000000000000000000000000001005

WASM Precompile

The WASM precompile contract facilitates execution, instantiation, and querying of WebAssembly (WASM) contracts on the Sei platform.

Functions

Function NameInput ParametersReturn ValueDescription
executecontractAddress: string, msg: Uint8Array, coins: Uint8Array{ response: Uint8Array }Executes a message on the specified contract with provided coins.
instantiatecodeID: string, admin: string, msg: Uint8Array, label: string, coins: Uint8Array{ contractAddr: string; data: Uint8Array }Instantiates a new contract with the specified code ID, admin, and coins.
querycontractAddress: string, req: Uint8Array{ response: Uint8Array }Queries the specified contract with the provided request.

Precompile Addresses

0x0000000000000000000000000000000000001002

IBC Precompile

The IBC precompile contract facilitates messages exchange between Sei and other IBC compatible blockchains.

Functions

Function NameInput ParametersReturn ValueDescription
transfertoAddress: string, port: string, channel: string, denom: string, amount: ethers.BigNumberish, revisionNumber: BigInt, revisionHeight: BigInt, timeoutTimestamp: BigInt, memo: String{ success: boolean }Transfers tokens from the caller's address to another on a different (IBC compatible) chain.
transferWithDefaultTimeouttoAddress: string, port: string, channel: string, denom: string, amount: ethers.BigNumberish, memo: String{ success: boolean }Transfers tokens from the caller's address to another on a different (IBC compatible) chain. Calculates default timeout height as opposed to transfer function

Precompile Addresses

0x0000000000000000000000000000000000001009