@lombard.finance/sdk
TypeScript icon, indicating that this package has built-in type declarations

3.6.14 • Public • Published

@lombard.finance/sdk

The Lombard's SDK package provides a set of functions that allow interacting with the Lombard protocol and its features.

Read more about Lombard's mission: https://www.lombard.finance

Installation

1. Dependencies installation

The SDK depends on the following packages:

  • axios
  • viem@2.23
  • bignumber.js@9
  • bitcoinjs-lib@6.1.5
  • @bitcoin-js/tiny-secp256k1-asmjs@2.2.3
  • @layerzerolabs/lz-v2-utilities@3.0.17

You may install them by running the following command:

npm i --save viem@^2.23.15 axios@^1 bignumber.js@^9 @bitcoin-js/tiny-secp256k1-asmjs@2.2.3 bitcoinjs-lib@6.1.5 @layerzerolabs/lz-v2-utilities@3.0.17

2. SDK installation

To install the SDK package, please run:

npm i --save @lombard.finance/sdk

Usage

All functions are documented with JSDoc comments. You can use your IDE's autocomplete feature to see the available methods and their parameters.

1. Depositing BTC in order to get LBTC (aka staking).

You can read more about LBTC here: https://docs.lombard.finance/lbtc-liquid-bitcoin/introduction-to-lbtc

If you'd wish to stake your BTC and get LBTC follow the below steps:

1.1 Get the current minting fee.

const fee = await getLBTCMintingFee({ chainId: ChainId.ethereum }); // The fee represented in satoshis (BigNumber)

1.2. Sign the network fee signature.

const expiry = Math.round((Date.now() + 24 * 60 * 60 * 1000) / 1000);
const { signature, typedData } = await signNetworkFee({
  fee, // The fee from step 1
  expiry, // The optional expiration unix timestamp. This parameter can be omitted, it default to 24h from now. We recommend to set this to at least 8h from now.
  account, // The destination account address from the connected wallet.
  chainId: ChainId.ethereum // The destination chain id.
  provider, // The EIP-1193 provider, e.g. the injected provider: window.ethereum
});

1.3. Store the signature to the Lombard's systems.

await storeNetworkFeeSignature({ signature, typedData, address }); // Pass the signature and typed data from step 2.

It is recommended to verify that the signature has been stored. Please use getNetworkFeeSignature.

const { expirationData, hasSignature, isDelayed } = await getNetworkFeeSignature({ address, chainId });

isDelayed is a flag determining whether the execution of auto-claimer using the stored signature is delayed due to the higher gas costs.

1.4. Get or generate the BTC deposit address.

let depositBtcAddress = await getDepositBtcAddress({ address, chainId });
if (!depositBtcAddress) {
  depositBtcAddress = await generateDepositBtcAddress({ 
    address,
    chainId,
    signature, // Pass here the signature from step 2.
    eip712Data: typedData // Pass here the typed data from step 2.
  });
}

1.5. Deposit BTC to the address.

Now you can deposit your BTC to the generated in the previous step BTC address. The funds will be claimed automatically by Lombard's claimer and transferred to the account (address).

1.6. Check the status of your deposit.

If you'd like to check the status of your deposit use getDepositsByAddress function.

const deposits = await getDepositsByAddress({ address });

Every entry in the result of the above function may consist of the following properties:

  • txid - the BTC transaction id,
  • index - the index of the actual deposit transaction,
  • blockHeight
  • blockTime
  • value - the amount of BTC deposited,
  • address - the destination address,
  • chainId - the destination chain id,
  • isClaimer - a flag determining whether the deposit has been already claimed,
  • claimedTxId - the corresponding claim transaction that transfer funds to the destination address,
  • rawPayload - the payload of the transaction (can be use to claim the funds manually),
  • signature - the signature used (can be used to claim the funds manually),
  • isRestricted - a flag determining whether the transaction has been marked as suspicious/restricted,
  • payload - the payload (corresponding to the Bascule drawbridge security),
  • sessionId
  • notarizationStatus - the notarization status of the deposit (pending, submitted, approved or failed),
  • sessionState - the state of the session (pending, completed, expired)

2. Manually claiming LBTC.

In case when a user deposited BTC to the BTC deposit address but the transaction has not been claimed automatically (due to expired signature or any other issue), you may want to claim LBTC manually as in the example below:

const txHash = await claimLBTC({
  data: rawPayload, // Pass the raw payload from the deposit data as presented in the previous step.
  proofSignature: signature, // Pass the signature from the deposit data.
  account, // The connected account address
  chainId, // The chain id
  provider, // The EIP-1193 provider,
  rpcUrl, // The optional RPC url.
})

The successful execution of the above will result with the transaction id.

3. Depositing BTC and automatically staking LBTC into the DeFi vault

You can read more about the DeFi vaults here: https://docs.lombard.finance/lbtc-liquid-bitcoin/defi-vaults/lombard-defi-vault

If you'd wish to stake and bake your BTC follow the steps below.

3.1. See what's the current stake and bake fee.

To check the current stake and bake fee you may use the following function:

const fee = await getStakeAndBakeFee({
  vaultKey: Vault.Veda, // The vault identifier, currently only "veda" is accepted.
  chainId, // The chain id.
  rpcUrl, // The options RPC url.
});
const expectedLBTCAmount = BigNumber(amountToBeDeposited).minus(fee);

The fee amount will be deducted from the claimed LBTC automatically.

3.2. Sign the stake and bake signature

const { signature, typedData } = await signStakeAndBake({
  account,   // The connected account address.
  expiry,    // Optional expiration timestamp (unix). Defaults to 24h from now. Recommended: at least 8h from now.
  value,     // The amount of the token (see `token` param).
  token,     // The token to sign with. Defaults to "BTC": the value is converted to LBTC using the current ratio. 
             // If "LBTC" is chosen, no conversion is applied.
  vaultKey: Vault.Veda, // The vault identifier. Currently only "veda" is accepted.
  chainId,   // The chain ID.
  provider,  // The EIP-1193 provider.
  rpcUrl,    // Optional RPC URL.
})
Param Type Required Description
account string The connected account address.
expiry number Optional expiration time (Unix timestamp). Defaults to 24h from now. Recommended: set at least 8h from now.
value string/number The token amount (interpreted based on the token param).
token "BTC" | "LBTC" The token to sign with. Defaults to "BTC". If "BTC", the amount is converted to LBTC using the current exchange ratio. If "LBTC", the value is used as-is (no conversion).
vaultKey Vault The vault identifier. Currently only "veda" is accepted.
chainId number The target chain ID.
provider EIP-1193 provider The connected Web3 provider.
rpcUrl string Optional RPC URL override.

Note:

  • If token is "BTC", the function automatically converts the value to the corresponding LBTC amount using the current exchange ratio.
  • If token is "LBTC", the value is used as-is (no conversion).

3.3. Store the signature to the Lombard's systems.

await storeStakeAndBakeSignature({
  signature, // Pass here the signature form the previous step.
  typedData, // Pass here the typed data from the previous step.
})

It is recommended to verify if the signature has been stored.

const data = await getUserStakeAndBakeSignature({
  userDestinationAddress: address,
  chainId,
})

3.4. Get or generate the BTC deposit address.

let depositBtcAddress = await getDepositBtcAddress({ address, chainId });
if (!depositBtcAddress) {
  depositBtcAddress = await generateDepositBtcAddress({ 
    address,
    chainId,
    signature, // Pass here the signature from step 2.
    signatureData: typedData // Pass here the typed data from step 2.
  });
}

3.5. Deposit BTC to the address.

Now you can deposit your BTC to the BTC deposit address from above. The funds will be automatically claimed and deposited to the DeFi vault.

3.6. Check the status of you deposit

const deposits = await getDepositsByAddress({ address });

3.7. Check the amount of shares acquired.

const { balance, exchangeRate, balanceLbtc } = await getSharesByAddress({
  vaultKey: Vault.Veda, // The vault identifier.
  address, // The account address.
  chainId, // The chain id.
  rpcUrl, // The optional RPC url
});

The above code results with:

  • balance - The amount of LBTCv shares owned by the account,
  • exchangeRate - The current LBTCv to LBTC exchange rate,
  • balanceLbtc - The value of the owned shares is LBTC.

4. Unstaking LBTC and getting BTC back.

Every LBTC is redeemable back to BTC, you can do that programmatically by following the steps:

4.1. Unstake LBTC.

const txaHash = await unstakeLBTC({
  btcAddress, // The address to which the funds will be redeemed.
  amount, // The amount of LBTC to unstake.
  account, // The account address.
  chainId, // The chain id.
  provider, // The EIP-1193 provider.
  rpcUrl, // The optional RPC url.
});

4.2. Check the status of your unstakes.

If you'd like to get the list of all unstaked made by an address, use this:

const unstakes = await getUnstakesByAddress({ address });

Every entry in the result of the above may consist of:

  • txHash - The unstake transaction hash,
  • chainId,
  • blockHeight,
  • unstakeDate,
  • fromAddress - The EVM source address,
  • toAddress - The BTC destination address of the funds,
  • amount - The amount unstaked,
  • payoutTxHash - The BTC transaction hash,
  • payoutTxIndex - The index of the actual payout transfer,
  • payoutTxStatus - The status of the payout, available values: PayoutTxStatus.Completed or PayoutTxStatus.Pending,
  • sanctioned - A flag indicating whether the unstake transaction has been sanctioned and flagged as suspicious.

5. Depositing LBTC to the DeFi vault.

If a user already has LBTC depositing to the DeFi vault can be done via the deposit function.

5.1. Making a deposit to the DeFi vault.

const txHash = await deposit({
  amount, // The deposit amount, e.g. 1.23 (LBTC)
  approve = true, // The optional flag determining whether approval should be done within deposit execution.
  token = 'LBTC', // The optional deposit token.
  vaultKey = Vault.Veda, // The optional vault identifier.
  account, // The account address.
  chainId, // The chain id.
  provider, // The EIP-1193 provider
  rpcUrl, // The optional RPC url
})

5.2. Checking the deposit history.

const deposits = await getVaultDeposits({
  account, // The account address.
  chainId, // The chain id.
  vaultKey = Vault.Veda // The optional vault identifier.
});

The above function returns an array of deposit data made by the specified user. Each entry contains:

  • txHash - the transaction hash,
  • blockNumber - the transaction's block number,
  • chainId - the chain id,
  • amount - the deposited amount,
  • shareAmount - the amount of shares received,
  • token - the deposit token.

5.3. Checking the user's DeFi vault balance.

In order to check the user's balance of the vault tokens, use this:

const { balance, exchangeRate, balanceLbtc } = await getSharesByAddress({
  vaultKey: Vault.Veda, // The vault identifier.
  address, // The account address.
  chainId, // The chain id.
  rpcUrl, // The optional RPC url
});

The above function returns the:

  • balance - balance of LBTCv,
  • exchangeRate - the current exchange rate between LBTCv and LBTC,
  • balanceLbtc - the value of LBTCv represented in LBTC.

6. Withdrawing LBTC from the DeFi vault.

6.1. Requesting a withdrawal from the DeFi vault

Requesting a withdrawal from the DeFi vault can be done via:

const txHash = await withdraw({
  amount, // The amount of shares.
  approve = true, // The optional flag determining if approve action should be done within this execution.
  token = 'LBTC', // The optional withdraw token.
  vaultKey = Vault.Veda, // The optional vault identifier.
  account, // The account address.
  chainId, // The chain id.
  provider, // The EIP-1192 provider.
  rpcUrl, // The optional RPC url
})

6.2. Checking the withdrawal history (tracking the withdrawal request)

In order to check the whole history or to track the particular withdrawal please use the following function:

const withdrawals = await getVaultWithdrawals({
  account, // The account address.
  chainId, // The chain id.
  vaultKey = Vault.Veda, // The optional vault identifier.
  rpcUrl, // The optional RPC url
})

The result of the above is an object with broken down withdrawals by their state:

{
  cancelled: [...], // The cancelled requests.
  expired: [...], // The requests that expired.
  fulfilled: [...], // The fulfilled requests (funds were transferred).
  open: [...], // The open withdrawal requests (still to be processed).
}

Each of the arrays from above consist of:

  • token - the withdrawal token (LBTC),
  • shareAmount - the amount of shares withdrawn,
  • amount - the amount of funds withdrawn,
  • minPrice - the min price of a share,
  • deadline - the expiration timestamp,
  • timestamp - the request timestamp,
  • txHash - the withdraw request transaction hash,
  • blockNumber - the request block number,
  • fulfilledTimestamp - the fulfilment timestamp,
  • fulfilledTxHash - the funds transfer transaction hash,
  • fulfilledBlockNumber - the fulfilment block number.

6.3. Cancelling the withdrawal

If you wish to cancel you open withdrawal request use this:

const txHash = await cancelWithdraw({
  token = 'LBTC', // The optional withdrawal asset.
  vaultKey = Vault.Veda, // The optional vault identifier.
  account, // The account address.
  chainId, // The chain id.
  provider, // The EIP-1193 provider.
  rpcUrl, // The optional RPC url.
});

7. Getting the points earned by an address.

If you'd like to check the amount of Lux points earned by an address then simply run the following function:

const points = await getPointsByAddress({ address: "0x...YOUR_ADDRESS" })

The function returns the object of shape:

  {
    /**
     * The number of points earned by holding LBTC.
     */
    holdingPoints: number;
    /**
     * The number of points earned by taking positions in DeFi vaults.
     */
    protocolPoints: number;
    /**
     * The number of points earned by your referrals.
     */
    referralPoints: number;
    /**
     * The number of points earned in the OKX campaign.
     */
    okxPoints: number;
    /**
     * The number of points earned by participating in the first flash event.
     */
    flashEvent1Points: number;
    /**
     * The number of points earned by participating in the second flash event.
     */
    flashEvent2Points: number;
    /**
     * The total number of points.
     */
    totalPoints: number;
    /**
     * The breakdown of points earned from each protocol.
     */
    protocolPointsBreakdown: IProtocolPointsBreakdown;
    /**
     * The amount of LUX points earned from badges.
     */
    badgesPoints: number;
    /**
     * The total amount of LUX points (without badges points).
     */
    totalWithoutBadgesPoints: number;
  }

8. Getting the DeFi vault points earned by an address.

const { 
  totalPoints, // The total points earned in the DeFi vault.
  pointsBreakdown // The points breakdown by network (chain).
} = await getVaultPoints({
  account, // The account address.
  vaultKey // The optional vault identifier.
})

9. Metrics

9.1. Getting the vault's TVL

The vault's TVL can be obtained by calling the getVaultTVL function.

const data = await getVaultTVL({ vaultKey: Vault.Veda });

The above returns:

  • btcBalance - the amount of BTC locked into the vault,
  • btcPrice - the current price of BTC,
  • tvl - the amount of USD locked into the vault.

9.2. Getting the vault's performance data.

The performance of the vault can be checked via the getVaultApy function. As in the example below:

const APYs = await getVaultApy({
  aggregationPeriod: 7, // The aggregation period in days, only 7, 14, and 30 are allowed.
  chainId: ChainId.ethereum, // The vault's chain - can be omitted, defaults to `Ethereum`.
  vaultKey: Vault.Veda // The vault identifier - can be omitted, default to `Vault.Veda`
});

The above returns an array of APY entries sorted in descending order (newest first). Each entry contains:

  • apy - the APY value,
  • allocations - the record of general allocations in protocols used by the vault,
  • breakdown - the detailed record of allocations and APY values broken down by chain and protocol,
  • timestamp - the timestamp of the entry.

9.3. Getting the LBTC statistics.

The simple set of LBTC statistics is accessible via getLBTCStats function.

const stats = await getLBTCStats({
  accountAddress, // The (optional) account address - passing the accountAddress ensures the relevant stats are returned
  partnerId, // The (optional) partner id - passing the partnerId will ensures the relevant stats are returned
  env // The optional environment flag
})

The stats contain:

  • historicalHolders - the number of all-time LBTC holders,
  • holders - the number of current LBTC holders,
  • price - the current BTC price,
  • supply - the number of LBTC minted,
  • tvl - the Lombard's TVL in USD.

9.4. Getting the LBTC exchange ratio.

LBTC is a yield-bearing token and its exchange rate to BTC is not guaranteed to be 1:1.

In order to check the current exchange rate use:

const ratioData = await getExchangeRatio();

// returns:
//
// ratioData = {
//     [Token.LBTC]: {
//         tokenBTCRatio: BigNumber(1)
//         BTCTokenRatio: BigNumber(1)
//     }
// }

The result of the above function is an object which contains:

  • tokenBTCRatio - The Token:BTC ratio answering the question of how many tokens will I get for 1 BTC.
  • BTCTokenRatio - The BTC:Token ratio (1 / tokenBTCRatio) answering the question of how many BTC will I get for 1 Token.

9.5. Getting the positions (yield) summary

The information about the yield acquired by an account can be obtained via the getPositionsSummary function as shown below:

const rewardsInfo = await getPositionsSummary({
  account, // The account address
  env // Optional env flag
});

The results of the above includes:

  • btcPrice: { price: BigNumber; timestamp: Date; }
    Contains the current price of BTC in USD and the time it was last fetched.
    • price (BigNumber): The price of 1 BTC in USD.
    • timestamp (Date): The timestamp when the price was last updated.
  • btcValue: BigNumber
    The total value of all holdings, expressed in BTC.
  • btcPnl: BigNumber
    The total profit or loss across all positions, represented in BTC.
  • snapshot: Array<{ token, type, balance, pnl, rate }>
    A list of individual positions used in PnL calculations. Each entry includes:
    • token (Token | undefined): The token associated with the position (e.g., Token.LBTC). May be undefined if unspecified.
    • type (PositionType): The classification or source of the position (e.g., staking, trading).
    • balance (BigNumber): The quantity of the token held.
    • pnl (BigNumber): The profit or loss for this specific position, in BTC.
    • rate (BigNumber): The conversion rate from token to BTC.
      Formula: balance * rate = BTC equivalent
  • lastUpdated: Date
    The timestamp when the position summary was last updated.
  • inProgress: boolean Indicates whether the backend is currently processing the PnL calculation.
    • true: A new calculation request was received, but the backend is experiencing high load or processing is not yet complete. The latest data is not yet available.
    • false: The most recent calculation has been completed and cached. Use lastUpdated to understand how fresh the data is:
      • If lastUpdated is recent (e.g., within the last few seconds), you're seeing the latest data.
      • If lastUpdated is up to 30 minutes old, you're seeing cached data from a recent request. The backend avoids recalculating within this caching window.

9.6. Getting the LBTC APY

To retrieve the current APY (annual percentage yield) for LBTC, call this function:

const apy = await getApy({
  account, // The optional account address. Pass it for more accurate APY data.
  env, // Optional env flag
})

The above returns:

  • baseApy: BigNumber
    The base APY for LBTC, representing the nominal yield without any bonuses or adjustments.
  • effectiveApy: BigNumber
    The effective APY for LBTC, including any additional rewards, compounding effects, or protocol-specific incentives.

To retrieve the estimated APY (annual percentage yield) for LBTC in the context of a specific partner integration, use the getEstimatedApy function:

const estimated = await getEstimatedApy({
  partnerId, // Optional partner identifier. Influences the estimated APY.
  env,       // Optional env flag
});

The above returns:

  • estimatedApy: BigNumber The estimated APY for LBTC, taking into account partner-specific incentives, compounding assumptions, and projected rewards. This value may differ from the effective APY depending on the partner context.

Note: If no partnerId is provided, the function returns a default estimate based on global assumptions.

9.7. Getting the additional rewards data

In order to retrieve the additional rewards that have been distributed or are pending use the below function:

const rewards = await getAdditionalRewards({
  account,
  env,
});

The above returns:

  • distributed: Array<{ name: string; amount: BigNumber }>
    A list of campaigns where BTC rewards have already been distributed.

    • name (string): Name of the reward campaign.
    • amount (BigNumber): Amount of BTC distributed for the campaign.
  • undistributed: Array<{ name: string; amount: BigNumber }>
    A list of campaigns where BTC rewards are still pending distribution.

    • name (string): Name of the reward campaign.
    • amount (BigNumber): Amount of BTC yet to be distributed.

/@lombard.finance/sdk/

    Package Sidebar

    Install

    npm i @lombard.finance/sdk

    Weekly Downloads

    928

    Version

    3.6.14

    License

    MIT

    Unpacked Size

    3.77 MB

    Total Files

    192

    Last publish

    Collaborators

    • lombard229
    • lombard_finance
    • meaganlombard