文件 1 的 21:ABDKMath64x64.sol
pragma solidity ^0.7.0;
library ABDKMath64x64 {
int128 private constant MIN_64x64 = -0x80000000000000000000000000000000;
int128 private constant MAX_64x64 = 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
function fromInt (int256 x) internal pure returns (int128) {
require (x >= -0x8000000000000000 && x <= 0x7FFFFFFFFFFFFFFF);
return int128 (x << 64);
}
function toInt (int128 x) internal pure returns (int64) {
return int64 (x >> 64);
}
function fromUInt (uint256 x) internal pure returns (int128) {
require (x <= 0x7FFFFFFFFFFFFFFF);
return int128 (x << 64);
}
function toUInt (int128 x) internal pure returns (uint64) {
require (x >= 0);
return uint64 (x >> 64);
}
function from128x128 (int256 x) internal pure returns (int128) {
int256 result = x >> 64;
require (result >= MIN_64x64 && result <= MAX_64x64);
return int128 (result);
}
function to128x128 (int128 x) internal pure returns (int256) {
return int256 (x) << 64;
}
function add (int128 x, int128 y) internal pure returns (int128) {
int256 result = int256(x) + y;
require (result >= MIN_64x64 && result <= MAX_64x64);
return int128 (result);
}
function sub (int128 x, int128 y) internal pure returns (int128) {
int256 result = int256(x) - y;
require (result >= MIN_64x64 && result <= MAX_64x64);
return int128 (result);
}
function mul (int128 x, int128 y) internal pure returns (int128) {
int256 result = int256(x) * y >> 64;
require (result >= MIN_64x64 && result <= MAX_64x64);
return int128 (result);
}
function muli (int128 x, int256 y) internal pure returns (int256) {
if (x == MIN_64x64) {
require (y >= -0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF &&
y <= 0x1000000000000000000000000000000000000000000000000);
return -y << 63;
} else {
bool negativeResult = false;
if (x < 0) {
x = -x;
negativeResult = true;
}
if (y < 0) {
y = -y;
negativeResult = !negativeResult;
}
uint256 absoluteResult = mulu (x, uint256 (y));
if (negativeResult) {
require (absoluteResult <=
0x8000000000000000000000000000000000000000000000000000000000000000);
return -int256 (absoluteResult);
} else {
require (absoluteResult <=
0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
return int256 (absoluteResult);
}
}
}
function mulu (int128 x, uint256 y) internal pure returns (uint256) {
if (y == 0) return 0;
require (x >= 0);
uint256 lo = (uint256 (x) * (y & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)) >> 64;
uint256 hi = uint256 (x) * (y >> 128);
require (hi <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
hi <<= 64;
require (hi <=
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF - lo);
return hi + lo;
}
function div (int128 x, int128 y) internal pure returns (int128) {
require (y != 0);
int256 result = (int256 (x) << 64) / y;
require (result >= MIN_64x64 && result <= MAX_64x64);
return int128 (result);
}
function divi (int256 x, int256 y) internal pure returns (int128) {
require (y != 0);
bool negativeResult = false;
if (x < 0) {
x = -x;
negativeResult = true;
}
if (y < 0) {
y = -y;
negativeResult = !negativeResult;
}
uint128 absoluteResult = divuu (uint256 (x), uint256 (y));
if (negativeResult) {
require (absoluteResult <= 0x80000000000000000000000000000000);
return -int128 (absoluteResult);
} else {
require (absoluteResult <= 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
return int128 (absoluteResult);
}
}
function divu (uint256 x, uint256 y) internal pure returns (int128) {
require (y != 0);
uint128 result = divuu (x, y);
require (result <= uint128 (MAX_64x64));
return int128 (result);
}
function neg (int128 x) internal pure returns (int128) {
require (x != MIN_64x64);
return -x;
}
function abs (int128 x) internal pure returns (int128) {
require (x != MIN_64x64);
return x < 0 ? -x : x;
}
function inv (int128 x) internal pure returns (int128) {
require (x != 0);
int256 result = int256 (0x100000000000000000000000000000000) / x;
require (result >= MIN_64x64 && result <= MAX_64x64);
return int128 (result);
}
function avg (int128 x, int128 y) internal pure returns (int128) {
return int128 ((int256 (x) + int256 (y)) >> 1);
}
function gavg (int128 x, int128 y) internal pure returns (int128) {
int256 m = int256 (x) * int256 (y);
require (m >= 0);
require (m <
0x4000000000000000000000000000000000000000000000000000000000000000);
return int128 (sqrtu (uint256 (m)));
}
function pow (int128 x, uint256 y) internal pure returns (int128) {
uint256 absoluteResult;
bool negativeResult = false;
if (x >= 0) {
absoluteResult = powu (uint256 (x) << 63, y);
} else {
absoluteResult = powu (uint256 (uint128 (-x)) << 63, y);
negativeResult = y & 1 > 0;
}
absoluteResult >>= 63;
if (negativeResult) {
require (absoluteResult <= 0x80000000000000000000000000000000);
return -int128 (absoluteResult);
} else {
require (absoluteResult <= 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
return int128 (absoluteResult);
}
}
function sqrt (int128 x) internal pure returns (int128) {
require (x >= 0);
return int128 (sqrtu (uint256 (x) << 64));
}
function log_2 (int128 x) internal pure returns (int128) {
require (x > 0);
int256 msb = 0;
int256 xc = x;
if (xc >= 0x10000000000000000) { xc >>= 64; msb += 64; }
if (xc >= 0x100000000) { xc >>= 32; msb += 32; }
if (xc >= 0x10000) { xc >>= 16; msb += 16; }
if (xc >= 0x100) { xc >>= 8; msb += 8; }
if (xc >= 0x10) { xc >>= 4; msb += 4; }
if (xc >= 0x4) { xc >>= 2; msb += 2; }
if (xc >= 0x2) msb += 1;
int256 result = msb - 64 << 64;
uint256 ux = uint256 (x) << uint256 (127 - msb);
for (int256 bit = 0x8000000000000000; bit > 0; bit >>= 1) {
ux *= ux;
uint256 b = ux >> 255;
ux >>= 127 + b;
result += bit * int256 (b);
}
return int128 (result);
}
function ln (int128 x) internal pure returns (int128) {
require (x > 0);
return int128 (
uint256 (log_2 (x)) * 0xB17217F7D1CF79ABC9E3B39803F2F6AF >> 128);
}
function exp_2 (int128 x) internal pure returns (int128) {
require (x < 0x400000000000000000);
if (x < -0x400000000000000000) return 0;
uint256 result = 0x80000000000000000000000000000000;
if (x & 0x8000000000000000 > 0)
result = result * 0x16A09E667F3BCC908B2FB1366EA957D3E >> 128;
if (x & 0x4000000000000000 > 0)
result = result * 0x1306FE0A31B7152DE8D5A46305C85EDEC >> 128;
if (x & 0x2000000000000000 > 0)
result = result * 0x1172B83C7D517ADCDF7C8C50EB14A791F >> 128;
if (x & 0x1000000000000000 > 0)
result = result * 0x10B5586CF9890F6298B92B71842A98363 >> 128;
if (x & 0x800000000000000 > 0)
result = result * 0x1059B0D31585743AE7C548EB68CA417FD >> 128;
if (x & 0x400000000000000 > 0)
result = result * 0x102C9A3E778060EE6F7CACA4F7A29BDE8 >> 128;
if (x & 0x200000000000000 > 0)
result = result * 0x10163DA9FB33356D84A66AE336DCDFA3F >> 128;
if (x & 0x100000000000000 > 0)
result = result * 0x100B1AFA5ABCBED6129AB13EC11DC9543 >> 128;
if (x & 0x80000000000000 > 0)
result = result * 0x10058C86DA1C09EA1FF19D294CF2F679B >> 128;
if (x & 0x40000000000000 > 0)
result = result * 0x1002C605E2E8CEC506D21BFC89A23A00F >> 128;
if (x & 0x20000000000000 > 0)
result = result * 0x100162F3904051FA128BCA9C55C31E5DF >> 128;
if (x & 0x10000000000000 > 0)
result = result * 0x1000B175EFFDC76BA38E31671CA939725 >> 128;
if (x & 0x8000000000000 > 0)
result = result * 0x100058BA01FB9F96D6CACD4B180917C3D >> 128;
if (x & 0x4000000000000 > 0)
result = result * 0x10002C5CC37DA9491D0985C348C68E7B3 >> 128;
if (x & 0x2000000000000 > 0)
result = result * 0x1000162E525EE054754457D5995292026 >> 128;
if (x & 0x1000000000000 > 0)
result = result * 0x10000B17255775C040618BF4A4ADE83FC >> 128;
if (x & 0x800000000000 > 0)
result = result * 0x1000058B91B5BC9AE2EED81E9B7D4CFAB >> 128;
if (x & 0x400000000000 > 0)
result = result * 0x100002C5C89D5EC6CA4D7C8ACC017B7C9 >> 128;
if (x & 0x200000000000 > 0)
result = result * 0x10000162E43F4F831060E02D839A9D16D >> 128;
if (x & 0x100000000000 > 0)
result = result * 0x100000B1721BCFC99D9F890EA06911763 >> 128;
if (x & 0x80000000000 > 0)
result = result * 0x10000058B90CF1E6D97F9CA14DBCC1628 >> 128;
if (x & 0x40000000000 > 0)
result = result * 0x1000002C5C863B73F016468F6BAC5CA2B >> 128;
if (x & 0x20000000000 > 0)
result = result * 0x100000162E430E5A18F6119E3C02282A5 >> 128;
if (x & 0x10000000000 > 0)
result = result * 0x1000000B1721835514B86E6D96EFD1BFE >> 128;
if (x & 0x8000000000 > 0)
result = result * 0x100000058B90C0B48C6BE5DF846C5B2EF >> 128;
if (x & 0x4000000000 > 0)
result = result * 0x10000002C5C8601CC6B9E94213C72737A >> 128;
if (x & 0x2000000000 > 0)
result = result * 0x1000000162E42FFF037DF38AA2B219F06 >> 128;
if (x & 0x1000000000 > 0)
result = result * 0x10000000B17217FBA9C739AA5819F44F9 >> 128;
if (x & 0x800000000 > 0)
result = result * 0x1000000058B90BFCDEE5ACD3C1CEDC823 >> 128;
if (x & 0x400000000 > 0)
result = result * 0x100000002C5C85FE31F35A6A30DA1BE50 >> 128;
if (x & 0x200000000 > 0)
result = result * 0x10000000162E42FF0999CE3541B9FFFCF >> 128;
if (x & 0x100000000 > 0)
result = result * 0x100000000B17217F80F4EF5AADDA45554 >> 128;
if (x & 0x80000000 > 0)
result = result * 0x10000000058B90BFBF8479BD5A81B51AD >> 128;
if (x & 0x40000000 > 0)
result = result * 0x1000000002C5C85FDF84BD62AE30A74CC >> 128;
if (x & 0x20000000 > 0)
result = result * 0x100000000162E42FEFB2FED257559BDAA >> 128;
if (x & 0x10000000 > 0)
result = result * 0x1000000000B17217F7D5A7716BBA4A9AE >> 128;
if (x & 0x8000000 > 0)
result = result * 0x100000000058B90BFBE9DDBAC5E109CCE >> 128;
if (x & 0x4000000 > 0)
result = result * 0x10000000002C5C85FDF4B15DE6F17EB0D >> 128;
if (x & 0x2000000 > 0)
result = result * 0x1000000000162E42FEFA494F1478FDE05 >> 128;
if (x & 0x1000000 > 0)
result = result * 0x10000000000B17217F7D20CF927C8E94C >> 128;
if (x & 0x800000 > 0)
result = result * 0x1000000000058B90BFBE8F71CB4E4B33D >> 128;
if (x & 0x400000 > 0)
result = result * 0x100000000002C5C85FDF477B662B26945 >> 128;
if (x & 0x200000 > 0)
result = result * 0x10000000000162E42FEFA3AE53369388C >> 128;
if (x & 0x100000 > 0)
result = result * 0x100000000000B17217F7D1D351A389D40 >> 128;
if (x & 0x80000 > 0)
result = result * 0x10000000000058B90BFBE8E8B2D3D4EDE >> 128;
if (x & 0x40000 > 0)
result = result * 0x1000000000002C5C85FDF4741BEA6E77E >> 128;
if (x & 0x20000 > 0)
result = result * 0x100000000000162E42FEFA39FE95583C2 >> 128;
if (x & 0x10000 > 0)
result = result * 0x1000000000000B17217F7D1CFB72B45E1 >> 128;
if (x & 0x8000 > 0)
result = result * 0x100000000000058B90BFBE8E7CC35C3F0 >> 128;
if (x & 0x4000 > 0)
result = result * 0x10000000000002C5C85FDF473E242EA38 >> 128;
if (x & 0x2000 > 0)
result = result * 0x1000000000000162E42FEFA39F02B772C >> 128;
if (x & 0x1000 > 0)
result = result * 0x10000000000000B17217F7D1CF7D83C1A >> 128;
if (x & 0x800 > 0)
result = result * 0x1000000000000058B90BFBE8E7BDCBE2E >> 128;
if (x & 0x400 > 0)
result = result * 0x100000000000002C5C85FDF473DEA871F >> 128;
if (x & 0x200 > 0)
result = result * 0x10000000000000162E42FEFA39EF44D91 >> 128;
if (x & 0x100 > 0)
result = result * 0x100000000000000B17217F7D1CF79E949 >> 128;
if (x & 0x80 > 0)
result = result * 0x10000000000000058B90BFBE8E7BCE544 >> 128;
if (x & 0x40 > 0)
result = result * 0x1000000000000002C5C85FDF473DE6ECA >> 128;
if (x & 0x20 > 0)
result = result * 0x100000000000000162E42FEFA39EF366F >> 128;
if (x & 0x10 > 0)
result = result * 0x1000000000000000B17217F7D1CF79AFA >> 128;
if (x & 0x8 > 0)
result = result * 0x100000000000000058B90BFBE8E7BCD6D >> 128;
if (x & 0x4 > 0)
result = result * 0x10000000000000002C5C85FDF473DE6B2 >> 128;
if (x & 0x2 > 0)
result = result * 0x1000000000000000162E42FEFA39EF358 >> 128;
if (x & 0x1 > 0)
result = result * 0x10000000000000000B17217F7D1CF79AB >> 128;
result >>= uint256 (63 - (x >> 64));
require (result <= uint256 (MAX_64x64));
return int128 (result);
}
function exp (int128 x) internal pure returns (int128) {
require (x < 0x400000000000000000);
if (x < -0x400000000000000000) return 0;
return exp_2 (
int128 (int256 (x) * 0x171547652B82FE1777D0FFDA0D23A7D12 >> 128));
}
function divuu (uint256 x, uint256 y) private pure returns (uint128) {
require (y != 0);
uint256 result;
if (x <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
result = (x << 64) / y;
else {
uint256 msb = 192;
uint256 xc = x >> 192;
if (xc >= 0x100000000) { xc >>= 32; msb += 32; }
if (xc >= 0x10000) { xc >>= 16; msb += 16; }
if (xc >= 0x100) { xc >>= 8; msb += 8; }
if (xc >= 0x10) { xc >>= 4; msb += 4; }
if (xc >= 0x4) { xc >>= 2; msb += 2; }
if (xc >= 0x2) msb += 1;
result = (x << 255 - msb) / ((y - 1 >> msb - 191) + 1);
require (result <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
uint256 hi = result * (y >> 128);
uint256 lo = result * (y & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
uint256 xh = x >> 192;
uint256 xl = x << 64;
if (xl < lo) xh -= 1;
xl -= lo;
lo = hi << 128;
if (xl < lo) xh -= 1;
xl -= lo;
assert (xh == hi >> 128);
result += xl / y;
}
require (result <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
return uint128 (result);
}
function powu (uint256 x, uint256 y) private pure returns (uint256) {
if (y == 0) return 0x80000000000000000000000000000000;
else if (x == 0) return 0;
else {
int256 msb = 0;
uint256 xc = x;
if (xc >= 0x100000000000000000000000000000000) { xc >>= 128; msb += 128; }
if (xc >= 0x10000000000000000) { xc >>= 64; msb += 64; }
if (xc >= 0x100000000) { xc >>= 32; msb += 32; }
if (xc >= 0x10000) { xc >>= 16; msb += 16; }
if (xc >= 0x100) { xc >>= 8; msb += 8; }
if (xc >= 0x10) { xc >>= 4; msb += 4; }
if (xc >= 0x4) { xc >>= 2; msb += 2; }
if (xc >= 0x2) msb += 1;
int256 xe = msb - 127;
if (xe > 0) x >>= uint256 (xe);
else x <<= uint256 (-xe);
uint256 result = 0x80000000000000000000000000000000;
int256 re = 0;
while (y > 0) {
if (y & 1 > 0) {
result = result * x;
y -= 1;
re += xe;
if (result >=
0x8000000000000000000000000000000000000000000000000000000000000000) {
result >>= 128;
re += 1;
} else result >>= 127;
if (re < -127) return 0;
require (re < 128);
} else {
x = x * x;
y >>= 1;
xe <<= 1;
if (x >=
0x8000000000000000000000000000000000000000000000000000000000000000) {
x >>= 128;
xe += 1;
} else x >>= 127;
if (xe < -127) return 0;
require (xe < 128);
}
}
if (re > 0) result <<= uint256 (re);
else if (re < 0) result >>= uint256 (-re);
return result;
}
}
function sqrtu (uint256 x) private pure returns (uint128) {
if (x == 0) return 0;
else {
uint256 xx = x;
uint256 r = 1;
if (xx >= 0x100000000000000000000000000000000) { xx >>= 128; r <<= 64; }
if (xx >= 0x10000000000000000) { xx >>= 64; r <<= 32; }
if (xx >= 0x100000000) { xx >>= 32; r <<= 16; }
if (xx >= 0x10000) { xx >>= 16; r <<= 8; }
if (xx >= 0x100) { xx >>= 8; r <<= 4; }
if (xx >= 0x10) { xx >>= 4; r <<= 2; }
if (xx >= 0x8) { r <<= 1; }
r = (r + x / r) >> 1;
r = (r + x / r) >> 1;
r = (r + x / r) >> 1;
r = (r + x / r) >> 1;
r = (r + x / r) >> 1;
r = (r + x / r) >> 1;
r = (r + x / r) >> 1;
uint256 r1 = x / r;
return uint128 (r < r1 ? r : r1);
}
}
}
文件 2 的 21:Address.sol
pragma solidity >=0.6.2 <0.8.0;
library Address {
function isContract(address account) internal view returns (bool) {
uint256 size;
assembly { size := extcodesize(account) }
return size > 0;
}
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{ value: amount }("");
require(success, "Address: unable to send value, recipient may have reverted");
}
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCall(target, data, "Address: low-level call failed");
}
function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
require(isContract(target), "Address: call to non-contract");
(bool success, bytes memory returndata) = target.call{ value: value }(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
function functionStaticCall(address target, bytes memory data, string memory errorMessage) internal view returns (bytes memory) {
require(isContract(target), "Address: static call to non-contract");
(bool success, bytes memory returndata) = target.staticcall(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function _verifyCallResult(bool success, bytes memory returndata, string memory errorMessage) private pure returns(bytes memory) {
if (success) {
return returndata;
} else {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
文件 3 的 21:Assimilators.sol
pragma solidity ^0.7.3;
import "./Address.sol";
import "./IAssimilator.sol";
import "./ABDKMath64x64.sol";
library Assimilators {
using ABDKMath64x64 for int128;
using Address for address;
IAssimilator public constant iAsmltr = IAssimilator(address(0));
function delegate(address _callee, bytes memory _data) internal returns (bytes memory) {
require(_callee.isContract(), "Assimilators/callee-is-not-a-contract");
(bool _success, bytes memory returnData_) = _callee.delegatecall(_data);
assembly {
if eq(_success, 0) {
revert(add(returnData_, 0x20), returndatasize())
}
}
return returnData_;
}
function getRate(address _assim) internal view returns (uint256 amount_) {
amount_ = IAssimilator(_assim).getRate();
}
function viewRawAmount(address _assim, int128 _amt) internal view returns (uint256 amount_) {
amount_ = IAssimilator(_assim).viewRawAmount(_amt);
}
function viewRawAmountLPRatio(
address _assim,
uint256 _baseWeight,
uint256 _quoteWeight,
int128 _amount
) internal view returns (uint256 amount_) {
amount_ = IAssimilator(_assim).viewRawAmountLPRatio(_baseWeight, _quoteWeight, address(this), _amount);
}
function viewNumeraireAmount(address _assim, uint256 _amt) internal view returns (int128 amt_) {
amt_ = IAssimilator(_assim).viewNumeraireAmount(_amt);
}
function viewNumeraireAmountAndBalance(address _assim, uint256 _amt)
internal
view
returns (int128 amt_, int128 bal_)
{
(amt_, bal_) = IAssimilator(_assim).viewNumeraireAmountAndBalance(address(this), _amt);
}
function viewNumeraireBalance(address _assim) internal view returns (int128 bal_) {
bal_ = IAssimilator(_assim).viewNumeraireBalance(address(this));
}
function viewNumeraireBalanceLPRatio(
uint256 _baseWeight,
uint256 _quoteWeight,
address _assim
) internal view returns (int128 bal_) {
bal_ = IAssimilator(_assim).viewNumeraireBalanceLPRatio(_baseWeight, _quoteWeight, address(this));
}
function intakeRaw(address _assim, uint256 _amt) internal returns (int128 amt_) {
bytes memory data = abi.encodeWithSelector(iAsmltr.intakeRaw.selector, _amt);
amt_ = abi.decode(delegate(_assim, data), (int128));
}
function intakeRawAndGetBalance(address _assim, uint256 _amt) internal returns (int128 amt_, int128 bal_) {
bytes memory data = abi.encodeWithSelector(iAsmltr.intakeRawAndGetBalance.selector, _amt);
(amt_, bal_) = abi.decode(delegate(_assim, data), (int128, int128));
}
function intakeNumeraire(address _assim, int128 _amt) internal returns (uint256 amt_) {
bytes memory data = abi.encodeWithSelector(iAsmltr.intakeNumeraire.selector, _amt);
amt_ = abi.decode(delegate(_assim, data), (uint256));
}
function intakeNumeraireLPRatio(
address _assim,
uint256 _baseWeight,
uint256 _quoteWeight,
int128 _amount
) internal returns (uint256 amt_) {
bytes memory data =
abi.encodeWithSelector(
iAsmltr.intakeNumeraireLPRatio.selector,
_baseWeight,
_quoteWeight,
address(this),
_amount
);
amt_ = abi.decode(delegate(_assim, data), (uint256));
}
function outputRaw(
address _assim,
address _dst,
uint256 _amt
) internal returns (int128 amt_) {
bytes memory data = abi.encodeWithSelector(iAsmltr.outputRaw.selector, _dst, _amt);
amt_ = abi.decode(delegate(_assim, data), (int128));
amt_ = amt_.neg();
}
function outputRawAndGetBalance(
address _assim,
address _dst,
uint256 _amt
) internal returns (int128 amt_, int128 bal_) {
bytes memory data = abi.encodeWithSelector(iAsmltr.outputRawAndGetBalance.selector, _dst, _amt);
(amt_, bal_) = abi.decode(delegate(_assim, data), (int128, int128));
amt_ = amt_.neg();
}
function outputNumeraire(
address _assim,
address _dst,
int128 _amt
) internal returns (uint256 amt_) {
bytes memory data = abi.encodeWithSelector(iAsmltr.outputNumeraire.selector, _dst, _amt.abs());
amt_ = abi.decode(delegate(_assim, data), (uint256));
}
}
文件 4 的 21:Context.sol
pragma solidity >=0.6.0 <0.8.0;
abstract contract Context {
function _msgSender() internal view virtual returns (address payable) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes memory) {
this;
return msg.data;
}
}
文件 5 的 21:Curve.sol
pragma solidity ^0.7.3;
import "./ABDKMath64x64.sol";
import "./Orchestrator.sol";
import "./ProportionalLiquidity.sol";
import "./Swaps.sol";
import "./ViewLiquidity.sol";
import "./Storage.sol";
import "./MerkleProver.sol";
import "./IFreeFromUpTo.sol";
library Curves {
using ABDKMath64x64 for int128;
event Approval(address indexed _owner, address indexed spender, uint256 value);
event Transfer(address indexed from, address indexed to, uint256 value);
function add(
uint256 x,
uint256 y,
string memory errorMessage
) private pure returns (uint256 z) {
require((z = x + y) >= x, errorMessage);
}
function sub(
uint256 x,
uint256 y,
string memory errorMessage
) private pure returns (uint256 z) {
require((z = x - y) <= x, errorMessage);
}
function transfer(
Storage.Curve storage curve,
address recipient,
uint256 amount
) external returns (bool) {
_transfer(curve, msg.sender, recipient, amount);
return true;
}
function approve(
Storage.Curve storage curve,
address spender,
uint256 amount
) external returns (bool) {
_approve(curve, msg.sender, spender, amount);
return true;
}
function transferFrom(
Storage.Curve storage curve,
address sender,
address recipient,
uint256 amount
) external returns (bool) {
_transfer(curve, sender, recipient, amount);
_approve(
curve,
sender,
msg.sender,
sub(curve.allowances[sender][msg.sender], amount, "Curve/insufficient-allowance")
);
return true;
}
function increaseAllowance(
Storage.Curve storage curve,
address spender,
uint256 addedValue
) external returns (bool) {
_approve(
curve,
msg.sender,
spender,
add(curve.allowances[msg.sender][spender], addedValue, "Curve/approval-overflow")
);
return true;
}
function decreaseAllowance(
Storage.Curve storage curve,
address spender,
uint256 subtractedValue
) external returns (bool) {
_approve(
curve,
msg.sender,
spender,
sub(curve.allowances[msg.sender][spender], subtractedValue, "Curve/allowance-decrease-underflow")
);
return true;
}
function _transfer(
Storage.Curve storage curve,
address sender,
address recipient,
uint256 amount
) private {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");
curve.balances[sender] = sub(curve.balances[sender], amount, "Curve/insufficient-balance");
curve.balances[recipient] = add(curve.balances[recipient], amount, "Curve/transfer-overflow");
emit Transfer(sender, recipient, amount);
}
function _approve(
Storage.Curve storage curve,
address _owner,
address spender,
uint256 amount
) private {
require(_owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
curve.allowances[_owner][spender] = amount;
emit Approval(_owner, spender, amount);
}
}
contract Curve is Storage, MerkleProver {
using SafeMath for uint256;
event Approval(address indexed _owner, address indexed spender, uint256 value);
event ParametersSet(uint256 alpha, uint256 beta, uint256 delta, uint256 epsilon, uint256 lambda);
event AssetIncluded(address indexed numeraire, address indexed reserve, uint256 weight);
event AssimilatorIncluded(
address indexed derivative,
address indexed numeraire,
address indexed reserve,
address assimilator
);
event PartitionRedeemed(address indexed token, address indexed redeemer, uint256 value);
event OwnershipTransfered(address indexed previousOwner, address indexed newOwner);
event FrozenSet(bool isFrozen);
event EmergencyAlarm(bool isEmergency);
event WhitelistingStopped();
event Trade(
address indexed trader,
address indexed origin,
address indexed target,
uint256 originAmount,
uint256 targetAmount
);
event Transfer(address indexed from, address indexed to, uint256 value);
modifier onlyOwner() {
require(msg.sender == owner, "Curve/caller-is-not-owner");
_;
}
modifier nonReentrant() {
require(notEntered, "Curve/re-entered");
notEntered = false;
_;
notEntered = true;
}
modifier transactable() {
require(!frozen, "Curve/frozen-only-allowing-proportional-withdraw");
_;
}
modifier isEmergency() {
require(emergency, "Curve/emergency-only-allowing-emergency-proportional-withdraw");
_;
}
modifier deadline(uint256 _deadline) {
require(block.timestamp < _deadline, "Curve/tx-deadline-passed");
_;
}
modifier inWhitelistingStage() {
require(whitelistingStage, "Curve/whitelist-stage-on-going");
_;
}
modifier notInWhitelistingStage() {
require(!whitelistingStage, "Curve/whitelist-stage-stopped");
_;
}
constructor(
string memory _name,
string memory _symbol,
address[] memory _assets,
uint256[] memory _assetWeights
) {
owner = msg.sender;
name = _name;
symbol = _symbol;
emit OwnershipTransfered(address(0), msg.sender);
Orchestrator.initialize(curve, numeraires, reserves, derivatives, _assets, _assetWeights);
}
function setParams(
uint256 _alpha,
uint256 _beta,
uint256 _feeAtHalt,
uint256 _epsilon,
uint256 _lambda
) external onlyOwner {
Orchestrator.setParams(curve, _alpha, _beta, _feeAtHalt, _epsilon, _lambda);
}
function excludeDerivative(address _derivative) external onlyOwner {
for (uint256 i = 0; i < numeraires.length; i++) {
if (_derivative == numeraires[i]) revert("Curve/cannot-delete-numeraire");
if (_derivative == reserves[i]) revert("Curve/cannot-delete-reserve");
}
delete curve.assimilators[_derivative];
}
function viewCurve()
external
view
returns (
uint256 alpha_,
uint256 beta_,
uint256 delta_,
uint256 epsilon_,
uint256 lambda_
)
{
return Orchestrator.viewCurve(curve);
}
function turnOffWhitelisting() external onlyOwner {
emit WhitelistingStopped();
whitelistingStage = false;
}
function setEmergency(bool _emergency) external onlyOwner {
emit EmergencyAlarm(_emergency);
emergency = _emergency;
}
function setFrozen(bool _toFreezeOrNotToFreeze) external onlyOwner {
emit FrozenSet(_toFreezeOrNotToFreeze);
frozen = _toFreezeOrNotToFreeze;
}
function transferOwnership(address _newOwner) external onlyOwner {
require(_newOwner != address(0), "Curve/new-owner-cannot-be-zeroth-address");
emit OwnershipTransfered(owner, _newOwner);
owner = _newOwner;
}
function originSwap(
address _origin,
address _target,
uint256 _originAmount,
uint256 _minTargetAmount,
uint256 _deadline
) external deadline(_deadline) transactable nonReentrant returns (uint256 targetAmount_) {
targetAmount_ = Swaps.originSwap(curve, _origin, _target, _originAmount, msg.sender);
require(targetAmount_ >= _minTargetAmount, "Curve/below-min-target-amount");
}
function viewOriginSwap(
address _origin,
address _target,
uint256 _originAmount
) external view transactable returns (uint256 targetAmount_) {
targetAmount_ = Swaps.viewOriginSwap(curve, _origin, _target, _originAmount);
}
function targetSwap(
address _origin,
address _target,
uint256 _maxOriginAmount,
uint256 _targetAmount,
uint256 _deadline
) external deadline(_deadline) transactable nonReentrant returns (uint256 originAmount_) {
originAmount_ = Swaps.targetSwap(curve, _origin, _target, _targetAmount, msg.sender);
require(originAmount_ <= _maxOriginAmount, "Curve/above-max-origin-amount");
}
function viewTargetSwap(
address _origin,
address _target,
uint256 _targetAmount
) external view transactable returns (uint256 originAmount_) {
originAmount_ = Swaps.viewTargetSwap(curve, _origin, _target, _targetAmount);
}
function depositWithWhitelist(
uint256 index,
address account,
uint256 amount,
bytes32[] calldata merkleProof,
uint256 _deposit,
uint256 _deadline
) external deadline(_deadline) transactable nonReentrant inWhitelistingStage returns (uint256, uint256[] memory) {
require(isWhitelisted(index, account, amount, merkleProof), "Curve/not-whitelisted");
require(msg.sender == account, "Curve/not-approved-user");
(uint256 curvesMinted_, uint256[] memory deposits_) =
ProportionalLiquidity.proportionalDeposit(curve, _deposit);
whitelistedDeposited[msg.sender] = whitelistedDeposited[msg.sender].add(curvesMinted_);
if (whitelistedDeposited[msg.sender] > 10000e18) {
revert("Curve/exceed-whitelist-maximum-deposit");
}
return (curvesMinted_, deposits_);
}
function deposit(uint256 _deposit, uint256 _deadline)
external
deadline(_deadline)
transactable
nonReentrant
notInWhitelistingStage
returns (uint256, uint256[] memory)
{
return ProportionalLiquidity.proportionalDeposit(curve, _deposit);
}
function viewDeposit(uint256 _deposit) external view transactable returns (uint256, uint256[] memory) {
return ProportionalLiquidity.viewProportionalDeposit(curve, _deposit);
}
function emergencyWithdraw(uint256 _curvesToBurn, uint256 _deadline)
external
isEmergency
deadline(_deadline)
nonReentrant
returns (uint256[] memory withdrawals_)
{
return ProportionalLiquidity.emergencyProportionalWithdraw(curve, _curvesToBurn);
}
function withdraw(uint256 _curvesToBurn, uint256 _deadline)
external
deadline(_deadline)
nonReentrant
returns (uint256[] memory withdrawals_)
{
if (whitelistingStage) {
whitelistedDeposited[msg.sender] = whitelistedDeposited[msg.sender].sub(_curvesToBurn);
}
return ProportionalLiquidity.proportionalWithdraw(curve, _curvesToBurn);
}
function viewWithdraw(uint256 _curvesToBurn) external view transactable returns (uint256[] memory) {
return ProportionalLiquidity.viewProportionalWithdraw(curve, _curvesToBurn);
}
function supportsInterface(bytes4 _interface) public pure returns (bool supports_) {
supports_ =
this.supportsInterface.selector == _interface ||
bytes4(0x7f5828d0) == _interface ||
bytes4(0x36372b07) == _interface;
}
function transfer(address _recipient, uint256 _amount) public nonReentrant returns (bool success_) {
success_ = Curves.transfer(curve, _recipient, _amount);
}
function transferFrom(
address _sender,
address _recipient,
uint256 _amount
) public nonReentrant returns (bool success_) {
success_ = Curves.transferFrom(curve, _sender, _recipient, _amount);
}
function approve(address _spender, uint256 _amount) public nonReentrant returns (bool success_) {
success_ = Curves.approve(curve, _spender, _amount);
}
function balanceOf(address _account) public view returns (uint256 balance_) {
balance_ = curve.balances[_account];
}
function totalSupply() public view returns (uint256 totalSupply_) {
totalSupply_ = curve.totalSupply;
}
function allowance(address _owner, address _spender) public view returns (uint256 allowance_) {
allowance_ = curve.allowances[_owner][_spender];
}
function liquidity() public view returns (uint256 total_, uint256[] memory individual_) {
return ViewLiquidity.viewLiquidity(curve);
}
function assimilator(address _derivative) public view returns (address assimilator_) {
assimilator_ = curve.assimilators[_derivative].addr;
}
}
文件 6 的 21:CurveMath.sol
pragma solidity ^0.7.3;
import "./Storage.sol";
import "./UnsafeMath64x64.sol";
import "./ABDKMath64x64.sol";
library CurveMath {
int128 private constant ONE = 0x10000000000000000;
int128 private constant MAX = 0x4000000000000000;
int128 private constant MAX_DIFF = -0x10C6F7A0B5EE;
int128 private constant ONE_WEI = 0x12;
using ABDKMath64x64 for int128;
using UnsafeMath64x64 for int128;
using ABDKMath64x64 for uint256;
function calculateFee(
int128 _gLiq,
int128[] memory _bals,
Storage.Curve storage curve,
int128[] memory _weights
) internal view returns (int128 psi_) {
int128 _beta = curve.beta;
int128 _delta = curve.delta;
psi_ = calculateFee(_gLiq, _bals, _beta, _delta, _weights);
}
function calculateFee(
int128 _gLiq,
int128[] memory _bals,
int128 _beta,
int128 _delta,
int128[] memory _weights
) internal pure returns (int128 psi_) {
uint256 _length = _bals.length;
for (uint256 i = 0; i < _length; i++) {
int128 _ideal = _gLiq.mul(_weights[i]);
psi_ += calculateMicroFee(_bals[i], _ideal, _beta, _delta);
}
}
function calculateMicroFee(
int128 _bal,
int128 _ideal,
int128 _beta,
int128 _delta
) private pure returns (int128 fee_) {
if (_bal < _ideal) {
int128 _threshold = _ideal.mul(ONE - _beta);
if (_bal < _threshold) {
int128 _feeMargin = _threshold - _bal;
fee_ = _feeMargin.div(_ideal);
fee_ = fee_.mul(_delta);
if (fee_ > MAX) fee_ = MAX;
fee_ = fee_.mul(_feeMargin);
} else fee_ = 0;
} else {
int128 _threshold = _ideal.mul(ONE + _beta);
if (_bal > _threshold) {
int128 _feeMargin = _bal - _threshold;
fee_ = _feeMargin.div(_ideal);
fee_ = fee_.mul(_delta);
if (fee_ > MAX) fee_ = MAX;
fee_ = fee_.mul(_feeMargin);
} else fee_ = 0;
}
}
function calculateTrade(
Storage.Curve storage curve,
int128 _oGLiq,
int128 _nGLiq,
int128[] memory _oBals,
int128[] memory _nBals,
int128 _inputAmt,
uint256 _outputIndex
) internal view returns (int128 outputAmt_) {
outputAmt_ = -_inputAmt;
int128 _lambda = curve.lambda;
int128[] memory _weights = curve.weights;
int128 _omega = calculateFee(_oGLiq, _oBals, curve, _weights);
int128 _psi;
for (uint256 i = 0; i < 32; i++) {
_psi = calculateFee(_nGLiq, _nBals, curve, _weights);
int128 prevAmount;
{
prevAmount = outputAmt_;
outputAmt_ = _omega < _psi ? -(_inputAmt + _omega - _psi) : -(_inputAmt + _lambda.mul(_omega - _psi));
}
if (outputAmt_ / 1e13 == prevAmount / 1e13) {
_nGLiq = _oGLiq + _inputAmt + outputAmt_;
_nBals[_outputIndex] = _oBals[_outputIndex] + outputAmt_;
enforceHalts(curve, _oGLiq, _nGLiq, _oBals, _nBals, _weights);
enforceSwapInvariant(_oGLiq, _omega, _nGLiq, _psi);
return outputAmt_;
} else {
_nGLiq = _oGLiq + _inputAmt + outputAmt_;
_nBals[_outputIndex] = _oBals[_outputIndex].add(outputAmt_);
}
}
revert("Curve/swap-convergence-failed");
}
function calculateLiquidityMembrane(
Storage.Curve storage curve,
int128 _oGLiq,
int128 _nGLiq,
int128[] memory _oBals,
int128[] memory _nBals
) internal view returns (int128 curves_) {
enforceHalts(curve, _oGLiq, _nGLiq, _oBals, _nBals, curve.weights);
int128 _omega;
int128 _psi;
{
int128 _beta = curve.beta;
int128 _delta = curve.delta;
int128[] memory _weights = curve.weights;
_omega = calculateFee(_oGLiq, _oBals, _beta, _delta, _weights);
_psi = calculateFee(_nGLiq, _nBals, _beta, _delta, _weights);
}
int128 _feeDiff = _psi.sub(_omega);
int128 _liqDiff = _nGLiq.sub(_oGLiq);
int128 _oUtil = _oGLiq.sub(_omega);
int128 _totalShells = curve.totalSupply.divu(1e18);
int128 _curveMultiplier;
if (_totalShells == 0) {
curves_ = _nGLiq.sub(_psi);
} else if (_feeDiff >= 0) {
_curveMultiplier = _liqDiff.sub(_feeDiff).div(_oUtil);
} else {
_curveMultiplier = _liqDiff.sub(curve.lambda.mul(_feeDiff));
_curveMultiplier = _curveMultiplier.div(_oUtil);
}
if (_totalShells != 0) {
curves_ = _totalShells.mul(_curveMultiplier);
enforceLiquidityInvariant(_totalShells, curves_, _oGLiq, _nGLiq, _omega, _psi);
}
}
function enforceSwapInvariant(
int128 _oGLiq,
int128 _omega,
int128 _nGLiq,
int128 _psi
) private pure {
int128 _nextUtil = _nGLiq - _psi;
int128 _prevUtil = _oGLiq - _omega;
int128 _diff = _nextUtil - _prevUtil;
require(0 < _diff || _diff >= MAX_DIFF, "Curve/swap-invariant-violation");
}
function enforceLiquidityInvariant(
int128 _totalShells,
int128 _newShells,
int128 _oGLiq,
int128 _nGLiq,
int128 _omega,
int128 _psi
) internal pure {
if (_totalShells == 0 || 0 == _totalShells + _newShells) return;
int128 _prevUtilPerShell = _oGLiq.sub(_omega).div(_totalShells);
int128 _nextUtilPerShell = _nGLiq.sub(_psi).div(_totalShells.add(_newShells));
int128 _diff = _nextUtilPerShell - _prevUtilPerShell;
require(0 < _diff || _diff >= MAX_DIFF, "Curve/liquidity-invariant-violation");
}
function enforceHalts(
Storage.Curve storage curve,
int128 _oGLiq,
int128 _nGLiq,
int128[] memory _oBals,
int128[] memory _nBals,
int128[] memory _weights
) private view {
uint256 _length = _nBals.length;
int128 _alpha = curve.alpha;
for (uint256 i = 0; i < _length; i++) {
int128 _nIdeal = _nGLiq.mul(_weights[i]);
if (_nBals[i] > _nIdeal) {
int128 _upperAlpha = ONE + _alpha;
int128 _nHalt = _nIdeal.mul(_upperAlpha);
if (_nBals[i] > _nHalt) {
int128 _oHalt = _oGLiq.mul(_weights[i]).mul(_upperAlpha);
if (_oBals[i] < _oHalt) revert("Curve/upper-halt");
if (_nBals[i] - _nHalt > _oBals[i] - _oHalt) revert("Curve/upper-halt");
}
} else {
int128 _lowerAlpha = ONE - _alpha;
int128 _nHalt = _nIdeal.mul(_lowerAlpha);
if (_nBals[i] < _nHalt) {
int128 _oHalt = _oGLiq.mul(_weights[i]);
_oHalt = _oHalt.mul(_lowerAlpha);
if (_oBals[i] > _oHalt) revert("Curve/lower-halt");
if (_nHalt - _nBals[i] > _oHalt - _oBals[i]) revert("Curve/lower-halt");
}
}
}
}
}
文件 7 的 21:ERC20.sol
pragma solidity >=0.6.0 <0.8.0;
import "./Context.sol";
import "./IERC20.sol";
import "./SafeMath.sol";
contract ERC20 is Context, IERC20 {
using SafeMath for uint256;
mapping (address => uint256) private _balances;
mapping (address => mapping (address => uint256)) private _allowances;
uint256 private _totalSupply;
string private _name;
string private _symbol;
uint8 private _decimals;
constructor (string memory name_, string memory symbol_) public {
_name = name_;
_symbol = symbol_;
_decimals = 18;
}
function name() public view returns (string memory) {
return _name;
}
function symbol() public view returns (string memory) {
return _symbol;
}
function decimals() public view returns (uint8) {
return _decimals;
}
function totalSupply() public view override returns (uint256) {
return _totalSupply;
}
function balanceOf(address account) public view override returns (uint256) {
return _balances[account];
}
function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(_msgSender(), recipient, amount);
return true;
}
function allowance(address owner, address spender) public view virtual override returns (uint256) {
return _allowances[owner][spender];
}
function approve(address spender, uint256 amount) public virtual override returns (bool) {
_approve(_msgSender(), spender, amount);
return true;
}
function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(sender, recipient, amount);
_approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance"));
return true;
}
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
return true;
}
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "ERC20: decreased allowance below zero"));
return true;
}
function _transfer(address sender, address recipient, uint256 amount) internal virtual {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");
_beforeTokenTransfer(sender, recipient, amount);
_balances[sender] = _balances[sender].sub(amount, "ERC20: transfer amount exceeds balance");
_balances[recipient] = _balances[recipient].add(amount);
emit Transfer(sender, recipient, amount);
}
function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: mint to the zero address");
_beforeTokenTransfer(address(0), account, amount);
_totalSupply = _totalSupply.add(amount);
_balances[account] = _balances[account].add(amount);
emit Transfer(address(0), account, amount);
}
function _burn(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: burn from the zero address");
_beforeTokenTransfer(account, address(0), amount);
_balances[account] = _balances[account].sub(amount, "ERC20: burn amount exceeds balance");
_totalSupply = _totalSupply.sub(amount);
emit Transfer(account, address(0), amount);
}
function _approve(address owner, address spender, uint256 amount) internal virtual {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
function _setupDecimals(uint8 decimals_) internal {
_decimals = decimals_;
}
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual { }
}
文件 8 的 21:IAssimilator.sol
pragma solidity ^0.7.3;
interface IAssimilator {
function getRate() external view returns (uint256);
function intakeRaw(uint256 amount) external returns (int128);
function intakeRawAndGetBalance(uint256 amount) external returns (int128, int128);
function intakeNumeraire(int128 amount) external returns (uint256);
function intakeNumeraireLPRatio(
uint256,
uint256,
address,
int128
) external returns (uint256);
function outputRaw(address dst, uint256 amount) external returns (int128);
function outputRawAndGetBalance(address dst, uint256 amount) external returns (int128, int128);
function outputNumeraire(address dst, int128 amount) external returns (uint256);
function viewRawAmount(int128) external view returns (uint256);
function viewRawAmountLPRatio(
uint256,
uint256,
address,
int128
) external view returns (uint256);
function viewNumeraireAmount(uint256) external view returns (int128);
function viewNumeraireBalanceLPRatio(
uint256,
uint256,
address
) external view returns (int128);
function viewNumeraireBalance(address) external view returns (int128);
function viewNumeraireAmountAndBalance(address, uint256) external view returns (int128, int128);
}
文件 9 的 21:IERC20.sol
pragma solidity >=0.6.0 <0.8.0;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
文件 10 的 21:IFreeFromUpTo.sol
pragma solidity ^0.7.3;
interface IFreeFromUpTo {
function freeFromUpTo(address from, uint256 value) external returns (uint256 freed);
}
文件 11 的 21:IOracle.sol
pragma solidity ^0.7.3;
interface IOracle {
function acceptOwnership() external;
function accessController() external view returns (address);
function aggregator() external view returns (address);
function confirmAggregator(address _aggregator) external;
function decimals() external view returns (uint8);
function description() external view returns (string memory);
function getAnswer(uint256 _roundId) external view returns (int256);
function getRoundData(uint80 _roundId)
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
function getTimestamp(uint256 _roundId) external view returns (uint256);
function latestAnswer() external view returns (int256);
function latestRound() external view returns (uint256);
function latestRoundData()
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
function latestTimestamp() external view returns (uint256);
function owner() external view returns (address);
function phaseAggregators(uint16) external view returns (address);
function phaseId() external view returns (uint16);
function proposeAggregator(address _aggregator) external;
function proposedAggregator() external view returns (address);
function proposedGetRoundData(uint80 _roundId)
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
function proposedLatestRoundData()
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
function setController(address _accessController) external;
function transferOwnership(address _to) external;
function version() external view returns (uint256);
}
文件 12 的 21:MerkleProof.sol
pragma solidity >=0.6.0 <0.8.0;
library MerkleProof {
function verify(bytes32[] memory proof, bytes32 root, bytes32 leaf) internal pure returns (bool) {
bytes32 computedHash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
bytes32 proofElement = proof[i];
if (computedHash <= proofElement) {
computedHash = keccak256(abi.encodePacked(computedHash, proofElement));
} else {
computedHash = keccak256(abi.encodePacked(proofElement, computedHash));
}
}
return computedHash == root;
}
}
文件 13 的 21:MerkleProver.sol
pragma solidity ^0.7.3;
import "./MerkleProof.sol";
contract MerkleProver {
bytes32 public immutable merkleRoot = bytes32(0xf4dbd0fb1957570029a847490cb3d731a45962072953ba7da80ff132ccd97d51);
function isWhitelisted(
uint256 index,
address account,
uint256 amount,
bytes32[] calldata merkleProof
) public view returns (bool) {
bytes32 node = keccak256(abi.encodePacked(index, account, amount));
return MerkleProof.verify(merkleProof, merkleRoot, node);
}
}
文件 14 的 21:Orchestrator.sol
pragma solidity ^0.7.3;
import "./ERC20.sol";
import "./SafeERC20.sol";
import "./ABDKMath64x64.sol";
import "./Storage.sol";
import "./CurveMath.sol";
library Orchestrator {
using SafeERC20 for IERC20;
using ABDKMath64x64 for int128;
using ABDKMath64x64 for uint256;
int128 private constant ONE_WEI = 0x12;
event ParametersSet(uint256 alpha, uint256 beta, uint256 delta, uint256 epsilon, uint256 lambda);
event AssetIncluded(address indexed numeraire, address indexed reserve, uint256 weight);
event AssimilatorIncluded(
address indexed derivative,
address indexed numeraire,
address indexed reserve,
address assimilator
);
function setParams(
Storage.Curve storage curve,
uint256 _alpha,
uint256 _beta,
uint256 _feeAtHalt,
uint256 _epsilon,
uint256 _lambda
) external {
require(0 < _alpha && _alpha < 1e18, "Curve/parameter-invalid-alpha");
require(_beta < _alpha, "Curve/parameter-invalid-beta");
require(_feeAtHalt <= 5e17, "Curve/parameter-invalid-max");
require(_epsilon <= 1e16, "Curve/parameter-invalid-epsilon");
require(_lambda <= 1e18, "Curve/parameter-invalid-lambda");
int128 _omega = getFee(curve);
curve.alpha = (_alpha + 1).divu(1e18);
curve.beta = (_beta + 1).divu(1e18);
curve.delta = (_feeAtHalt).divu(1e18).div(uint256(2).fromUInt().mul(curve.alpha.sub(curve.beta))) + ONE_WEI;
curve.epsilon = (_epsilon + 1).divu(1e18);
curve.lambda = (_lambda + 1).divu(1e18);
int128 _psi = getFee(curve);
require(_omega >= _psi, "Curve/parameters-increase-fee");
emit ParametersSet(_alpha, _beta, curve.delta.mulu(1e18), _epsilon, _lambda);
}
function getFee(Storage.Curve storage curve) private view returns (int128 fee_) {
int128 _gLiq;
int128[] memory _bals = new int128[](2);
for (uint256 i = 0; i < _bals.length; i++) {
int128 _bal = Assimilators.viewNumeraireBalance(curve.assets[i].addr);
_bals[i] = _bal;
_gLiq += _bal;
}
fee_ = CurveMath.calculateFee(_gLiq, _bals, curve.beta, curve.delta, curve.weights);
}
function initialize(
Storage.Curve storage curve,
address[] storage numeraires,
address[] storage reserves,
address[] storage derivatives,
address[] calldata _assets,
uint256[] calldata _assetWeights
) external {
require(_assetWeights.length == 2, "Curve/assetWeights-must-be-length-two");
require(_assets.length % 5 == 0, "Curve/assets-must-be-divisible-by-five");
for (uint256 i = 0; i < _assetWeights.length; i++) {
uint256 ix = i * 5;
numeraires.push(_assets[ix]);
derivatives.push(_assets[ix]);
reserves.push(_assets[2 + ix]);
if (_assets[ix] != _assets[2 + ix]) derivatives.push(_assets[2 + ix]);
includeAsset(
curve,
_assets[ix],
_assets[1 + ix],
_assets[2 + ix],
_assets[3 + ix],
_assets[4 + ix],
_assetWeights[i]
);
}
}
function includeAsset(
Storage.Curve storage curve,
address _numeraire,
address _numeraireAssim,
address _reserve,
address _reserveAssim,
address _reserveApproveTo,
uint256 _weight
) private {
require(_numeraire != address(0), "Curve/numeraire-cannot-be-zeroth-address");
require(_numeraireAssim != address(0), "Curve/numeraire-assimilator-cannot-be-zeroth-address");
require(_reserve != address(0), "Curve/reserve-cannot-be-zeroth-address");
require(_reserveAssim != address(0), "Curve/reserve-assimilator-cannot-be-zeroth-address");
require(_weight < 1e18, "Curve/weight-must-be-less-than-one");
if (_numeraire != _reserve) IERC20(_numeraire).safeApprove(_reserveApproveTo, uint256(-1));
Storage.Assimilator storage _numeraireAssimilator = curve.assimilators[_numeraire];
_numeraireAssimilator.addr = _numeraireAssim;
_numeraireAssimilator.ix = uint8(curve.assets.length);
Storage.Assimilator storage _reserveAssimilator = curve.assimilators[_reserve];
_reserveAssimilator.addr = _reserveAssim;
_reserveAssimilator.ix = uint8(curve.assets.length);
int128 __weight = _weight.divu(1e18).add(uint256(1).divu(1e18));
curve.weights.push(__weight);
curve.assets.push(_numeraireAssimilator);
emit AssetIncluded(_numeraire, _reserve, _weight);
emit AssimilatorIncluded(_numeraire, _numeraire, _reserve, _numeraireAssim);
if (_numeraireAssim != _reserveAssim) {
emit AssimilatorIncluded(_reserve, _numeraire, _reserve, _reserveAssim);
}
}
function includeAssimilator(
Storage.Curve storage curve,
address _derivative,
address _numeraire,
address _reserve,
address _assimilator,
address _derivativeApproveTo
) private {
require(_derivative != address(0), "Curve/derivative-cannot-be-zeroth-address");
require(_numeraire != address(0), "Curve/numeraire-cannot-be-zeroth-address");
require(_reserve != address(0), "Curve/numeraire-cannot-be-zeroth-address");
require(_assimilator != address(0), "Curve/assimilator-cannot-be-zeroth-address");
IERC20(_numeraire).safeApprove(_derivativeApproveTo, uint256(-1));
Storage.Assimilator storage _numeraireAssim = curve.assimilators[_numeraire];
curve.assimilators[_derivative] = Storage.Assimilator(_assimilator, _numeraireAssim.ix);
emit AssimilatorIncluded(_derivative, _numeraire, _reserve, _assimilator);
}
function viewCurve(Storage.Curve storage curve)
external
view
returns (
uint256 alpha_,
uint256 beta_,
uint256 delta_,
uint256 epsilon_,
uint256 lambda_
)
{
alpha_ = curve.alpha.mulu(1e18);
beta_ = curve.beta.mulu(1e18);
delta_ = curve.delta.mulu(1e18);
epsilon_ = curve.epsilon.mulu(1e18);
lambda_ = curve.lambda.mulu(1e18);
}
}
文件 15 的 21:ProportionalLiquidity.sol
pragma solidity ^0.7.3;
import "./Assimilators.sol";
import "./Storage.sol";
import "./UnsafeMath64x64.sol";
import "./ABDKMath64x64.sol";
import "./CurveMath.sol";
library ProportionalLiquidity {
using ABDKMath64x64 for uint256;
using ABDKMath64x64 for int128;
using UnsafeMath64x64 for int128;
event Transfer(address indexed from, address indexed to, uint256 value);
int128 public constant ONE = 0x10000000000000000;
int128 public constant ONE_WEI = 0x12;
function proportionalDeposit(Storage.Curve storage curve, uint256 _deposit)
external
returns (uint256 curves_, uint256[] memory)
{
int128 __deposit = _deposit.divu(1e18);
uint256 _length = curve.assets.length;
uint256[] memory deposits_ = new uint256[](_length);
(int128 _oGLiq, int128[] memory _oBals) = getGrossLiquidityAndBalancesForDeposit(curve);
(int128 _oGLiqProp, int128[] memory _oBalsProp) = getGrossLiquidityAndBalances(curve);
if (_oGLiq == 0) {
for (uint256 i = 0; i < _length; i++) {
int128 _d = __deposit.mul(curve.weights[i]);
deposits_[i] = Assimilators.intakeNumeraire(curve.assets[i].addr, _d.add(ONE_WEI));
}
} else {
int128 _multiplier = __deposit.div(_oGLiq);
uint256 _baseWeight = curve.weights[0].mulu(1e18);
uint256 _quoteWeight = curve.weights[1].mulu(1e18);
for (uint256 i = 0; i < _length; i++) {
deposits_[i] = Assimilators.intakeNumeraireLPRatio(
curve.assets[i].addr,
_baseWeight,
_quoteWeight,
_oBals[i].mul(_multiplier).add(ONE_WEI)
);
}
}
int128 _totalShells = curve.totalSupply.divu(1e18);
int128 _newShells = __deposit;
if (_totalShells > 0) {
_newShells = __deposit.div(_oGLiq);
_newShells = _newShells.mul(_totalShells);
}
requireLiquidityInvariant(curve, _totalShells, _newShells, _oGLiqProp, _oBalsProp);
mint(curve, msg.sender, curves_ = _newShells.mulu(1e18));
return (curves_, deposits_);
}
function viewProportionalDeposit(Storage.Curve storage curve, uint256 _deposit)
external
view
returns (uint256 curves_, uint256[] memory)
{
int128 __deposit = _deposit.divu(1e18);
uint256 _length = curve.assets.length;
(int128 _oGLiq, int128[] memory _oBals) = getGrossLiquidityAndBalancesForDeposit(curve);
uint256[] memory deposits_ = new uint256[](_length);
if (_oGLiq == 0) {
for (uint256 i = 0; i < _length; i++) {
deposits_[i] = Assimilators.viewRawAmount(
curve.assets[i].addr,
__deposit.mul(curve.weights[i]).add(ONE_WEI)
);
}
} else {
int128 _multiplier = __deposit.div(_oGLiq);
uint256 _baseWeight = curve.weights[0].mulu(1e18);
uint256 _quoteWeight = curve.weights[1].mulu(1e18);
for (uint256 i = 0; i < _length; i++) {
deposits_[i] = Assimilators.viewRawAmountLPRatio(
curve.assets[i].addr,
_baseWeight,
_quoteWeight,
_oBals[i].mul(_multiplier).add(ONE_WEI)
);
}
}
int128 _totalShells = curve.totalSupply.divu(1e18);
int128 _newShells = __deposit;
if (_totalShells > 0) {
_newShells = __deposit.div(_oGLiq);
_newShells = _newShells.mul(_totalShells);
}
curves_ = _newShells.mulu(1e18);
return (curves_, deposits_);
}
function emergencyProportionalWithdraw(Storage.Curve storage curve, uint256 _withdrawal)
external
returns (uint256[] memory)
{
uint256 _length = curve.assets.length;
(, int128[] memory _oBals) = getGrossLiquidityAndBalances(curve);
uint256[] memory withdrawals_ = new uint256[](_length);
int128 _totalShells = curve.totalSupply.divu(1e18);
int128 __withdrawal = _withdrawal.divu(1e18);
int128 _multiplier = __withdrawal.div(_totalShells);
for (uint256 i = 0; i < _length; i++) {
withdrawals_[i] = Assimilators.outputNumeraire(
curve.assets[i].addr,
msg.sender,
_oBals[i].mul(_multiplier)
);
}
burn(curve, msg.sender, _withdrawal);
return withdrawals_;
}
function proportionalWithdraw(Storage.Curve storage curve, uint256 _withdrawal)
external
returns (uint256[] memory)
{
uint256 _length = curve.assets.length;
(int128 _oGLiq, int128[] memory _oBals) = getGrossLiquidityAndBalances(curve);
uint256[] memory withdrawals_ = new uint256[](_length);
int128 _totalShells = curve.totalSupply.divu(1e18);
int128 __withdrawal = _withdrawal.divu(1e18);
int128 _multiplier = __withdrawal.div(_totalShells);
for (uint256 i = 0; i < _length; i++) {
withdrawals_[i] = Assimilators.outputNumeraire(
curve.assets[i].addr,
msg.sender,
_oBals[i].mul(_multiplier)
);
}
requireLiquidityInvariant(curve, _totalShells, __withdrawal.neg(), _oGLiq, _oBals);
burn(curve, msg.sender, _withdrawal);
return withdrawals_;
}
function viewProportionalWithdraw(Storage.Curve storage curve, uint256 _withdrawal)
external
view
returns (uint256[] memory)
{
uint256 _length = curve.assets.length;
(, int128[] memory _oBals) = getGrossLiquidityAndBalances(curve);
uint256[] memory withdrawals_ = new uint256[](_length);
int128 _multiplier = _withdrawal.divu(1e18).div(curve.totalSupply.divu(1e18));
for (uint256 i = 0; i < _length; i++) {
withdrawals_[i] = Assimilators.viewRawAmount(curve.assets[i].addr, _oBals[i].mul(_multiplier));
}
return withdrawals_;
}
function getGrossLiquidityAndBalancesForDeposit(Storage.Curve storage curve)
internal
view
returns (int128 grossLiquidity_, int128[] memory)
{
uint256 _length = curve.assets.length;
int128[] memory balances_ = new int128[](_length);
uint256 _baseWeight = curve.weights[0].mulu(1e18);
uint256 _quoteWeight = curve.weights[1].mulu(1e18);
for (uint256 i = 0; i < _length; i++) {
int128 _bal = Assimilators.viewNumeraireBalanceLPRatio(_baseWeight, _quoteWeight, curve.assets[i].addr);
balances_[i] = _bal;
grossLiquidity_ += _bal;
}
return (grossLiquidity_, balances_);
}
function getGrossLiquidityAndBalances(Storage.Curve storage curve)
internal
view
returns (int128 grossLiquidity_, int128[] memory)
{
uint256 _length = curve.assets.length;
int128[] memory balances_ = new int128[](_length);
for (uint256 i = 0; i < _length; i++) {
int128 _bal = Assimilators.viewNumeraireBalance(curve.assets[i].addr);
balances_[i] = _bal;
grossLiquidity_ += _bal;
}
return (grossLiquidity_, balances_);
}
function requireLiquidityInvariant(
Storage.Curve storage curve,
int128 _curves,
int128 _newShells,
int128 _oGLiq,
int128[] memory _oBals
) private view {
(int128 _nGLiq, int128[] memory _nBals) = getGrossLiquidityAndBalances(curve);
int128 _beta = curve.beta;
int128 _delta = curve.delta;
int128[] memory _weights = curve.weights;
int128 _omega = CurveMath.calculateFee(_oGLiq, _oBals, _beta, _delta, _weights);
int128 _psi = CurveMath.calculateFee(_nGLiq, _nBals, _beta, _delta, _weights);
CurveMath.enforceLiquidityInvariant(_curves, _newShells, _oGLiq, _nGLiq, _omega, _psi);
}
function burn(
Storage.Curve storage curve,
address account,
uint256 amount
) private {
curve.balances[account] = burnSub(curve.balances[account], amount);
curve.totalSupply = burnSub(curve.totalSupply, amount);
emit Transfer(msg.sender, address(0), amount);
}
function mint(
Storage.Curve storage curve,
address account,
uint256 amount
) private {
curve.totalSupply = mintAdd(curve.totalSupply, amount);
curve.balances[account] = mintAdd(curve.balances[account], amount);
emit Transfer(address(0), msg.sender, amount);
}
function mintAdd(uint256 x, uint256 y) private pure returns (uint256 z) {
require((z = x + y) >= x, "Curve/mint-overflow");
}
function burnSub(uint256 x, uint256 y) private pure returns (uint256 z) {
require((z = x - y) <= x, "Curve/burn-underflow");
}
}
文件 16 的 21:SafeERC20.sol
pragma solidity >=0.6.0 <0.8.0;
import "./IERC20.sol";
import "./SafeMath.sol";
import "./Address.sol";
library SafeERC20 {
using SafeMath for uint256;
using Address for address;
function safeTransfer(IERC20 token, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
function safeApprove(IERC20 token, address spender, uint256 value) internal {
require((value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender).add(value);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender).sub(value, "SafeERC20: decreased allowance below zero");
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function _callOptionalReturn(IERC20 token, bytes memory data) private {
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
if (returndata.length > 0) {
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
文件 17 的 21:SafeMath.sol
pragma solidity >=0.6.0 <0.8.0;
library SafeMath {
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
}
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, "SafeMath: division by zero");
}
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
uint256 c = a / b;
return c;
}
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
}
文件 18 的 21:Storage.sol
pragma solidity ^0.7.3;
import "./IOracle.sol";
import "./Assimilators.sol";
contract Storage {
struct Curve {
int128 alpha;
int128 beta;
int128 delta;
int128 epsilon;
int128 lambda;
int128[] weights;
Assimilator[] assets;
mapping(address => Assimilator) assimilators;
mapping(address => IOracle) oracles;
uint256 totalSupply;
mapping(address => uint256) balances;
mapping(address => mapping(address => uint256)) allowances;
}
struct Assimilator {
address addr;
uint8 ix;
}
Curve public curve;
address public owner;
string public name;
string public symbol;
uint8 public constant decimals = 18;
address[] public derivatives;
address[] public numeraires;
address[] public reserves;
bool public frozen = false;
bool public emergency = false;
bool public whitelistingStage = true;
bool internal notEntered = true;
mapping(address => uint256) public whitelistedDeposited;
}
文件 19 的 21:Swaps.sol
pragma solidity ^0.7.3;
import "./Assimilators.sol";
import "./Storage.sol";
import "./CurveMath.sol";
import "./UnsafeMath64x64.sol";
import "./ABDKMath64x64.sol";
import "./SafeMath.sol";
library Swaps {
using ABDKMath64x64 for int128;
using UnsafeMath64x64 for int128;
using ABDKMath64x64 for uint256;
using SafeMath for uint256;
event Trade(
address indexed trader,
address indexed origin,
address indexed target,
uint256 originAmount,
uint256 targetAmount
);
int128 public constant ONE = 0x10000000000000000;
function getOriginAndTarget(
Storage.Curve storage curve,
address _o,
address _t
) private view returns (Storage.Assimilator memory, Storage.Assimilator memory) {
Storage.Assimilator memory o_ = curve.assimilators[_o];
Storage.Assimilator memory t_ = curve.assimilators[_t];
require(o_.addr != address(0), "Curve/origin-not-supported");
require(t_.addr != address(0), "Curve/target-not-supported");
return (o_, t_);
}
function originSwap(
Storage.Curve storage curve,
address _origin,
address _target,
uint256 _originAmount,
address _recipient
) external returns (uint256 tAmt_) {
(Storage.Assimilator memory _o, Storage.Assimilator memory _t) = getOriginAndTarget(curve, _origin, _target);
if (_o.ix == _t.ix)
return Assimilators.outputNumeraire(_t.addr, _recipient, Assimilators.intakeRaw(_o.addr, _originAmount));
(int128 _amt, int128 _oGLiq, int128 _nGLiq, int128[] memory _oBals, int128[] memory _nBals) =
getOriginSwapData(curve, _o.ix, _t.ix, _o.addr, _originAmount);
_amt = CurveMath.calculateTrade(curve, _oGLiq, _nGLiq, _oBals, _nBals, _amt, _t.ix);
_amt = _amt.us_mul(ONE - curve.epsilon);
tAmt_ = Assimilators.outputNumeraire(_t.addr, _recipient, _amt);
emit Trade(msg.sender, _origin, _target, _originAmount, tAmt_);
}
function viewOriginSwap(
Storage.Curve storage curve,
address _origin,
address _target,
uint256 _originAmount
) external view returns (uint256 tAmt_) {
(Storage.Assimilator memory _o, Storage.Assimilator memory _t) = getOriginAndTarget(curve, _origin, _target);
if (_o.ix == _t.ix)
return Assimilators.viewRawAmount(_t.addr, Assimilators.viewNumeraireAmount(_o.addr, _originAmount));
(int128 _amt, int128 _oGLiq, int128 _nGLiq, int128[] memory _nBals, int128[] memory _oBals) =
viewOriginSwapData(curve, _o.ix, _t.ix, _originAmount, _o.addr);
_amt = CurveMath.calculateTrade(curve, _oGLiq, _nGLiq, _oBals, _nBals, _amt, _t.ix);
_amt = _amt.us_mul(ONE - curve.epsilon);
tAmt_ = Assimilators.viewRawAmount(_t.addr, _amt.abs());
}
function targetSwap(
Storage.Curve storage curve,
address _origin,
address _target,
uint256 _targetAmount,
address _recipient
) external returns (uint256 oAmt_) {
(Storage.Assimilator memory _o, Storage.Assimilator memory _t) = getOriginAndTarget(curve, _origin, _target);
if (_o.ix == _t.ix)
return Assimilators.intakeNumeraire(_o.addr, Assimilators.outputRaw(_t.addr, _recipient, _targetAmount));
if (curve.assets[1].addr == _o.addr) {
_targetAmount = _targetAmount.mul(1e8).div(Assimilators.getRate(_t.addr));
}
(int128 _amt, int128 _oGLiq, int128 _nGLiq, int128[] memory _oBals, int128[] memory _nBals) =
getTargetSwapData(curve, _t.ix, _o.ix, _t.addr, _recipient, _targetAmount);
_amt = CurveMath.calculateTrade(curve, _oGLiq, _nGLiq, _oBals, _nBals, _amt, _o.ix);
if (curve.assets[1].addr == _o.addr) {
_amt = _amt.mul(Assimilators.getRate(_t.addr).divu(1e8));
}
_amt = _amt.us_mul(ONE + curve.epsilon);
oAmt_ = Assimilators.intakeNumeraire(_o.addr, _amt);
emit Trade(msg.sender, _origin, _target, oAmt_, _targetAmount);
}
function viewTargetSwap(
Storage.Curve storage curve,
address _origin,
address _target,
uint256 _targetAmount
) external view returns (uint256 oAmt_) {
(Storage.Assimilator memory _o, Storage.Assimilator memory _t) = getOriginAndTarget(curve, _origin, _target);
if (_o.ix == _t.ix)
return Assimilators.viewRawAmount(_o.addr, Assimilators.viewNumeraireAmount(_t.addr, _targetAmount));
if (curve.assets[1].addr == _o.addr) {
_targetAmount = _targetAmount.mul(1e8).div(Assimilators.getRate(_t.addr));
}
(int128 _amt, int128 _oGLiq, int128 _nGLiq, int128[] memory _nBals, int128[] memory _oBals) =
viewTargetSwapData(curve, _t.ix, _o.ix, _targetAmount, _t.addr);
_amt = CurveMath.calculateTrade(curve, _oGLiq, _nGLiq, _oBals, _nBals, _amt, _o.ix);
if (curve.assets[1].addr == _o.addr) {
_amt = _amt.mul(Assimilators.getRate(_t.addr).divu(1e8));
}
_amt = _amt.us_mul(ONE + curve.epsilon);
oAmt_ = Assimilators.viewRawAmount(_o.addr, _amt);
}
function getOriginSwapData(
Storage.Curve storage curve,
uint256 _inputIx,
uint256 _outputIx,
address _assim,
uint256 _amt
)
private
returns (
int128 amt_,
int128 oGLiq_,
int128 nGLiq_,
int128[] memory,
int128[] memory
)
{
uint256 _length = curve.assets.length;
int128[] memory oBals_ = new int128[](_length);
int128[] memory nBals_ = new int128[](_length);
Storage.Assimilator[] memory _reserves = curve.assets;
for (uint256 i = 0; i < _length; i++) {
if (i != _inputIx) nBals_[i] = oBals_[i] = Assimilators.viewNumeraireBalance(_reserves[i].addr);
else {
int128 _bal;
(amt_, _bal) = Assimilators.intakeRawAndGetBalance(_assim, _amt);
oBals_[i] = _bal.sub(amt_);
nBals_[i] = _bal;
}
oGLiq_ += oBals_[i];
nGLiq_ += nBals_[i];
}
nGLiq_ = nGLiq_.sub(amt_);
nBals_[_outputIx] = ABDKMath64x64.sub(nBals_[_outputIx], amt_);
return (amt_, oGLiq_, nGLiq_, oBals_, nBals_);
}
function getTargetSwapData(
Storage.Curve storage curve,
uint256 _inputIx,
uint256 _outputIx,
address _assim,
address _recipient,
uint256 _amt
)
private
returns (
int128 amt_,
int128 oGLiq_,
int128 nGLiq_,
int128[] memory,
int128[] memory
)
{
uint256 _length = curve.assets.length;
int128[] memory oBals_ = new int128[](_length);
int128[] memory nBals_ = new int128[](_length);
Storage.Assimilator[] memory _reserves = curve.assets;
for (uint256 i = 0; i < _length; i++) {
if (i != _inputIx) nBals_[i] = oBals_[i] = Assimilators.viewNumeraireBalance(_reserves[i].addr);
else {
int128 _bal;
(amt_, _bal) = Assimilators.outputRawAndGetBalance(_assim, _recipient, _amt);
oBals_[i] = _bal.sub(amt_);
nBals_[i] = _bal;
}
oGLiq_ += oBals_[i];
nGLiq_ += nBals_[i];
}
nGLiq_ = nGLiq_.sub(amt_);
nBals_[_outputIx] = ABDKMath64x64.sub(nBals_[_outputIx], amt_);
return (amt_, oGLiq_, nGLiq_, oBals_, nBals_);
}
function viewOriginSwapData(
Storage.Curve storage curve,
uint256 _inputIx,
uint256 _outputIx,
uint256 _amt,
address _assim
)
private
view
returns (
int128 amt_,
int128 oGLiq_,
int128 nGLiq_,
int128[] memory,
int128[] memory
)
{
uint256 _length = curve.assets.length;
int128[] memory nBals_ = new int128[](_length);
int128[] memory oBals_ = new int128[](_length);
for (uint256 i = 0; i < _length; i++) {
if (i != _inputIx) nBals_[i] = oBals_[i] = Assimilators.viewNumeraireBalance(curve.assets[i].addr);
else {
int128 _bal;
(amt_, _bal) = Assimilators.viewNumeraireAmountAndBalance(_assim, _amt);
oBals_[i] = _bal;
nBals_[i] = _bal.add(amt_);
}
oGLiq_ += oBals_[i];
nGLiq_ += nBals_[i];
}
nGLiq_ = nGLiq_.sub(amt_);
nBals_[_outputIx] = ABDKMath64x64.sub(nBals_[_outputIx], amt_);
return (amt_, oGLiq_, nGLiq_, nBals_, oBals_);
}
function viewTargetSwapData(
Storage.Curve storage curve,
uint256 _inputIx,
uint256 _outputIx,
uint256 _amt,
address _assim
)
private
view
returns (
int128 amt_,
int128 oGLiq_,
int128 nGLiq_,
int128[] memory,
int128[] memory
)
{
uint256 _length = curve.assets.length;
int128[] memory nBals_ = new int128[](_length);
int128[] memory oBals_ = new int128[](_length);
for (uint256 i = 0; i < _length; i++) {
if (i != _inputIx) nBals_[i] = oBals_[i] = Assimilators.viewNumeraireBalance(curve.assets[i].addr);
else {
int128 _bal;
(amt_, _bal) = Assimilators.viewNumeraireAmountAndBalance(_assim, _amt);
amt_ = amt_.neg();
oBals_[i] = _bal;
nBals_[i] = _bal.add(amt_);
}
oGLiq_ += oBals_[i];
nGLiq_ += nBals_[i];
}
nGLiq_ = nGLiq_.sub(amt_);
nBals_[_outputIx] = ABDKMath64x64.sub(nBals_[_outputIx], amt_);
return (amt_, oGLiq_, nGLiq_, nBals_, oBals_);
}
}
文件 20 的 21:UnsafeMath64x64.sol
pragma solidity ^0.7.3;
library UnsafeMath64x64 {
function us_mul (int128 x, int128 y) internal pure returns (int128) {
int256 result = int256(x) * y >> 64;
return int128 (result);
}
function us_div (int128 x, int128 y) internal pure returns (int128) {
int256 result = (int256 (x) << 64) / y;
return int128 (result);
}
}
文件 21 的 21:ViewLiquidity.sol
pragma solidity ^0.7.3;
import "./Storage.sol";
import "./Assimilators.sol";
import "./ABDKMath64x64.sol";
library ViewLiquidity {
using ABDKMath64x64 for int128;
function viewLiquidity(Storage.Curve storage curve)
external
view
returns (uint256 total_, uint256[] memory individual_)
{
uint256 _length = curve.assets.length;
individual_ = new uint256[](_length);
for (uint256 i = 0; i < _length; i++) {
uint256 _liquidity = Assimilators.viewNumeraireBalance(curve.assets[i].addr).mulu(1e18);
total_ += _liquidity;
individual_[i] = _liquidity;
}
return (total_, individual_);
}
}
{
"compilationTarget": {
"Curve.sol": "Curve"
},
"evmVersion": "istanbul",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_symbol","type":"string"},{"internalType":"address[]","name":"_assets","type":"address[]"},{"internalType":"uint256[]","name":"_assetWeights","type":"uint256[]"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"numeraire","type":"address"},{"indexed":true,"internalType":"address","name":"reserve","type":"address"},{"indexed":false,"internalType":"uint256","name":"weight","type":"uint256"}],"name":"AssetIncluded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"derivative","type":"address"},{"indexed":true,"internalType":"address","name":"numeraire","type":"address"},{"indexed":true,"internalType":"address","name":"reserve","type":"address"},{"indexed":false,"internalType":"address","name":"assimilator","type":"address"}],"name":"AssimilatorIncluded","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bool","name":"isEmergency","type":"bool"}],"name":"EmergencyAlarm","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bool","name":"isFrozen","type":"bool"}],"name":"FrozenSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransfered","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"alpha","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"beta","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"delta","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"epsilon","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"lambda","type":"uint256"}],"name":"ParametersSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token","type":"address"},{"indexed":true,"internalType":"address","name":"redeemer","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"PartitionRedeemed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"trader","type":"address"},{"indexed":true,"internalType":"address","name":"origin","type":"address"},{"indexed":true,"internalType":"address","name":"target","type":"address"},{"indexed":false,"internalType":"uint256","name":"originAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"targetAmount","type":"uint256"}],"name":"Trade","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[],"name":"WhitelistingStopped","type":"event"},{"inputs":[{"internalType":"address","name":"_owner","type":"address"},{"internalType":"address","name":"_spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"allowance_","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"success_","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_derivative","type":"address"}],"name":"assimilator","outputs":[{"internalType":"address","name":"assimilator_","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"balance_","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"curve","outputs":[{"internalType":"int128","name":"alpha","type":"int128"},{"internalType":"int128","name":"beta","type":"int128"},{"internalType":"int128","name":"delta","type":"int128"},{"internalType":"int128","name":"epsilon","type":"int128"},{"internalType":"int128","name":"lambda","type":"int128"},{"internalType":"uint256","name":"totalSupply","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_deposit","type":"uint256"},{"internalType":"uint256","name":"_deadline","type":"uint256"}],"name":"deposit","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"index","type":"uint256"},{"internalType":"address","name":"account","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"bytes32[]","name":"merkleProof","type":"bytes32[]"},{"internalType":"uint256","name":"_deposit","type":"uint256"},{"internalType":"uint256","name":"_deadline","type":"uint256"}],"name":"depositWithWhitelist","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"derivatives","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"emergency","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_curvesToBurn","type":"uint256"},{"internalType":"uint256","name":"_deadline","type":"uint256"}],"name":"emergencyWithdraw","outputs":[{"internalType":"uint256[]","name":"withdrawals_","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_derivative","type":"address"}],"name":"excludeDerivative","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"frozen","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"index","type":"uint256"},{"internalType":"address","name":"account","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"bytes32[]","name":"merkleProof","type":"bytes32[]"}],"name":"isWhitelisted","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"liquidity","outputs":[{"internalType":"uint256","name":"total_","type":"uint256"},{"internalType":"uint256[]","name":"individual_","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"merkleRoot","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"numeraires","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_origin","type":"address"},{"internalType":"address","name":"_target","type":"address"},{"internalType":"uint256","name":"_originAmount","type":"uint256"},{"internalType":"uint256","name":"_minTargetAmount","type":"uint256"},{"internalType":"uint256","name":"_deadline","type":"uint256"}],"name":"originSwap","outputs":[{"internalType":"uint256","name":"targetAmount_","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"reserves","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bool","name":"_emergency","type":"bool"}],"name":"setEmergency","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bool","name":"_toFreezeOrNotToFreeze","type":"bool"}],"name":"setFrozen","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_alpha","type":"uint256"},{"internalType":"uint256","name":"_beta","type":"uint256"},{"internalType":"uint256","name":"_feeAtHalt","type":"uint256"},{"internalType":"uint256","name":"_epsilon","type":"uint256"},{"internalType":"uint256","name":"_lambda","type":"uint256"}],"name":"setParams","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"_interface","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"supports_","type":"bool"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_origin","type":"address"},{"internalType":"address","name":"_target","type":"address"},{"internalType":"uint256","name":"_maxOriginAmount","type":"uint256"},{"internalType":"uint256","name":"_targetAmount","type":"uint256"},{"internalType":"uint256","name":"_deadline","type":"uint256"}],"name":"targetSwap","outputs":[{"internalType":"uint256","name":"originAmount_","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"totalSupply_","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_recipient","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"success_","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_sender","type":"address"},{"internalType":"address","name":"_recipient","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"success_","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"turnOffWhitelisting","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"viewCurve","outputs":[{"internalType":"uint256","name":"alpha_","type":"uint256"},{"internalType":"uint256","name":"beta_","type":"uint256"},{"internalType":"uint256","name":"delta_","type":"uint256"},{"internalType":"uint256","name":"epsilon_","type":"uint256"},{"internalType":"uint256","name":"lambda_","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_deposit","type":"uint256"}],"name":"viewDeposit","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_origin","type":"address"},{"internalType":"address","name":"_target","type":"address"},{"internalType":"uint256","name":"_originAmount","type":"uint256"}],"name":"viewOriginSwap","outputs":[{"internalType":"uint256","name":"targetAmount_","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_origin","type":"address"},{"internalType":"address","name":"_target","type":"address"},{"internalType":"uint256","name":"_targetAmount","type":"uint256"}],"name":"viewTargetSwap","outputs":[{"internalType":"uint256","name":"originAmount_","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_curvesToBurn","type":"uint256"}],"name":"viewWithdraw","outputs":[{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"whitelistedDeposited","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"whitelistingStage","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_curvesToBurn","type":"uint256"},{"internalType":"uint256","name":"_deadline","type":"uint256"}],"name":"withdraw","outputs":[{"internalType":"uint256[]","name":"withdrawals_","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"}]