Portfolio Management API
Overview
The Velvet Portfolio Management API enables you to manage and rebalance tokenized portfolios (vaults) programmatically. With this API, you can:
Retrieve and inspect all portfolios (vaults) created by a given owner.
Rebalance portfolios by selling one token and buying another.
Execute trades using on-chain calls.
Deposit and withdraw tokens from portfolios.
Obtain helper information, such as the number of portfolio tokens a user holds.
This guide will walk you through key endpoints and illustrate how to integrate the provided data and call parameters into your applications.
Prerequisites
A wallet with a private key to sign blockchain transactions.
A JSON-RPC provider URL for your target chain (for instance, Base chain with chainId = 8453).
Familiarity with Ethereum-compatible contracts, tokens, and basic web3 concepts.
Trade Flow Overview
To execute a trade withing a Velvet portfolio (vault), follow these steps:
Fetch All Created Vaults: Retrieve all portfolios (vaults) associated with a specific owner.
Use the Rebalance API Endpoint: Generate the necessary call data and parameters for the intended trade.
Execute the Trade On-Chain: Use the parameters from the Rebalance API to call the vault’s rebalance contract function.
Fetch All Created Vaults
Endpoint: GET https://api.velvet.capital/api/v3/portfolio/owner/<OWNER_WALLET_ADDRESS>?chain=base
Description: Retrieves all vaults created by a specified owner on the Velvet V3 platform.
URL Parameters:
OWNER_WALLET_ADDRESS
(string): The Ethereum address of the owner.
Response:
Copy
Usage: From the response, note the rebalancing
address, which will be needed to prepare the trade in the next step.
Rebalance API Endpoint
Endpoint: POST https://eventsapi.velvetdao.xyz/api/v3/rebalance
Description: Generates the necessary parameters (call data, handler address, etc.) to execute a trade within a portfolio’s rebalance contract.
Request Body:
Copy
Parameters:
rebalanceAddress
(string, required): The rebalance contract address retrieved from the "Fetch All Created Vaults" response.sellToken
(string, required): The token address you want to sell.buyToken
(string, required): The token address you want to buy.sellAmount
(string, required): The amount ofsellToken
to sell (in smallest unit, e.g., if USDC is 6 decimals,1 USDC
=1000000
).slippage
(string, required): Allowed slippage in basis points. For example,"100"
means 1%.remainingTokens
(array, required): The list of tokens that will remain in the vault after the trade.owner
(string, required): The vault owner’s Ethereum address.
Response:
Copy
Note: Use the returned data (newTokens
, sellTokens
, sellAmounts
, handler
, and callData
) to execute the trade in the next step.
Executing the Trade On-Chain
After obtaining the necessary parameters from the Rebalance API, you can execute the trade by interacting directly with the rebalance contract on-chain.
Prerequisite: Install Ethers.js:
Copy
Example Code:
Copy
Rebalance Transaction API
Endpoint: POST https://eventsapi.velvetdao.xyz/api/v3/rebalance/txn
Description: Initiates a rebalance transaction directly. Useful when you already know the parameters required to perform the rebalance.
Request Body:
Copy
Parameters:
Similar to the Rebalance API Endpoint (above), but returns direct transaction data for immediate execution.
Portfolio Deposit API
Endpoint: POST https://eventsapi.velvetdao.xyz/api/v3/portfolio/deposit
Description: Deposits a specified amount of tokens into a given portfolio.
Request Body:
Copy
Parameters:
portfolio
: The portfolio contract address.depositAmount
: The amount to deposit (in token’s smallest unit).depositToken
: The token’s contract address being deposited.user
: The depositor’s Ethereum address.depositType
: The deposit mode (e.g.,batch
).tokenType
: The type of token (e.g.,erc20
).
Response: Returns transaction data (to
, data
, gasLimit
, gasPrice
) needed to broadcast the transaction.
Example Code: After approval, send the returned transaction data via wallet.sendTransaction(tx)
.
Portfolio Withdraw API
Endpoint: POST https://eventsapi.velvetdao.xyz/api/v3/portfolio/withdraw
Description: Initiates a withdrawal from a specified portfolio.
Request Body:
Copy
Parameters:
portfolio
: The portfolio address.withdrawAmount
: The withdrawal amount (in smallest unit).withdrawToken
: The token address to withdraw.user
: The user’s Ethereum address initiating the withdrawal.withdrawType
: The withdrawal mode (e.g.batch
).tokenType
: The type of token (e.g.,erc20
).
Response: Returns transaction data needed to finalize the withdrawal transaction on-chain.
Edge Case: If the portfolio contains only one token and the user wishes to withdraw that same token, use the multiTokenWithdrawal
function from the portfolio contract’s ABI.
Helper Functions
Get Portfolio Token Amount
Function: getPortfolioTokenAmount(portfolioAddress, account, chainId=8453)
Description: Returns the number of portfolio tokens a user holds.
Example:
Copy
Conclusion
With the endpoints and code samples provided, you should be able to:
Discover and manage all your Velvet vaults.
Programmatically prepare and execute trades through the Rebalance interface.
Deposit and withdraw tokens from portfolios.
Monitor and query token amounts within portfolios.
For a seamless integration, ensure you handle proper token approvals and confirm transactions on-chain. By following these guidelines and examples, you can fully leverage the Velvet Portfolio Management API.
Happy coding!
Last updated