Intents.js SDK

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.

Install the Intents.js SDK via npm to integrate cross-chain DeFi functionality in your dApps.

Installation

Install the SDK:

npm install intents.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';

const chainConfigs = {
  1: {
    rpcUrl: 'YOUR_ETH_RPC_URL',
    bundlerUrl: 'https://eth.bundler.borsa.network',
  },
  56: {
    rpcUrl: 'YOUR_BNB_RPC_URL',
    bundlerUrl: 'https://bsc.bundler.Borsa.network',
  },
};

const signer = new ethers.Wallet('your private key');

// Create an instance of the IntentBuilder
const intentBuilder = await IntentBuilder.createInstance(chainConfigs);
// Create an Account instance
const account = await Account.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).

const usdtAmount = 244.7;
const ethAmount = 0.1;
const usdtAddress = '0xdac17f958d2ee523a2206206994597c13d831ec7';

// Define the source state (where the funds are coming from)
const from = new Loan({
  address: PROJECTS.Aave,
  amount: amountToBigInt(usdtAmount, 18),
  chainId: toBigInt(CHAINS.BNBChain),
  asset: usdtAddress,
});

// Define the target state (where the funds are going)
const to = new Stake({
  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.

const solvedHash = await intentBuilder.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.

const receipt = await intentBuilder.getReceipt(1, solvedHash);

const txHash = receipt.result.receipt.transactionHash;

console.log(
  `View your transaction on-chain:
   Transaction Hash: ${txHash}
   Explorer Link: https://etherscan.io/tx/${txHash}`
);

Last updated