Skip to content

Commit

Permalink
Include minSharesOut in allocate inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuba committed Dec 14, 2023
1 parent a3aa149 commit 2c08d1d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
14 changes: 9 additions & 5 deletions core/contracts/Dispatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ contract Dispatcher is Router, Ownable {
error VaultUnauthorized();
error InvalidVaultsWeight(uint16 vaultsWeight, uint16 vaultsTotalWeight);
error TotalAmountZero();
error InvalidMinSharesOutListLength(uint256 current, uint256 expected);

struct VaultInfo {
bool authorized;
Expand Down Expand Up @@ -204,10 +205,13 @@ contract Dispatcher is Router, Ownable {
// allocation. We may need improved solution to calculate exactly how much
// tBTC should be deposited or withdrawn from each vault.
// TODO: Make callable only by the maintainer bot.
// TODO: Add pre-calculated minSharesOut values for each deposit.
// TODO: Consider having constant total weight, e.g. 1000, so the vaults can
// have
function allocate() public {
function allocate(uint256[] memory minSharesOutList) public {
if (minSharesOutList.length != vaults.length)
revert InvalidMinSharesOutListLength(
minSharesOutList.length,
vaults.length
);

uint16 vaultsWeight = vaultsWeight();
if (
vaultsTotalWeight == 0 ||
Expand Down Expand Up @@ -236,7 +240,7 @@ contract Dispatcher is Router, Ownable {

// TODO: Pre-calculate the minSharesOut value off-chain as a slippage protection
// before calling the allocate function.
uint256 minSharesOut = vault.previewDeposit(depositAmount);
uint256 minSharesOut = minSharesOutList[i];

// Allocate tBTC to Vault.
depositToVault(vault, depositAmount, minSharesOut);
Expand Down
6 changes: 3 additions & 3 deletions core/test/Dispatcher.Routing.POC.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe("Dispatcher Routing", () => {
.connect(governance)
.setVaultWeights([await vault1.getAddress()], [vault1Weight])

await dispatcher.allocate()
await dispatcher.allocate([expectedVault1Shares, 0])

expect(await tbtc.balanceOf(await acre.getAddress())).to.be.equal(0)
expect(await tbtc.balanceOf(await dispatcher.getAddress())).to.be.equal(0)
Expand All @@ -221,7 +221,7 @@ describe("Dispatcher Routing", () => {
.connect(governance)
.setVaultWeights([await vault1.getAddress()], [vault1Weight])

await dispatcher.allocate()
await dispatcher.allocate([expectedVault1Shares, 0])

expect(await tbtc.balanceOf(await acre.getAddress())).to.be.equal(
staker1Amount - vault1DepositAmount,
Expand Down Expand Up @@ -255,7 +255,7 @@ describe("Dispatcher Routing", () => {
[vault1Weight, vault2Weight],
)

await dispatcher.allocate()
await dispatcher.allocate([expectedVault1Shares, expectedVault2Shares])

expect(await tbtc.balanceOf(await acre.getAddress())).to.be.equal(0)
expect(await tbtc.balanceOf(await dispatcher.getAddress())).to.be.equal(0)
Expand Down

0 comments on commit 2c08d1d

Please sign in to comment.