Skip to content
This repository was archived by the owner on Apr 1, 2021. It is now read-only.

Added SushiBarWrapAdapter #4

Closed
Closed
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
109 changes: 109 additions & 0 deletions contracts/protocol/integration/SushiBarWrapAdapter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity 0.6.10;

/**
* @title SushiBarWrapAdapter
* @author Yam Finance
*
* Wrap adapter for depositing/withdrawing sushi to/from SushiBar (xSushi)
*/
contract SushiBarWrapAdapter {

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove extra line


// Address of SUSHI token
address public immutable sushiToken;

// Address of xSUSHI token
address public immutable xsushiToken;

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

/**
* State variables
*
* @param _sushiToken Address of SUSHI token
* @param _xsushiToken Address of XSUSHI token (also the SushiBar)
*/
constructor(
address _sushiToken,
address _xsushiToken
)
public
{
sushiToken = _sushiToken;
xsushiToken = _xsushiToken;
}

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

/**
* Generates the calldata to wrap sushi into xsushi.
*
* @param _underlyingToken Address of the component to be wrapped
* @param _wrappedToken Address of the wrapped component
* @param _underlyingUnits Total quantity of underlying units to wrap
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can maybe make note that this can only be SUSHI, same for wrappedToken and xSUSHI

*
* @return address Target contract address
* @return uint256 Unused, always 0
* @return bytes Wrap calldata
*/
function getWrapCallData(
address _underlyingToken,
address _wrappedToken,
uint256 _underlyingUnits
)
external
view
returns (address, uint256, bytes memory)
{
require(_underlyingToken == sushiToken, "Must be a valid token pair");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Different error messages would be better. Underlying token must be SUSHI or something to that effect

require(_wrappedToken == xsushiToken, "Must be a valid token pair");

// enter(uint256 _amount)
bytes memory callData = abi.encodeWithSignature("enter(uint256)", _underlyingUnits);

return (xsushiToken, 0, callData);
}

/**
* Generates the calldata to unwrap xsushi to sushi
*
* @param _underlyingToken Address of the component to be unwrapped to
* @param _wrappedToken Address of the wrapped component
* @param _wrappedTokenUnits Total quantity of wrapped units to wrap
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Total quantity of wrapped units to unwrap - can maybe make a note that this is only xSUSHI

*
* @return address Target contract address
* @return uint256 Unused, always 0
* @return bytes Unwrap calldata
*/
function getUnwrapCallData(
address _underlyingToken,
address _wrappedToken,
uint256 _wrappedTokenUnits
)
external
view
returns (address, uint256, bytes memory)
{
require(_underlyingToken == sushiToken, "Must be a valid token pair");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get different error messages for these so it's apparent which one is wrong

require(_wrappedToken == xsushiToken, "Must be a valid token pair");

// leave(uint256 _amount)
bytes memory callData = abi.encodeWithSignature("leave(uint256)", _wrappedTokenUnits);

return (xsushiToken, 0, callData);

}

/**
* Returns the address to approve source tokens for wrapping.
*
* @return address Address of the contract to approve tokens to. This is the SushiBar (xSushi) contract.
*/
function getSpenderAddress(address /*_underlyingToken*/, address /*_wrappedToken*/) external view returns(address) {
return address(xsushiToken);
}
}
173 changes: 173 additions & 0 deletions test/protocol/integration/sushiWrapAdapter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import "module-alias/register";
import { BigNumber } from "@ethersproject/bignumber";

import { Address, Account } from "@utils/types";
import { ZERO } from "@utils/constants";
import DeployHelper from "@utils/deploys";
import {
addSnapshotBeforeRestoreAfterEach,
ether,
getAccounts,
getWaffleExpect,
bigNumberToData
} from "@utils/index";
import { SushiBarWrapAdapter } from "@utils/contracts";

const expect = getWaffleExpect();

describe("SushiWrapAdapter", () => {
let owner: Account;
let deployer: DeployHelper;
let sushiWrapAdapter: SushiBarWrapAdapter;
let underlyingToken: Account;
let wrappedToken: Account;
let ethWrappedToken: Account;
let otherUnderlyingToken: Account;

before(async () => {
[
owner,
underlyingToken,
wrappedToken,
ethWrappedToken,
otherUnderlyingToken,
] = await getAccounts();

deployer = new DeployHelper(owner.wallet);
sushiWrapAdapter = await deployer.adapters.deploySushiBarWrapAdapter(underlyingToken.address, wrappedToken.address);
});

addSnapshotBeforeRestoreAfterEach();

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

async function subject(): Promise<any> {
return deployer.adapters.deploySushiBarWrapAdapter(underlyingToken.address, wrappedToken.address);
}

it("should have the correct sushi and sushiBar addresses", async () => {
const deployedSushiBarWrapAdapter = await subject();

const actualSushi = await deployedSushiBarWrapAdapter.sushiToken();
const actualSushiBar = await deployedSushiBarWrapAdapter.xsushiToken();
expect(actualSushi).to.eq(underlyingToken.address);
expect(actualSushiBar).to.eq(wrappedToken.address);
});
});

describe("#getSpenderAddress", async () => {
async function subject(): Promise<any> {
return sushiWrapAdapter.getSpenderAddress(underlyingToken.address, wrappedToken.address);
}

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

expect(spender).to.eq(wrappedToken.address);
});
});

describe("#getWrapCallData", async () => {
let subjectUnderlyingToken: Address;
let subjectWrappedToken: Address;
let subjectUnderlyingUnits: BigNumber;
const depositSignature = "0xa59f3e0c"; // enter(uint256)
const generateCallData = (token: Address, units: BigNumber) =>
depositSignature +
bigNumberToData(units);

beforeEach(async () => {
subjectUnderlyingToken = underlyingToken.address;
subjectWrappedToken = wrappedToken.address;
subjectUnderlyingUnits = ether(2);
});

async function subject(): Promise<any> {
return sushiWrapAdapter.getWrapCallData(subjectUnderlyingToken, subjectWrappedToken, subjectUnderlyingUnits);
}

it("should return correct data for valid pair", async () => {
const [targetAddress, ethValue, callData] = await subject();

const expectedCallData = generateCallData(subjectUnderlyingToken, subjectUnderlyingUnits);

expect(targetAddress).to.eq(wrappedToken.address);
expect(ethValue).to.eq(ZERO);
expect(callData).to.eq(expectedCallData);
});



describe("when invalid underlying token", () => {
beforeEach(async () => {
subjectUnderlyingToken = otherUnderlyingToken.address;
subjectWrappedToken = wrappedToken.address;
});

it("should revert", async () => {
await expect(subject()).to.be.revertedWith("Must be a valid token pair");
});
});

describe("when invalid wrapped token", () => {
beforeEach(async () => {
subjectUnderlyingToken = underlyingToken.address;
subjectWrappedToken = ethWrappedToken.address;
});

it("should revert", async () => {
await expect(subject()).to.be.revertedWith("Must be a valid token pair");
});
});
});

describe("#getUnwrapCallData", async () => {
let subjectUnderlyingToken: Address;
let subjectWrappedToken: Address;
let subjectWrappedTokenUnits: BigNumber;
const redeemSignature = "0x67dfd4c9"; // leave(uint256)
const generateCallData = (units: BigNumber) => redeemSignature + bigNumberToData(units);

beforeEach(async () => {
subjectUnderlyingToken = underlyingToken.address;
subjectWrappedToken = wrappedToken.address;
subjectWrappedTokenUnits = ether(2);
});

async function subject(): Promise<any> {
return sushiWrapAdapter.getUnwrapCallData(subjectUnderlyingToken, subjectWrappedToken, subjectWrappedTokenUnits);
}

it("should return correct data for valid pair", async () => {
const [targetAddress, ethValue, callData] = await subject();

const expectedCallData = generateCallData(subjectWrappedTokenUnits);

expect(targetAddress).to.eq(subjectWrappedToken);
expect(ethValue).to.eq(ZERO);
expect(callData).to.eq(expectedCallData);
});

describe("when invalid underlying token", () => {
beforeEach(async () => {
subjectUnderlyingToken = otherUnderlyingToken.address;
subjectWrappedToken = wrappedToken.address;
});

it("should revert", async () => {
await expect(subject()).to.be.revertedWith("Must be a valid token pair");
});
});

describe("when invalid wrapped token", () => {
beforeEach(async () => {
subjectUnderlyingToken = underlyingToken.address;
subjectWrappedToken = ethWrappedToken.address;
});

it("should revert", async () => {
await expect(subject()).to.be.revertedWith("Must be a valid token pair");
});
});
});
});
1 change: 1 addition & 0 deletions utils/contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ export { UniswapYieldStrategy } from "../../typechain/UniswapYieldStrategy";
export { WETH9 } from "../../typechain/WETH9";
export { WrapAdapterMock } from "../../typechain/WrapAdapterMock";
export { WrapModule } from "../../typechain/WrapModule";
export { SushiBarWrapAdapter } from "../../typechain/SushiBarWrapAdapter";
12 changes: 11 additions & 1 deletion utils/deploys/deployAdapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
OneInchExchangeAdapter,
AaveMigrationWrapAdapter,
AaveWrapAdapter,
UniswapPairPriceAdapter
UniswapPairPriceAdapter,
SushiBarWrapAdapter
} from "../contracts";

import { Address, Bytes } from "./../types";
Expand All @@ -21,6 +22,7 @@ import { OneInchExchangeAdapter__factory } from "../../typechain/factories/OneIn
import { AaveMigrationWrapAdapter__factory } from "../../typechain/factories/AaveMigrationWrapAdapter__factory";
import { AaveWrapAdapter__factory } from "../../typechain/factories/AaveWrapAdapter__factory";
import { UniswapPairPriceAdapter__factory } from "../../typechain/factories/UniswapPairPriceAdapter__factory";
import { SushiBarWrapAdapter__factory } from "../../typechain/factories/SushiBarWrapAdapter__factory";

export default class DeployAdapters {
private _deployerSigner: Signer;
Expand Down Expand Up @@ -77,7 +79,15 @@ export default class DeployAdapters {
return await new UniswapPairPriceAdapter__factory(this._deployerSigner).deploy(controller, uniswapFactory, uniswapPools);
}

public async deploySushiBarWrapAdapter(
sushi: Address,
sushiBar: Address
): Promise<SushiBarWrapAdapter> {
return await new SushiBarWrapAdapter__factory(this._deployerSigner).deploy(sushi, sushiBar);
}

public async getUniswapPairPriceAdapter(uniswapAdapterAddress: Address): Promise<UniswapPairPriceAdapter> {
return await new UniswapPairPriceAdapter__factory(this._deployerSigner).attach(uniswapAdapterAddress);
}

}