Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
107 changes: 107 additions & 0 deletions contracts/protocol/integration/UniswapV2TransferFeeExchangeAdapter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
Copyright 2021 Set Labs Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

SPDX-License-Identifier: Apache License, Version 2.0
*/

pragma solidity 0.6.10;
pragma experimental "ABIEncoderV2";

/**
* @title UniswapV2TransferFeeExchangeAdapter
* @author Set Protocol
*
* Exchange adapter for Uniswap V2 Router02 that supports trading tokens with transfer fees
*/
contract UniswapV2TransferFeeExchangeAdapter {

/* ============ State Variables ============ */

// Address of Uniswap V2 Router02 contract
address public immutable router;

/* ============ Constructor ============ */

/**
* Set state variables
*
* @param _router Address of Uniswap V2 Router02 contract
*/
constructor(address _router) public {
router = _router;
}

/* ============ External Getter Functions ============ */

/**
* Return calldata for Uniswap V2 Router02 when trading a token with a transfer fee
*
* @param _sourceToken Address of source token to be sold
* @param _destinationToken Address of destination token to buy
* @param _destinationAddress Address that assets should be transferred to
* @param _sourceQuantity Amount of source token to sell
* @param _minDestinationQuantity Min amount of destination token to buy
* @param _data Arbitrary bytes containing trade call data
*
* @return address Target contract address
* @return uint256 Call value
* @return bytes Trade calldata
*/
function getTradeCalldata(
address _sourceToken,
address _destinationToken,
address _destinationAddress,
uint256 _sourceQuantity,
uint256 _minDestinationQuantity,
bytes memory _data
)
external
view
returns (address, uint256, bytes memory)
{
address[] memory path;

if(_data.length == 0){
path = new address[](2);
path[0] = _sourceToken;
path[1] = _destinationToken;
} else {
path = abi.decode(_data, (address[]));
}

bytes memory callData = abi.encodeWithSignature(
"swapExactTokensForTokensSupportingFeeOnTransferTokens(uint256,uint256,address[],address,uint256)",
_sourceQuantity,
_minDestinationQuantity,
path,
_destinationAddress,
block.timestamp
);
return (router, 0, callData);
}

/**
* Returns the address to approve source tokens to for trading. This is the Uniswap router address
*
* @return address Address of the contract to approve tokens to
*/
function getSpender()
external
view
returns (address)
{
return router;
}
}
167 changes: 167 additions & 0 deletions test/protocol/integration/uniswapV2TransferFeeExchangeAdapter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import "module-alias/register";

import { BigNumber } from "@ethersproject/bignumber";
import { defaultAbiCoder } from "ethers/lib/utils";

import { Address, Bytes } from "@utils/types";
import { Account } from "@utils/test/types";
import {
EMPTY_BYTES,
ZERO,
} from "@utils/constants";
import { UniswapV2TransferFeeExchangeAdapter } from "@utils/contracts";
import DeployHelper from "@utils/deploys";
import {
ether,
} from "@utils/index";
import {
addSnapshotBeforeRestoreAfterEach,
getAccounts,
getSystemFixture,
getUniswapFixture,
getWaffleExpect,
getLastBlockTimestamp
} from "@utils/test/index";

import { SystemFixture, UniswapFixture } from "@utils/fixtures";

const expect = getWaffleExpect();

describe("UniswapV2TransferFeeExchangeAdapter", () => {
let owner: Account;
let mockSetToken: Account;
let deployer: DeployHelper;
let setup: SystemFixture;
let uniswapSetup: UniswapFixture;

let uniswapV2TransferFeeExchangeAdapter: UniswapV2TransferFeeExchangeAdapter;

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

deployer = new DeployHelper(owner.wallet);
setup = getSystemFixture(owner.address);
await setup.initialize();

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

uniswapV2TransferFeeExchangeAdapter = await deployer.adapters.deployUniswapV2TransferFeeExchangeAdapter(uniswapSetup.router.address);
});

addSnapshotBeforeRestoreAfterEach();

describe("constructor", async () => {
let subjectUniswapRouter: Address;

beforeEach(async () => {
subjectUniswapRouter = uniswapSetup.router.address;
});

async function subject(): Promise<any> {
return await deployer.adapters.deployUniswapV2TransferFeeExchangeAdapter(subjectUniswapRouter);
}

it("should have the correct router address", async () => {
const deployedUniswapV2TransferFeeExchangeAdapter = await subject();

const actualRouterAddress = await deployedUniswapV2TransferFeeExchangeAdapter.router();
expect(actualRouterAddress).to.eq(uniswapSetup.router.address);
});
});

describe("getSpender", async () => {
async function subject(): Promise<any> {
return await uniswapV2TransferFeeExchangeAdapter.getSpender();
}

it("should return the correct spender address", async () => {
const spender = await subject();

expect(spender).to.eq(uniswapSetup.router.address);
});
});

describe("getTradeCalldata", async () => {
let sourceAddress: Address;
let destinationAddress: Address;
let sourceQuantity: BigNumber;
let destinationQuantity: BigNumber;

let subjectMockSetToken: Address;
let subjectSourceToken: Address;
let subjectDestinationToken: Address;
let subjectSourceQuantity: BigNumber;
let subjectMinDestinationQuantity: BigNumber;
let subjectData: Bytes;

beforeEach(async () => {
sourceAddress = setup.wbtc.address; // WBTC Address
sourceQuantity = BigNumber.from(100000000); // Trade 1 WBTC
destinationAddress = setup.dai.address; // DAI Address
destinationQuantity = ether(30000); // Receive at least 30k DAI

subjectSourceToken = sourceAddress;
subjectDestinationToken = destinationAddress;
subjectMockSetToken = mockSetToken.address;
subjectSourceQuantity = sourceQuantity;
subjectMinDestinationQuantity = destinationQuantity;
subjectData = EMPTY_BYTES;
});

async function subject(): Promise<any> {
return await uniswapV2TransferFeeExchangeAdapter.getTradeCalldata(
subjectSourceToken,
subjectDestinationToken,
subjectMockSetToken,
subjectSourceQuantity,
subjectMinDestinationQuantity,
subjectData,
);
}

it("should return the correct trade calldata", async () => {
const calldata = await subject();
const callTimestamp = await getLastBlockTimestamp();
const expectedCallData = uniswapSetup.router.interface.encodeFunctionData("swapExactTokensForTokensSupportingFeeOnTransferTokens", [
sourceQuantity,
destinationQuantity,
[sourceAddress, destinationAddress],
subjectMockSetToken,
callTimestamp,
]);
expect(JSON.stringify(calldata)).to.eq(JSON.stringify([uniswapSetup.router.address, ZERO, expectedCallData]));
});

describe("when passed in custom path to trade data", async () => {
beforeEach(async () => {
const path = [sourceAddress, setup.weth.address, destinationAddress];
subjectData = defaultAbiCoder.encode(
["address[]"],
[path]
);
});

it("should return the correct trade calldata", async () => {
const calldata = await subject();
const callTimestamp = await getLastBlockTimestamp();
const expectedCallData = uniswapSetup.router.interface.encodeFunctionData("swapExactTokensForTokensSupportingFeeOnTransferTokens", [
sourceQuantity,
destinationQuantity,
[sourceAddress, setup.weth.address, destinationAddress],
subjectMockSetToken,
callTimestamp,
]);
expect(JSON.stringify(calldata)).to.eq(JSON.stringify([uniswapSetup.router.address, ZERO, expectedCallData]));
});
});
});
});
Loading