BenQi¶
Connector for BenQi lending protocol (Compound V2 fork on Avalanche).
almanak.framework.connectors.benqi
¶
BENQI Lending Connector (Compound V2 fork on Avalanche).
This module provides adapters and utilities for interacting with BENQI, a leading lending/borrowing protocol on Avalanche using the qiToken architecture.
BENQI Features: - Compound V2-style qiToken model - Supply to earn yield (mint qiTokens) - Borrow against collateral (enterMarkets + borrow) - Multiple asset markets: AVAX, USDC, USDT, WETH.e, BTC.b, sAVAX
Supported Chains: - Avalanche
Example
from almanak.framework.connectors.benqi import ( BenqiAdapter, BenqiConfig, BenqiReceiptParser, )
config = BenqiConfig( chain="avalanche", wallet_address="0x...", ) adapter = BenqiAdapter(config)
Supply USDC¶
result = adapter.supply(asset="USDC", amount=Decimal("1000"))
Parse receipts¶
parser = BenqiReceiptParser(underlying_decimals=6) events = parser.parse_receipt(receipt)
BenqiAdapter
¶
Adapter for BENQI lending protocol on Avalanche.
Provides methods to build transactions for supply, withdraw, borrow, and repay operations using the qiToken (Compound V2) architecture.
get_market_info
¶
Get market info for an asset.
get_qi_token_address
¶
Get the qiToken address for an asset.
supply
¶
Build a supply (lend) transaction.
For ERC20 tokens: calls mint(uint256) on the qiToken. For AVAX: calls mint() payable on qiAVAX.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
asset
|
str
|
Asset symbol (e.g., "USDC", "AVAX") |
必需 |
amount
|
Decimal
|
Amount in underlying token units (e.g., 1000 USDC) |
必需 |
返回:
| 类型 | 描述 |
|---|---|
TransactionResult
|
TransactionResult with TX data for execution |
withdraw
¶
withdraw(
asset: str,
amount: Decimal,
*,
withdraw_all: bool = False,
redeem_amount: int | None = None,
) -> TransactionResult
Build a withdraw transaction.
Calls redeemUnderlying(uint256) on the qiToken to withdraw exact underlying amount. For withdraw_all with redeem_amount, uses redeem(uint256) with exact qiToken count. For withdraw_all with amount > 0, uses redeemUnderlying(amount_wei) with the tracked amount.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
asset
|
str
|
Asset symbol |
必需 |
amount
|
Decimal
|
Amount of underlying to withdraw |
必需 |
withdraw_all
|
bool
|
If True, withdraw entire balance |
False
|
redeem_amount
|
int | None
|
Exact qiToken amount for redeem() when withdraw_all=True. Not yet wired from the compiler — reserved for future use when the compiler can query on-chain qiToken balances. |
None
|
返回:
| 类型 | 描述 |
|---|---|
TransactionResult
|
TransactionResult with TX data |
borrow
¶
Build a borrow transaction.
Calls borrow(uint256) on the qiToken. Requires collateral to be supplied and enterMarkets() called first.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
asset
|
str
|
Asset symbol to borrow |
必需 |
amount
|
Decimal
|
Amount to borrow in underlying units |
必需 |
返回:
| 类型 | 描述 |
|---|---|
TransactionResult
|
TransactionResult with TX data |
repay
¶
Build a repay transaction.
For ERC20: calls repayBorrow(uint256) on the qiToken. For AVAX: calls repayBorrow() payable on qiAVAX. For repay_all: uses MAX_UINT256 to repay full debt.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
asset
|
str
|
Asset symbol |
必需 |
amount
|
Decimal
|
Amount to repay |
必需 |
repay_all
|
bool
|
If True, repay entire outstanding debt |
False
|
返回:
| 类型 | 描述 |
|---|---|
TransactionResult
|
TransactionResult with TX data |
enter_markets
¶
Build an enterMarkets transaction to enable assets as collateral.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
assets
|
list[str]
|
List of asset symbols to enable as collateral |
必需 |
返回:
| 类型 | 描述 |
|---|---|
TransactionResult
|
TransactionResult with TX data |
exit_market
¶
Build an exitMarket transaction to remove an asset from collateral.
Note: exitMarket is per-asset (not array), unlike enterMarkets. Will revert if the user has outstanding borrows against this collateral.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
asset
|
str
|
Asset symbol to remove from collateral |
必需 |
返回:
| 类型 | 描述 |
|---|---|
TransactionResult
|
TransactionResult with TX data |
BenqiConfig
dataclass
¶
Configuration for BENQI adapter.
BenqiMarketInfo
dataclass
¶
BenqiMarketInfo(
asset: str,
qi_token_address: str,
underlying_address: str | None,
decimals: int,
is_native: bool,
)
Information about a BENQI market (qiToken).
BenqiPosition
dataclass
¶
User position in BENQI.
TransactionResult
dataclass
¶
TransactionResult(
success: bool,
tx_data: dict[str, Any] | None = None,
gas_estimate: int = 0,
description: str = "",
error: str | None = None,
)
Result of building a transaction.
BenqiEvent
dataclass
¶
BenqiEventType
¶
Bases: Enum
BENQI event types.
BenqiReceiptParser
¶
Parse BENQI transaction receipts.
Extracts supply, withdraw, borrow, and repay amounts from Compound V2-style events (Mint, Redeem, Borrow, RepayBorrow).
parse_receipt
¶
Parse a transaction receipt for BENQI events.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
receipt
|
dict[str, Any]
|
Transaction receipt dict with 'logs' field |
必需 |
qi_token_address
|
str | None
|
Optional filter for specific qiToken |
None
|
返回:
| 类型 | 描述 |
|---|---|
ParseResult
|
ParseResult with extracted events and amounts |
parse_logs
¶
parse_logs(
logs: list[dict[str, Any]],
tx_hash: str = "",
block_number: int = 0,
) -> list[BenqiEvent]
Parse a list of log entries into BENQI events.
extract_supply_data
¶
Extract supply data from a parsed transaction receipt.
Called by ResultEnricher for SUPPLY intents.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
result
|
dict[str, Any]
|
Transaction receipt dict with 'logs' field |
必需 |
返回:
| 类型 | 描述 |
|---|---|
dict | None
|
Dict with supply_amount and qi_tokens_minted, or None if not found |
extract_borrow_data
¶
Extract borrow data from a parsed transaction receipt.
Called by ResultEnricher for BORROW intents.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
result
|
dict[str, Any]
|
Transaction receipt dict with 'logs' field |
必需 |
返回:
| 类型 | 描述 |
|---|---|
dict | None
|
Dict with borrow_amount, or None if not found |
extract_withdraw_data
¶
Extract withdraw data from a parsed transaction receipt.
Called by ResultEnricher for WITHDRAW intents.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
result
|
dict[str, Any]
|
Transaction receipt dict with 'logs' field |
必需 |
返回:
| 类型 | 描述 |
|---|---|
dict | None
|
Dict with withdraw_amount, or None if not found |
extract_repay_data
¶
Extract repay data from a parsed transaction receipt.
Called by ResultEnricher for REPAY intents.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
result
|
dict[str, Any]
|
Transaction receipt dict with 'logs' field |
必需 |
返回:
| 类型 | 描述 |
|---|---|
dict | None
|
Dict with repay_amount, or None if not found |
ParseResult
dataclass
¶
ParseResult(
success: bool,
events: list[BenqiEvent] = list(),
supply_amount: Decimal = Decimal("0"),
withdraw_amount: Decimal = Decimal("0"),
borrow_amount: Decimal = Decimal("0"),
repay_amount: Decimal = Decimal("0"),
qi_tokens_minted: Decimal = Decimal("0"),
qi_tokens_redeemed: Decimal = Decimal("0"),
error: str | None = None,
)
Result of parsing a transaction receipt.