Skip to content

Commit

Permalink
Swaps working
Browse files Browse the repository at this point in the history
  • Loading branch information
alsco77 committed Feb 17, 2021
1 parent abb9c60 commit 8b78f4e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 24 deletions.
47 changes: 28 additions & 19 deletions contracts/feeders/FeederPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ contract FeederPool is

// Internal swap between fAsset and mAsset
if (inputExists && outputExists) {
return _swapLocal(inputIdx, outputIdx, _inputQuantity, _minOutputQuantity, _recipient);
return _swapLocal(inputIdx, outputIdx, _inputQuantity, _minOutputQuantity, false, _recipient);
}
// TODO - do we want to support mAsset -> mpAsset and mpAsset -> mAsset here? I don't see the point
// If we do, need to re-jig this
Expand All @@ -402,13 +402,14 @@ contract FeederPool is
// Swapping out of fAsset
// Swap into mAsset > Redeem into mpAsset
if (inputExists) {
uint256 mAssetQuantity = _swapLocal(inputIdx, 0, _inputQuantity, 0, address(this));
uint256 mAssetQuantity = _swapLocal(inputIdx, 0, _inputQuantity, 0, false, address(this));
return IMasset(mAsset).redeem(_output, mAssetQuantity, _minOutputQuantity, _recipient);
}
// Else we are swapping into fAsset
// Mint mAsset from mp > Swap into fAsset here
IERC20(_input).safeTransferFrom(msg.sender, address(this), _inputQuantity);
uint256 mAssetQuantity = IMasset(mAsset).mint(_input, _inputQuantity, 0, address(this));
return _swapLocal(0, outputIdx, mAssetQuantity, _minOutputQuantity, _recipient);
return _swapLocal(0, outputIdx, mAssetQuantity, _minOutputQuantity, true, _recipient);
}

/**
Expand Down Expand Up @@ -484,24 +485,31 @@ contract FeederPool is
uint8 _outputIdx,
uint256 _inputQuantity,
uint256 _minOutputQuantity,
bool _skipDeposit,
address _recipient
) internal returns (uint256 swapOutput) {
// 2. Load cache
BassetData[] memory allBassets = bAssetData;
Cache memory cache = _getCacheDetails();
// 3. Deposit the input tokens
BassetPersonal memory inputPersonal = bAssetPersonal[_outputIdx];
uint256 quantityDeposited =
Manager.depositTokens(
inputPersonal,
allBassets[_inputIdx].ratio,
_inputQuantity,
cache.maxCache
);
// 3.1. Update the input balance
bAssetData[_inputIdx].vaultBalance =
allBassets[_inputIdx].vaultBalance +
SafeCast.toUint128(quantityDeposited);
BassetPersonal memory inputPersonal = bAssetPersonal[_inputIdx];
// TODO - consider if this is dangerous to trust supplied _inputQty
uint256 quantityDeposited = _inputQuantity;
{
if(!_skipDeposit) {
quantityDeposited = Manager.depositTokens(
inputPersonal,
allBassets[_inputIdx].ratio,
_inputQuantity,
cache.maxCache
);
}
// 3.1. Update the input balance
bAssetData[_inputIdx].vaultBalance =
allBassets[_inputIdx].vaultBalance +
SafeCast.toUint128(quantityDeposited);
}


// 3. Validate the swap
uint256 scaledFee;
Expand All @@ -510,7 +518,7 @@ contract FeederPool is
_inputIdx,
_outputIdx,
quantityDeposited,
_outputIdx == 0 ? 0 : swapFee,
0, // TODO - revert to _outputIdx == 0 ? 0 : swapFee. Need to resovle stack too deep
_getConfig()
);
require(swapOutput >= _minOutputQuantity, "Output qty < minimum qty");
Expand All @@ -519,15 +527,16 @@ contract FeederPool is
//4.1. Decrease output bal
// TODO - if recipient == address(this) then no need to transfer anything
BassetPersonal memory outputPersonal = bAssetPersonal[_outputIdx];
uint8 outputIdx = _outputIdx; // TODO -revert back
Manager.withdrawTokens(
swapOutput,
outputPersonal,
allBassets[_outputIdx],
allBassets[outputIdx],
_recipient,
cache.maxCache
);
bAssetData[_outputIdx].vaultBalance =
allBassets[_outputIdx].vaultBalance -
bAssetData[outputIdx].vaultBalance =
allBassets[outputIdx].vaultBalance -
SafeCast.toUint128(swapOutput);
// Save new surplus to storage
// TODO - re-jig the fees and increase LP token value
Expand Down
30 changes: 25 additions & 5 deletions test/feeders/basic-fns.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,46 @@ describe("Feeder Pools", () => {

// Mint with mAsset
let approval = await feederMachine.approveFeeder(mAsset, pool.address, 100)
await pool.mint(mAsset.address, approval, 95, sa.default.address)
await pool.mint(mAsset.address, approval, simpleToExactAmount(95), sa.default.address)

// Mint with fAsset
approval = await feederMachine.approveFeeder(fAsset, pool.address, 100)
await pool.mint(fAsset.address, approval, 95, sa.default.address)
await pool.mint(fAsset.address, approval, simpleToExactAmount(95), sa.default.address)
})
it("should mint single via main pool", async () => {
const { pool, mAssetDetails } = feeder
const { bAssets } = mAssetDetails
// Mint with mpAsset[0]
let approval = await feederMachine.approveFeeder(bAssets[0], pool.address, 100)
await pool.mint(bAssets[0].address, approval, 95, sa.default.address)
await pool.mint(bAssets[0].address, approval, simpleToExactAmount(95), sa.default.address)
// Mint with mpAsset[1]
approval = await feederMachine.approveFeeder(bAssets[1], pool.address, 100)
await pool.mint(bAssets[1].address, approval, 95, sa.default.address)
await pool.mint(bAssets[1].address, approval, simpleToExactAmount(95), sa.default.address)
})
})
describe("testing some swaps", () => {
describe.only("testing some swaps", () => {
before(async () => {
await runSetup()
})
it("should swap locally", async () => {
const { pool, mAsset, fAsset } = feeder
// Swap mAsset -> fAsset
const approval = await feederMachine.approveFeeder(mAsset, pool.address, 10)
await pool.swap(mAsset.address, fAsset.address, approval, simpleToExactAmount("9.5"), sa.default.address)
})
it("should swap into mpAsset", async () => {
const { pool, fAsset, mAssetDetails } = feeder
const { bAssets } = mAssetDetails
// fAsset -> mpAsset
const approval = await feederMachine.approveFeeder(fAsset, pool.address, 10)
await pool.swap(fAsset.address, bAssets[0].address, approval, simpleToExactAmount("9.5"), sa.default.address)
})
it("should swap out of mpAsset", async () => {
const { pool, fAsset, mAssetDetails } = feeder
const { bAssets } = mAssetDetails
// mpAsset -> fAsset
const approval = await feederMachine.approveFeeder(bAssets[0], pool.address, 10)
await pool.swap(bAssets[0].address, fAsset.address, approval, simpleToExactAmount("9.5"), sa.default.address)
})
})
})

0 comments on commit 8b78f4e

Please sign in to comment.