The intents.js SDK is designed to simplify complex DeFi operations by abstracting multi-chain transactions. It allows developers to create and execute intents, which are high-level descriptions of what the user wants to achieve, like staking, swapping tokens, or lending, without needing to handle multiple steps and protocols manually.
Installation
Install the SDK:
npminstallintents.js
Setup
Import the necessary modules and initialize the Account and IntentBuilder
import { Account, IntentBuilder, PROJECTS, CHAINS, toBigInt, amountToBigInt } from'intents.js';import { ethers } from'ethers';constchainConfigs= {1: { rpcUrl:'YOUR_ETH_RPC_URL', bundlerUrl:'https://eth.bundler.borsa.network', },56: { rpcUrl:'YOUR_BNB_RPC_URL', bundlerUrl:'https://bsc.bundler.Borsa.network', },};constsigner=newethers.Wallet('your private key');// Create an instance of the IntentBuilderconstintentBuilder=awaitIntentBuilder.createInstance(chainConfigs);// Create an Account instanceconstaccount=awaitAccount.createInstance(signer, chainConfigs);
Creating an Intent
An intent defines the source and target states. Here, we create an intent for a multi-chain staking operation. Funds are moved from AAVE on the BNB Chain to be staked on Lido (Ethereum).
constusdtAmount=244.7;constethAmount=0.1;constusdtAddress='0xdac17f958d2ee523a2206206994597c13d831ec7';// Define the source state (where the funds are coming from)constfrom=newLoan({ address:PROJECTS.Aave, amount:amountToBigInt(usdtAmount,18), chainId:toBigInt(CHAINS.BNBChain), asset: usdtAddress,});// Define the target state (where the funds are going)constto=newStake({ amount:amountToBigInt(ethAmount,18), address:PROJECTS.Lido, chainId:toBigInt(CHAINS.Ethereum),});
Executing the Intent
Once your intent is defined, you can execute it with the following code. This submits the intent as an ERC-4337 user operation on the Borsa network.
constsolvedHash=awaitintentBuilder.execute(from, to, account);
Fetching the Transaction Receipt
After the transaction is executed, you can fetch the on-chain receipt to get transaction details, such as the transaction hash.
constreceipt=awaitintentBuilder.getReceipt(1, solvedHash);consttxHash=receipt.result.receipt.transactionHash;console.log(`View your transaction on-chain: Transaction Hash: ${txHash} Explorer Link: https://etherscan.io/tx/${txHash}`);