Viem Integration
Kriptonio provides native integration with Viem via functions that convert Kriptonio SDK wallet into Viem compatible account and wallet client instances.
Under the hood, Kriptonio forwards all Viem calls to the Kriptonio wallet instance, which means that you still benefit from features like transaction sponsorship, without worrying about underlying complexity.
Installation
To use Kriptonio SDK with Viem, you need to install the @kriptonio/sdk-viem
package along with @kriptonio/sdk
and
viem
packages.
yarn add @kriptonio/sdk @kriptonio/sdk-viem viem
Wallet to Wallet Client
To convert a Kriptonio wallet instance into a Viem client instance, use the createKriptonioClient
function.
import { ChainId, KriptonioSdk } from '@kriptonio/sdk';
import { createKriptonioClient } from '@kriptonio/sdk-viem';
const sdk = new KriptonioSdk({
accessToken: 'your-access-token',
});
const wallet = await sdk.wallet.generate({
chainId: ChainId.BaseSepolia,
type: 'kernel',
});
const client = createKriptonioClient(wallet);
Wallet to Account
To convert a Kriptonio wallet instance into a Viem account instance, use the
createAccount
function.
import { ChainId, KriptonioSdk } from '@kriptonio/sdk';
import { createAccount } from '@kriptonio/sdk-viem';
const sdk = new KriptonioSdk({
accessToken: 'your-access-token',
});
const wallet = await sdk.wallet.generate({
chainId: ChainId.BaseSepolia,
type: 'kernel',
});
const account = createAccount(wallet);
Now you are able to access signMessage
, signTypedData
, and other Viem account methods.
Send Transaction
You can use the client
as a Wallet Client compatible object to send
transactions and other actions that you normally do through Viem clients.
import { ChainId, KriptonioSdk } from '@kriptonio/sdk';
import { createKriptonioClient } from '@kriptonio/sdk-viem';
const sdk = new KriptonioSdk({
accessToken: 'your-access-token',
});
const wallet = await sdk.wallet.generate({
chainId: ChainId.BaseSepolia,
type: 'kernel',
});
const client = createKriptonioClient(wallet);
const hash = await client.sendTransaction({
to: client.account.address,
value: 0n,
});
Please notice that in this example, we have generated a smart wallet, and the transaction will be automatically sponsored by Kriptonio's paymaster. All in the background, without any additional complexity for you.
Deploy Contract
To deploy a contract using a Viem client, use the Viem deployContract
function.
import { ChainId, KriptonioSdk } from '@kriptonio/sdk';
import { createKriptonioClient } from '@kriptonio/sdk-viem';
const sdk = new KriptonioSdk({
accessToken: 'your-access-token',
});
const wallet = await sdk.wallet.generate({
chainId: ChainId.BaseSepolia,
type: 'kernel',
});
const client = createKriptonioClient(wallet);
const transactionHash = await client.deployContract({
abi: [],
bytecode: '0x',
args: [],
});
Write to Contract
Writing to a contract is also a standard Viem operation. Use the getContract
method to get a contract, and then call
write.functionName
to execute a contract write function.
import { ChainId, KriptonioSdk } from '@kriptonio/sdk';
import { createKriptonioClient } from '@kriptonio/sdk-viem';
import { getContract } from 'viem';
const sdk = new KriptonioSdk({
accessToken: 'your-access-token',
});
const wallet = await sdk.wallet.generate({
chainId: ChainId.BaseSepolia,
type: 'kernel',
});
const client = createKriptonioClient(wallet);
const exampleAbi = [
{
name: 'incrementCount',
type: 'function',
inputs: [
{
name: 'value',
type: 'uint256',
internalType: 'uint256',
},
],
outputs: [],
stateMutability: 'nonpayable',
},
] as const;
const viemContract = getContract({
address: '0x123',
abi: exampleAbi,
client,
});
const transactionHash = await viemContract.write.incrementCount([2n]);