mirror of
https://github.com/Instadapp/dsa-connectors-old.git
synced 2024-07-29 22:47:46 +00:00
Merge pull request #6 from InstaDApp/curvesbtc-connector
Curvesbtc connector
This commit is contained in:
commit
3138fad284
|
@ -1,36 +1,41 @@
|
|||
pragma solidity ^0.6.0;
|
||||
|
||||
import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol";
|
||||
|
||||
contract DSMath {
|
||||
uint constant WAD = 10 ** 18;
|
||||
uint constant RAY = 10 ** 27;
|
||||
uint constant WAD = 10 ** 18;
|
||||
uint constant RAY = 10 ** 27;
|
||||
|
||||
function add(uint x, uint y) internal pure returns (uint z) {
|
||||
require((z = x + y) >= x, "math-not-safe");
|
||||
}
|
||||
function add(uint x, uint y) internal pure returns (uint z) {
|
||||
z = SafeMath.add(x, y);
|
||||
}
|
||||
|
||||
function sub(uint x, uint y) internal pure returns (uint z) {
|
||||
require((z = x - y) <= x, "ds-math-sub-underflow");
|
||||
}
|
||||
function sub(uint x, uint y) internal pure returns (uint z) {
|
||||
z = SafeMath.sub(x, y);
|
||||
}
|
||||
|
||||
function mul(uint x, uint y) internal pure returns (uint z) {
|
||||
require(y == 0 || (z = x * y) / y == x, "math-not-safe");
|
||||
}
|
||||
function mul(uint x, uint y) internal pure returns (uint z) {
|
||||
z = SafeMath.mul(x, y);
|
||||
}
|
||||
|
||||
function div(uint x, uint y) internal pure returns (uint z) {
|
||||
z = SafeMath.div(x, y);
|
||||
}
|
||||
|
||||
function wmul(uint x, uint y) internal pure returns (uint z) {
|
||||
z = add(mul(x, y), WAD / 2) / WAD;
|
||||
}
|
||||
function wmul(uint x, uint y) internal pure returns (uint z) {
|
||||
z = SafeMath.add(SafeMath.mul(x, y), WAD / 2) / WAD;
|
||||
}
|
||||
|
||||
function wdiv(uint x, uint y) internal pure returns (uint z) {
|
||||
z = add(mul(x, WAD), y / 2) / y;
|
||||
}
|
||||
function wdiv(uint x, uint y) internal pure returns (uint z) {
|
||||
z = SafeMath.add(SafeMath.mul(x, WAD), y / 2) / y;
|
||||
}
|
||||
|
||||
function rdiv(uint x, uint y) internal pure returns (uint z) {
|
||||
z = add(mul(x, RAY), y / 2) / y;
|
||||
}
|
||||
function rdiv(uint x, uint y) internal pure returns (uint z) {
|
||||
z = SafeMath.add(SafeMath.mul(x, RAY), y / 2) / y;
|
||||
}
|
||||
|
||||
function rmul(uint x, uint y) internal pure returns (uint z) {
|
||||
z = add(mul(x, y), RAY / 2) / RAY;
|
||||
}
|
||||
function rmul(uint x, uint y) internal pure returns (uint z) {
|
||||
z = SafeMath.add(SafeMath.mul(x, y), RAY / 2) / RAY;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,8 +51,8 @@ contract Stores {
|
|||
/**
|
||||
* @dev Connector Details - needs to be changed before deployment
|
||||
*/
|
||||
function connectorID() public pure returns(uint model, uint id) {
|
||||
function connectorID() public view returns(uint model, uint id) {
|
||||
(model, id) = (0, 0);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,224 +6,223 @@ import { Stores } from "../common/stores.sol";
|
|||
import { DSMath } from "../common/math.sol";
|
||||
|
||||
interface ICurve {
|
||||
function underlying_coins(int128 tokenId) external view returns (address token);
|
||||
function calc_token_amount(uint256[4] calldata amounts, bool deposit) external returns (uint256 amount);
|
||||
function add_liquidity(uint256[4] calldata amounts, uint256 min_mint_amount) external;
|
||||
function get_dy(int128 sellTokenId, int128 buyTokenId, uint256 sellTokenAmt) external returns (uint256 buyTokenAmt);
|
||||
function exchange(int128 sellTokenId, int128 buyTokenId, uint256 sellTokenAmt, uint256 minBuyToken) external;
|
||||
function remove_liquidity_imbalance(uint256[4] calldata amounts, uint256 max_burn_amount) external;
|
||||
function underlying_coins(int128 tokenId) external view returns (address token);
|
||||
function calc_token_amount(uint256[4] calldata amounts, bool deposit) external returns (uint256 amount);
|
||||
function add_liquidity(uint256[4] calldata amounts, uint256 min_mint_amount) external;
|
||||
function get_dy(int128 sellTokenId, int128 buyTokenId, uint256 sellTokenAmt) external returns (uint256 buyTokenAmt);
|
||||
function exchange(int128 sellTokenId, int128 buyTokenId, uint256 sellTokenAmt, uint256 minBuyToken) external;
|
||||
function remove_liquidity_imbalance(uint256[4] calldata amounts, uint256 max_burn_amount) external;
|
||||
}
|
||||
|
||||
interface ICurveZap {
|
||||
function calc_withdraw_one_coin(uint256 _token_amount, int128 i) external returns (uint256 amount);
|
||||
function calc_withdraw_one_coin(uint256 _token_amount, int128 i) external returns (uint256 amount);
|
||||
}
|
||||
|
||||
|
||||
contract CurveHelpers is Stores, DSMath {
|
||||
/**
|
||||
* @dev Return Curve Swap Address
|
||||
*/
|
||||
function getCurveSwapAddr() internal pure returns (address) {
|
||||
return 0xA5407eAE9Ba41422680e2e00537571bcC53efBfD;
|
||||
}
|
||||
/**
|
||||
* @dev Return Curve Swap Address
|
||||
*/
|
||||
function getCurveSwapAddr() internal pure returns (address) {
|
||||
return 0xA5407eAE9Ba41422680e2e00537571bcC53efBfD;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Return Curve Token Address
|
||||
*/
|
||||
function getCurveTokenAddr() internal pure returns (address) {
|
||||
return 0xC25a3A3b969415c80451098fa907EC722572917F;
|
||||
}
|
||||
/**
|
||||
* @dev Return Curve Token Address
|
||||
*/
|
||||
function getCurveTokenAddr() internal pure returns (address) {
|
||||
return 0xC25a3A3b969415c80451098fa907EC722572917F;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Return Curve Zap Address
|
||||
*/
|
||||
function getCurveZapAddr() internal pure returns (address) {
|
||||
return 0xFCBa3E75865d2d561BE8D220616520c171F12851;
|
||||
}
|
||||
/**
|
||||
* @dev Return Curve Zap Address
|
||||
*/
|
||||
function getCurveZapAddr() internal pure returns (address) {
|
||||
return 0xFCBa3E75865d2d561BE8D220616520c171F12851;
|
||||
}
|
||||
|
||||
function convert18ToDec(uint _dec, uint256 _amt) internal pure returns (uint256 amt) {
|
||||
amt = (_amt / 10 ** (18 - _dec));
|
||||
}
|
||||
function convert18ToDec(uint _dec, uint256 _amt) internal pure returns (uint256 amt) {
|
||||
amt = (_amt / 10 ** (18 - _dec));
|
||||
}
|
||||
|
||||
function convertTo18(uint _dec, uint256 _amt) internal pure returns (uint256 amt) {
|
||||
amt = mul(_amt, 10 ** (18 - _dec));
|
||||
}
|
||||
function convertTo18(uint _dec, uint256 _amt) internal pure returns (uint256 amt) {
|
||||
amt = mul(_amt, 10 ** (18 - _dec));
|
||||
}
|
||||
|
||||
function getTokenI(address token) internal pure returns (int128 i) {
|
||||
if (token == address(0x6B175474E89094C44Da98b954EedeAC495271d0F)) {
|
||||
// DAI Token
|
||||
i = 0;
|
||||
} else if (token == address(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48)) {
|
||||
// USDC Token
|
||||
i = 1;
|
||||
} else if (token == address(0xdAC17F958D2ee523a2206206994597C13D831ec7)) {
|
||||
// USDT Token
|
||||
i = 2;
|
||||
} else if (token == address(0x57Ab1ec28D129707052df4dF418D58a2D46d5f51)) {
|
||||
// sUSD Token
|
||||
i = 3;
|
||||
} else {
|
||||
revert("token-not-found.");
|
||||
}
|
||||
function getTokenI(address token) internal pure returns (int128 i) {
|
||||
if (token == address(0x6B175474E89094C44Da98b954EedeAC495271d0F)) {
|
||||
// DAI Token
|
||||
i = 0;
|
||||
} else if (token == address(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48)) {
|
||||
// USDC Token
|
||||
i = 1;
|
||||
} else if (token == address(0xdAC17F958D2ee523a2206206994597C13D831ec7)) {
|
||||
// USDT Token
|
||||
i = 2;
|
||||
} else if (token == address(0x57Ab1ec28D129707052df4dF418D58a2D46d5f51)) {
|
||||
// sUSD Token
|
||||
i = 3;
|
||||
} else {
|
||||
revert("token-not-found.");
|
||||
}
|
||||
}
|
||||
|
||||
function getTokenAddr(ICurve curve, uint256 i) internal view returns (address token) {
|
||||
token = curve.underlying_coins(int128(i));
|
||||
require(token != address(0), "token-not-found.");
|
||||
}
|
||||
function getTokenAddr(ICurve curve, uint256 i) internal view returns (address token) {
|
||||
token = curve.underlying_coins(int128(i));
|
||||
require(token != address(0), "token-not-found.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
contract CurveProtocol is CurveHelpers {
|
||||
|
||||
event LogSell(
|
||||
address indexed buyToken,
|
||||
address indexed sellToken,
|
||||
uint256 buyAmt,
|
||||
uint256 sellAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
event LogDeposit(address token, uint256 amt, uint256 mintAmt, uint256 getId, uint256 setId);
|
||||
event LogWithdraw(address token, uint256 amt, uint256 burnAmt, uint256 getId, uint256 setId);
|
||||
event LogSell(
|
||||
address indexed buyToken,
|
||||
address indexed sellToken,
|
||||
uint256 buyAmt,
|
||||
uint256 sellAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
event LogDeposit(address token, uint256 amt, uint256 mintAmt, uint256 getId, uint256 setId);
|
||||
event LogWithdraw(address token, uint256 amt, uint256 burnAmt, uint256 getId, uint256 setId);
|
||||
|
||||
/**
|
||||
* @dev Sell Stable ERC20_Token.
|
||||
* @param buyAddr buying token address.
|
||||
* @param sellAddr selling token amount.
|
||||
* @param sellAmt selling token amount.
|
||||
* @param unitAmt unit amount of buyAmt/sellAmt with slippage.
|
||||
* @param getId Get token amount at this ID from `InstaMemory` Contract.
|
||||
* @param setId Set token amount at this ID in `InstaMemory` Contract.
|
||||
/**
|
||||
* @dev Sell Stable ERC20_Token.
|
||||
* @param buyAddr buying token address.
|
||||
* @param sellAddr selling token amount.
|
||||
* @param sellAmt selling token amount.
|
||||
* @param unitAmt unit amount of buyAmt/sellAmt with slippage.
|
||||
* @param getId Get token amount at this ID from `InstaMemory` Contract.
|
||||
* @param setId Set token amount at this ID in `InstaMemory` Contract.
|
||||
*/
|
||||
function sell(
|
||||
address buyAddr,
|
||||
address sellAddr,
|
||||
uint sellAmt,
|
||||
uint unitAmt,
|
||||
uint getId,
|
||||
uint setId
|
||||
) external payable {
|
||||
uint _sellAmt = getUint(getId, sellAmt);
|
||||
ICurve curve = ICurve(getCurveSwapAddr());
|
||||
TokenInterface _buyToken = TokenInterface(buyAddr);
|
||||
TokenInterface _sellToken = TokenInterface(sellAddr);
|
||||
_sellAmt = _sellAmt == uint(-1) ? _sellToken.balanceOf(address(this)) : _sellAmt;
|
||||
_sellToken.approve(address(curve), _sellAmt);
|
||||
function sell(
|
||||
address buyAddr,
|
||||
address sellAddr,
|
||||
uint sellAmt,
|
||||
uint unitAmt,
|
||||
uint getId,
|
||||
uint setId
|
||||
) external payable {
|
||||
uint _sellAmt = getUint(getId, sellAmt);
|
||||
ICurve curve = ICurve(getCurveSwapAddr());
|
||||
TokenInterface _buyToken = TokenInterface(buyAddr);
|
||||
TokenInterface _sellToken = TokenInterface(sellAddr);
|
||||
_sellAmt = _sellAmt == uint(-1) ? _sellToken.balanceOf(address(this)) : _sellAmt;
|
||||
_sellToken.approve(address(curve), _sellAmt);
|
||||
|
||||
uint _slippageAmt = convert18ToDec(_buyToken.decimals(), wmul(unitAmt, convertTo18(_sellToken.decimals(), _sellAmt)));
|
||||
uint _slippageAmt = convert18ToDec(_buyToken.decimals(), wmul(unitAmt, convertTo18(_sellToken.decimals(), _sellAmt)));
|
||||
|
||||
uint intialBal = _buyToken.balanceOf(address(this));
|
||||
curve.exchange(getTokenI(sellAddr), getTokenI(buyAddr), _sellAmt, _slippageAmt);
|
||||
uint finalBal = _buyToken.balanceOf(address(this));
|
||||
uint intialBal = _buyToken.balanceOf(address(this));
|
||||
curve.exchange(getTokenI(sellAddr), getTokenI(buyAddr), _sellAmt, _slippageAmt);
|
||||
uint finalBal = _buyToken.balanceOf(address(this));
|
||||
|
||||
uint _buyAmt = sub(finalBal, intialBal);
|
||||
uint _buyAmt = sub(finalBal, intialBal);
|
||||
|
||||
setUint(setId, _buyAmt);
|
||||
setUint(setId, _buyAmt);
|
||||
|
||||
emit LogSell(buyAddr, sellAddr, _buyAmt, _sellAmt, getId, setId);
|
||||
bytes32 _eventCode = keccak256("LogSell(address,address,uint256,uint256,uint256,uint256)");
|
||||
bytes memory _eventParam = abi.encode(buyAddr, sellAddr, _buyAmt, _sellAmt, getId, setId);
|
||||
emitEvent(_eventCode, _eventParam);
|
||||
emit LogSell(buyAddr, sellAddr, _buyAmt, _sellAmt, getId, setId);
|
||||
bytes32 _eventCode = keccak256("LogSell(address,address,uint256,uint256,uint256,uint256)");
|
||||
bytes memory _eventParam = abi.encode(buyAddr, sellAddr, _buyAmt, _sellAmt, getId, setId);
|
||||
emitEvent(_eventCode, _eventParam);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Deposit Token.
|
||||
* @param token token address.
|
||||
* @param amt token amount.
|
||||
* @param unitAmt unit amount of curve_amt/token_amt with slippage.
|
||||
* @param getId Get token amount at this ID from `InstaMemory` Contract.
|
||||
* @param setId Set token amount at this ID in `InstaMemory` Contract.
|
||||
*/
|
||||
function deposit(
|
||||
address token,
|
||||
uint amt,
|
||||
uint unitAmt,
|
||||
uint getId,
|
||||
uint setId
|
||||
) external payable {
|
||||
uint256 _amt = getUint(getId, amt);
|
||||
TokenInterface tokenContract = TokenInterface(token);
|
||||
|
||||
_amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
|
||||
uint[4] memory _amts;
|
||||
_amts[uint(getTokenI(token))] = _amt;
|
||||
|
||||
tokenContract.approve(getCurveSwapAddr(), _amt);
|
||||
|
||||
uint _amt18 = convertTo18(tokenContract.decimals(), _amt);
|
||||
uint _slippageAmt = wmul(unitAmt, _amt18);
|
||||
|
||||
TokenInterface curveTokenContract = TokenInterface(getCurveTokenAddr());
|
||||
uint initialCurveBal = curveTokenContract.balanceOf(address(this));
|
||||
|
||||
ICurve(getCurveSwapAddr()).add_liquidity(_amts, _slippageAmt);
|
||||
|
||||
uint finalCurveBal = curveTokenContract.balanceOf(address(this));
|
||||
|
||||
uint mintAmt = sub(finalCurveBal, initialCurveBal);
|
||||
|
||||
setUint(setId, mintAmt);
|
||||
|
||||
emit LogDeposit(token, _amt, mintAmt, getId, setId);
|
||||
bytes32 _eventCode = keccak256("LogDeposit(address,uint256,uint256,uint256,uint256)");
|
||||
bytes memory _eventParam = abi.encode(token, _amt, mintAmt, getId, setId);
|
||||
emitEvent(_eventCode, _eventParam);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Withdraw Token.
|
||||
* @param token token address.
|
||||
* @param amt token amount.
|
||||
* @param unitAmt unit amount of curve_amt/token_amt with slippage.
|
||||
* @param getId Get token amount at this ID from `InstaMemory` Contract.
|
||||
* @param setId Set token amount at this ID in `InstaMemory` Contract.
|
||||
*/
|
||||
function withdraw(
|
||||
address token,
|
||||
uint256 amt,
|
||||
uint256 unitAmt,
|
||||
uint getId,
|
||||
uint setId
|
||||
) external payable {
|
||||
uint _amt = getUint(getId, amt);
|
||||
int128 tokenId = getTokenI(token);
|
||||
|
||||
TokenInterface curveTokenContract = TokenInterface(getCurveTokenAddr());
|
||||
ICurveZap curveZap = ICurveZap(getCurveZapAddr());
|
||||
ICurve curveSwap = ICurve(getCurveSwapAddr());
|
||||
|
||||
uint _curveAmt;
|
||||
uint[4] memory _amts;
|
||||
if (_amt == uint(-1)) {
|
||||
_curveAmt = curveTokenContract.balanceOf(address(this));
|
||||
_amt = curveZap.calc_withdraw_one_coin(_curveAmt, tokenId);
|
||||
_amts[uint(tokenId)] = _amt;
|
||||
} else {
|
||||
_amts[uint(tokenId)] = _amt;
|
||||
_curveAmt = curveSwap.calc_token_amount(_amts, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Deposit Token.
|
||||
* @param token token address.
|
||||
* @param amt token amount.
|
||||
* @param unitAmt unit amount of curve_amt/token_amt with slippage.
|
||||
* @param getId Get token amount at this ID from `InstaMemory` Contract.
|
||||
* @param setId Set token amount at this ID in `InstaMemory` Contract.
|
||||
*/
|
||||
function deposit(
|
||||
address token,
|
||||
uint amt,
|
||||
uint unitAmt,
|
||||
uint getId,
|
||||
uint setId
|
||||
) external payable {
|
||||
uint256 _amt = getUint(getId, amt);
|
||||
TokenInterface tokenContract = TokenInterface(token);
|
||||
|
||||
_amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
|
||||
uint[4] memory _amts;
|
||||
_amts[uint(getTokenI(token))] = _amt;
|
||||
uint _amt18 = convertTo18(TokenInterface(token).decimals(), _amt);
|
||||
uint _slippageAmt = wmul(unitAmt, _amt18);
|
||||
|
||||
tokenContract.approve(getCurveSwapAddr(), _amt);
|
||||
curveTokenContract.approve(address(curveSwap), 0);
|
||||
curveTokenContract.approve(address(curveSwap), _slippageAmt);
|
||||
|
||||
uint _amt18 = convertTo18(tokenContract.decimals(), _amt);
|
||||
uint _slippageAmt = wmul(unitAmt, _amt18);
|
||||
curveSwap.remove_liquidity_imbalance(_amts, _slippageAmt);
|
||||
|
||||
TokenInterface curveTokenContract = TokenInterface(getCurveTokenAddr());
|
||||
uint initialCurveBal = curveTokenContract.balanceOf(address(this));
|
||||
setUint(setId, _amt);
|
||||
|
||||
ICurve(getCurveSwapAddr()).add_liquidity(_amts, _slippageAmt);
|
||||
|
||||
uint finalCurveBal = curveTokenContract.balanceOf(address(this));
|
||||
|
||||
uint mintAmt = sub(finalCurveBal, initialCurveBal);
|
||||
|
||||
setUint(setId, mintAmt);
|
||||
|
||||
emit LogDeposit(token, _amt, mintAmt, getId, setId);
|
||||
bytes32 _eventCode = keccak256("LogDeposit(address,uint256,uint256,uint256,uint256)");
|
||||
bytes memory _eventParam = abi.encode(token, _amt, mintAmt, getId, setId);
|
||||
emitEvent(_eventCode, _eventParam);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Withdraw Token.
|
||||
* @param token token address.
|
||||
* @param amt token amount.
|
||||
* @param unitAmt unit amount of curve_amt/token_amt with slippage.
|
||||
* @param getId Get token amount at this ID from `InstaMemory` Contract.
|
||||
* @param setId Set token amount at this ID in `InstaMemory` Contract.
|
||||
*/
|
||||
function withdraw(
|
||||
address token,
|
||||
uint256 amt,
|
||||
uint256 unitAmt,
|
||||
uint getId,
|
||||
uint setId
|
||||
) external payable {
|
||||
uint _amt = getUint(getId, amt);
|
||||
int128 tokenId = getTokenI(token);
|
||||
|
||||
TokenInterface curveTokenContract = TokenInterface(getCurveTokenAddr());
|
||||
ICurveZap curveZap = ICurveZap(getCurveZapAddr());
|
||||
ICurve curveSwap = ICurve(getCurveSwapAddr());
|
||||
|
||||
uint _curveAmt;
|
||||
uint[4] memory _amts;
|
||||
if (_amt == uint(-1)) {
|
||||
_curveAmt = curveTokenContract.balanceOf(address(this));
|
||||
_amt = curveZap.calc_withdraw_one_coin(_curveAmt, tokenId);
|
||||
_amts[uint(tokenId)] = _amt;
|
||||
} else {
|
||||
_amts[uint(tokenId)] = _amt;
|
||||
_curveAmt = curveSwap.calc_token_amount(_amts, false);
|
||||
}
|
||||
|
||||
|
||||
uint _amt18 = convertTo18(TokenInterface(token).decimals(), _amt);
|
||||
uint _slippageAmt = wmul(unitAmt, _amt18);
|
||||
|
||||
curveTokenContract.approve(address(curveSwap), 0);
|
||||
curveTokenContract.approve(address(curveSwap), _slippageAmt);
|
||||
|
||||
curveSwap.remove_liquidity_imbalance(_amts, _slippageAmt);
|
||||
|
||||
setUint(setId, _amt);
|
||||
|
||||
emit LogWithdraw(token, _amt, _curveAmt, getId, setId);
|
||||
bytes32 _eventCode = keccak256("LogWithdraw(address,uint256,uint256,uint256,uint256)");
|
||||
bytes memory _eventParam = abi.encode(token, _amt, _curveAmt, getId, setId);
|
||||
emitEvent(_eventCode, _eventParam);
|
||||
}
|
||||
emit LogWithdraw(token, _amt, _curveAmt, getId, setId);
|
||||
bytes32 _eventCode = keccak256("LogWithdraw(address,uint256,uint256,uint256,uint256)");
|
||||
bytes memory _eventParam = abi.encode(token, _amt, _curveAmt, getId, setId);
|
||||
emitEvent(_eventCode, _eventParam);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
contract ConnectCurve is CurveProtocol {
|
||||
string public name = "Curve-susd-v1.2";
|
||||
string public name = "Curve-susd-v1.2";
|
||||
}
|
||||
|
|
207
contracts/connectors/curvesbtc.sol
Normal file
207
contracts/connectors/curvesbtc.sol
Normal file
|
@ -0,0 +1,207 @@
|
|||
pragma solidity ^0.6.0;
|
||||
|
||||
// import files from common directory
|
||||
import { Stores } from "../common/stores.sol";
|
||||
import { DSMath } from "../common/math.sol";
|
||||
import { TokenInterface } from "../common/interfaces.sol";
|
||||
|
||||
interface ICurve {
|
||||
function coins(int128 tokenId) external view returns (address token);
|
||||
function calc_token_amount(uint256[3] calldata amounts, bool deposit) external returns (uint256 amount);
|
||||
function add_liquidity(uint256[3] calldata amounts, uint256 min_mint_amount) external;
|
||||
function get_dy(int128 sellTokenId, int128 buyTokenId, uint256 sellTokenAmt) external returns (uint256 buyTokenAmt);
|
||||
function exchange(int128 sellTokenId, int128 buyTokenId, uint256 sellTokenAmt, uint256 minBuyToken) external;
|
||||
function remove_liquidity_imbalance(uint256[3] calldata amounts, uint256 max_burn_amount) external;
|
||||
function calc_withdraw_one_coin(uint256 _token_amount, int128 i) external returns (uint256 amount);
|
||||
}
|
||||
|
||||
contract CurveSBTCHelpers is Stores, DSMath{
|
||||
/**
|
||||
* @dev Return Curve Swap Address
|
||||
*/
|
||||
function getCurveSwapAddr() internal pure returns (address) {
|
||||
return 0x7fC77b5c7614E1533320Ea6DDc2Eb61fa00A9714;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Return Curve Token Address
|
||||
*/
|
||||
function getCurveTokenAddr() internal pure returns (address) {
|
||||
return 0x075b1bb99792c9E1041bA13afEf80C91a1e70fB3;
|
||||
}
|
||||
|
||||
function convert18ToDec(uint _dec, uint256 _amt) internal pure returns (uint256 amt) {
|
||||
amt = div(_amt, 10 ** (18 - _dec));
|
||||
}
|
||||
|
||||
function convertTo18(uint _dec, uint256 _amt) internal pure returns (uint256 amt) {
|
||||
amt = mul(_amt, 10 ** (18 - _dec));
|
||||
}
|
||||
|
||||
function getTokenI(address token) internal pure returns (int128 i) {
|
||||
if (token == address(0xEB4C2781e4ebA804CE9a9803C67d0893436bB27D)) {
|
||||
// RenBTC Token
|
||||
i = 0;
|
||||
} else if (token == address(0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599)) {
|
||||
// WBTC Token
|
||||
i = 1;
|
||||
} else if (token == address(0xfE18be6b3Bd88A2D2A7f928d00292E7a9963CfC6)) {
|
||||
// SBTC Token
|
||||
i = 2;
|
||||
} else {
|
||||
revert("token-not-found.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
contract CurveSBTCProtocol is CurveSBTCHelpers {
|
||||
|
||||
// Events
|
||||
event LogSell(
|
||||
address indexed buyToken,
|
||||
address indexed sellToken,
|
||||
uint256 buyAmt,
|
||||
uint256 sellAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
event LogDeposit(address token, uint256 amt, uint256 mintAmt, uint256 getId, uint256 setId);
|
||||
event LogWithdraw(address token, uint256 amt, uint256 burnAmt, uint256 getId, uint256 setId);
|
||||
|
||||
/**
|
||||
* @dev Sell ERC20_Token.
|
||||
* @param buyAddr buying token address.
|
||||
* @param sellAddr selling token amount.
|
||||
* @param sellAmt selling token amount.
|
||||
* @param unitAmt unit amount of buyAmt/sellAmt with slippage.
|
||||
* @param getId Get token amount at this ID from `InstaMemory` Contract.
|
||||
* @param setId Set token amount at this ID in `InstaMemory` Contract.
|
||||
*/
|
||||
function sell(
|
||||
address buyAddr,
|
||||
address sellAddr,
|
||||
uint sellAmt,
|
||||
uint unitAmt,
|
||||
uint getId,
|
||||
uint setId
|
||||
) external payable {
|
||||
uint _sellAmt = getUint(getId, sellAmt);
|
||||
ICurve curve = ICurve(getCurveSwapAddr());
|
||||
TokenInterface _buyToken = TokenInterface(buyAddr);
|
||||
TokenInterface _sellToken = TokenInterface(sellAddr);
|
||||
_sellAmt = _sellAmt == uint(-1) ? _sellToken.balanceOf(address(this)) : _sellAmt;
|
||||
_sellToken.approve(address(curve), _sellAmt);
|
||||
|
||||
uint _slippageAmt = convert18ToDec(_buyToken.decimals(), wmul(unitAmt, convertTo18(_sellToken.decimals(), _sellAmt)));
|
||||
|
||||
uint intialBal = _buyToken.balanceOf(address(this));
|
||||
curve.exchange(getTokenI(sellAddr), getTokenI(buyAddr), _sellAmt, _slippageAmt);
|
||||
uint finalBal = _buyToken.balanceOf(address(this));
|
||||
|
||||
uint _buyAmt = sub(finalBal, intialBal);
|
||||
|
||||
setUint(setId, _buyAmt);
|
||||
|
||||
emit LogSell(buyAddr, sellAddr, _buyAmt, _sellAmt, getId, setId);
|
||||
bytes32 _eventCode = keccak256("LogSell(address,address,uint256,uint256,uint256,uint256)");
|
||||
bytes memory _eventParam = abi.encode(buyAddr, sellAddr, _buyAmt, _sellAmt, getId, setId);
|
||||
emitEvent(_eventCode, _eventParam);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Deposit Token.
|
||||
* @param token token address.
|
||||
* @param amt token amount.
|
||||
* @param unitAmt unit amount of curve_amt/token_amt with slippage.
|
||||
* @param getId Get token amount at this ID from `InstaMemory` Contract.
|
||||
* @param setId Set token amount at this ID in `InstaMemory` Contract.
|
||||
*/
|
||||
function deposit(
|
||||
address token,
|
||||
uint amt,
|
||||
uint unitAmt,
|
||||
uint getId,
|
||||
uint setId
|
||||
) external payable {
|
||||
uint256 _amt = getUint(getId, amt);
|
||||
TokenInterface tokenContract = TokenInterface(token);
|
||||
|
||||
_amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
|
||||
uint[3] memory _amts;
|
||||
_amts[uint(getTokenI(token))] = _amt;
|
||||
|
||||
tokenContract.approve(getCurveSwapAddr(), _amt);
|
||||
|
||||
uint _amt18 = convertTo18(tokenContract.decimals(), _amt);
|
||||
uint _slippageAmt = wmul(unitAmt, _amt18);
|
||||
|
||||
TokenInterface curveTokenContract = TokenInterface(getCurveTokenAddr());
|
||||
uint initialCurveBal = curveTokenContract.balanceOf(address(this));
|
||||
|
||||
ICurve(getCurveSwapAddr()).add_liquidity(_amts, _slippageAmt);
|
||||
|
||||
uint finalCurveBal = curveTokenContract.balanceOf(address(this));
|
||||
|
||||
uint mintAmt = sub(finalCurveBal, initialCurveBal);
|
||||
|
||||
setUint(setId, mintAmt);
|
||||
|
||||
emit LogDeposit(token, _amt, mintAmt, getId, setId);
|
||||
bytes32 _eventCode = keccak256("LogDeposit(address,uint256,uint256,uint256,uint256)");
|
||||
bytes memory _eventParam = abi.encode(token, _amt, mintAmt, getId, setId);
|
||||
emitEvent(_eventCode, _eventParam);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Withdraw Token.
|
||||
* @param token token address.
|
||||
* @param amt token amount.
|
||||
* @param unitAmt unit amount of curve_amt/token_amt with slippage.
|
||||
* @param getId Get token amount at this ID from `InstaMemory` Contract.
|
||||
* @param setId Set token amount at this ID in `InstaMemory` Contract.
|
||||
*/
|
||||
function withdraw(
|
||||
address token,
|
||||
uint256 amt,
|
||||
uint256 unitAmt,
|
||||
uint getId,
|
||||
uint setId
|
||||
) external payable {
|
||||
uint _amt = getUint(getId, amt);
|
||||
int128 tokenId = getTokenI(token);
|
||||
|
||||
TokenInterface curveTokenContract = TokenInterface(getCurveTokenAddr());
|
||||
ICurve curveSwap = ICurve(getCurveSwapAddr());
|
||||
|
||||
uint _curveAmt;
|
||||
uint[3] memory _amts;
|
||||
if (_amt == uint(-1)) {
|
||||
_curveAmt = curveTokenContract.balanceOf(address(this));
|
||||
_amt = curveSwap.calc_withdraw_one_coin(_curveAmt, tokenId);
|
||||
_amts[uint(tokenId)] = _amt;
|
||||
} else {
|
||||
_amts[uint(tokenId)] = _amt;
|
||||
_curveAmt = curveSwap.calc_token_amount(_amts, false);
|
||||
}
|
||||
|
||||
uint _amt18 = convertTo18(TokenInterface(token).decimals(), _amt);
|
||||
uint _slippageAmt = wmul(unitAmt, _amt18);
|
||||
|
||||
curveTokenContract.approve(address(curveSwap), 0);
|
||||
curveTokenContract.approve(address(curveSwap), _slippageAmt);
|
||||
|
||||
curveSwap.remove_liquidity_imbalance(_amts, _slippageAmt);
|
||||
|
||||
setUint(setId, _amt);
|
||||
|
||||
emit LogWithdraw(token, _amt, _curveAmt, getId, setId);
|
||||
bytes32 _eventCode = keccak256("LogWithdraw(address,uint256,uint256,uint256,uint256)");
|
||||
bytes memory _eventParam = abi.encode(token, _amt, _curveAmt, getId, setId);
|
||||
emitEvent(_eventCode, _eventParam);
|
||||
}
|
||||
}
|
||||
|
||||
contract ConnectSBTCCurve is CurveSBTCProtocol {
|
||||
string public name = "Curve-sbtc-v1";
|
||||
}
|
||||
|
|
@ -1,5 +1,12 @@
|
|||
const Connector = artifacts.require("CurveProtocol"); // Change the Connector name while deploying.
|
||||
const CurveProtocol = artifacts.require("CurveProtocol");
|
||||
const ConnectSBTCCurve = artifacts.require("ConnectSBTCCurve");
|
||||
|
||||
module.exports = function(deployer) {
|
||||
deployer.deploy(Connector);
|
||||
const connectorsABI = require("../test/abi/connectors.json");
|
||||
let connectorsAddr = "0xD6A602C01a023B98Ecfb29Df02FBA380d3B21E0c";
|
||||
let connectorInstance = new web3.eth.Contract(connectorsABI, connectorsAddr);
|
||||
|
||||
module.exports = async function(deployer) {
|
||||
deployer.deploy(CurveProtocol);
|
||||
let connectorLength = await connectorInstance.methods.connectorLength().call();
|
||||
deployer.deploy(ConnectSBTCCurve, 1, +connectorLength + 1);
|
||||
};
|
||||
|
|
1108
package-lock.json
generated
1108
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
|
@ -5,10 +5,11 @@
|
|||
"main": "truffle-config.js",
|
||||
"directories": {},
|
||||
"scripts": {
|
||||
"test": "npm run ganache sleep 5 && truffle test && npm run stop",
|
||||
"test": "truffle test",
|
||||
"coverage": "./node_modules/.bin/solidity-coverage",
|
||||
"solium": "solium -d contracts/",
|
||||
"build-contracts": "sol-merger \"./contracts/connectors/mock.sol\" ./contracts/build"
|
||||
"build-contracts": "sol-merger \"./contracts/connectors/mock.sol\" ./contracts/build",
|
||||
"ganache": "ganache-cli --deterministic --unlock 0xfcd22438ad6ed564a1c26151df73f6b33b817b56 -f https://mainnet.infura.io/v3/<Your Key>"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
@ -35,6 +36,9 @@
|
|||
"truffle-verify": "^1.0.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@openzeppelin/test-helpers": "^0.5.6",
|
||||
"@studydefi/money-legos": "^2.3.5",
|
||||
"ganache-cli": "^6.10.0-beta.2",
|
||||
"sol-merger": "^2.0.1",
|
||||
"solidity-coverage": "0.5.11",
|
||||
"solium": "1.2.3"
|
||||
|
|
|
@ -2,7 +2,8 @@ const CurveProtocol = artifacts.require('CurveProtocol')
|
|||
const daiABI = require('./abi/dai');
|
||||
const erc20 = require('./abi/erc20')
|
||||
const swap_abi = require('./abi/swap')
|
||||
const { ether, balance } = require('openzeppelin-test-helpers');
|
||||
const { ether, balance } = require('@openzeppelin/test-helpers');
|
||||
const uniswap = require("@studydefi/money-legos/uniswap");
|
||||
|
||||
const BN = require('bn.js')
|
||||
|
||||
|
@ -11,7 +12,7 @@ const expect = chai.expect
|
|||
chai.use(require('chai-bn')(BN));
|
||||
|
||||
// userAddress must be unlocked using --unlock ADDRESS
|
||||
const userAddress = '0x9eb7f2591ed42dee9315b6e2aaf21ba85ea69f8c';
|
||||
const userAddress = '0xfcd22438ad6ed564a1c26151df73f6b33b817b56';
|
||||
const daiAddress = '0x6b175474e89094c44da98b954eedeac495271d0f';
|
||||
const daiContract = new web3.eth.Contract(daiABI, daiAddress);
|
||||
|
||||
|
@ -24,99 +25,136 @@ const swapContract = new web3.eth.Contract(swap_abi, swap)
|
|||
const swapToken = '0xC25a3A3b969415c80451098fa907EC722572917F'
|
||||
const tokenContract = new web3.eth.Contract(erc20, swapToken)
|
||||
|
||||
|
||||
|
||||
contract('Curve Protocol', async accounts => {
|
||||
let account, contract;
|
||||
|
||||
beforeEach(async function() {
|
||||
account = accounts[0]
|
||||
contract = await CurveProtocol.deployed()
|
||||
|
||||
it('should send ether to the DAI address', async () => {
|
||||
let account = accounts[0]
|
||||
let contract = await CurveProtocol.deployed()
|
||||
// Send 0.1 eth to userAddress to have gas to send an ERC20 tx.
|
||||
await web3.eth.sendTransaction({
|
||||
from: accounts[0],
|
||||
to: userAddress,
|
||||
value: ether('0.1')
|
||||
});
|
||||
const ethBalance = await balance.current(userAddress);
|
||||
expect(+ethBalance).to.be.at.least(+ether('0.1'))
|
||||
});
|
||||
let uniswapFactory = new web3.eth.Contract(
|
||||
uniswap.factory.abi,
|
||||
uniswap.factory.address
|
||||
);
|
||||
|
||||
it('should transfer DAI to CurveProtocol', async () => {
|
||||
let account = accounts[0]
|
||||
let contract = await CurveProtocol.deployed()
|
||||
// Get 100 DAI for first 5 accounts
|
||||
// daiAddress is passed to ganache-cli with flag `--unlock`
|
||||
// so we can use the `transfer` method
|
||||
await daiContract.methods
|
||||
.transfer(contract.address, ether('100').toString())
|
||||
.send({ from: userAddress, gasLimit: 800000 });
|
||||
const daiBalance = await daiContract.methods.balanceOf(contract.address).call();
|
||||
expect(+daiBalance).to.be.at.least(+ether('100'))
|
||||
});
|
||||
const daiExchangeAddress = await uniswapFactory.methods.getExchange(
|
||||
daiAddress,
|
||||
).call();
|
||||
|
||||
it('should approve DAI to CurveProtocol', async() => {
|
||||
let account = accounts[0]
|
||||
let contract = await CurveProtocol.deployed()
|
||||
const daiExchange = new web3.eth.Contract(
|
||||
uniswap.exchange.abi,
|
||||
daiExchangeAddress
|
||||
);
|
||||
|
||||
await daiContract.methods
|
||||
.approve(contract.address, ether('100').toString())
|
||||
.send({ from: account, gasLimit: 800000 });
|
||||
const daiAllowance = await daiContract.methods.allowance(account, contract.address).call()
|
||||
expect(+daiAllowance).to.be.at.least(+ether('100'))
|
||||
});
|
||||
const daiBefore = await daiContract.methods.balanceOf(userAddress).call();
|
||||
|
||||
it('should exchange', async () => {
|
||||
let account = accounts[0]
|
||||
let contract = await CurveProtocol.deployed()
|
||||
await daiExchange.methods.ethToTokenSwapInput(
|
||||
1, // min amount of token retrieved
|
||||
2525644800, // random timestamp in the future (year 2050)
|
||||
).send(
|
||||
{
|
||||
gas: 4000000,
|
||||
value: ether("5"),
|
||||
from: userAddress
|
||||
}
|
||||
);
|
||||
|
||||
// Get 100 DAI for first 5 accounts
|
||||
let get_dy = await contract.get_dy.call(0, 1, ether('1').toString())
|
||||
let min_dy = +get_dy * 0.99
|
||||
let receipt = await contract.exchange(0, 1, ether('1').toString(), 1, { from: account })
|
||||
let buyAmount = receipt.logs[0].args.buyAmount.toString()
|
||||
expect(+buyAmount).to.be.at.least(min_dy);
|
||||
let daiAfter = await daiContract.methods.balanceOf(userAddress).call();
|
||||
|
||||
});
|
||||
expect(daiAfter - daiBefore).to.be.at.least(+ether("1000"));
|
||||
});
|
||||
|
||||
it('should add liquidity', async () => {
|
||||
let account = accounts[0]
|
||||
let contract = await CurveProtocol.deployed()
|
||||
it('should send ether to the user address', async () => {
|
||||
const ethBalanceBefore = await balance.current(userAddress);
|
||||
// Send 0.1 eth to userAddress to have gas to send an ERC20 tx.
|
||||
await web3.eth.sendTransaction({
|
||||
from: accounts[0],
|
||||
to: userAddress,
|
||||
value: ether('0.1')
|
||||
});
|
||||
const ethBalanceAfter = await balance.current(userAddress);
|
||||
expect(+ethBalanceAfter - +ethBalanceBefore).to.equal(+ether('0.1'))
|
||||
});
|
||||
|
||||
let amounts = [ether('1').toString(), 0, 0, 0]
|
||||
let token_amount = await contract.calc_token_amount.call(amounts, true)
|
||||
it('should transfer DAI to CurveProtocol', async () => {
|
||||
// Get 100 DAI for first 5 accounts
|
||||
// daiAddress is passed to ganache-cli with flag `--unlock`
|
||||
// so we can use the `transfer` method
|
||||
//
|
||||
// Note: This only works as userAddress has 2546221640728945323079640 Dai,
|
||||
// should remove this dependence in the future
|
||||
const daiBalanceUser = await daiContract.methods.balanceOf(userAddress).call();
|
||||
await daiContract.methods
|
||||
.transfer(contract.address, ether('100').toString())
|
||||
.send({ from: userAddress, gasLimit: 800000 });
|
||||
const daiBalance = await daiContract.methods.balanceOf(contract.address).call();
|
||||
expect(+daiBalance).to.be.at.least(+ether('100'))
|
||||
});
|
||||
|
||||
let receipt = await contract.add_liquidity(amounts, 1, { from: account })
|
||||
let mintAmount = receipt.logs[0].args.mintAmount.toString()
|
||||
expect(+mintAmount).to.be.at.least(+mintAmount)
|
||||
})
|
||||
it('should approve DAI to CurveProtocol', async() => {
|
||||
await daiContract.methods
|
||||
.approve(contract.address, ether('100').toString())
|
||||
.send({ from: account, gasLimit: 800000 });
|
||||
const daiAllowance = await daiContract.methods.allowance(account, contract.address).call()
|
||||
expect(+daiAllowance).to.be.at.least(+ether('100'))
|
||||
});
|
||||
|
||||
it('should remove liquidity imbalance', async () => {
|
||||
let account = accounts[0]
|
||||
let contract = await CurveProtocol.deployed()
|
||||
/* Deprecated as CurveProtocol is not ICurve and exchange has been implemented into sell method
|
||||
it('should exchange', async () => {
|
||||
let account = accounts[0]
|
||||
let contract = await CurveProtocol.deployed()
|
||||
|
||||
let tokenBalance = await tokenContract.methods.balanceOf(contract.address).call()
|
||||
let receipt = await contract.remove_liquidity_imbalance(["100000000000", 0, 0, 0], { from: account })
|
||||
let burnAmount = receipt.logs[0].args.burnAmount.toString()
|
||||
let tokenBalanceAfter = await tokenContract.methods.balanceOf(contract.address).call()
|
||||
// Get 100 DAI for first 5 accounts
|
||||
console.log('before');
|
||||
let get_dy = await contract.get_dy.call(0, 1, ether('1').toString())
|
||||
console.log('after');
|
||||
let min_dy = +get_dy * 0.99
|
||||
let receipt = await contract.exchange(0, 1, ether('1').toString(), 1, { from: account })
|
||||
let buyAmount = receipt.logs[0].args.buyAmount.toString()
|
||||
expect(+buyAmount).to.be.at.least(min_dy);
|
||||
});
|
||||
*/
|
||||
|
||||
//weird Ganache errors sometimes "cannot decode event"
|
||||
console.log(+tokenBalance, +tokenBalanceAfter, +burnAmount)
|
||||
//expect(BN(tokenBalance)).to.be.a.bignumber.equal(BN(tokenBalanceAfter).add(burnAmount))
|
||||
/* Deprecated as CurveProtocol is not ICurve and calc_token_amount has been implemented into withdraw method
|
||||
it('should add liquidity', async () => {
|
||||
let account = accounts[0]
|
||||
let contract = await CurveProtocol.deployed()
|
||||
|
||||
})
|
||||
let amounts = [ether('1').toString(), 0, 0, 0]
|
||||
let token_amount = await contract.calc_token_amount.call(amounts, true)
|
||||
|
||||
it('should remove liquidity in one coin', async() => {
|
||||
let account = accounts[0]
|
||||
let contract = await CurveProtocol.deployed()
|
||||
let receipt = await contract.add_liquidity(amounts, 1, { from: account })
|
||||
let mintAmount = receipt.logs[0].args.mintAmount.toString()
|
||||
expect(+mintAmount).to.be.at.least(+mintAmount)
|
||||
})
|
||||
|
||||
let daiBalance = await daiContract.methods.balanceOf(contract.address).call()
|
||||
let receipt = await contract.remove_liquidity_one_coin("100000000000", 0, 1, { from: account })
|
||||
let withdrawnAmount = receipt.logs[0].args.withdrawnAmount.toString()
|
||||
let daiBalanceAfter = await daiContract.methods.balanceOf(contract.address).call()
|
||||
it('should remove liquidity imbalance', async () => {
|
||||
let account = accounts[0]
|
||||
let contract = await CurveProtocol.deployed()
|
||||
|
||||
//weird Ganache errors sometimes "cannot decode event"
|
||||
console.log(+daiBalance, +daiBalanceAfter, +withdrawnAmount)
|
||||
//expect(BN(daiBalance)).to.be.a.bignumber.equal(BN(daiBalanceAfter).sub(withdrawnAmount));
|
||||
})
|
||||
});
|
||||
let tokenBalance = await tokenContract.methods.balanceOf(contract.address).call()
|
||||
let receipt = await contract.remove_liquidity_imbalance(["100000000000", 0, 0, 0], { from: account })
|
||||
let burnAmount = receipt.logs[0].args.burnAmount.toString()
|
||||
let tokenBalanceAfter = await tokenContract.methods.balanceOf(contract.address).call()
|
||||
|
||||
//weird Ganache errors sometimes "cannot decode event"
|
||||
console.log(+tokenBalance, +tokenBalanceAfter, +burnAmount)
|
||||
//expect(BN(tokenBalance)).to.be.a.bignumber.equal(BN(tokenBalanceAfter).add(burnAmount))
|
||||
|
||||
})
|
||||
|
||||
it('should remove liquidity in one coin', async() => {
|
||||
let account = accounts[0]
|
||||
let contract = await CurveProtocol.deployed()
|
||||
|
||||
let daiBalance = await daiContract.methods.balanceOf(contract.address).call()
|
||||
let receipt = await contract.remove_liquidity_one_coin("100000000000", 0, 1, { from: account })
|
||||
let withdrawnAmount = receipt.logs[0].args.withdrawnAmount.toString()
|
||||
let daiBalanceAfter = await daiContract.methods.balanceOf(contract.address).call()
|
||||
|
||||
//weird Ganache errors sometimes "cannot decode event"
|
||||
console.log(+daiBalance, +daiBalanceAfter, +withdrawnAmount)
|
||||
//expect(BN(daiBalance)).to.be.a.bignumber.equal(BN(daiBalanceAfter).sub(withdrawnAmount));
|
||||
})
|
||||
*/
|
||||
});
|
||||
|
|
196
test/CurveSBTCProtocol.js
Normal file
196
test/CurveSBTCProtocol.js
Normal file
|
@ -0,0 +1,196 @@
|
|||
const {
|
||||
BN, // Big Number support
|
||||
expectEvent, // Assertions for emitted events
|
||||
expectRevert, // Assertions for transactions that should fail
|
||||
balance,
|
||||
ether
|
||||
} = require('@openzeppelin/test-helpers');
|
||||
|
||||
const ConnectSBTCCurve = artifacts.require('ConnectSBTCCurve');
|
||||
const erc20 = require("@studydefi/money-legos/erc20");
|
||||
const uniswap = require("@studydefi/money-legos/uniswap");
|
||||
const sbtcABI = require("./abi/sbtc.json");
|
||||
const erc20ABI = require("./abi/erc20.js");
|
||||
const connectorsABI = require("./abi/connectors.json");
|
||||
const accountABI = require("./abi/account.json");
|
||||
const curveSwap = require("./abi/curveSwap.json");
|
||||
|
||||
contract('ConnectSBTCCurve', async accounts => {
|
||||
const [sender, receiver] = accounts;
|
||||
let masterAddress = "0xfcd22438ad6ed564a1c26151df73f6b33b817b56";
|
||||
let accountID = 7;
|
||||
let dsrAddr = "0xEEB007bea2Bbb0cA6502217E8867f8f7b021B8D5";
|
||||
|
||||
let connectorsAddr = "0xD6A602C01a023B98Ecfb29Df02FBA380d3B21E0c";
|
||||
let connectorInstance = new web3.eth.Contract(connectorsABI, connectorsAddr);
|
||||
|
||||
// let accountAddr = "0x939Daad09fC4A9B8f8A9352A485DAb2df4F4B3F8";
|
||||
let accountInstance = new web3.eth.Contract(accountABI, dsrAddr);
|
||||
let connectSBTCCurve;
|
||||
let wbtcContract = new web3.eth.Contract(erc20.wbtc.abi, erc20.wbtc.address);
|
||||
|
||||
before(async function () {
|
||||
connectSBTCCurve = await ConnectSBTCCurve.deployed();
|
||||
|
||||
let uniswapFactory = new web3.eth.Contract(
|
||||
uniswap.factory.abi,
|
||||
uniswap.factory.address
|
||||
);
|
||||
|
||||
const wbtcExchangeAddress = await uniswapFactory.methods.getExchange(
|
||||
erc20.wbtc.address,
|
||||
).call();
|
||||
|
||||
const wbtcExchange = new web3.eth.Contract(
|
||||
uniswap.exchange.abi,
|
||||
wbtcExchangeAddress
|
||||
);
|
||||
|
||||
const wbtcBefore = await wbtcContract.methods.balanceOf(sender).call();
|
||||
console.log("Sender WBTC Before: ", wbtcBefore.toString());
|
||||
|
||||
const balanceBefore = await web3.eth.getBalance(sender);
|
||||
console.log("Sender Balance Before: ", balanceBefore.toString());
|
||||
|
||||
await wbtcExchange.methods.ethToTokenSwapInput(
|
||||
1, // min amount of token retrieved
|
||||
2525644800, // random timestamp in the future (year 2050)
|
||||
).send(
|
||||
{
|
||||
gas: 4000000,
|
||||
value: ether("10"),
|
||||
from: sender
|
||||
}
|
||||
);
|
||||
|
||||
let wbtcAfter = await wbtcContract.methods.balanceOf(sender).call();
|
||||
console.log("Sender WBTC After: ", wbtcAfter.toString());
|
||||
const balanceAfter = await web3.eth.getBalance(sender);
|
||||
console.log("Sender Balance After: ", balanceAfter.toString());
|
||||
|
||||
expect(wbtcAfter - wbtcBefore).to.be.at.least(10000000);
|
||||
|
||||
// send WBTC to master
|
||||
await wbtcContract.methods.transfer(dsrAddr, 10000000).send({from: sender});
|
||||
// send WBTC to connector
|
||||
// await wbtcContract.methods.transfer(connectSBTCCurve.address, 10000000).send({from: sender});
|
||||
|
||||
// Send ETH to master
|
||||
await web3.eth.sendTransaction({from: sender, to: masterAddress, value: ether("5")});
|
||||
|
||||
let connectorID = await connectSBTCCurve.connectorID();
|
||||
|
||||
// Enable the the given connector address
|
||||
await connectorInstance.methods.enable(connectSBTCCurve.address).send({from: masterAddress});
|
||||
// check if the give connector address is enabled.
|
||||
let isEnabled = await connectorInstance.methods.connectors(connectSBTCCurve.address).call();
|
||||
assert.ok(isEnabled);
|
||||
});
|
||||
|
||||
it('can sell WBTC for SBTC', async function () {
|
||||
const sbtcContract = new web3.eth.Contract(sbtcABI, "0xfe18be6b3bd88a2d2a7f928d00292e7a9963cfc6");
|
||||
|
||||
const sbtcBefore = await sbtcContract.methods.balanceOf(dsrAddr).call();
|
||||
console.log("Master SBTC Before: ", sbtcBefore.toString());
|
||||
let wbtcBefore = await wbtcContract.methods.balanceOf(dsrAddr).call();
|
||||
console.log("Master WBTC Before: ", wbtcBefore.toString());
|
||||
|
||||
const encoded = await connectSBTCCurve.contract.methods.sell(
|
||||
"0xfe18be6b3bd88a2d2a7f928d00292e7a9963cfc6",
|
||||
erc20.wbtc.address,
|
||||
10000000,
|
||||
( 0.09 / 0.1 * 1e18 ).toString(),
|
||||
0,
|
||||
0,
|
||||
).encodeABI();
|
||||
|
||||
await wbtcContract.methods.approve("0x7fC77b5c7614E1533320Ea6DDc2Eb61fa00A9714", 10000000).send({from: masterAddress});
|
||||
const curveSwapContract = new web3.eth.Contract(curveSwap, "0x7fC77b5c7614E1533320Ea6DDc2Eb61fa00A9714");
|
||||
const tx = await curveSwapContract.methods.exchange(1, 2, 1000000, 1).send({ from: masterAddress });
|
||||
console.log(tx);
|
||||
|
||||
// const tx = await connectSBTCCurve.contract.methods.sell(
|
||||
// "0xfe18be6b3bd88a2d2a7f928d00292e7a9963cfc6",
|
||||
// erc20.wbtc.address,
|
||||
// 1000000,
|
||||
// 1,
|
||||
// 0,
|
||||
// 0,
|
||||
// ).send({from: masterAddress});
|
||||
// console.log(tx);
|
||||
|
||||
//Inputs for `cast()` function of DSA Account.
|
||||
const castInputs = [
|
||||
[connectSBTCCurve.address],
|
||||
[encoded],
|
||||
masterAddress
|
||||
]
|
||||
|
||||
// Execute `cast()` function
|
||||
// const tx = await accountInstance.methods.cast(...castInputs).send({from: masterAddress});
|
||||
// console.log(tx);
|
||||
|
||||
let wbtcAfter = await wbtcContract.methods.balanceOf(dsrAddr).call();
|
||||
console.log("Master WBTC After: ", wbtcAfter.toString());
|
||||
const sbtcAfter = await sbtcContract.methods.balanceOf(dsrAddr).call();
|
||||
console.log("Master SBTC After: ", sbtcAfter.toString());
|
||||
expect(sbtcAfter - sbtcBefore).to.be.at.least(+ether("0.09"));
|
||||
});
|
||||
|
||||
it('can add and remove liquidity for wbtc', async function() {
|
||||
const curveTokenContract = new web3.eth.Contract(
|
||||
erc20ABI,
|
||||
"0x075b1bb99792c9e1041ba13afef80c91a1e70fb3"
|
||||
)
|
||||
|
||||
let wbtcBefore = await wbtcContract.methods.balanceOf(dsrAddr).call();
|
||||
console.log("Master WBTC Before: ", wbtcBefore.toString());
|
||||
|
||||
const encodedDeposit = await connectSBTCCurve.contract.methods.deposit(
|
||||
erc20.wbtc.address,
|
||||
10000000,
|
||||
( 0.09 / 0.1 * 1e18 ).toString(),
|
||||
0,
|
||||
0
|
||||
).encodeABI();
|
||||
|
||||
//Inputs for `cast()` function of DSA Account.
|
||||
const castInputsDeposit = [
|
||||
[connectSBTCCurve.address],
|
||||
[encodedDeposit],
|
||||
masterAddress
|
||||
]
|
||||
|
||||
// Execute `cast()` function
|
||||
const txDeposit = await accountInstance.methods.cast(...castInputsDeposit).send({from: masterAddress});
|
||||
console.log(txDeposit);
|
||||
|
||||
const balanceDeposit = await curveTokenContract.methods.balanceOf(dsrAddr);
|
||||
|
||||
expect(balanceDeposit).to.be.at.least(ether("0.09"));
|
||||
|
||||
const encodedWithdraw = await connectSBTCCurve.contract.methods.withdraw(
|
||||
erc20.wbtc.address,
|
||||
10000000,
|
||||
( 0.09 / 0.1 * 1e18 ).toString(),
|
||||
0,
|
||||
0
|
||||
).encodeABI();
|
||||
|
||||
//Inputs for `cast()` function of DSA Account.
|
||||
const castInputsWithdraw = [
|
||||
[connectSBTCCurve.address],
|
||||
[encodedWithdraw],
|
||||
masterAddress
|
||||
]
|
||||
|
||||
// Execute `cast()` function
|
||||
const txWithdraw = await accountInstance.methods.cast(...castInputsWithdraw).send({from: masterAddress});
|
||||
console.log(txWithdraw);
|
||||
|
||||
const balanceWithdraw = await curveTokenContract.methods.balanceOf(dsrAddr);
|
||||
|
||||
expect(balanceWithdraw).to.equal(0);
|
||||
});
|
||||
|
||||
});
|
1
test/abi/account.json
Normal file
1
test/abi/account.json
Normal file
|
@ -0,0 +1 @@
|
|||
[{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"origin","type":"address"},{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"LogCast","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"}],"name":"LogDisable","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"}],"name":"LogEnable","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bool","name":"_shield","type":"bool"}],"name":"LogSwitchShield","type":"event"},{"inputs":[{"internalType":"address[]","name":"_targets","type":"address[]"},{"internalType":"bytes[]","name":"_datas","type":"bytes[]"},{"internalType":"address","name":"_origin","type":"address"}],"name":"cast","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"user","type":"address"}],"name":"disable","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"user","type":"address"}],"name":"enable","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"instaIndex","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"user","type":"address"}],"name":"isAuth","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"shield","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bool","name":"_shield","type":"bool"}],"name":"switchShield","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"version","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"stateMutability":"payable","type":"receive"}]
|
1
test/abi/connectors.json
Normal file
1
test/abi/connectors.json
Normal file
|
@ -0,0 +1 @@
|
|||
[{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"addr","type":"address"}],"name":"LogAddController","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"connector","type":"address"}],"name":"LogDisable","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"connector","type":"address"}],"name":"LogEnable","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"connector","type":"address"}],"name":"LogEnableStatic","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"addr","type":"address"}],"name":"LogRemoveController","type":"event"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"chief","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"connectorArray","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"connectorCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"connectorLength","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"connectors","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_connector","type":"address"}],"name":"disable","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_userAddress","type":"address"}],"name":"disableChief","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_connector","type":"address"}],"name":"enable","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_userAddress","type":"address"}],"name":"enableChief","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_connector","type":"address"}],"name":"enableStatic","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"instaIndex","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address[]","name":"_connectors","type":"address[]"}],"name":"isConnector","outputs":[{"internalType":"bool","name":"isOk","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address[]","name":"_connectors","type":"address[]"}],"name":"isStaticConnector","outputs":[{"internalType":"bool","name":"isOk","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"staticConnectorArray","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"staticConnectorLength","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"staticConnectors","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"}]
|
1
test/abi/curveSwap.json
Normal file
1
test/abi/curveSwap.json
Normal file
File diff suppressed because one or more lines are too long
1
test/abi/sbtc.json
Normal file
1
test/abi/sbtc.json
Normal file
|
@ -0,0 +1 @@
|
|||
[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"spender","type":"address"},{"name":"value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_owner","type":"address"}],"name":"nominateNewOwner","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"from","type":"address"},{"name":"to","type":"address"},{"name":"value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"nominatedOwner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_target","type":"address"}],"name":"setTarget","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"acceptOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"callData","type":"bytes"},{"name":"numTopics","type":"uint256"},{"name":"topic1","type":"bytes32"},{"name":"topic2","type":"bytes32"},{"name":"topic3","type":"bytes32"},{"name":"topic4","type":"bytes32"}],"name":"_emit","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"useDELEGATECALL","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"value","type":"bool"}],"name":"setUseDELEGATECALL","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"target","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"owner","type":"address"},{"name":"spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_owner","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"newTarget","type":"address"}],"name":"TargetUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"newOwner","type":"address"}],"name":"OwnerNominated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"oldOwner","type":"address"},{"indexed":false,"name":"newOwner","type":"address"}],"name":"OwnerChanged","type":"event"}]
|
|
@ -54,6 +54,12 @@ module.exports = {
|
|||
port: 8545, // Standard Ethereum port (default: none)
|
||||
network_id: "*", // Any network (default: none)
|
||||
},
|
||||
// Tenderly Proxy
|
||||
proxy: {
|
||||
host: "127.0.0.1", // Localhost (default: none)
|
||||
port: 9545, // Standard Ethereum port (default: none)
|
||||
network_id: "*", // Any network (default: none)
|
||||
},
|
||||
|
||||
// Another network with more advanced options...
|
||||
// advanced: {
|
||||
|
@ -108,7 +114,7 @@ module.exports = {
|
|||
// Configure your compilers
|
||||
compilers: {
|
||||
solc: {
|
||||
version: "v0.6.0", // Fetch exact version from solc-bin (default: truffle's version)
|
||||
version: "v0.6.2", // Fetch exact version from solc-bin (default: truffle's version)
|
||||
// docker: true, // Use "0.5.1" you've installed locally with docker (default: false)
|
||||
// settings: { // See the solidity docs for advice about optimization and evmVersion
|
||||
// optimizer: {
|
||||
|
|
Loading…
Reference in New Issue
Block a user