-
Notifications
You must be signed in to change notification settings - Fork 24
Added SushiBarWrapAdapter #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 ============ */ | ||
|
||
|
||
// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Different error messages would be better. |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* | ||
* @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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
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"); | ||
}); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can remove extra line