owned this note
owned this note
Published
Linked with GitHub
# Yearn Router Contract Integration Guide
![](https://i.imgur.com/6pAw7xy.jpg)
This guide will walk you through the process of integrating the Yearn Router Contract into your frontend application to enable deposits at Yearn. By following this and applying through the partner's [form](https://yearnfinance.typeform.com/to/uP7xOJUN?typeform-source=docs.yearn.finance), you can receive partner fees. Once applied and processes followed, partner fees will be automatically redistributed.
There are two methods for integration: **Delegated Deposit** and **Wrapper**. In this guide, we'll focus on the Delegated Deposit, where the deposit is made by the partner's contract and vault tokens sent to the user's wallet. For more information on both methods, please refer to the [Yearn Partners Integration Guide](https://docs.yearn.finance/partners/integration_guide).
## Router Contract Live Addresses
- Ethereum: `to be added after deploy`
- Polygon: [`0x8da9f46d9b1e02d27713b176838740fb5cfcc636`](https://polygonscan.com/address/0x8da9f46d9b1e02d27713b176838740fb5cfcc636#code)
## Yearn Router Contract ABI
To interact with the Yearn Router Contract, you'll need its ABI (Application Binary Interface). You can find the ABI in the contract's source code:
- [ABI in JSON format](https://api.polygonscan.com/api?module=contract&action=getabi&address=0x8da9f46d9b1e02d27713b176838740fb5cfcc636)
- [ABI in RAW format](http://api.polygonscan.com/api?module=contract&action=getabi&address=0x8da9f46d9b1e02d27713b176838740fb5cfcc636&format=raw)
Add the ABI to your frontend application, so you can use it to create a contract instance.
## Use Ethers to make a deposit in frontend
To interact with the Yearn Router Contract, you can use the Ethers.js library. First, install the library using npm or yarn:
```bash
npm install --save ethers
```
Next, import the library and create a contract instance using the ABI and contract address:
```javascript
import { ethers } from 'ethers';
import yearnRouterAbi from './yearnRouterAbi.json';
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const yearnRouterAddress = '0x8da9f46d9b1e02d27713b176838740fb5cfcc636'; // Replace with Ethereum address when available
const yearnRouterContract = new ethers.Contract(yearnRouterAddress, yearnRouterAbi, signer);
```
Now you can call the `depositToVault` function to make a deposit:
```javascript
async function depositToVault(vaultAddress, amount, minSharesOut) {
const vaultContractInstance = new ethers.Contract(vaultAddress, vaultAbi, signer);
const tokenAddress = await vaultContractInstance.asset();
const tokenContract = new ethers.Contract(tokenAddress, erc20Abi, signer);
// Approve the router contract to spend the tokens
const approveTx = await tokenContract.approve(yearnRouterAddress, amount);
await approveTx.wait();
// Deposit tokens to the vault on behalf of the user
const depositTx = await yearnRouterContract.depositToVault(vaultAddress, amount, minSharesOut);
const receipt = await depositTx.wait();
console.log('Deposit successful:', receipt);
}
```
Note that you'll also need the ERC20 ABI to interact with the token contract for approval. You can find it [here](https://github.com/ethereum/EIPs/blob/master/assets/eip-20/contract/IERC20Metadata.sol). Here's an example of how to call the `depositToVault` function with sample parameters:
```javascript
const vaultAddress = '0x1234567890123456789012345678901234567890'; // Replace with the actual vault address
const amount = ethers.utils.parseUnits('100', 18); // Deposit 100 tokens (assuming 18 decimals)
const minSharesOut = ethers.utils.parseUnits('99', 18); // Expect at least 99 shares out (assuming 18 decimals)
depositToVault(vaultAddress, amount, minSharesOut)
.then(() => console.log('Deposit successful'))
.catch((error) => console.error('Deposit failed:', error));
```
This example assumes that the token and shares have 18 decimals. Adjust the `parseUnits` function accordingly if the token or shares have a different number of decimals.
## Additional Notes
- Before making the first deposit into a vault using the router, make sure the vault has been approved to pull tokens from the router. This only needs to be done once per vault.
- For withdrawals, you need to approve the router to be able to pull the vault token from the user.
- The deposit process can be further optimized by using the `permit` function along with `multicall` to reduce the number of transactions. This feature is still being worked on and will be updated in the future.
- The syntax for the partner contracts may change slightly when implementing the V3 version, but the overall process remains the same.