-
Notifications
You must be signed in to change notification settings - Fork 106
Add kyber index exchange adapter #80
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
contracts/protocol/integration/index-exchange/KyberIndexExchangeAdapter.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
Copyright 2020 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 KyberIndexExchangeAdapter | ||
* @author Set Protocol | ||
* | ||
* A Kyber exchange adapter that returns calldata for trading with GeneralIndexModule, | ||
* allows trading a fixed input amount or for a fixed output amount. | ||
*/ | ||
|
||
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol"; | ||
|
||
import { IIndexExchangeAdapter } from "../../../interfaces/IIndexExchangeAdapter.sol"; | ||
import { IKyberNetworkProxy } from "../../../interfaces/external/IKyberNetworkProxy.sol"; | ||
import { PreciseUnitMath } from "../../../lib/PreciseUnitMath.sol"; | ||
|
||
contract KyberIndexExchangeAdapter is IIndexExchangeAdapter { | ||
using SafeMath for uint256; | ||
using PreciseUnitMath for uint256; | ||
|
||
/* ============ Structs ============ */ | ||
|
||
/** | ||
* Struct containing information for trade function | ||
*/ | ||
struct KyberTradeInfo { | ||
uint256 sourceTokenDecimals; // Decimals of the token to send | ||
uint256 destinationTokenDecimals; // Decimals of the token to receive | ||
uint256 conversionRate; // Derived conversion rate from min receive quantity | ||
} | ||
|
||
/* ============ State Variables ============ */ | ||
|
||
// Address of Kyber Network Proxy | ||
address public kyberNetworkProxyAddress; | ||
|
||
/* ============ Constructor ============ */ | ||
|
||
/** | ||
* Set state variables | ||
* | ||
* @param _kyberNetworkProxyAddress Address of Kyber Network Proxy contract | ||
*/ | ||
constructor( | ||
address _kyberNetworkProxyAddress | ||
) | ||
public | ||
{ | ||
kyberNetworkProxyAddress = _kyberNetworkProxyAddress; | ||
} | ||
|
||
/* ============ External Getter Functions ============ */ | ||
|
||
/** | ||
* Calculate Kyber trade encoded calldata. To be invoked on the SetToken. | ||
* | ||
* @param _sourceToken Address of source token to be sold | ||
* @param _destinationToken Address of destination token to buy | ||
* @param _destinationAddress Address that output assets should be transferred to | ||
* @param _sourceQuantity Fixed/Max amount of source token to sell | ||
* @param _destinationQuantity Min/Fixed amount of destination tokens to receive | ||
* | ||
* @return address Target contract address | ||
* @return uint256 Call value | ||
* @return bytes Trade calldata | ||
*/ | ||
function getTradeCalldata( | ||
address _sourceToken, | ||
address _destinationToken, | ||
address _destinationAddress, | ||
bool /*_isSendTokenFixed*/, | ||
uint256 _sourceQuantity, | ||
uint256 _destinationQuantity, | ||
bytes memory /*_data*/ | ||
) | ||
override | ||
external | ||
view | ||
returns (address, uint256, bytes memory) | ||
{ | ||
KyberTradeInfo memory kyberTradeInfo; | ||
|
||
kyberTradeInfo.sourceTokenDecimals = ERC20(_sourceToken).decimals(); | ||
kyberTradeInfo.destinationTokenDecimals = ERC20(_destinationToken).decimals(); | ||
|
||
// Get conversion rate from minimum receive token quantity. | ||
0xSachinK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// dstQty * (10 ** 18) * (10 ** dstDecimals) / (10 ** srcDecimals) / srcQty | ||
kyberTradeInfo.conversionRate = _destinationQuantity | ||
.mul(PreciseUnitMath.preciseUnit()) | ||
.mul(10 ** kyberTradeInfo.sourceTokenDecimals) | ||
.div(10 ** kyberTradeInfo.destinationTokenDecimals) | ||
.div(_sourceQuantity); | ||
|
||
// Encode method data for SetToken to invoke | ||
bytes memory methodData = abi.encodeWithSignature( | ||
"trade(address,uint256,address,address,uint256,uint256,address)", | ||
_sourceToken, | ||
_sourceQuantity, | ||
_destinationToken, | ||
_destinationAddress, | ||
PreciseUnitMath.maxUint256(), // Sell entire amount of sourceToken | ||
0xSachinK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
kyberTradeInfo.conversionRate, // Trade with implied conversion rate | ||
address(0) // No referrer address | ||
); | ||
|
||
return (kyberNetworkProxyAddress, 0, methodData); | ||
} | ||
|
||
/** | ||
* Returns the address to approve source tokens to for trading. This is the Kyber Network | ||
* Proxy address | ||
* | ||
* @return address Address of the contract to approve tokens to | ||
*/ | ||
function getSpender() | ||
override | ||
external | ||
view | ||
returns (address) | ||
{ | ||
return kyberNetworkProxyAddress; | ||
} | ||
} |
150 changes: 150 additions & 0 deletions
150
test/protocol/integration/index-exchange/kyberIndexExchangeAdapter.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import "module-alias/register"; | ||
|
||
import { ethers } from "hardhat"; | ||
import { BigNumber } from "@ethersproject/bignumber"; | ||
|
||
import { Address, Bytes } from "@utils/types"; | ||
import { Account } from "@utils/test/types"; | ||
import { | ||
ADDRESS_ZERO, | ||
EMPTY_BYTES, | ||
ZERO, | ||
} from "@utils/constants"; | ||
import { KyberIndexExchangeAdapter, KyberNetworkProxyMock } from "@utils/contracts"; | ||
import DeployHelper from "@utils/deploys"; | ||
import { | ||
ether, | ||
} from "@utils/index"; | ||
import { | ||
addSnapshotBeforeRestoreAfterEach, | ||
getAccounts, | ||
getSystemFixture, | ||
getWaffleExpect, | ||
} from "@utils/test/index"; | ||
|
||
import { SystemFixture } from "@utils/fixtures"; | ||
|
||
const expect = getWaffleExpect(); | ||
|
||
|
||
describe("KyberIndexExchangeAdapter", () => { | ||
let owner: Account; | ||
let mockSetToken: Account; | ||
let deployer: DeployHelper; | ||
let setup: SystemFixture; | ||
|
||
let kyberNetworkProxy: KyberNetworkProxyMock; | ||
let wbtcRate: BigNumber; | ||
let kyberIndexExchangeAdapter: KyberIndexExchangeAdapter; | ||
|
||
before(async () => { | ||
[ | ||
owner, | ||
mockSetToken, | ||
] = await getAccounts(); | ||
|
||
deployer = new DeployHelper(owner.wallet); | ||
setup = getSystemFixture(owner.address); | ||
await setup.initialize(); | ||
|
||
wbtcRate = ether(33); // 1 WBTC = 33 ETH | ||
|
||
// Mock Kyber reserve only allows trading from/to WETH | ||
kyberNetworkProxy = await deployer.mocks.deployKyberNetworkProxyMock(setup.weth.address); | ||
await kyberNetworkProxy.addToken( | ||
setup.wbtc.address, | ||
wbtcRate, | ||
8 | ||
); | ||
|
||
kyberIndexExchangeAdapter = await deployer.adapters.deployKyberIndexExchangeAdapter(kyberNetworkProxy.address); | ||
}); | ||
|
||
addSnapshotBeforeRestoreAfterEach(); | ||
|
||
describe("constructor", async () => { | ||
let subjectKyberNetworkProxy: Address; | ||
|
||
beforeEach(async () => { | ||
subjectKyberNetworkProxy = kyberNetworkProxy.address; | ||
}); | ||
|
||
async function subject(): Promise<any> { | ||
return await deployer.adapters.deployKyberIndexExchangeAdapter(subjectKyberNetworkProxy); | ||
} | ||
|
||
it("should have the correct Kyber proxy address", async () => { | ||
const deployedKyberIndexExchangeAdapter = await subject(); | ||
|
||
const actualKyberAddress = await deployedKyberIndexExchangeAdapter.kyberNetworkProxyAddress(); | ||
expect(actualKyberAddress).to.eq(kyberNetworkProxy.address); | ||
}); | ||
}); | ||
|
||
|
||
describe("getSpender", async () => { | ||
async function subject(): Promise<any> { | ||
return await kyberIndexExchangeAdapter.getSpender(); | ||
} | ||
|
||
it("should return the correct spender address", async () => { | ||
const spender = await subject(); | ||
|
||
expect(spender).to.eq(kyberNetworkProxy.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 subjectDestinationQuantity: BigNumber; | ||
let subjectData: Bytes; | ||
|
||
beforeEach(async () => { | ||
sourceAddress = setup.wbtc.address; // WBTC Address | ||
sourceQuantity = BigNumber.from(100000000); // Trade 1 WBTC | ||
destinationAddress = setup.weth.address; // WETH Address | ||
destinationQuantity = ether(33); // Receive at least 33 ETH | ||
|
||
subjectSourceToken = sourceAddress; | ||
subjectDestinationToken = destinationAddress; | ||
subjectMockSetToken = mockSetToken.address; | ||
subjectSourceQuantity = sourceQuantity; | ||
subjectDestinationQuantity = destinationQuantity; | ||
subjectData = EMPTY_BYTES; | ||
}); | ||
|
||
async function subject(): Promise<any> { | ||
return await kyberIndexExchangeAdapter.getTradeCalldata( | ||
subjectSourceToken, | ||
subjectDestinationToken, | ||
subjectMockSetToken, | ||
true, | ||
subjectSourceQuantity, | ||
subjectDestinationQuantity, | ||
subjectData, | ||
); | ||
} | ||
|
||
it("should return the correct trade calldata", async () => { | ||
const calldata = await subject(); | ||
const expectedCallData = kyberNetworkProxy.interface.encodeFunctionData("trade", [ | ||
sourceAddress, | ||
sourceQuantity, | ||
destinationAddress, | ||
mockSetToken.address, | ||
ethers.constants.MaxUint256, | ||
wbtcRate, | ||
ADDRESS_ZERO, | ||
]); | ||
expect(JSON.stringify(calldata)).to.eq(JSON.stringify([kyberNetworkProxy.address, ZERO, expectedCallData])); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.