Skip to content

Add TradeSplitter #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 44 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
2d80b9b
add _getUniSplit function
ncitron Jun 10, 2021
754f7ec
add swapping
ncitron Jun 10, 2021
c78f5c4
only rever when total output < min output
ncitron Jun 10, 2021
48ac58b
use better math for split
ncitron Jun 10, 2021
c483e20
add tests
ncitron Jun 10, 2021
1561c74
rename contracts and functions to be inline with spec
ncitron Jun 14, 2021
c58ff34
add multi hop support
ncitron Jun 15, 2021
88b2218
add tradeExactOutput
ncitron Jun 15, 2021
682e782
clean up imports
ncitron Jun 15, 2021
e72ff11
shorten name
ncitron Jun 15, 2021
8f2c762
add javadocs
ncitron Jun 15, 2021
e762696
add getQuote function
ncitron Jun 15, 2021
613d507
clean up ratio calculation
ncitron Jun 15, 2021
70dc237
remove hardhat console
ncitron Jun 15, 2021
aea65ac
add TradeSplitterExchangeAdapter
ncitron Jun 15, 2021
7b6ae5a
fix typo causing tests to break
ncitron Jun 15, 2021
443caea
add TradeSplitterIndexExchangeAdapter
ncitron Jun 15, 2021
d30e838
improve coverage
ncitron Jun 15, 2021
2c6c425
increase coverage
ncitron Jun 15, 2021
1eaae6f
refactor
ncitron Jun 16, 2021
3331e08
remove redundant if in getQuote
ncitron Jun 16, 2021
cca52e3
improve reverts
ncitron Jun 16, 2021
be37f79
improve coverage
ncitron Jun 16, 2021
3743225
move TradeSplitter into products
ncitron Jun 16, 2021
82da6c9
add TradeSplitterExchangeAdapter integration test
ncitron Jun 16, 2021
012ed03
split getQuote into two functions for exact input/output
ncitron Jun 16, 2021
07b037d
add TradeSplitter GIM integration tests
ncitron Jun 17, 2021
dbb1638
Merge branch 'master' into ncitron/trade-splitter
ncitron Jun 17, 2021
c0788a4
fix tests
ncitron Jun 17, 2021
d0962c9
Merge branch 'ncitron/trade-splitter' of github.com:SetProtocol/set-p…
ncitron Jun 17, 2021
3586385
make TradeSplitter adhere to the UniswapV2Router interface
ncitron Jun 17, 2021
e31a6a0
use uniswap v2 adapters for trade splitter
ncitron Jun 17, 2021
cc92517
cleanup
ncitron Jun 22, 2021
977f3c5
adhere to uniswap interface for getting quotes
ncitron Jun 22, 2021
ac29226
change name of contracts
ncitron Jun 22, 2021
6d6220f
improve AMMSplitter tests
ncitron Jun 22, 2021
f91dc43
refactoring
ncitron Jun 23, 2021
7a9ce15
fix bug with intermediary tokens with less than 18 decimals
ncitron Jun 23, 2021
2ca3991
refactor
ncitron Jun 23, 2021
3e7f294
add extra tests
ncitron Jun 23, 2021
a775af0
update revert messages
ncitron Jun 23, 2021
5293847
improve comments
ncitron Jun 24, 2021
edc4d51
add events
ncitron Jun 24, 2021
dbeb0e1
use input and output tokens instead of path for events
ncitron Jun 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions contracts/interfaces/external/IUniswapV2Factory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pragma solidity 0.6.10;

interface IUniswapV2Factory {
event PairCreated(address indexed token0, address indexed token1, address pair, uint);

function getPair(address tokenA, address tokenB) external view returns (address pair);
function allPairs(uint) external view returns (address pair);
function allPairsLength() external view returns (uint);

function feeTo() external view returns (address);
function feeToSetter() external view returns (address);

function createPair(address tokenA, address tokenB) external returns (address pair);
}
440 changes: 440 additions & 0 deletions contracts/product/AMMSplitter.sol

Large diffs are not rendered by default.

293 changes: 293 additions & 0 deletions test/integration/ammSplitterGeneralIndexModule.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
import "module-alias/register";
import { BigNumber } from "@ethersproject/bignumber";

import { Address } from "@utils/types";
import { Account } from "@utils/test/types";
import { ADDRESS_ZERO, MAX_UINT_256, ZERO } from "@utils/constants";
import {
GeneralIndexModule,
SetToken,
AMMSplitter,
UniswapV2IndexExchangeAdapter
} from "@utils/contracts";
import DeployHelper from "@utils/deploys";
import {
bitcoin,
ether,
preciseDiv,
preciseMul,
} from "@utils/index";
import {
cacheBeforeEach,
getAccounts,
getSystemFixture,
getUniswapFixture,
getWaffleExpect
} from "@utils/test/index";
import { SystemFixture, UniswapFixture } from "@utils/fixtures";
import { ContractTransaction } from "ethers";

const expect = getWaffleExpect();

describe("AMMSplitterGeneralIndexModule", () => {
let owner: Account;
let trader: Account;
let positionModule: Account;
let deployer: DeployHelper;
let setup: SystemFixture;

let uniswapSetup: UniswapFixture;
let sushiswapSetup: UniswapFixture;
let tradeSplitter: AMMSplitter;

let index: SetToken;
let indexModule: GeneralIndexModule;

let tradeSplitterAdapter: UniswapV2IndexExchangeAdapter;
let tradeSplitterAdapterName: string;

let indexComponents: Address[];
let indexUnits: BigNumber[];

before(async () => {
[
owner,
trader,
positionModule,
] = await getAccounts();

deployer = new DeployHelper(owner.wallet);
setup = getSystemFixture(owner.address);
uniswapSetup = getUniswapFixture(owner.address);
sushiswapSetup = getUniswapFixture(owner.address);

await setup.initialize();
await uniswapSetup.initialize(owner, setup.weth.address, setup.wbtc.address, setup.dai.address);
await sushiswapSetup.initialize(owner, setup.weth.address, setup.wbtc.address, setup.dai.address);

indexModule = await deployer.modules.deployGeneralIndexModule(
setup.controller.address,
setup.weth.address
);
await setup.controller.addModule(indexModule.address);
await setup.controller.addModule(positionModule.address);


tradeSplitter = await deployer.product.deployAMMSplitter(uniswapSetup.router.address, sushiswapSetup.router.address);
tradeSplitterAdapter = await deployer.adapters.deployUniswapV2IndexExchangeAdapter(tradeSplitter.address);
tradeSplitterAdapterName = "TRADESPLITTER";


await setup.integrationRegistry.batchAddIntegration(
[ indexModule.address ],
[ tradeSplitterAdapterName ],
[ tradeSplitterAdapter.address ]
);
});

cacheBeforeEach(async () => {
indexComponents = [setup.wbtc.address, setup.dai.address];
indexUnits = [ bitcoin(0.01), ether(4000) ];

index = await setup.createSetToken(
indexComponents,
indexUnits,
[setup.issuanceModule.address, indexModule.address, positionModule.address],
);

await setup.issuanceModule.initialize(index.address, ADDRESS_ZERO);
await index.connect(positionModule.wallet).initializeModule();

await setup.weth.connect(owner.wallet).approve(sushiswapSetup.router.address, ether(2000));
await setup.dai.connect(owner.wallet).approve(sushiswapSetup.router.address, ether(400000));
await sushiswapSetup.router.connect(owner.wallet).addLiquidity(
setup.weth.address,
setup.dai.address,
ether(200),
ether(400000),
ether(148),
ether(173000),
owner.address,
MAX_UINT_256
);

await setup.weth.connect(owner.wallet).approve(uniswapSetup.router.address, ether(1000));
await setup.wbtc.connect(owner.wallet).approve(uniswapSetup.router.address, ether(26));
await uniswapSetup.router.addLiquidity(
setup.weth.address,
setup.wbtc.address,
ether(1000),
bitcoin(25.5555),
ether(999),
ether(25.3),
owner.address,
MAX_UINT_256
);
});

describe("when module is initalized", async () => {
let subjectSetToken: SetToken;
let subjectCaller: Account;

let newComponents: Address[];
let newTargetUnits: BigNumber[];
let oldTargetUnits: BigNumber[];
let issueAmount: BigNumber;

async function initSetToken(
setToken: SetToken, components: Address[], tradeMaximums: BigNumber[], exchanges: string[], coolOffPeriods: BigNumber[]
) {
await indexModule.initialize(setToken.address);
await indexModule.setTradeMaximums(setToken.address, components, tradeMaximums);
await indexModule.setExchanges(setToken.address, components, exchanges);
await indexModule.setCoolOffPeriods(setToken.address, components, coolOffPeriods);
await indexModule.setTraderStatus(setToken.address, [trader.address], [true]);
}

const startRebalance = async () => {
await setup.approveAndIssueSetToken(subjectSetToken, issueAmount);
await indexModule.startRebalance(
subjectSetToken.address,
newComponents,
newTargetUnits,
oldTargetUnits,
await index.positionMultiplier()
);
};

before(async () => {
newComponents = [];
oldTargetUnits = [bitcoin(0.1), ether(1)];
newTargetUnits = [];
issueAmount = ether("20.000000000000000001");
});

cacheBeforeEach(async () => {
await initSetToken(
index,
[setup.wbtc.address, setup.dai.address],
[bitcoin(1000), ether(100000)],
[tradeSplitterAdapterName, tradeSplitterAdapterName],
[ZERO, ZERO]
);
});

describe("#trade", async () => {
let subjectComponent: Address;
let subjectEthQuantityLimit: BigNumber;

let expectedOut: BigNumber;

const initializeSubjectVariables = () => {
subjectSetToken = index;
subjectCaller = trader;
subjectComponent = setup.dai.address;
subjectEthQuantityLimit = ZERO;
};

async function subject(): Promise<ContractTransaction> {
return await indexModule.connect(subjectCaller.wallet).trade(
subjectSetToken.address,
subjectComponent,
subjectEthQuantityLimit
);
}

describe("with default target units", async () => {
beforeEach(async () => {
initializeSubjectVariables();

expectedOut = (await tradeSplitter.getAmountsOut(
preciseMul(ether(3999), issueAmount),
[ setup.dai.address, setup.weth.address ]
))[1];

subjectEthQuantityLimit = BigNumber.from(0);
});
cacheBeforeEach(startRebalance);

it("should sell using TradeSplitter", async () => {
const currentWethAmount = await setup.weth.balanceOf(subjectSetToken.address);
const totalSupply = await subjectSetToken.totalSupply();

await subject();

const expectedWethPositionUnits = preciseDiv(currentWethAmount.add(expectedOut), totalSupply);
const expectedDaiPositionUnits = ether(1);

const wethPositionUnits = await subjectSetToken.getDefaultPositionRealUnit(setup.weth.address);
const daiPositionUnits = await subjectSetToken.getDefaultPositionRealUnit(setup.dai.address);

expect(wethPositionUnits).to.eq(expectedWethPositionUnits);
expect(daiPositionUnits).to.eq(expectedDaiPositionUnits);
});
});
});

describe("#tradeRemainingWETH", async () => {

let subjectComponent: Address;
let subjectEthQuantityLimit: BigNumber;

let expectedOut: BigNumber;

const initializeSubjectVariables = () => {
subjectSetToken = index;
subjectCaller = trader;
subjectComponent = setup.dai.address;
subjectEthQuantityLimit = ZERO;
};

async function subject(): Promise<ContractTransaction> {
return await indexModule.connect(subjectCaller.wallet).tradeRemainingWETH(
subjectSetToken.address,
subjectComponent,
subjectEthQuantityLimit
);
}

describe("with default target units", async () => {

beforeEach(async () => {
initializeSubjectVariables();
});
cacheBeforeEach(startRebalance);

context("when it is the last trade", async () => {

beforeEach(async () => {
await indexModule.connect(subjectCaller.wallet).trade(
subjectSetToken.address,
subjectComponent,
subjectEthQuantityLimit
);

expectedOut = (await tradeSplitter.getAmountsOut(
await setup.weth.balanceOf(subjectSetToken.address),
[ setup.weth.address, setup.wbtc.address ]
))[1];

subjectComponent = setup.wbtc.address;
subjectEthQuantityLimit = ZERO;
});

it("should buy using TradeSplitter", async () => {
const currentWbtcAmount = await setup.wbtc.balanceOf(subjectSetToken.address);
const totalSupply = await subjectSetToken.totalSupply();

await subject();

const expectedWbtcPositionUnits = preciseDiv(currentWbtcAmount.add(expectedOut), totalSupply);
const expectedWethPositionUnits = ether(0);

const wethPositionUnits = await subjectSetToken.getDefaultPositionRealUnit(setup.weth.address);
const wbtcPositionUnits = await subjectSetToken.getDefaultPositionRealUnit(setup.wbtc.address);

expect(wbtcPositionUnits).to.eq(expectedWbtcPositionUnits);
expect(wethPositionUnits).to.eq(expectedWethPositionUnits);
});
});
});
});
});
});
Loading