balancer / balancer-core

Balancer on the EVM

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I Authorize an User to use the Balancer Pools? Which Function do I need to call?

Nachoxt17 opened this issue · comments

  • Hello, I am Developing this BalancerOperator.sol Smart Contract:
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.9;
pragma experimental ABIEncoderV2;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./IBPool.sol";
import "./IVault.sol";

/// @title Balancer Pool Operator Example.
/// @author Ignacio Ceaglio for 10Clouds.

/*
 * - Balancer 50%USDC/50%WETH Pool and L.P. Token Smart Contract Address:
 * - 0x96646936b91d6B9D7D0c47C496AfBF3D6ec7B6f8
 * - Balancer 50%USDC/50%WETH Vault Smart Contract Address:
 * - 0xBA12222222228d8Ba445958a75a0704d566BF2C8
 * - Balancer 50%USDC/50%WETH Vault Authorizer Smart Contract Address:
 * - 0xa331d84ec860bf466b4cdccfb4ac09a1b43f3ae6
 * The Vault holds all pool tokens and performs the related bookkeeping. It serves
 * as a single entry point for pool interactions like joins, exits and swaps and
 * delegates to the respective pools.
 * - Pool ID: 0x96646936b91d6b9d7d0c47c496afbf3d6ec7b6f8000200000000000000000019
 * Index 0: USDC - 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
 * Index 1: WETH - 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2
 */

contract BalancerOperator is IVault {
    /** @dev When providing your assets, you must ensure that the tokens are sorted numerically by token address. It's also important
    to note that the values in maxAmountsIn correspond to the same index value in assets, so these arrays must be made in parallel
    after sorting.*/
    function addLiquidity(
        address vaultAddr,
        bytes32 poolId,
        address[] calldata assets,
        uint256[] calldata maxAmountsIn
    ) external {
        uint256 oneUint256 = 1;
        bytes memory userDataEncoded = abi.encode(oneUint256, maxAmountsIn, oneUint256); //https://dev.balancer.fi/helpers/encoding
        JoinPoolRequest memory InRequest = JoinPoolRequest(assets, maxAmountsIn, userDataEncoded, false);
        IVault(vaultAddr).joinPool(poolId, msg.sender, msg.sender, InRequest);
    }

    /** @dev When providing your assets, you must ensure that the tokens are sorted numerically by token address. It's also important
    to note that the values in maxAmountsIn correspond to the same index value in assets, so these arrays must be made in parallel
    after sorting.*/
    function removeLiquidity(
        address vaultAddr,
        bytes32 poolId,
        address[] calldata assets,
        uint256[] calldata minAmountsOut
    ) external {
        uint256 oneUint256 = 1;
        bytes memory userDataEncoded = abi.encode(oneUint256, minAmountsOut, oneUint256); //https://dev.balancer.fi/helpers/encoding
        ExitPoolRequest memory OutRequest = ExitPoolRequest(assets, minAmountsOut, userDataEncoded, false);
        IVault(vaultAddr).exitPool(poolId, msg.sender, payable(msg.sender), OutRequest);
    }

    function exchangeTokens(
        //address poolAddr,
        address vaultAddr,
        bytes32 poolId,
        address tokenInAddress,
        uint256 maxAmountIn,
        address tokenOutAddress
    ) external {
        uint256 oneUint256 = 1;
        //uint256 maxPrice = (110 * IBPool(poolAddr).getSpotPrice(tokenInAddress, tokenOutAddress)) / 100;
        //uint256 tokenAmountOut = maxPrice * maxAmountIn;

        /**IERC20(tokenInAddress).approve(address(this), maxAmountIn);
        IERC20(tokenInAddress).transferFrom(msg.sender, address(this), maxAmountIn);
        IERC20(tokenInAddress).approve(vaultAddr, maxAmountIn);*/

        bytes memory userDataEncoded = abi.encode(oneUint256, maxAmountIn, oneUint256); //https://dev.balancer.fi/helpers/encoding
        SingleSwap memory SingleSwapRequest = SingleSwap(
            poolId,
            SwapKind.GIVEN_OUT,
            tokenInAddress,
            tokenOutAddress,
            maxAmountIn,
            userDataEncoded
        );
        FundManagement memory FundManagementRequest = FundManagement(msg.sender, false, payable(msg.sender), false);
        IVault(vaultAddr).swap(SingleSwapRequest, FundManagementRequest, maxAmountIn, (block.timestamp + 3 minutes));

        //IERC20(tokenOutAddress).transfer(msg.sender, tokenAmountOut);
    }

    //...
}
  • and when I try to test it with Hardhat by Forking the Ethereum MainNet, I have this Error Log:
1) Balancer Pool S.Contract
       Balancer Pool Adds Liquidity:
     Error: VM Exception while processing transaction: reverted with reason string 'BAL#401'
      at <UnrecognizedContract>.<unknown> (0xba12222222228d8ba445958a75a0704d566bf2c8)
      at BalancerOperator.addLiquidity (contracts/swaps/Balancer/BalancerOperator.sol:39)
      at async HardhatNode._mineBlockWithPendingTxs (node_modules\hardhat\src\internal\hardhat-network\provider\node.ts:1773:23)
      at async HardhatNode.mineBlock (node_modules\hardhat\src\internal\hardhat-network\provider\node.ts:466:16)
      at async EthModule._sendTransactionAndReturnHash (node_modules\hardhat\src\internal\hardhat-network\provider\modules\eth.ts:1504:18)
      at async HardhatNetworkProvider.request (node_modules\hardhat\src\internal\hardhat-network\provider\provider.ts:118:18)
      at async EthersProviderWrapper.send (node_modules\@nomiclabs\hardhat-ethers\src\internal\ethers-provider-wrapper.ts:13:20)

  2) Balancer Pool S.Contract
       Balancer Pool Removes Liquidity:
     Error: VM Exception while processing transaction: reverted with reason string 'BAL#401'
      at <UnrecognizedContract>.<unknown> (0xba12222222228d8ba445958a75a0704d566bf2c8)
      at BalancerOperator.removeLiquidity (contracts/swaps/Balancer/BalancerOperator.sol:54)
      at async HardhatNode._mineBlockWithPendingTxs (node_modules\hardhat\src\internal\hardhat-network\provider\node.ts:1773:23)
      at async HardhatNode.mineBlock (node_modules\hardhat\src\internal\hardhat-network\provider\node.ts:466:16)
      at async EthModule._sendTransactionAndReturnHash (node_modules\hardhat\src\internal\hardhat-network\provider\modules\eth.ts:1504:18)
      at async HardhatNetworkProvider.request (node_modules\hardhat\src\internal\hardhat-network\provider\provider.ts:118:18)
      at async EthersProviderWrapper.send (node_modules\@nomiclabs\hardhat-ethers\src\internal\ethers-provider-wrapper.ts:13:20)

  3) Balancer Pool S.Contract
       Balancer Pool Exchanges Tokens:
     Error: VM Exception while processing transaction: reverted with reason string 'BAL#401'
      at <UnrecognizedContract>.<unknown> (0xba12222222228d8ba445958a75a0704d566bf2c8)
      at BalancerOperator.exchangeTokens (contracts/swaps/Balancer/BalancerOperator.sol:83)
      at async HardhatNode._mineBlockWithPendingTxs (node_modules\hardhat\src\internal\hardhat-network\provider\node.ts:1773:23)
      at async HardhatNode.mineBlock (node_modules\hardhat\src\internal\hardhat-network\provider\node.ts:466:16)
      at async EthModule._sendTransactionAndReturnHash (node_modules\hardhat\src\internal\hardhat-network\provider\modules\eth.ts:1504:18)
      at async HardhatNetworkProvider.request (node_modules\hardhat\src\internal\hardhat-network\provider\provider.ts:118:18)
      at async EthersProviderWrapper.send (node_modules\@nomiclabs\hardhat-ethers\src\internal\ethers-provider-wrapper.ts:13:20)