Page cover image

Wallet Integration

Example code + mini tutorial showing Portfolio and Transactional integrations with Vaults.fyi

The example will:

  • Check user balances (idle assets) and display them in a formatted table

  • Display the best deposit options filtered for USDC/USDS only

  • Generate a transactional payload for depositing into a vault that includes optional deposit fee for wallets

  • Show all user positions across different vaults

Run the tutorial code

  1. Clone or download tutorial code

git clone https://github.com/WallfacerLabs/js-examples.git

  1. Create virtual environment and install dependencies:

cd js_examples/
npm install

  1. Set your API key as an environment variable:

export VAULTS_FYI_API_KEY="your_api_key_here"

Run

npm run wallet_integration

Step by step

1. SDK Initialization

import pkg from '@vaultsfyi/sdk';
const { VaultsSdk } = pkg;

// Initialize the SDK
const vaultsFyi = new VaultsSdk({
  apiKey: process.env.VAULTS_FYI_API_KEY,
 
});

2. Viewing User Balances

async function getUserBalances() {
  const idleAssets = await vaultsFyi.getIdleAssets({
    path: { userAddress: '0xdB79e7E9e1412457528e40db9fCDBe69f558777d' }
  });
  
  return idleAssets;
}

3. Finding Best Deposit Options

async function getBestDepositOptions() {
  const depositOptions = await vaultsFyi.getDepositOptions({
    path: { userAddress: '0xdB79e7E9e1412457528e40db9fCDBe69f558777d' },
    query: { allowedAssets: ['USDC', 'USDS'] }
  });
  
  return depositOptions;
}

4. Generating Deposit Transactions

async function generateDepositTransaction(vaultAddress, amount, userAddress, network, assetAddress) {
  const transaction = await vaultsFyi.getActions({
    path: { 
      action: 'deposit',
      userAddress: userAddress,
      network: network, 
      vaultAddress: vaultAddress
    },
    query: { 
      amount: amount,
      assetAddress: assetAddress,
      simulate: false
    }
  });
  
  return transaction;
}

5. Viewing User Positions

async function getUserPositions() {
  const positions = await vaultsFyi.getPositions({
    path: { userAddress: '0xdB79e7E9e1412457528e40db9fCDBe69f558777d' }
  });
  
  return positions;
}
  

Last updated