Skip to content

Commit

Permalink
cleanup repo for release. remove empty guides to be added later
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemcdonald committed Feb 13, 2023
1 parent 312748f commit fd9bb1d
Show file tree
Hide file tree
Showing 20 changed files with 265 additions and 307 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Balancer Docs

Balancer documentation built using Vuepress and extension of a custom theme. Full markdown docs can be found in the [docs](./docs/) folder.

## Local Setup

```
npm install
npm run build-theme
npm run dev
```
2 changes: 1 addition & 1 deletion docs/concepts/pools/more/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Balancer is a flexibile protocol and as such there are many choices an user or p

## Pool Types

Choosing the type of pool to use is straightforward based on a few simple factors. The primary being the expected price variations between the tokens in the pool. For most non-stable assets a [Weighted Pool](/concepts/pools/weighted.md) is the right choice. For assets that are stable like stablecoins or assets that are stable against each other with a known price rate (ex: wstETH/weth), a [Composable Stable Pool](/concepts/pools/stable.md) allows for much deeper liquidity.
Choosing the type of pool to use is straightforward based on a few simple factors. The primary being the expected price variations between the tokens in the pool. For most non-stable assets a [Weighted Pool](/concepts/pools/weighted.md) is the right choice. For assets that are stable like stablecoins or assets that are stable against each other with a known price rate (ex: wstETH/weth), a [Composable Stable Pool](/concepts/pools/composable-stable.md) allows for much deeper liquidity.

The Balancer Dapp has a [pool creation interface](https://app.balancer.fi/#/ethereum/pool/create) for weighted pools. For stable pool creation, reach out to our devs on [Discord](https://discord.balancer.fi/) for assistance.

Expand Down
21 changes: 7 additions & 14 deletions docs/guides/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,14 @@ heroImage: /images/backgrounds/blue.png
# Developer Guides

## Arbitrageurs
- [Get Spot Price of a Pool](/guides/arbitrageurs/get-spot-price.html)
- [Execute a batch swap](/guides/arbitrageurs/execute-a-batch-swap.md)
- [Deposit internal balances and swap for minimal gas]()
- [Create paths through bb-a-usd and other yield pools]()

- [Get Spot Price of a Pool](/guides/arbitrageurs/get-spot-price.md)

## Aggregators
- [Use the SOR to find the best swap path]()
- [Calculate swap outputs for yield pools]()

## Integrators
- [Format Data for Joins & Exits]()
- [Get the APR for a pool]()
- [Get pool and swaps data from the subgraph]()
- [Use the SOR to find the best swap path](/guides/aggregators/sor-basics.md)

## Builders

- [Deploy a pool from a factory]()
- [Verify a pool on Etherscan]()
- [Build a protocol on top of Balancer]()
- [Join a pool from another smart contract]()
- [Join a pool using the SDK](/guides/builders/join-pool.md)
- [Exit a pool using the SDK](/guides/builders/exit-pool.md)
1 change: 0 additions & 1 deletion docs/guides/aggregators/boosted-pools.md

This file was deleted.

7 changes: 0 additions & 7 deletions docs/guides/arbitrageurs/execute-a-batch-swap.md

This file was deleted.

10 changes: 0 additions & 10 deletions docs/guides/arbitrageurs/get-spot-price.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,3 @@ There are few different ways to go about getting the spot price. For example, if
### Weighted Pool Spot Price Using the SDK

@[code{11-22} ts](code/sdk-spot-price.ts)

### All Pools Spot Price Using the SDK

### Boosted Pool Spot Price

TODO

### Spot Price of Complex Path

TODO
1 change: 0 additions & 1 deletion docs/guides/arbitrageurs/utilizing-internal-balances.md

This file was deleted.

1 change: 0 additions & 1 deletion docs/guides/builders/deploy-pool.md

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
title: Exiting a pool
order: 2
---

# How to exit a pool using Balancer SDK?

Balancer Pool Tokens (BPT) grant you access to a share of the pool's underlying tokens. There are three ways you can redeem them:
Expand All @@ -17,18 +18,19 @@ Balancer Pool Tokens (BPT) grant you access to a share of the pool's underlying
Set up the SDK and get the pool with liquidity service methods as `pool` ([methods interface](https://github.com/balancer-labs/balancer-sdk/blob/master/balancer-js/src/types.ts/#L321))

```javascript
import { BalancerSDK } from '@balancer-labs/sdk'
import { BalancerSDK } from '@balancer-labs/sdk';

const balancer = new BalancerSDK({
network: 1, // mainnet
rpcUrl: 'https://rpc.ankr.com/eth'
rpcUrl: 'https://rpc.ankr.com/eth',
});

// stETH pool
const poolId = '0x32296969ef14eb0c6d29669c550d4a0449130230000200000000000000000080'
const poolId =
'0x32296969ef14eb0c6d29669c550d4a0449130230000200000000000000000080';

// Get the SDK pool with service methods
const pool = await balancer.pools.find(poolId)
const pool = await balancer.pools.find(poolId);
```

### Single token exit
Expand All @@ -39,13 +41,13 @@ Receive an equal portion of all the underlying. Use `buildExitExactBPTIn` method
/**
* Builds an exit calldata and returns params ready for
* ethers sendTransaction call.
*
*
* @param address user address exiting the pool
* @param bptIn amount of BPT redeemed
* @param slippage acceptable slippage in bsp, eg. 100 = 1%
* @param unwrap optional, unwraps wrapped native tokens upon exit
* @param token optional, address of a single token to exit to
*
*
* @returns Object with ethers sendTransaction params
* {
* to: string
Expand Down Expand Up @@ -91,47 +93,49 @@ const { to, data } = pool.buildExitExactTokensOut(
Finally broadcast transaction using your signer.

```javascript
const signer = balancer.provider.getSigner()

const exitReciept = await (
await signer.sendTransaction({ to, data })
).wait()
const signer = balancer.provider.getSigner();

const exitReciept = await (await signer.sendTransaction({ to, data })).wait();
```

## Boosted pools

Exit boosted pools by swapping BPT for the underlying asset. As an example, when exiting the Stargate pool composed of STG and bbaUSD, you can exit to STG or bbaUSD BPT, but you can as well exit to DAI, USDC, or USDT as they are all part of the bbaUSD pool.
Exit boosted pools by swapping BPT for the underlying asset. As an example, when exiting the Stargate pool composed of STG and bbaUSD, you can exit to STG or bbaUSD BPT, but you can as well exit to DAI, USDC, or USDT as they are all part of the bbaUSD pool.

To do that in one transaction we can use the balancer relayer contract where it's possible to use outputs from one action as inputs in the other action. For example to exit to DAI, first exit the weighted pool to bbaUSD and then swap to bbaDAI, and finally to DAI.

![](./2023-02-03-14-16-20.png)
![](/images/exit-swap-diagram.png)

Balancer Vault actions accept two types of amounts: wad strings and chained references. Chained references allow a specific output of an action to be labeled so it can be used later as an input. This enables multiple actions to be linked together without knowing the intermediate token amounts. For example, the exit amount from an STG pool can be used as an input for a swap in the bbaUSD pool. To create a chained reference, use `Relayer.toChainReference` with any string as long as it's unique in a single call.

### Example script

```javascript
import { Relayer, WeightedPoolEncoder } from '@balancer-labs/sdk'
import { Relayer, WeightedPoolEncoder } from '@balancer-labs/sdk';

// Stargate bbaUSD BPT address
const stgBptAddress = '0x4ce0bd7debf13434d3ae127430e9bd4291bfb61f'
const stgBptAddress = '0x4ce0bd7debf13434d3ae127430e9bd4291bfb61f';

// Fetch BPT amount
const amount = await balancer.contracts.ERC20(stgBptAddress, signer).balanceOf(address).then(b => b.toString())
const amount = await balancer.contracts
.ERC20(stgBptAddress, signer)
.balanceOf(address)
.then(b => b.toString());

// Setup relayer
const relayer = balancer.contracts.relayerV4.address
const relayer = balancer.contracts.relayerV4.address;
// Authorise relayer to spend your vault balance
// needs to be done only once
await balancer.contracts.vault.connect(signer).setRelayerApproval(address, relayer, true)
await balancer.contracts.vault
.connect(signer)
.setRelayerApproval(address, relayer, true);

// Define chained reference for using the exit amount as an input in a swap
const EXIT_BBAUSD = Relayer.toChainedReference('1')
const EXIT_BBAUSD = Relayer.toChainedReference('1');

// Encode the exit action by specify type of the exit and the amount
// exitExactBPTInForOneTokenOut(amountIn, indexOf a tokenOut)
const userData = WeightedPoolEncoder.exitExactBPTInForOneTokenOut(amount, 0)
const userData = WeightedPoolEncoder.exitExactBPTInForOneTokenOut(amount, 0);

// Build exit
const exit = Relayer.encodeExitPool({
Expand All @@ -146,32 +150,28 @@ const exit = Relayer.encodeExitPool({
toInternalBalance: true,
},
outputReferences: [{ index: 0, key: EXIT_BBAUSD }],
})
});

// Build swap
const swap = Relayer.encodeBatchSwap({
swapType: 0,
swaps: [
{
poolId: bbaUSD,
assetInIndex: 0, // bbaUSD
assetInIndex: 0, // bbaUSD
assetOutIndex: 1, // bbaDAI
amount: EXIT_BBAUSD,
userData: '0x',
},
{
poolId: bbaDai,
assetInIndex: 1, // bbaDAI
assetInIndex: 1, // bbaDAI
assetOutIndex: 2, // DAI
amount: '0', // 0 amount means the vault batchswap will use the previous step's output
amount: '0', // 0 amount means the vault batchswap will use the previous step's output
userData: '0x',
}
],
assets: [
usdAdd,
daiAdd,
dai,
},
],
assets: [usdAdd, daiAdd, dai],
funds: {
fromInternalBalance: true,
recipient: address,
Expand All @@ -181,19 +181,22 @@ const swap = Relayer.encodeBatchSwap({
limits: [MaxInt256, '0', '0'], // +amount for max to send, -amount for min to receive
deadline: `${Math.ceil(Date.now() / 1000) + 3600}`, // 1 hour from now
value: '0',
outputReferences: []
outputReferences: [],
});

// Query to make sure multicall is passing
const staticCall = await balancer.contracts.relayerV4.connect(signer).callStatic.multicall([exit, swap]);
const staticCall = await balancer.contracts.relayerV4
.connect(signer)
.callStatic.multicall([exit, swap]);

const data = balancer.contracts.relayerV4.interface.encodeFunctionData('multicall', [
[exit, swap]
]);
const data = balancer.contracts.relayerV4.interface.encodeFunctionData(
'multicall',
[[exit, swap]]
);

const tx = await (await signer.sendTransaction({ to: relayer, data })).wait()
const tx = await (await signer.sendTransaction({ to: relayer, data })).wait();
```

### How to check limits?

TODO: Query to check how many tokens will be returned by the vault and set limits before sending transaction.
TODO: Query to check how many tokens will be returned by the vault and set limits before sending transaction.
Loading

0 comments on commit fd9bb1d

Please sign in to comment.