EVM

The Ethereum Virtual Machine (EVM) is the runtime environment for smart contracts in Ethereum. It's a fundamental part of the Ethereum network and is used by many other blockchain platforms that aim to provide Ethereum-like functionality. Our EVM SDK provides a simple interface to interact with EVM-compatible blockchains.

Core Concepts

  1. Accounts: Ethereum addresses that can hold balance and send transactions

  2. Transactions: Signed data packages that store a message to be sent from an externally owned account

  3. Gas: The fee required to execute operations on the Ethereum network

  4. Smart Contracts: Programs that run on the Ethereum blockchain

Setting up your wallet

To interact with EVM-compatible chains, you'll need a wallet with a private key. You can use tools like MetaMask or hardware wallets to generate and manage your keys securely.

For testing purposes, you can use faucets to get testnet tokens. For example, for Sepolia testnet:

Initialization

Import the SDK and create a new instance:

import { EVM } from "@kynesyslabs/demosdk/xm-websdk"

const rpc_url = "https://sepolia.infura.io/v3/YOUR-PROJECT-ID"
const instance = await EVM.create(rpc_url)

Connecting your wallet

To create signed transaction payloads, you need to connect your wallet to the SDK:

const privateKey = "0x1234567890abcdef..." // Your private key
await instance.connectWallet(privateKey)

You can view the address of your connected wallet using the getAddress method:

const address = instance.getAddress()
console.log(`Address: ${address}`)

Creating a token transfer payload

To create a signed transaction payload to transfer ETH, you can use the preparePay method:

const recipientAddress = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e"
const amount = "0.1" // Amount in ETH
const signedTx = await instance.preparePay(recipientAddress, amount)

The signedTx is a signed transaction that can be used a DEMOS transaction.

Creating multiple transfer payload

You can prepare multiple signed transaction payloads at once using the preparePays method:

const transfers = [
    { address: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e", amount: "0.1" },
    { address: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e", amount: "0.2" },
]
const signedTxs = await instance.preparePays(transfers)

The signedTxs is an array of signed transaction payloads that can be used in a DEMOS transaction.

Checking balance

You can check the balance of an address using the getBalance method:

const balance = await instance.getBalance(
    "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
)
console.log(`Balance: ${balance} ETH`)

Signing messages

const message = "Hello, world!"

// signing
const signature = await instance.signMessage(message)

// verifying signature
const verified = await instance.verifyMessage(
    message,
    signature,
    instance.getAddress(),
)

expect(verified).toBe(true)

Working with smart contracts

The SDK provides methods to interact with smart contracts:

const contractAddress = "0x..."
const abi = [...] // Contract ABI

const contract = await instance.getContractInstance(contractAddress, abi)

// Read from contract
const result = await instance.readFromContract(contract, "functionName", [arg1, arg2])

// Write to contract
await instance.writeToContract(contract, "functionName", [arg1, arg2])

Listening for events (Not implemented)

You can listen for specific events or all events from a contract:

// Listen for a specific event
instance.listenForEvent("EventName", contractAddress, abi)

// Listen for all events
instance.listenForAllEvents(contractAddress, abi)

Cleaning up

To disconnect your wallet and clean up resources, you can use the disconnect method:

await instance.disconnect()

Advanced usage

For more advanced use cases, you can access underlying properties and methods of the EVM instance:

Property/Method
Description
API Reference

instance.provider

The JsonRpcProvider instance for interacting with the Ethereum network

instance.wallet

The Wallet instance representing the connected account

instance.contracts

A Map of Contract instances for interacting with smart contracts

instance.signTransaction(s)

Method(s) to sign one or multiple transactions

-

You can use these properties and methods to perform custom operations:

const blockNumber = await instance.provider.getBlockNumber()

API Reference

https://kynesyslabs.github.io/demosdk-api-ref/classes/xmwebsdk.EVM.html

Resources

Last updated