Nexera-Fi SDKUsage ExamplesSupported Adapters

Balancer Adapter Showcase

In this demonstration, we'll highlight the application of the Balancer V2 Adapter within the Nexera-Fi SDK for executing a token swap operation, assuming that the strategy Orchestrator instance is already deployed and configured.
(see Deployment & Configuration Guide, Initializing Adapters)

Specifically, we will swap WETH tokens for BAL.

Make sure you've appropriately initialized the NexeraFiSDK, covering configurations essential for further interactions.

const chainId: ChainId = 1; // Ethereum Mainnet
 
// Target Assets
// Note: Assets at play should be already whitelisted in the `Orchestrator`   
const WETH = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"; // Token in balance
const BAL = "0xba100000625a3754423978a60c9317c58a424e3d"; // Target asset
 
// Target Pool ID
const POOLID = "0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014";
 
// Target Adapter (i.e. BalancerAdapter)
const BalancerAdapter = nexeraFiSdk.getAdapters(chainId).BalancerAdapter;
 
// Connect to your Orchestrator instance
// Note: `BalancerAdapter` should be already whitelisted and properly initialized in the `Orchestrator`
const orchestratorAddress = "..."; // Replace with the address of the instance you own 
const Orchestrator = nexeraFiSdk.getOrchestrator(orchestratorAddress, wallet_signer);
 
// Get nonce for Tx
let nonce_ = await wallet_signer.getTransactionCount();
 
// Send WETH to Orchestrator
// (Adapters can only manage assets that are allocated to the Orchestrator)
await WethContract.connect(wallet_signer).transfer(
    Orchestrator.address,
    ethers.utils.parseEther("1"), // 1 WETH
    {nonce: nonce_}
);
 
/* ------------------- Construct `out` & `expectedIn` token compositions, and `extraData` ------------------- */
// Tokens to send (i.e. 1 WETH)
const out = [
    { 
        token: WETH,
        amount: ethers.utils.parseEther("1")
    },
];
 
// Tokens to receive (i.e. BAL)
// `amount` field is for Minimum amount of tokens expected to be received (set to "0" for no limit indication)
const expectedIn = [
    { token: BAL, amount: "0" }
];
 
 
// Data to protocol function (i.e poolId, kind, fromInternal, toInternal, userData, limit and deadline)
const SwapKind = BalancerAdapter.extraOperations.SWAP_KIND;
const extraSwapData = ethers.utils.defaultAbiCoder.encode(
    ['tuple(bytes32,uint256,bool,bool,bytes,uint256,uint256)'], 
    // poolId, kind, fromInternal, toInternal, userData, limit, deadline
    [[POOLID, SwapKind.GIVEN_IN, false, false, "0x", 0, BigNumber.from(2).pow(256).sub(1)]]
);
 
// Extra data contains extra arguments used by the function (i.e Type of operation or data to protocol function)
const SwapOperation = BalancerAdapter.operations.SWAP;
const extraData = ethers.utils.defaultAbiCoder.encode(
    ['tuple(uint256,bytes)'],
    [[SwapOperation.SWAP, extraSwapData]]
);
 
// Use `callStatic` to simulate the Tx and acquire what is received (asset-wise) by the Orchestrator
const totalReceived = await Orchestrator.callStatic.execute(
    [{
        adapter: BalancerAdapter.address,
        op: Operation.SWAP,  // `Operation` enum is exported by the NexeraFi-SDK package
        send: out,
        minReceive: expectedIn,
        extraData: extraData
    }]
);
 
nonce_ = await wallet_signer.getTransactionCount(); // Get nonce for Tx
 
// Execute Swap action
await Orchestrator.execute(
    [{
        adapter: BalancerAdapter.address,
        op: Operation.SWAP,
        send: out,
        minReceive: expectedIn,
        extraData: extraData
    }],
    {nonce: nonce_}
);
 
nonce_ = await wallet_signer.getTransactionCount(); // Get nonce for Tx
 
// Withdraw specified assets from the Orchestrator and transfer them to a designated receiver
await Orchestrator.withdraw(BAL, totalReceived[0][0].amount, wallet_signer.address, {nonce: nonce_});