Getting started with Klaster
This is a simple tutorial demonstrating how to get started with the Klaster SDK and how to execute your first Interchain
Transaction (or iTx
).
Tutorial outcomes
-
The ability to pay gas in ERC20 tokens on any chain, while executing transaction(s) on any other (cross-chain gas abstraction)
-
The ability to execute multiple transcations on multiple blockchains, with a single off-chain signature
Steps
Install Klaster and viem
npm i klaster-sdk viem
Setup mock accounts
In order to get started with this tutorial, you're going to need a private key which will be able to sign the iTx
hash.
const privKey = generatePrivateKey();
const account = privateKeyToAccount(privKey);
Initialize Klaster
Initialize the Klaster SDK. In order to start the Klaster SDK, you need to provide it with accountInitData
. This is the
information which Klaster needs to derive a counterfactural ERC4337
Smart Contract Accounts. Klaster supports various Account
Vendors and you can check the supported options in the Accounts section of the docs.
// Intialize Klaster with a Biconomy account
const klaster = await initKlaster({
accountInitData: loadBicoV2Account({
owner: account.address,
}),
// NodeURL provided by the SDK is hosted by Klaster
nodeUrl: klasterNodeHost.default,
});
Send assets to the Smart Contract Account
Once Klaster has derived the address of your Smart Contract Account, you need to send some assets there in order to be able to
execute the iTx. These accounts are gasless
- which means that you don't have to send any ETH
or MATIC
or other native
token to them. The transaction fees can be paid in USDC
, USDT
, LINK
, stETH
and many other tokens...
Encode a test/mock transaction
For this getting started tutorial, you will encode a simple send ether transaction. The transaction sends the ether back to its sender - so it's a useless transaction. It's just meant to demonstrate how to encode an iTx
// Encode a simple transaction which sends 0.00001 of base currency. In our case - ETH
const sendETH = rawTx({
gasLimit: 75000n,
to: account.address, // Send back to the sender address. This is just for demo purposes
value: parseEther("0.00001"),
});
Build your Interchain Transaction
Now, you have everything you need to build your Interchain Transaction. Take the sendETH
transactions you've encoded and call
the singleTx
helper function in the steps
property of the iTx.
Steps is an array of TxBatch
objects. Every TxBatch
object contains the following:
rawTx[]
- an array of encoded EVM transaction to executechainId
- the chain on which to execute those transactions
Every iTx contains an array of TxBatch
objects.
// Build an iTx with a helper function
const iTx = buildItx({
// Encode steps or "leaf" transactions in the iTx
steps: [
// Send ETH on Optimsim
singleTx(optimism.id, sendETH),
// Send ETH on Base
singleTx(base.id, sendETH)
],
// Pay for tx fees on Polygon in USDC
feeTx: klaster.encodePaymentFee(polygon.id, "USDC"),
});
Fetch a quote from the Klaster Node
In order to execute your iTx
, you must pay a Klaster Node some transaction fee. The Klaster Nodes can accept any token they
like or even execute the transaction for zero payment.
// Fetch a quote from the Klaster Node. The node will respond with the
// amount of funds it'll charge to execute the transaction
const quote = await klaster.getQuote(iTx)
Signing the iTx
If you're satisfied with the quoted price by the node, you can give permission to the Klaster Node to execute the transactions
contained within the iTx
by signing the iTx hash with your private key.
// Sign the iTx hash with your private key. With viem, it's
// important to use the message β raw - otherwise the hash
// will be wrong
const signed = await account.signMessage({
message: {
raw: quote.itxHash
}
})
Exeuting the iTx
Once the user has signed the iTx - you can pass the signed hash to the node, together with the Quote
- by calling the execute
function on the SDK.
// Once the Node has strated executing the transaction
// it'll return the iTx hash from the execute call
const result = await klaster.execute(quote, signed)
console.log(result.itxHash)
Tracking the progress of the transaction
In order to track the progress of your executed tranaction, you can use the Klaster Explorer.
If you wish to generate explorer links - you can form the URLs like so:
https://explorer.klaster.io/details/{iTxHash}