Skip to content

Ethers Integration

Ethers is one of the most popular libraries for interacting with EVM-based blockchains. Kriptonio SDK provides a simple function to convert Kriptonio SDK Wallet to an ethers compatible signer.

Installation

To use Kriptonio SDK with ethers, you need to install @kriptonio/sdk-ethers package along with @kriptonio/sdk and ethers v6 packages.

yarn add @kriptonio/sdk @kriptonio/sdk-ethers ethers@6

Wallet to Signer

To create an ethers compatible signer instance from a Kriptonio wallet, use the createKriptonioSigner function.

import { ChainId, KriptonioSdk } from '@kriptonio/sdk';
import { createKriptonioSigner } from '@kriptonio/sdk-ethers';
 
const sdk = new KriptonioSdk({
  accessToken: 'your-access-token',
});
 
const wallet = await sdk.wallet.generate({
  chainId: ChainId.BaseSepolia,
  type: 'kernel',
});
 
const signer = createKriptonioSigner(wallet);

Send Transaction

You can now use all ethers signer methods, for example, sending a transaction.

import { ChainId, KriptonioSdk } from '@kriptonio/sdk';
import { createKriptonioSigner } from '@kriptonio/sdk-ethers';
 
const sdk = new KriptonioSdk({
  accessToken: 'your-access-token',
});
 
const wallet = await sdk.wallet.generate({
  chainId: ChainId.BaseSepolia,
  type: 'kernel',
});
 
const signer = createKriptonioSigner(wallet);
 

const transaction = await signer.sendTransaction({
  to: await signer.getAddress(),
  value: 0,
});

Under the hood, Kriptonio SDK wallet is used for sending transactions, which means that features like gas sponsorship are automatically working.

Deploy Contract

We can deploy a contract via ethers ContractFactory and created ethers signer.

import { ChainId, KriptonioSdk } from '@kriptonio/sdk';
import { createKriptonioSigner } from '@kriptonio/sdk-ethers';
import ethers from 'ethers';
 
const sdk = new KriptonioSdk({
  accessToken: 'your-access-token',
});
 
const wallet = await sdk.wallet.generate({
  chainId: ChainId.BaseSepolia,
  type: 'kernel',
});
 
const exampleAbi = [
  {
    name: 'incrementCount',
    type: 'function',
    inputs: [
      {
        name: 'value',
        type: 'uint256',
        internalType: 'uint256',
      },
    ],
    outputs: [],
    stateMutability: 'nonpayable',
  },
] as const;
 
const signer = createKriptonioSigner(wallet);
 

const contract = new ethers.ContractFactory(
  exampleAbi, // contract abi
  '0x123', // compiled bytecode
  signer,
);
 
const deploymentTx = await contract.getDeployTransaction();
const transaction = await signer.sendTransaction(deploymentTx);