Deposit

Depositing into a smart vault

This chapters explains the technical implmenentation of depositing into smart vaults using Yelay protocol.

To see an example depositing implementation of the SDK in the frontend and try it out yourself, check out the the Yelay v2 SDK Cookbook.

Approval of assets

Before depositing assets into a vault, you must first grant approval for the Vault to interact with your tokens. It's crucial to understand that the SDK does not automatically handle asset approvals on your behalf. As a result, you are responsible for ensuring that the necessary approvals are in place before utilizing the deposit functionality.

When you initiate a deposit of assets into a Vault, you effectively transfer those assets to a smart contract that the Vault governs. However, explicit permission is required for the smart contract to interact with your tokens successfully. This permission is obtained by approving a specific quantity of tokens to be accessed and potentially transferred by the smart contract.

The 'approve' function grants the smart contract the authority to access and transfer a predetermined amount of your tokens on your behalf. This mechanism acts as a security measure, ensuring that the smart contract cannot access more tokens than you have explicitly authorized. It's important to note that the approval process is a one-time operation and is distinct from the actual deposit transaction.

Therefore, before proceeding with a token deposit into a Vault, you must invoke the 'approve' function to grant the Vault's smart contract the necessary permission to transfer the specified quantity of tokens from your address. This requirement explains why you frequently encounter the 'approve' function being called before executing the deposit transaction.

Aprove assets usign the SDK


const erc20Instance: ERC20 = ERC20__factory.connect( 'your_token_contract_address_here', signer_or_fireblocks_provider, ); 

const approveTx = await erc20Instance.approve( await config.getChainAddresses(signer_or_fireblocks_provider)).ISmartVaultManager, amount_to_approve, ); 

const approveResult = await approveTx.wait();

Depositing into a smart vault

When the user selects a desired smart vault and the amount they want to invest, they call the function on the Smart vault manager contract. This function first runs some checks called 'guards' and 'actions' and then marks the deposit with some additional metadata.

After that, the deposit NFT (dNFT) with the above-mentioned data is minted. That is an ERC20 token, used as a receipt for the deposit.

In the next step, the assets of the deposit enter the pending state by getting transferred to Yelay Master wallet.

The details about funds management after this step can be found in the chapter about flush->DHW->sync.

Deposit using the SDK

const deposit = async () => {
    const depositBag: DepositBagStruct = {
        smartVault: "vault_address",
        assets: ["amount"],
        receiver: signer.address,
        referral: ethers.constants.AddressZero,
        doFlush: false,
    };
    const tx = await yelaySDK.deposit(
        depositBag
    )
    const recpt = await tx.wait()
    console.log(recpt)
}

The DepositBagStruct represents a data structure with various properties used in a deposit. Let's take a closer look at each of these properties.

  • smartVault - Vault address to deposit to.

  • assets - The deposit amount, considering the token's decimal notation (if you want to deposit 100 USDC, you have to write 100000000).

  • receiver - This is the address of the user that will be credited, i.e. will receive the deposit NFT. Most often this equals the address of the user initiating the deposit. The address might be different if a user is depositing on behalf of another user.

  • referral - This parameter can be omitted. It is used when the user would like to mark/label a deposit for possible future referral fee distribution (doesn't impact code execution, but will be shown in transaction metadata).

  • doFlush - 'Flush' parameter usually needs to be set to false. Vaults will be flushed by Yelay before running DoHardWork.

Swap and deposit

Yelay offers a convenient 'swap and deposit' feature for users who don't hold the specific assets required by a Vault. This functionality allows users to swap their existing assets for their desired asset and simultaneously deposit them into the Vault, all within a single transaction.

To ensure the best possible swap rates, Yelay uses https://1inch.io/, an aggregator DEX that identifies the most optimal swap routes across various DeFi exchanges.

Hypothetically, any tokens could be used for swap as long as there's a swap route available.

Yelay supports 1:1 swaps (e.g., USDC to WETH) as well as 1:N and N:1 swaps. However, Yelay does not currently facilitate N:M swaps (e.g., 3 input tokens to 2 output tokens) due to the complexity involved in calculating the most efficient path for such a vast array of potential combinations.

When users need to convert between ETH and WETH, Yelay employs a dedicated function that wraps ETH into WETH, rather than relying on the swapping mechanism.

Swap and deposit using the SDK

When you need to wrap ETH (Ethereum) into WETH (Wrapped Ethereum), use the following method:

const swapAndDeposit = async () => {
    console.log("swap & deposit")
    const vaultAddressGB = "vault_address" 
    const wethTokenAddress = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
    const swapAndDeposit = await yelaySDK.swapAndDeposit({
        inTokens: [wethTokenAddress],
        inAmounts: [BigNumber.from(0)],
        smartVault: vaultAddressGB,
        swapInfo: [],
        receiver: "user_address",
        referral: ethers.constants.AddressZero,
        doFlush: false
    }, {
        value: ethers.utils.parseEther("0.1")
    })
    const res = await swapAndDeposit.wait()
}

The DepositBagStruct represents a data structure with various properties used in a deposit. Here's a detailed breakdown of each property and its role within the deposit process:

  • inTokens[] - Addresses of tokens that you want to swap.

  • inAmounts[] - The deposit amounts, considering the token's decimal notation (if you want to deposit 100 USDC, you have to write 100000000). This exact amount will be sent to the "Swapper" contract, surplus will be sent back to the user.

  • smartVault - Vault address to deposit to.

  • swapInfo[] - Array of objects with swap instructions {swapTarget, token, swapCallData}.

  • receiver - Receiver address of the deposit NFT.

  • referral - Custom field reserved for users to set and manage their own referral information.

  • doFlush - 'Flush' parameter usually needs to be set to false. Vaults will be flushed by Yelay before running DoHardWork.

Last updated