编译器
0.8.21+commit.d9974bed
文件 1 的 31:ABDKMath64x64.sol
pragma solidity ^0.8.13;
library ABDKMath64x64 {
int128 private constant MIN_64x64 = -0x80000000000000000000000000000000;
int128 private constant MAX_64x64 = 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
function fromInt(int256 x) internal pure returns (int128) {
unchecked {
require(x >= -0x8000000000000000 && x <= 0x7FFFFFFFFFFFFFFF);
return int128(x << 64);
}
}
function toInt(int128 x) internal pure returns (int64) {
unchecked {
return int64(x >> 64);
}
}
function fromUInt(uint256 x) internal pure returns (int128) {
unchecked {
require(x <= 0x7FFFFFFFFFFFFFFF);
return int128(int256(x << 64));
}
}
function toUInt(int128 x) internal pure returns (uint64) {
unchecked {
require(x >= 0);
return uint64(uint128(x >> 64));
}
}
function from128x128(int256 x) internal pure returns (int128) {
unchecked {
int256 result = x >> 64;
require(result >= MIN_64x64 && result <= MAX_64x64);
return int128(result);
}
}
function to128x128(int128 x) internal pure returns (int256) {
unchecked {
return int256(x) << 64;
}
}
function add(int128 x, int128 y) internal pure returns (int128) {
unchecked {
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) {
unchecked {
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) {
unchecked {
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) {
unchecked {
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) {
unchecked {
if (y == 0) return 0;
require(x >= 0);
uint256 lo = (uint256(int256(x)) * (y & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)) >> 64;
uint256 hi = uint256(int256(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) {
unchecked {
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) {
unchecked {
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) {
unchecked {
require(y != 0);
uint128 result = divuu(x, y);
require(result <= uint128(MAX_64x64));
return int128(result);
}
}
function neg(int128 x) internal pure returns (int128) {
unchecked {
require(x != MIN_64x64);
return -x;
}
}
function abs(int128 x) internal pure returns (int128) {
unchecked {
require(x != MIN_64x64);
return x < 0 ? -x : x;
}
}
function inv(int128 x) internal pure returns (int128) {
unchecked {
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) {
unchecked {
return int128((int256(x) + int256(y)) >> 1);
}
}
function gavg(int128 x, int128 y) internal pure returns (int128) {
unchecked {
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) {
unchecked {
bool negative = x < 0 && y & 1 == 1;
uint256 absX = uint128(x < 0 ? -x : x);
uint256 absResult;
absResult = 0x100000000000000000000000000000000;
if (absX <= 0x10000000000000000) {
absX <<= 63;
while (y != 0) {
if (y & 0x1 != 0) {
absResult = absResult * absX >> 127;
}
absX = absX * absX >> 127;
if (y & 0x2 != 0) {
absResult = absResult * absX >> 127;
}
absX = absX * absX >> 127;
if (y & 0x4 != 0) {
absResult = absResult * absX >> 127;
}
absX = absX * absX >> 127;
if (y & 0x8 != 0) {
absResult = absResult * absX >> 127;
}
absX = absX * absX >> 127;
y >>= 4;
}
absResult >>= 64;
} else {
uint256 absXShift = 63;
if (absX < 0x1000000000000000000000000) {
absX <<= 32;
absXShift -= 32;
}
if (absX < 0x10000000000000000000000000000) {
absX <<= 16;
absXShift -= 16;
}
if (absX < 0x1000000000000000000000000000000) {
absX <<= 8;
absXShift -= 8;
}
if (absX < 0x10000000000000000000000000000000) {
absX <<= 4;
absXShift -= 4;
}
if (absX < 0x40000000000000000000000000000000) {
absX <<= 2;
absXShift -= 2;
}
if (absX < 0x80000000000000000000000000000000) {
absX <<= 1;
absXShift -= 1;
}
uint256 resultShift = 0;
while (y != 0) {
require(absXShift < 64);
if (y & 0x1 != 0) {
absResult = absResult * absX >> 127;
resultShift += absXShift;
if (absResult > 0x100000000000000000000000000000000) {
absResult >>= 1;
resultShift += 1;
}
}
absX = absX * absX >> 127;
absXShift <<= 1;
if (absX >= 0x100000000000000000000000000000000) {
absX >>= 1;
absXShift += 1;
}
y >>= 1;
}
require(resultShift < 64);
absResult >>= 64 - resultShift;
}
int256 result = negative ? -int256(absResult) : int256(absResult);
require(result >= MIN_64x64 && result <= MAX_64x64);
return int128(result);
}
}
function sqrt(int128 x) internal pure returns (int128) {
unchecked {
require(x >= 0);
return int128(sqrtu(uint256(int256(x)) << 64));
}
}
function log_2(int128 x) internal pure returns (int128) {
unchecked {
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(int256(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) {
unchecked {
require(x > 0);
return int128(int256(uint256(int256(log_2(x))) * 0xB17217F7D1CF79ABC9E3B39803F2F6AF >> 128));
}
}
function exp_2(int128 x) internal pure returns (int128) {
unchecked {
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(int256(63 - (x >> 64)));
require(result <= uint256(int256(MAX_64x64)));
return int128(int256(result));
}
}
function exp(int128 x) internal pure returns (int128) {
unchecked {
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) {
unchecked {
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 sqrtu(uint256 x) private pure returns (uint128) {
unchecked {
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 的 31:Address.sol
pragma solidity ^0.8.1;
library Address {
function isContract(address account) internal view returns (bool) {
return account.code.length > 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 functionCallWithValue(target, data, 0, "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");
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResultFromTarget(target, 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) {
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
function functionDelegateCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata,
string memory errorMessage
) internal view returns (bytes memory) {
if (success) {
if (returndata.length == 0) {
require(isContract(target), "Address: call to non-contract");
}
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
function verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
function _revert(bytes memory returndata, string memory errorMessage) private pure {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
文件 3 的 31:AssimilatorV3.sol
pragma solidity ^0.8.13;
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/utils/math/Math.sol";
import "../lib/ABDKMath64x64.sol";
import "../interfaces/IAssimilator.sol";
import "../interfaces/IOracle.sol";
import "../interfaces/IWeth.sol";
contract AssimilatorV3 is IAssimilator {
using ABDKMath64x64 for int128;
using ABDKMath64x64 for uint256;
using SafeMath for uint256;
using SafeERC20 for IERC20Metadata;
IERC20Metadata public immutable pairToken;
IOracle public immutable oracle;
IERC20Metadata public immutable token;
uint256 public immutable oracleDecimals;
uint256 public immutable tokenDecimals;
uint256 public immutable pairTokenDecimals;
address public immutable wETH;
constructor(
address _wETH,
address _pairToken,
IOracle _oracle,
address _token,
uint256 _tokenDecimals,
uint256 _oracleDecimals
) {
wETH = _wETH;
oracle = _oracle;
token = IERC20Metadata(_token);
oracleDecimals = _oracleDecimals;
tokenDecimals = _tokenDecimals;
pairToken = IERC20Metadata(_pairToken);
pairTokenDecimals = pairToken.decimals();
}
function underlyingToken() external view override returns (address) {
return address(token);
}
function getWeth() external view override returns (address) {
return wETH;
}
function getRate() public view override returns (uint256) {
(, int256 price,,,) = oracle.latestRoundData();
require(price >= 0, "invalid price oracle");
return uint256(price);
}
function intakeRawAndGetBalance(uint256 _amount)
external
payable
override
returns (int128 amount_, int128 balance_)
{
require(_amount > 0, "zero amount!");
uint256 balanceBefore = token.balanceOf(address(this));
token.safeTransferFrom(msg.sender, address(this), _amount);
uint256 balanceAfter = token.balanceOf(address(this));
uint256 diff = _amount - (balanceAfter - balanceBefore);
if (diff > 0) {
intakeMoreFromFoT(_amount, diff);
}
uint256 _balance = token.balanceOf(address(this));
uint256 _rate = getRate();
balance_ = ((_balance * _rate) / 10 ** oracleDecimals).divu(10 ** tokenDecimals);
amount_ = ((_amount * _rate) / 10 ** oracleDecimals).divu(10 ** tokenDecimals);
}
function intakeRaw(uint256 _amount) external payable override returns (int128 amount_) {
require(_amount > 0, "zero amount!");
uint256 balanceBefore = token.balanceOf(address(this));
token.safeTransferFrom(msg.sender, address(this), _amount);
uint256 balanceAfter = token.balanceOf(address(this));
uint256 diff = _amount - (balanceAfter - balanceBefore);
if (diff > 0) {
intakeMoreFromFoT(_amount, diff);
}
uint256 _rate = getRate();
amount_ = ((_amount * _rate) / 10 ** oracleDecimals).divu(10 ** tokenDecimals);
}
function intakeNumeraire(int128 _amount) external payable override returns (uint256 amount_) {
uint256 _rate = getRate();
amount_ = Math.ceilDiv(_amount.mulu(10 ** (tokenDecimals + oracleDecimals + 18)), _rate * 1e18);
require(amount_ > 0, "zero amount!");
uint256 balanceBefore = token.balanceOf(address(this));
token.safeTransferFrom(msg.sender, address(this), amount_);
uint256 balanceAfter = token.balanceOf(address(this));
uint256 diff = amount_ - (balanceAfter - balanceBefore);
if (diff > 0) intakeMoreFromFoT(amount_, diff);
}
function intakeNumeraireLPRatio(
uint256 _minBaseAmount,
uint256 _maxBaseAmount,
uint256 _baseAmount,
uint256 _minpairTokenAmount,
uint256 _maxpairTokenAmount,
uint256 _quoteAmount,
address token0
) external payable override returns (uint256 amount_) {
if (token0 == address(token)) {
amount_ = _baseAmount;
} else {
amount_ = _quoteAmount;
}
require(amount_ > 0, "zero amount!");
if (token0 == address(token)) {
require(amount_ > _minBaseAmount && amount_ <= _maxBaseAmount, "Assimilator/LP Ratio imbalanced!");
} else {
require(amount_ > _minpairTokenAmount && amount_ <= _maxpairTokenAmount, "Assimilator/LP Ratio imbalanced!");
}
uint256 balanceBefore = token.balanceOf(address(this));
token.safeTransferFrom(msg.sender, address(this), amount_);
uint256 balanceAfter = token.balanceOf(address(this));
uint256 diff = amount_ - (balanceAfter - balanceBefore);
if (diff > 0) intakeMoreFromFoT(amount_, diff);
}
function intakeMoreFromFoT(uint256 amount_, uint256 diff) internal {
require(amount_ > 0, "zero amount!");
uint256 feePercentage = diff.mul(1e5).div(amount_).add(1);
uint256 additionalIntakeAmt = (diff * 1e5) / (1e5 - feePercentage);
token.safeTransferFrom(msg.sender, address(this), additionalIntakeAmt);
}
function outputRawAndGetBalance(address _dst, uint256 _amount)
external
override
returns (int128 amount_, int128 balance_)
{
require(_amount > 0, "zero amount!");
uint256 _rate = getRate();
token.safeTransfer(_dst, _amount);
uint256 _balance = token.balanceOf(address(this));
amount_ = ((_amount * _rate)).divu(10 ** (tokenDecimals + oracleDecimals));
balance_ = ((_balance * _rate)).divu(10 ** (tokenDecimals + oracleDecimals));
}
function outputRaw(address _dst, uint256 _amount) external override returns (int128 amount_) {
require(_amount > 0, "zero amount!");
uint256 _rate = getRate();
token.safeTransfer(_dst, _amount);
amount_ = ((_amount * _rate)).divu(10 ** (tokenDecimals + oracleDecimals));
}
function outputNumeraire(address _dst, int128 _amount, bool _toETH)
external
payable
override
returns (uint256 amount_)
{
uint256 _rate = getRate();
amount_ = Math.ceilDiv(_amount.mulu(10 ** (tokenDecimals + oracleDecimals + 18)), _rate * 1e18);
require(amount_ > 0, "zero amount!");
if (_toETH) {
IWETH(wETH).withdraw(amount_);
(bool success,) = payable(_dst).call{value: amount_}("");
require(success, "Assimilator/Transfer ETH Failed");
} else {
token.safeTransfer(_dst, amount_);
}
}
function viewRawAmount(int128 _amount) external view override returns (uint256 amount_) {
uint256 _rate = getRate();
amount_ = Math.ceilDiv(_amount.mulu(10 ** (tokenDecimals + oracleDecimals + 18)), _rate * 1e18);
}
function viewRawAmountLPRatio(uint256 _baseWeight, uint256 _pairTokenWeight, address _addr, int128 _amount)
external
view
override
returns (uint256 amount_)
{
uint256 _tokenBal = token.balanceOf(_addr);
if (_tokenBal <= 0) return 0;
_tokenBal = _tokenBal.mul(10 ** (18 + pairTokenDecimals)).div(_baseWeight);
uint256 _pairTokenBal = pairToken.balanceOf(_addr).mul(10 ** (18 + tokenDecimals)).div(_pairTokenWeight);
uint256 _rate = _pairTokenBal.mul(1e6).div(_tokenBal);
amount_ = Math.ceilDiv(_amount.mulu(10 ** tokenDecimals * 1e6 * 1e18), _rate * 1e18);
}
function viewNumeraireAmount(uint256 _amount) external view override returns (int128 amount_) {
uint256 _rate = getRate();
amount_ = ((_amount * _rate) / 10 ** oracleDecimals).divu(10 ** tokenDecimals);
}
function viewNumeraireBalance(address _addr) external view override returns (int128 balance_) {
uint256 _rate = getRate();
uint256 _balance = token.balanceOf(_addr);
if (_balance <= 0) return ABDKMath64x64.fromUInt(0);
balance_ = ((_balance * _rate) / 10 ** oracleDecimals).divu(10 ** tokenDecimals);
}
function viewNumeraireAmountAndBalance(address _addr, uint256 _amount)
external
view
override
returns (int128 amount_, int128 balance_)
{
uint256 _rate = getRate();
amount_ = ((_amount * _rate) / 10 ** oracleDecimals).divu(10 ** tokenDecimals);
uint256 _balance = token.balanceOf(_addr);
balance_ = ((_balance * _rate) / 10 ** oracleDecimals).divu(10 ** tokenDecimals);
}
function viewNumeraireBalanceLPRatio(uint256 _baseWeight, uint256 _pairTokenWeight, address _addr)
external
view
override
returns (int128 balance_)
{
uint256 _tokenBal = token.balanceOf(_addr);
if (_tokenBal <= 0) return ABDKMath64x64.fromUInt(0);
uint256 _pairTokenBal = pairToken.balanceOf(_addr).mul(1e18).div(_pairTokenWeight);
uint256 _rate = _pairTokenBal.mul(1e18).div(_tokenBal.mul(1e18).div(_baseWeight));
balance_ = ((_tokenBal * _rate) / 10 ** pairTokenDecimals).divu(1e18);
}
function transferFee(int128 _amount, address _treasury) external payable override {
uint256 _rate = getRate();
if (_amount < 0) _amount = -(_amount);
uint256 amount = _amount.mulu(10 ** (tokenDecimals + oracleDecimals + 18)) / (_rate * 1e18);
token.safeTransfer(_treasury, amount);
}
}
文件 4 的 31:Assimilators.sol
pragma solidity ^0.8.13;
import "@openzeppelin/contracts/utils/Address.sol";
import "./interfaces/IAssimilator.sol";
import "./lib/ABDKMath64x64.sol";
import "./Structs.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, IntakeNumLpRatioInfo memory info) internal returns (uint256 amt_) {
bytes memory data = abi.encodeWithSelector(
iAsmltr.intakeNumeraireLPRatio.selector,
info.minBase,
info.maxBase,
info.baseAmt,
info.minQuote,
info.maxQuote,
info.quoteAmt,
info.token0
);
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, bool _toETH) internal returns (uint256 amt_) {
bytes memory data = abi.encodeWithSelector(iAsmltr.outputNumeraire.selector, _dst, _amt.abs(), _toETH);
amt_ = abi.decode(delegate(_assim, data), (uint256));
}
function transferFee(address _assim, int128 _amt, address _treasury) internal {
bytes memory data = abi.encodeWithSelector(iAsmltr.transferFee.selector, _amt, _treasury);
delegate(_assim, data);
}
}
文件 5 的 31:Context.sol
pragma solidity ^0.8.0;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
文件 6 的 31:Curve.sol
pragma solidity ^0.8.13;
pragma experimental ABIEncoderV2;
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./interfaces/IWeth.sol";
import "./interfaces/IAssimilatorFactory.sol";
import "./lib/ABDKMath64x64.sol";
import "./lib/FullMath.sol";
import "./lib/NoDelegateCall.sol";
import "./Orchestrator.sol";
import "./ProportionalLiquidity.sol";
import "./Swaps.sol";
import "./ViewLiquidity.sol";
import "./Storage.sol";
import "./interfaces/ICurveFactory.sol";
import "./interfaces/IAssimilator.sol";
import "./interfaces/ICurve.sol";
import "./interfaces/IConfig.sol";
import "./Structs.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, NoDelegateCall, ICurve {
using SafeMath for uint256;
using ABDKMath64x64 for int128;
using SafeERC20 for IERC20;
address private curveFactory;
address private immutable wETH;
IConfig private config;
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 Trade(
address indexed trader,
address indexed origin,
address indexed target,
uint256 originAmount,
uint256 targetAmount,
int128 rawProtocolFee
);
event Transfer(address indexed from, address indexed to, uint256 value);
modifier onlyOwner() {
require(msg.sender == owner || msg.sender == config.getProtocolTreasury(), "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 isNotEmergency() {
require(!emergency, "Curve/emergency-only-allowing-emergency-proportional-withdraw");
_;
}
modifier deadline(uint256 _deadline) {
require(block.timestamp < _deadline, "Curve/tx-deadline-passed");
_;
}
modifier globallyTransactable() {
require(!config.getGlobalFrozenState(), "Curve/frozen-globally-only-allowing-proportional-withdraw");
_;
}
constructor(
string memory _name,
string memory _symbol,
address[] memory _assets,
uint256[] memory _assetWeights,
address _factory,
address _config
) {
require(_factory != address(0), "Curve/curve factory zero address!");
owner = msg.sender;
name = _name;
symbol = _symbol;
curveFactory = _factory;
config = IConfig(_config);
emit OwnershipTransfered(address(0), msg.sender);
wETH = ICurveFactory(_factory).wETH();
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 setAssimilator(address _baseCurrency, address _baseAssim, address _quoteCurrency, address _quoteAssim)
external
onlyOwner
{
Orchestrator.setAssimilator(curve, _baseCurrency, _baseAssim, _quoteCurrency, _quoteAssim);
}
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 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-zero-address");
emit OwnershipTransfered(owner, _newOwner);
owner = _newOwner;
}
function originSwap(
address _origin,
address _target,
uint256 _originAmount,
uint256 _minTargetAmount,
uint256 _deadline
)
external
deadline(_deadline)
globallyTransactable
transactable
noDelegateCall
isNotEmergency
nonReentrant
returns (uint256 targetAmount_)
{
OriginSwapData memory _swapData;
_swapData._origin = _origin;
_swapData._target = _target;
_swapData._originAmount = _originAmount;
_swapData._recipient = msg.sender;
_swapData._curveFactory = curveFactory;
uint256 balanceBefore = IERC20(_target).balanceOf(_swapData._recipient);
Swaps.originSwap(curve, _swapData, false);
uint256 balanceAfter = IERC20(_target).balanceOf(_swapData._recipient);
targetAmount_ = balanceAfter - balanceBefore;
require(targetAmount_ >= _minTargetAmount, "Curve/below-min-target-amount");
}
function originSwapFromETH(address _target, uint256 _minTargetAmount, uint256 _deadline)
external
payable
deadline(_deadline)
globallyTransactable
transactable
noDelegateCall
isNotEmergency
nonReentrant
returns (uint256 targetAmount_)
{
IWETH(wETH).deposit{value: msg.value}();
IERC20(wETH).safeTransferFrom(address(this), msg.sender, msg.value);
OriginSwapData memory _swapData;
_swapData._origin = wETH;
_swapData._target = _target;
_swapData._originAmount = msg.value;
_swapData._recipient = msg.sender;
_swapData._curveFactory = curveFactory;
targetAmount_ = Swaps.originSwap(curve, _swapData, false);
require(targetAmount_ >= _minTargetAmount, "Curve/below-min-target-amount");
}
function originSwapToETH(address _origin, uint256 _originAmount, uint256 _minTargetAmount, uint256 _deadline)
external
deadline(_deadline)
globallyTransactable
transactable
noDelegateCall
isNotEmergency
nonReentrant
returns (uint256 targetAmount_)
{
OriginSwapData memory _swapData;
_swapData._origin = _origin;
_swapData._target = wETH;
_swapData._originAmount = _originAmount;
_swapData._recipient = msg.sender;
_swapData._curveFactory = curveFactory;
targetAmount_ = Swaps.originSwap(curve, _swapData, true);
require(targetAmount_ >= _minTargetAmount, "Curve/below-min-target-amount");
}
function viewOriginSwap(address _origin, address _target, uint256 _originAmount)
external
view
globallyTransactable
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)
globallyTransactable
transactable
noDelegateCall
isNotEmergency
nonReentrant
returns (uint256 originAmount_)
{
TargetSwapData memory _swapData;
_swapData._origin = _origin;
_swapData._target = _target;
_swapData._targetAmount = _targetAmount;
_swapData._recipient = msg.sender;
_swapData._curveFactory = curveFactory;
originAmount_ = Swaps.targetSwap(curve, _swapData);
require(originAmount_ <= _maxOriginAmount, "Curve/above-max-origin-amount");
}
function viewTargetSwap(address _origin, address _target, uint256 _targetAmount)
external
view
globallyTransactable
transactable
returns (uint256 originAmount_)
{
originAmount_ = Swaps.viewTargetSwap(curve, _origin, _target, _targetAmount);
}
function deposit(
uint256 _deposit,
uint256 _minQuoteAmount,
uint256 _minBaseAmount,
uint256 _maxQuoteAmount,
uint256 _maxBaseAmount,
uint256 _deadline
)
external
deadline(_deadline)
globallyTransactable
transactable
nonReentrant
noDelegateCall
isNotEmergency
returns (uint256 curvesMinted_, uint256[] memory deposits_)
{
require(_deposit > 0, "Curve/deposit_below_zero");
(curvesMinted_, deposits_) = viewDeposit(_deposit);
DepositData memory _depositData;
_depositData.deposits = _deposit;
_depositData.minQuote = _minQuoteAmount;
_depositData.minBase = _minBaseAmount;
_depositData.maxQuote = _maxQuoteAmount;
_depositData.maxBase = _maxBaseAmount;
_depositData.baseAmt = deposits_[0];
_depositData.quoteAmt = deposits_[1];
_depositData.token0 = reserves[0];
_depositData.token0Bal = IERC20(reserves[0]).balanceOf(address(this));
_depositData.token1Bal = IERC20(reserves[1]).balanceOf(address(this));
(curvesMinted_, deposits_) = ProportionalLiquidity.proportionalDeposit(curve, _depositData);
return (curvesMinted_, deposits_);
}
function depositETH(
uint256 _deposit,
uint256 _minQuoteAmount,
uint256 _minBaseAmount,
uint256 _maxQuoteAmount,
uint256 _maxBaseAmount,
uint256 _deadline
)
external
payable
deadline(_deadline)
globallyTransactable
transactable
nonReentrant
noDelegateCall
isNotEmergency
returns (uint256 curvesMinted_, uint256[] memory deposits_)
{
require(_deposit > 0, "Curve/deposit_below_zero");
(curvesMinted_, deposits_) = viewDeposit(_deposit);
IWETH(wETH).deposit{value: msg.value}();
IERC20(wETH).safeTransferFrom(address(this), msg.sender, msg.value);
DepositData memory _depositData;
_depositData.deposits = _deposit;
_depositData.minQuote = _minQuoteAmount;
_depositData.minBase = _minBaseAmount;
_depositData.maxQuote = _maxQuoteAmount;
_depositData.maxBase = _maxBaseAmount;
_depositData.baseAmt = deposits_[0];
_depositData.quoteAmt = deposits_[1];
_depositData.token0 = reserves[0];
_depositData.token0Bal = IERC20(reserves[0]).balanceOf(address(this));
_depositData.token1Bal = IERC20(reserves[1]).balanceOf(address(this));
(curvesMinted_, deposits_) = ProportionalLiquidity.proportionalDeposit(curve, _depositData);
uint256 remainder = 0;
if (IAssimilator(curve.assets[0].addr).underlyingToken() == wETH) {
remainder = msg.value - deposits_[0];
} else if (IAssimilator(curve.assets[1].addr).underlyingToken() == wETH) {
remainder = msg.value - deposits_[1];
} else {
revert("Curve/Deposit ETH failed");
}
if (remainder > 0) {
IERC20(wETH).safeTransferFrom(msg.sender, address(this), remainder);
IWETH(wETH).withdraw(remainder);
(bool success,) = msg.sender.call{value: remainder}("");
require(success, "Curve/ETH transfer failed");
}
return (curvesMinted_, deposits_);
}
function viewDeposit(uint256 _deposit)
public
view
globallyTransactable
transactable
returns (uint256, uint256[] memory)
{
uint256 deposit_;
uint256[] memory outs_ = new uint256[](2);
(deposit_, outs_) = ProportionalLiquidity.viewProportionalDeposit(curve, _deposit);
uint256 ratio = (_deposit * 1e36) / deposit_;
outs_[0] = (outs_[0] * ratio) / 1e36;
outs_[1] = (outs_[1] * ratio) / 1e36;
return (_deposit, outs_);
}
function emergencyWithdraw(uint256 _curvesToBurn, uint256 _deadline)
external
isEmergency
deadline(_deadline)
nonReentrant
noDelegateCall
returns (uint256[] memory withdrawals_)
{
return ProportionalLiquidity.proportionalWithdraw(curve, _curvesToBurn, false);
}
function withdraw(uint256 _curvesToBurn, uint256 _deadline)
external
deadline(_deadline)
nonReentrant
noDelegateCall
isNotEmergency
returns (uint256[] memory withdrawals_)
{
return ProportionalLiquidity.proportionalWithdraw(curve, _curvesToBurn, false);
}
function withdrawETH(uint256 _curvesToBurn, uint256 _deadline)
external
deadline(_deadline)
nonReentrant
noDelegateCall
isNotEmergency
returns (uint256[] memory withdrawals_)
{
return ProportionalLiquidity.proportionalWithdraw(curve, _curvesToBurn, true);
}
function viewWithdraw(uint256 _curvesToBurn)
external
view
globallyTransactable
transactable
returns (uint256[] memory)
{
return ProportionalLiquidity.viewProportionalWithdraw(curve, _curvesToBurn);
}
function getWeth() external view override returns (address) {
return wETH;
}
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
noDelegateCall
isNotEmergency
returns (bool success_)
{
success_ = Curves.transfer(curve, _recipient, _amount);
}
function transferFrom(address _sender, address _recipient, uint256 _amount)
public
nonReentrant
noDelegateCall
isNotEmergency
returns (bool success_)
{
success_ = Curves.transferFrom(curve, _sender, _recipient, _amount);
}
function approve(address _spender, uint256 _amount) public nonReentrant noDelegateCall 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;
}
receive() external payable {}
}
文件 7 的 31:CurveMath.sol
pragma solidity ^0.8.13;
import "./Storage.sol";
import "./lib/UnsafeMath64x64.sol";
import "./lib/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.mul(_delta);
fee_ = fee_.div(_ideal);
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.mul(_delta);
fee_ = fee_.div(_ideal);
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);
}
}
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 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-1");
if (_nBals[i] - _nHalt > _oBals[i] - _oHalt) {
revert("Curve/upper-halt-2");
}
}
} 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");
}
}
}
}
}
}
文件 8 的 31:ERC20.sol
pragma solidity ^0.8.0;
import "./IERC20.sol";
import "./extensions/IERC20Metadata.sol";
import "../../utils/Context.sol";
contract ERC20 is Context, IERC20, IERC20Metadata {
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
uint256 private _totalSupply;
string private _name;
string private _symbol;
constructor(string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
}
function name() public view virtual override returns (string memory) {
return _name;
}
function symbol() public view virtual override returns (string memory) {
return _symbol;
}
function decimals() public view virtual override returns (uint8) {
return 18;
}
function totalSupply() public view virtual override returns (uint256) {
return _totalSupply;
}
function balanceOf(address account) public view virtual override returns (uint256) {
return _balances[account];
}
function transfer(address to, uint256 amount) public virtual override returns (bool) {
address owner = _msgSender();
_transfer(owner, to, 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) {
address owner = _msgSender();
_approve(owner, spender, amount);
return true;
}
function transferFrom(address from, address to, uint256 amount) public virtual override returns (bool) {
address spender = _msgSender();
_spendAllowance(from, spender, amount);
_transfer(from, to, amount);
return true;
}
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
address owner = _msgSender();
_approve(owner, spender, allowance(owner, spender) + addedValue);
return true;
}
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
address owner = _msgSender();
uint256 currentAllowance = allowance(owner, spender);
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
unchecked {
_approve(owner, spender, currentAllowance - subtractedValue);
}
return true;
}
function _transfer(address from, address to, uint256 amount) internal virtual {
require(from != address(0), "ERC20: transfer from the zero address");
require(to != address(0), "ERC20: transfer to the zero address");
_beforeTokenTransfer(from, to, amount);
uint256 fromBalance = _balances[from];
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
_balances[from] = fromBalance - amount;
_balances[to] += amount;
}
emit Transfer(from, to, amount);
_afterTokenTransfer(from, to, 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 += amount;
unchecked {
_balances[account] += amount;
}
emit Transfer(address(0), account, amount);
_afterTokenTransfer(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);
uint256 accountBalance = _balances[account];
require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
unchecked {
_balances[account] = accountBalance - amount;
_totalSupply -= amount;
}
emit Transfer(account, address(0), amount);
_afterTokenTransfer(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 _spendAllowance(address owner, address spender, uint256 amount) internal virtual {
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance != type(uint256).max) {
require(currentAllowance >= amount, "ERC20: insufficient allowance");
unchecked {
_approve(owner, spender, currentAllowance - amount);
}
}
}
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual {}
function _afterTokenTransfer(address from, address to, uint256 amount) internal virtual {}
}
文件 9 的 31:FullMath.sol
pragma solidity ^0.8.0;
library FullMath {
function mulDiv(uint256 a, uint256 b, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
uint256 prod0;
uint256 prod1;
assembly {
let mm := mulmod(a, b, not(0))
prod0 := mul(a, b)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
if (prod1 == 0) {
require(denominator > 0);
assembly {
result := div(prod0, denominator)
}
return result;
}
require(denominator > prod1);
uint256 remainder;
assembly {
remainder := mulmod(a, b, denominator)
}
assembly {
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
uint256 twos = (0 - denominator) & denominator;
assembly {
denominator := div(denominator, twos)
}
assembly {
prod0 := div(prod0, twos)
}
assembly {
twos := add(div(sub(0, twos), twos), 1)
}
prod0 |= prod1 * twos;
uint256 inv = (3 * denominator) ^ 2;
inv *= 2 - denominator * inv;
inv *= 2 - denominator * inv;
inv *= 2 - denominator * inv;
inv *= 2 - denominator * inv;
inv *= 2 - denominator * inv;
inv *= 2 - denominator * inv;
result = prod0 * inv;
return result;
}
}
function mulDivRoundingUp(uint256 a, uint256 b, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
result = mulDiv(a, b, denominator);
if (mulmod(a, b, denominator) > 0) {
require(result < type(uint256).max);
result++;
}
}
}
}
文件 10 的 31:IAssimilator.sol
pragma solidity ^0.8.13;
interface IAssimilator {
function oracleDecimals() external view returns (uint256);
function underlyingToken() external view returns (address);
function getWeth() external view returns (address);
function tokenDecimals() external view returns (uint256);
function getRate() external view returns (uint256);
function intakeRaw(uint256 amount) external payable returns (int128);
function intakeRawAndGetBalance(uint256 amount) external payable returns (int128, int128);
function intakeNumeraire(int128 amount) external payable returns (uint256);
function intakeNumeraireLPRatio(uint256, uint256, uint256, uint256, uint256, uint256, address)
external
payable
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, bool toETH) external payable 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);
function transferFee(int128, address) external payable;
}
文件 11 的 31:IAssimilatorFactory.sol
pragma solidity ^0.8.13;
import "../assimilators/AssimilatorV3.sol";
import "../interfaces/IOracle.sol";
interface IAssimilatorFactory {
function getAssimilator(address _token, address _quote) external view returns (AssimilatorV3);
function newAssimilator(address _quote, IOracle _oracle, address _token, uint256 _tokenDecimals)
external
returns (AssimilatorV3);
}
文件 12 的 31:IConfig.sol
pragma solidity ^0.8.13;
interface IConfig {
function getGlobalFrozenState() external view returns (bool);
function getProtocolFee() external view returns (int128);
function getProtocolTreasury() external view returns (address);
function setGlobalFrozen(bool) external;
function updateProtocolTreasury(address) external;
function updateProtocolFee(int128) external;
}
文件 13 的 31:ICurve.sol
pragma solidity ^0.8.13;
interface ICurve {
function getWeth() external view returns (address);
}
文件 14 的 31:ICurveFactory.sol
pragma solidity ^0.8.13;
import "./IAssimilatorFactory.sol";
interface ICurveFactory {
function getProtocolFee() external view returns (int128);
function getProtocolTreasury() external view returns (address);
function assimilatorFactory() external view returns (IAssimilatorFactory);
function wETH() external view returns (address);
function isDFXCurve(address) external view returns (bool);
}
文件 15 的 31:IERC20.sol
pragma solidity ^0.8.0;
interface IERC20 {
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, 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 from, address to, uint256 amount) external returns (bool);
}
文件 16 的 31:IERC20Metadata.sol
pragma solidity ^0.8.0;
import "../IERC20.sol";
interface IERC20Metadata is IERC20 {
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
}
文件 17 的 31:IERC20Permit.sol
pragma solidity ^0.8.0;
interface IERC20Permit {
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
function nonces(address owner) external view returns (uint256);
function DOMAIN_SEPARATOR() external view returns (bytes32);
}
文件 18 的 31:IOracle.sol
pragma solidity ^0.8.13;
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);
}
文件 19 的 31:IWeth.sol
pragma solidity ^0.8.13;
interface IWETH {
function deposit() external payable;
function transfer(address to, uint256 value) external returns (bool);
function withdraw(uint256) external;
}
文件 20 的 31:Math.sol
pragma solidity ^0.8.0;
library Math {
enum Rounding {
Down,
Up,
Zero
}
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return a > b ? a : b;
}
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
function average(uint256 a, uint256 b) internal pure returns (uint256) {
return (a & b) + (a ^ b) / 2;
}
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
return a == 0 ? 0 : (a - 1) / b + 1;
}
function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
uint256 prod0;
uint256 prod1;
assembly {
let mm := mulmod(x, y, not(0))
prod0 := mul(x, y)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
if (prod1 == 0) {
return prod0 / denominator;
}
require(denominator > prod1, "Math: mulDiv overflow");
uint256 remainder;
assembly {
remainder := mulmod(x, y, denominator)
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
uint256 twos = denominator & (~denominator + 1);
assembly {
denominator := div(denominator, twos)
prod0 := div(prod0, twos)
twos := add(div(sub(0, twos), twos), 1)
}
prod0 |= prod1 * twos;
uint256 inverse = (3 * denominator) ^ 2;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
result = prod0 * inverse;
return result;
}
}
function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
uint256 result = mulDiv(x, y, denominator);
if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
result += 1;
}
return result;
}
function sqrt(uint256 a) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 result = 1 << (log2(a) >> 1);
unchecked {
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
return min(result, a / result);
}
}
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = sqrt(a);
return result + (rounding == Rounding.Up && result * result < a ? 1 : 0);
}
}
function log2(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 128;
}
if (value >> 64 > 0) {
value >>= 64;
result += 64;
}
if (value >> 32 > 0) {
value >>= 32;
result += 32;
}
if (value >> 16 > 0) {
value >>= 16;
result += 16;
}
if (value >> 8 > 0) {
value >>= 8;
result += 8;
}
if (value >> 4 > 0) {
value >>= 4;
result += 4;
}
if (value >> 2 > 0) {
value >>= 2;
result += 2;
}
if (value >> 1 > 0) {
result += 1;
}
}
return result;
}
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log2(value);
return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0);
}
}
function log10(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >= 10 ** 64) {
value /= 10 ** 64;
result += 64;
}
if (value >= 10 ** 32) {
value /= 10 ** 32;
result += 32;
}
if (value >= 10 ** 16) {
value /= 10 ** 16;
result += 16;
}
if (value >= 10 ** 8) {
value /= 10 ** 8;
result += 8;
}
if (value >= 10 ** 4) {
value /= 10 ** 4;
result += 4;
}
if (value >= 10 ** 2) {
value /= 10 ** 2;
result += 2;
}
if (value >= 10 ** 1) {
result += 1;
}
}
return result;
}
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log10(value);
return result + (rounding == Rounding.Up && 10 ** result < value ? 1 : 0);
}
}
function log256(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 16;
}
if (value >> 64 > 0) {
value >>= 64;
result += 8;
}
if (value >> 32 > 0) {
value >>= 32;
result += 4;
}
if (value >> 16 > 0) {
value >>= 16;
result += 2;
}
if (value >> 8 > 0) {
result += 1;
}
}
return result;
}
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log256(value);
return result + (rounding == Rounding.Up && 1 << (result << 3) < value ? 1 : 0);
}
}
}
文件 21 的 31:NoDelegateCall.sol
pragma solidity ^0.8.13;
abstract contract NoDelegateCall {
address private immutable original;
constructor() {
original = address(this);
}
function checkNotDelegateCall() private view {
require(address(this) == original);
}
modifier noDelegateCall() {
checkNotDelegateCall();
_;
}
}
文件 22 的 31:Orchestrator.sol
pragma solidity ^0.8.13;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "./lib/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 setAssimilator(
Storage.Curve storage curve,
address _baseCurrency,
address _baseAssim,
address _quoteCurrency,
address _quoteAssim
) external {
require(_baseCurrency != address(0), "Curve/numeraire-cannot-be-zero-address");
require(_baseAssim != address(0), "Curve/numeraire-assimilator-cannot-be-zero-address");
require(_quoteCurrency != address(0), "Curve/reserve-cannot-be-zero-address");
require(_quoteAssim != address(0), "Curve/reserve-assimilator-cannot-be-zero-address");
Storage.Assimilator storage _baseAssimilator = curve.assimilators[_baseCurrency];
_baseAssimilator.addr = _baseAssim;
Storage.Assimilator storage _quoteAssimilator = curve.assimilators[_quoteCurrency];
_quoteAssimilator.addr = _quoteAssim;
curve.assets[0] = _baseAssimilator;
curve.assets[1] = _quoteAssimilator;
}
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-zero-address");
require(_numeraireAssim != address(0), "Curve/numeraire-assimilator-cannot-be-zero-address");
require(_reserve != address(0), "Curve/reserve-cannot-be-zero-address");
require(_reserveAssim != address(0), "Curve/reserve-assimilator-cannot-be-zero-address");
require(_weight < 1e18, "Curve/weight-must-be-less-than-one");
if (_numeraire != _reserve) {
IERC20(_numeraire).safeApprove(_reserveApproveTo, type(uint256).max);
}
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 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);
}
}
文件 23 的 31:ProportionalLiquidity.sol
pragma solidity ^0.8.13;
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "./Assimilators.sol";
import "./Storage.sol";
import "./lib/UnsafeMath64x64.sol";
import "./lib/ABDKMath64x64.sol";
import "./CurveMath.sol";
import "./Structs.sol";
import "./interfaces/IAssimilator.sol";
import "./interfaces/ICurve.sol";
library ProportionalLiquidity {
using ABDKMath64x64 for uint256;
using ABDKMath64x64 for int128;
using UnsafeMath64x64 for int128;
using SafeMath for uint256;
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, DepositData memory depositData)
external
returns (uint256 curves_, uint256[] memory)
{
int128 __deposit = depositData.deposits.divu(1e18);
uint256 _length = curve.assets.length;
uint256[] memory deposits_ = new uint256[](_length);
(int128 _oGLiq, int128[] memory _oBals) = getGrossLiquidityAndBalancesForDeposit(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++) {
IntakeNumLpRatioInfo memory info;
info.minBase = depositData.minBase;
info.maxBase = depositData.maxBase;
info.baseAmt = depositData.baseAmt;
info.minQuote = depositData.minQuote;
info.quoteAmt = depositData.quoteAmt;
info.maxQuote = depositData.maxQuote;
info.token0 = depositData.token0;
deposits_[i] = Assimilators.intakeNumeraireLPRatio(curve.assets[i].addr, info);
}
}
curves_ = depositData.deposits;
require(curves_ > 0, "Proportional Liquidity/can't mint negative amount");
mint(curve, msg.sender, curves_);
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.mul(_totalShells);
_newShells = _newShells.div(_oGLiq);
}
curves_ = _newShells.mulu(1e18);
return (curves_, deposits_);
}
function proportionalWithdraw(Storage.Curve storage curve, uint256 _withdrawal, bool _toETH)
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++) {
if (
_toETH
&& (
IAssimilator(curve.assets[i].addr).underlyingToken() == IAssimilator(curve.assets[i].addr).getWeth()
)
) {
withdrawals_[i] =
Assimilators.outputNumeraire(curve.assets[i].addr, msg.sender, _oBals[i].mul(_multiplier), true);
} else {
withdrawals_[i] =
Assimilators.outputNumeraire(curve.assets[i].addr, msg.sender, _oBals[i].mul(_multiplier), false);
}
}
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 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 {
uint256 minLock = 1e15;
if (curve.totalSupply == 0) {
require(amount > minLock, "Proportional Liquidity/amount too small!");
uint256 toMintAmt = amount - minLock;
curve.totalSupply = mintAdd(curve.totalSupply, toMintAmt);
curve.balances[account] = mintAdd(curve.balances[account], toMintAmt);
emit Transfer(address(0), msg.sender, toMintAmt);
curve.totalSupply = mintAdd(curve.totalSupply, minLock);
curve.balances[address(0)] = mintAdd(curve.balances[address(0)], minLock);
emit Transfer(address(this), address(0), minLock);
} else {
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");
}
}
文件 24 的 31:SafeERC20.sol
pragma solidity ^0.8.0;
import "../IERC20.sol";
import "../extensions/IERC20Permit.sol";
import "../../../utils/Address.sol";
library SafeERC20 {
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) + value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
uint256 newAllowance = oldAllowance - value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
}
function safePermit(
IERC20Permit token,
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
uint256 nonceBefore = token.nonces(owner);
token.permit(owner, spender, value, deadline, v, r, s);
uint256 nonceAfter = token.nonces(owner);
require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
}
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");
}
}
}
文件 25 的 31:SafeMath.sol
pragma solidity ^0.8.0;
library SafeMath {
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
}
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
if (b > a) return (false, 0);
return (true, a - b);
}
}
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
if (a == 0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
}
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
if (b == 0) return (false, 0);
return (true, a / b);
}
}
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
if (b == 0) return (false, 0);
return (true, a % b);
}
}
function add(uint256 a, uint256 b) internal pure returns (uint256) {
return a + b;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return a - b;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
return a * b;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return a / b;
}
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return a % b;
}
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
unchecked {
require(b <= a, errorMessage);
return a - b;
}
}
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
unchecked {
require(b > 0, errorMessage);
return a / b;
}
}
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
unchecked {
require(b > 0, errorMessage);
return a % b;
}
}
}
文件 26 的 31:Storage.sol
pragma solidity ^0.8.13;
import "./interfaces/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 notEntered = true;
}
文件 27 的 31:Structs.sol
pragma solidity ^0.8.13;
import "./interfaces/ICurveFactory.sol";
import "./interfaces/IOracle.sol";
struct OriginSwapData {
address _origin;
address _target;
uint256 _originAmount;
address _recipient;
address _curveFactory;
}
struct TargetSwapData {
address _origin;
address _target;
uint256 _targetAmount;
address _recipient;
address _curveFactory;
}
struct SwapInfo {
int128 totalAmount;
int128 totalFee;
int128 amountToUser;
int128 amountToTreasury;
int128 protocolFeePercentage;
address treasury;
ICurveFactory curveFactory;
}
struct DepositData {
uint256 deposits;
uint256 minQuote;
uint256 minBase;
uint256 quoteAmt;
uint256 maxQuote;
uint256 maxBase;
uint256 baseAmt;
address token0;
uint256 token0Bal;
uint256 token1Bal;
}
struct IntakeNumLpRatioInfo {
uint256 baseWeight;
uint256 minBase;
uint256 maxBase;
uint256 baseAmt;
uint256 quoteWeight;
uint256 minQuote;
uint256 maxQuote;
uint256 quoteAmt;
int128 amount;
address token0;
uint256 token0Bal;
uint256 token1Bal;
}
文件 28 的 31:Swaps.sol
pragma solidity ^0.8.13;
pragma experimental ABIEncoderV2;
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./Structs.sol";
import "./Assimilators.sol";
import "./Storage.sol";
import "./CurveMath.sol";
import "./lib/UnsafeMath64x64.sol";
import "./lib/ABDKMath64x64.sol";
library Swaps {
using ABDKMath64x64 for int128;
using ABDKMath64x64 for int256;
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 rawProtocolFee
);
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, OriginSwapData memory _swapData, bool toETH)
external
returns (uint256 tAmt_)
{
(Storage.Assimilator memory _o, Storage.Assimilator memory _t) =
getOriginAndTarget(curve, _swapData._origin, _swapData._target);
require(_swapData._origin != _swapData._target, "swap/same-origin-target");
if (_o.ix == _t.ix) {
return Assimilators.outputNumeraire(
_t.addr, _swapData._recipient, Assimilators.intakeRaw(_o.addr, _swapData._originAmount), toETH
);
}
SwapInfo memory _swapInfo;
(int128 _amt, int128 _oGLiq, int128 _nGLiq, int128[] memory _oBals, int128[] memory _nBals) =
getOriginSwapData(curve, _o.ix, _t.ix, _o.addr, _swapData._originAmount);
_swapInfo.totalAmount = _amt;
_amt = CurveMath.calculateTrade(curve, _oGLiq, _nGLiq, _oBals, _nBals, _amt, _t.ix);
_swapInfo.curveFactory = ICurveFactory(_swapData._curveFactory);
_swapInfo.amountToUser = _amt.us_mul(ONE - curve.epsilon);
_swapInfo.totalFee = _swapInfo.amountToUser - _amt;
_swapInfo.protocolFeePercentage = _swapInfo.curveFactory.getProtocolFee();
_swapInfo.treasury = _swapInfo.curveFactory.getProtocolTreasury();
_swapInfo.amountToTreasury = _swapInfo.totalFee.muli(_swapInfo.protocolFeePercentage).divi(100000);
Assimilators.transferFee(_t.addr, _swapInfo.amountToTreasury, _swapInfo.treasury);
tAmt_ = Assimilators.outputNumeraire(_t.addr, _swapData._recipient, _swapInfo.amountToUser, toETH);
emit Trade(
msg.sender, _swapData._origin, _swapData._target, _swapData._originAmount, tAmt_, _swapInfo.amountToTreasury
);
}
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, TargetSwapData memory _swapData)
external
returns (uint256 oAmt_)
{
(Storage.Assimilator memory _o, Storage.Assimilator memory _t) =
getOriginAndTarget(curve, _swapData._origin, _swapData._target);
require(_swapData._origin != _swapData._target, "swap/same-origin-target");
if (_o.ix == _t.ix) {
return Assimilators.intakeNumeraire(
_o.addr, Assimilators.outputRaw(_t.addr, _swapData._recipient, _swapData._targetAmount)
);
}
(int128 _amt, int128 _oGLiq, int128 _nGLiq, int128[] memory _oBals, int128[] memory _nBals) =
getTargetSwapData(curve, _t.ix, _o.ix, _t.addr, _swapData._recipient, _swapData._targetAmount);
_amt = CurveMath.calculateTrade(curve, _oGLiq, _nGLiq, _oBals, _nBals, _amt, _o.ix);
SwapInfo memory _swapInfo;
_swapInfo.totalAmount = _amt;
_swapInfo.curveFactory = ICurveFactory(_swapData._curveFactory);
_swapInfo.amountToUser = _amt.us_mul(ONE + curve.epsilon);
_swapInfo.totalFee = _swapInfo.amountToUser - _amt;
_swapInfo.protocolFeePercentage = _swapInfo.curveFactory.getProtocolFee();
_swapInfo.treasury = _swapInfo.curveFactory.getProtocolTreasury();
_swapInfo.amountToTreasury = _swapInfo.totalFee.muli(_swapInfo.protocolFeePercentage).divi(100000);
Assimilators.transferFee(_o.addr, _swapInfo.amountToTreasury, _swapInfo.treasury);
oAmt_ = Assimilators.intakeNumeraire(_o.addr, _swapInfo.amountToUser);
emit Trade(
msg.sender, _swapData._origin, _swapData._target, oAmt_, _swapData._targetAmount, _swapInfo.amountToTreasury
);
}
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));
}
(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);
_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_);
}
}
文件 29 的 31:UnsafeMath64x64.sol
pragma solidity ^0.8.13;
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);
}
}
文件 30 的 31:ViewLiquidity.sol
pragma solidity ^0.8.13;
import "./Storage.sol";
import "./Assimilators.sol";
import "./lib/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_);
}
}
文件 31 的 31:Zap.sol
pragma solidity ^0.8.13;
pragma experimental ABIEncoderV2;
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import "./Curve.sol";
import "./interfaces/IWeth.sol";
import "./interfaces/ICurve.sol";
import "./interfaces/IOracle.sol";
import "./interfaces/ICurveFactory.sol";
import "./assimilators/AssimilatorV3.sol";
contract Zap {
using SafeMath for uint256;
using SafeERC20 for IERC20Metadata;
struct ZapData {
address curve;
address base;
address quote;
uint256 zapAmount;
uint256 curveBaseBal;
uint8 curveBaseDecimals;
uint256 curveQuoteBal;
}
struct ZapDepositData {
uint256 curBaseAmount;
uint256 curQuoteAmount;
uint256 maxBaseAmount;
uint256 maxQuoteAmount;
}
ICurveFactory public immutable curveFactory;
modifier isDFXCurve(address _curve) {
require(curveFactory.isDFXCurve(_curve), "zap/invalid-curve");
_;
}
constructor(address _factory) {
curveFactory = ICurveFactory(_factory);
}
function unzap(
address _curve,
uint256 _lpAmount,
uint256 _deadline,
uint256 _minTokenAmount,
address _token,
bool _toETH
) public isDFXCurve(_curve) returns (uint256) {
address wETH = ICurve(_curve).getWeth();
IERC20Metadata base = IERC20Metadata(Curve(payable(_curve)).numeraires(0));
IERC20Metadata quote = IERC20Metadata(Curve(payable(_curve)).numeraires(1));
require(_token == address(base) || _token == address(quote), "zap/token-not-supported");
IERC20Metadata(_curve).safeTransferFrom(msg.sender, address(this), _lpAmount);
Curve(payable(_curve)).withdraw(_lpAmount, _deadline);
if (_token == address(base)) {
uint256 baseAmount = base.balanceOf(address(this));
base.safeApprove(_curve, 0);
base.safeApprove(_curve, type(uint256).max);
Curve(payable(_curve)).originSwap(address(base), address(quote), baseAmount, 0, _deadline);
uint256 quoteAmount = quote.balanceOf(address(this));
require(quoteAmount >= _minTokenAmount, "!Unzap/not-enough-token-amount");
if (address(quote) == wETH && _toETH) {
IWETH(wETH).withdraw(quoteAmount);
(bool success,) = payable(msg.sender).call{value: quoteAmount}("");
require(success, "zap/unzap-to-eth-failed");
} else {
quote.safeTransfer(msg.sender, quoteAmount);
}
return quoteAmount;
} else {
uint256 quoteAmount = quote.balanceOf(address(this));
quote.safeApprove(_curve, 0);
quote.safeApprove(_curve, type(uint256).max);
Curve(payable(_curve)).originSwap(address(quote), address(base), quoteAmount, 0, _deadline);
uint256 baseAmount = base.balanceOf(address(this));
require(baseAmount >= _minTokenAmount, "!Unzap/not-enough-token-amount");
if (address(base) == wETH && _toETH) {
IWETH(wETH).withdraw(baseAmount);
(bool success,) = payable(msg.sender).call{value: baseAmount}("");
require(success, "zap/unzap-to-eth-failed");
} else {
base.safeTransfer(msg.sender, baseAmount);
}
return baseAmount;
}
}
function zap(address _curve, uint256 _zapAmount, uint256 _deadline, uint256 _minLPAmount, address _token)
public
isDFXCurve(_curve)
returns (uint256)
{
uint256 _zapAmount_ = _zapAmount;
IERC20Metadata base = IERC20Metadata(Curve(payable(_curve)).numeraires(0));
IERC20Metadata quote = IERC20Metadata(Curve(payable(_curve)).numeraires(1));
require(_token == address(base) || _token == address(quote), "zap/token-not-supported");
bool isFromBase = _token == address(base) ? true : false;
(, uint256 lptAmt) =
isFromBase ? _estimateDeposit(_curve, _zapAmount, 0) : _estimateDeposit(_curve, 0, _zapAmount);
(, uint256[] memory outs) = Curve(payable(_curve)).viewDeposit(lptAmt);
uint256 swapAmount = isFromBase ? _zapAmount - outs[0] : _zapAmount - outs[1];
if (isFromBase) {
_zapFromBase(_curve, base, address(quote), swapAmount, block.timestamp + 1, _zapAmount_);
} else {
_zapFromQuote(_curve, address(base), quote, swapAmount, block.timestamp + 1, _zapAmount_);
}
return _zap(_curve, base, quote, _deadline, _minLPAmount);
}
function zapETH(address _curve, uint256 _deadline, uint256 _minLPAmount)
public
payable
isDFXCurve(_curve)
returns (uint256)
{
require(msg.value > 0, "zap/zap-amount-is-zero");
address _token = ICurve(_curve).getWeth();
IWETH(_token).deposit{value: msg.value}();
IERC20Metadata(_token).safeTransferFrom(address(this), msg.sender, msg.value);
IERC20Metadata base = IERC20Metadata(Curve(payable(_curve)).numeraires(0));
IERC20Metadata quote = IERC20Metadata(Curve(payable(_curve)).numeraires(1));
require(_token == address(base) || _token == address(quote), "zap/token-not-supported");
bool isFromBase = _token == address(base) ? true : false;
(, uint256 lptAmt) =
isFromBase ? _estimateDeposit(_curve, msg.value, 0) : _estimateDeposit(_curve, 0, msg.value);
(, uint256[] memory outs) = Curve(payable(_curve)).viewDeposit(lptAmt);
uint256 swapAmount = isFromBase ? msg.value - outs[0] : msg.value - outs[1];
if (isFromBase) {
_zapFromBase(_curve, base, address(quote), swapAmount, _deadline, msg.value);
} else {
_zapFromQuote(_curve, address(base), quote, swapAmount, _deadline, msg.value);
}
return _zap(_curve, base, quote, _deadline, _minLPAmount);
}
function _zapFromBase(
address _curve,
IERC20Metadata _base,
address _quote,
uint256 _swapAmount,
uint256 _deadline,
uint256 _zapAmount
) private {
(_base).safeTransferFrom(msg.sender, address(this), _zapAmount);
(_base).safeApprove(_curve, 0);
(_base).safeApprove(_curve, _swapAmount);
Curve(payable(_curve)).originSwap(address(_base), _quote, _swapAmount, 0, _deadline);
}
function _zapFromQuote(
address _curve,
address _base,
IERC20Metadata _quote,
uint256 _swapAmount,
uint256 _deadline,
uint256 _zapAmount
) private {
_quote.safeTransferFrom(msg.sender, address(this), _zapAmount);
_quote.safeApprove(_curve, 0);
_quote.safeApprove(_curve, _swapAmount);
Curve(payable(_curve)).originSwap(address(_quote), _base, _swapAmount, 0, _deadline);
}
function _zap(address _curve, IERC20Metadata _base, IERC20Metadata _quote, uint256 _deadline, uint256 _minLPAmount)
private
returns (uint256)
{
(uint256 depositAmount,,) = _calcDepositAmount(
_curve,
_base,
ZapDepositData({
curBaseAmount: _base.balanceOf(address(this)),
curQuoteAmount: _quote.balanceOf(address(this)),
maxBaseAmount: _base.balanceOf(address(this)),
maxQuoteAmount: _quote.balanceOf(address(this))
})
);
_base.safeApprove(_curve, 0);
_base.safeApprove(_curve, _base.balanceOf(address(this)));
_quote.safeApprove(_curve, 0);
_quote.safeApprove(_curve, _quote.balanceOf(address(this)));
(uint256 lpAmount,) =
Curve(payable(_curve)).deposit(depositAmount, 0, 0, type(uint256).max, type(uint256).max, _deadline);
require(lpAmount >= _minLPAmount, "!Zap/not-enough-lp-amount");
IERC20Metadata(_curve).safeTransfer(msg.sender, IERC20Metadata(_curve).balanceOf(address(this)));
_base.safeTransfer(msg.sender, _base.balanceOf(address(this)));
_quote.safeTransfer(msg.sender, _quote.balanceOf(address(this)));
return lpAmount;
}
function calcSwapAmountForZapFromBase(address _curve, uint256 _zapAmount) public view returns (uint256) {
(, uint256 ret) = calcSwapAmountForZap(_curve, _zapAmount, true);
return ret;
}
function calcSwapAmountForZapFromQuote(address _curve, uint256 _zapAmount) public view returns (uint256) {
(, uint256 ret) = calcSwapAmountForZap(_curve, _zapAmount, false);
return ret;
}
function calcSwapAmountForZap(address _curve, uint256 _zapAmount, bool isFromBase)
public
view
returns (address, uint256)
{
address base = Curve(payable(_curve)).reserves(0);
IERC20Metadata quote = IERC20Metadata(Curve(payable(_curve)).reserves(1));
uint256 curveBaseBal = IERC20Metadata(base).balanceOf(_curve);
uint8 curveBaseDecimals = IERC20Metadata(base).decimals();
uint256 curveQuoteBal = quote.balanceOf(_curve);
uint256 initialSwapAmount = _zapAmount.div(2);
if (isFromBase) {
return (
base,
_calcBaseSwapAmount(
initialSwapAmount,
ZapData({
curve: _curve,
base: base,
quote: address(quote),
zapAmount: _zapAmount,
curveBaseBal: curveBaseBal,
curveBaseDecimals: curveBaseDecimals,
curveQuoteBal: curveQuoteBal
})
)
);
}
return (
base,
_calcQuoteSwapAmount(
initialSwapAmount,
ZapData({
curve: _curve,
base: base,
quote: address(quote),
zapAmount: _zapAmount,
curveBaseBal: curveBaseBal,
curveBaseDecimals: curveBaseDecimals,
curveQuoteBal: curveQuoteBal
})
)
);
}
function calcMaxDepositAmountGivenQuote(address _curve, uint256 _quoteAmount)
public
view
returns (uint256, uint256, uint256[] memory)
{
(uint256 maxBaseAmount,,) = calcMaxBaseForDeposit(_curve, _quoteAmount);
address base = Curve(payable(_curve)).reserves(0);
return _calcDepositAmount(
_curve,
IERC20Metadata(base),
ZapDepositData({
curBaseAmount: maxBaseAmount,
curQuoteAmount: _quoteAmount,
maxBaseAmount: maxBaseAmount,
maxQuoteAmount: _quoteAmount
})
);
}
function calcMaxDepositAmountGivenBase(address _curve, uint256 _baseAmount)
public
view
returns (uint256, uint256, uint256[] memory)
{
(uint256 maxQuoteAmount,,) = calcMaxQuoteForDeposit(_curve, _baseAmount);
address base = Curve(payable(_curve)).reserves(0);
return _calcDepositAmount(
_curve,
IERC20Metadata(base),
ZapDepositData({
curBaseAmount: _baseAmount,
curQuoteAmount: maxQuoteAmount,
maxBaseAmount: _baseAmount,
maxQuoteAmount: maxQuoteAmount
})
);
}
function calcMaxBaseForDeposit(address _curve, uint256 _quoteAmount)
public
view
returns (uint256 baseAmount, uint256 usdAmount, uint256 lptAmount)
{
uint8 curveQuoteDecimals = IERC20Metadata(Curve(payable(_curve)).reserves(1)).decimals();
require(curveQuoteDecimals <= 18, "zap/big-decimals");
(uint256 _lptAmount, uint256[] memory outs) = Curve(payable(_curve)).viewDeposit(2e18);
baseAmount = _quoteAmount.mul(outs[0]).div(outs[1]);
lptAmount = _lptAmount.mul(_quoteAmount).div(outs[1]);
(usdAmount,) = _estimateDeposit(_curve, baseAmount, _quoteAmount);
}
function calcMaxQuoteForDeposit(address _curve, uint256 _baseAmount)
public
view
returns (uint256 quoteAmount, uint256 usdAmount, uint256 lptAmount)
{
(uint256 _lptAmount, uint256[] memory outs) = Curve(payable(_curve)).viewDeposit(2e18);
quoteAmount = _baseAmount.mul(outs[1]).div(outs[0]);
lptAmount = _lptAmount.mul(_baseAmount).div(outs[0]);
(usdAmount,) = _estimateDeposit(_curve, _baseAmount, quoteAmount);
}
function _roundDown(uint256 a) internal pure returns (uint256) {
return a.mul(99999999).div(100000000);
}
function _calcQuoteSwapAmount(uint256 initialSwapAmount, ZapData memory zapData) internal view returns (uint256) {
uint256 swapAmount = initialSwapAmount;
uint256 delta = initialSwapAmount.div(2);
uint256 recvAmount;
uint256 curveRatio;
uint256 userRatio;
for (uint256 i = 0; i < 32; i++) {
recvAmount = Curve(payable(zapData.curve)).viewOriginSwap(zapData.quote, zapData.base, swapAmount);
require(zapData.curveBaseDecimals <= 18, "zap/big-decimals");
userRatio = recvAmount.mul(10 ** (36 - uint256(zapData.curveBaseDecimals))).div(
zapData.zapAmount.sub(swapAmount).mul(1e12)
);
curveRatio = zapData.curveBaseBal.sub(recvAmount).mul(10 ** (36 - uint256(zapData.curveBaseDecimals))).div(
zapData.curveQuoteBal.add(swapAmount).mul(1e12)
);
if (userRatio.div(1e16) == curveRatio.div(1e16)) {
return swapAmount;
}
else if (userRatio > curveRatio) {
swapAmount = swapAmount.sub(delta);
} else if (userRatio < curveRatio) {
swapAmount = swapAmount.add(delta);
}
if (swapAmount > zapData.zapAmount) {
swapAmount = zapData.zapAmount - 1;
}
delta = delta.div(2);
}
revert("Zap/not-converging");
}
function _calcBaseSwapAmount(uint256 initialSwapAmount, ZapData memory zapData) internal view returns (uint256) {
uint256 swapAmount = initialSwapAmount;
uint256 delta = initialSwapAmount.div(2);
uint256 recvAmount;
uint256 curveRatio;
uint256 userRatio;
for (uint256 i = 0; i < 32; i++) {
recvAmount = Curve(payable(zapData.curve)).viewOriginSwap(zapData.base, zapData.quote, swapAmount);
require(zapData.curveBaseDecimals <= 18, "zap/big-decimals");
userRatio = zapData.zapAmount.sub(swapAmount).mul(10 ** (36 - uint256(zapData.curveBaseDecimals))).div(
recvAmount.mul(1e12)
);
curveRatio = zapData.curveBaseBal.add(swapAmount).mul(10 ** (36 - uint256(zapData.curveBaseDecimals))).div(
zapData.curveQuoteBal.sub(recvAmount).mul(1e12)
);
if (userRatio.div(1e16) == curveRatio.div(1e16)) {
return swapAmount;
}
else if (userRatio > curveRatio) {
swapAmount = swapAmount.add(delta);
} else if (userRatio < curveRatio) {
swapAmount = swapAmount.sub(delta);
}
if (swapAmount > zapData.zapAmount) {
swapAmount = zapData.zapAmount - 1;
}
delta = delta.div(2);
}
revert("Zap/not-converging");
}
function _calcDepositAmount(address _curve, IERC20Metadata _base, ZapDepositData memory dd)
internal
view
returns (uint256, uint256, uint256[] memory)
{
IERC20Metadata quote = IERC20Metadata(Curve(payable(_curve)).numeraires(1));
require(_base.decimals() <= 18, "zap/big-decimals");
uint256 curveRatio =
_base.balanceOf(_curve).mul(10 ** (36 - _base.decimals())).div(quote.balanceOf(_curve).mul(1e12));
uint256 quoteDepositAmount = dd.curQuoteAmount.mul(1e12);
uint256 baseDepositAmount = dd.curBaseAmount.mul(10 ** (18 - _base.decimals()));
uint256 depositAmount = quoteDepositAmount.add(baseDepositAmount.mul(1e18).div(curveRatio));
depositAmount = _roundDown(depositAmount);
(uint256 lps, uint256[] memory outs) = Curve(payable(_curve)).viewDeposit(depositAmount);
uint256 baseDelta = outs[0] > dd.maxBaseAmount ? outs[0].sub(dd.curBaseAmount) : 0;
uint256 quoteDelta = outs[1] > dd.maxQuoteAmount ? outs[1].sub(dd.curQuoteAmount) : 0;
if (baseDelta > 0 || quoteDelta > 0) {
dd.curBaseAmount = _roundDown(dd.curBaseAmount.sub(baseDelta));
dd.curQuoteAmount = _roundDown(dd.curQuoteAmount.sub(quoteDelta));
return _calcDepositAmount(_curve, _base, dd);
}
return (depositAmount, lps, outs);
}
function _estimateDeposit(address _curve, uint256 _baseAmt, uint256 _quoteAmt)
internal
view
returns (uint256 usdAmt, uint256 lptAmt)
{
Curve curve = Curve(payable(_curve));
IERC20Metadata base = IERC20Metadata(curve.reserves(0));
IERC20Metadata quote = IERC20Metadata(curve.reserves(1));
uint256 curveBaseAmt = base.balanceOf(_curve);
uint256 curveQuoteAmt = quote.balanceOf(_curve);
AssimilatorV3 baseAssim = AssimilatorV3(curve.assimilator(address(base)));
AssimilatorV3 quoteAssim = AssimilatorV3(curve.assimilator(address(quote)));
usdAmt = _getUsdAmount(baseAssim, base, _baseAmt) + _getUsdAmount(quoteAssim, quote, _quoteAmt);
uint256 curveTotalUsd =
_getUsdAmount(baseAssim, base, curveBaseAmt) + _getUsdAmount(quoteAssim, quote, curveQuoteAmt);
lptAmt = usdAmt.mul(curve.totalSupply()).div(curveTotalUsd);
}
function _getUsdAmount(AssimilatorV3 _assim, IERC20Metadata _token, uint256 _amt) internal view returns (uint256) {
IOracle oracle = IOracle(_assim.oracle());
(, int256 price,,,) = oracle.latestRoundData();
return _amt.mul(uint256(price)).mul(10 ** (18 - oracle.decimals())).div(10 ** (_token.decimals()));
}
}
{
"compilationTarget": {
"src/Zap.sol": "Zap"
},
"evmVersion": "paris",
"libraries": {
"src/Curve.sol:Curves": "0x85e9690c2d4c6cae31d96f9b5afe6d111d165157",
"src/Orchestrator.sol:Orchestrator": "0x1190da269ffe6f4567fed0c3c9bba01f88372254",
"src/ProportionalLiquidity.sol:ProportionalLiquidity": "0x0a7e79b7a4912ea42f82e25b9c033919b97f6fb0",
"src/Swaps.sol:Swaps": "0x74982d2fd25ca0e0dee702e40affd93bae04d3a6",
"src/ViewLiquidity.sol:ViewLiquidity": "0x1134daeaa652e11360631e3cc93e688204d9354b"
},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": [
":@forge-std/=lib/forge-std/src/",
":@openzeppelin/=lib/openzeppelin-contracts/",
":ds-test/=lib/forge-std/lib/ds-test/src/",
":forge-std/=lib/forge-std/src/",
":openzeppelin-contracts/=lib/openzeppelin-contracts/"
]
}
[{"inputs":[{"internalType":"address","name":"_factory","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"_curve","type":"address"},{"internalType":"uint256","name":"_quoteAmount","type":"uint256"}],"name":"calcMaxBaseForDeposit","outputs":[{"internalType":"uint256","name":"baseAmount","type":"uint256"},{"internalType":"uint256","name":"usdAmount","type":"uint256"},{"internalType":"uint256","name":"lptAmount","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_curve","type":"address"},{"internalType":"uint256","name":"_baseAmount","type":"uint256"}],"name":"calcMaxDepositAmountGivenBase","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_curve","type":"address"},{"internalType":"uint256","name":"_quoteAmount","type":"uint256"}],"name":"calcMaxDepositAmountGivenQuote","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_curve","type":"address"},{"internalType":"uint256","name":"_baseAmount","type":"uint256"}],"name":"calcMaxQuoteForDeposit","outputs":[{"internalType":"uint256","name":"quoteAmount","type":"uint256"},{"internalType":"uint256","name":"usdAmount","type":"uint256"},{"internalType":"uint256","name":"lptAmount","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_curve","type":"address"},{"internalType":"uint256","name":"_zapAmount","type":"uint256"},{"internalType":"bool","name":"isFromBase","type":"bool"}],"name":"calcSwapAmountForZap","outputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_curve","type":"address"},{"internalType":"uint256","name":"_zapAmount","type":"uint256"}],"name":"calcSwapAmountForZapFromBase","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_curve","type":"address"},{"internalType":"uint256","name":"_zapAmount","type":"uint256"}],"name":"calcSwapAmountForZapFromQuote","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"curveFactory","outputs":[{"internalType":"contract ICurveFactory","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_curve","type":"address"},{"internalType":"uint256","name":"_lpAmount","type":"uint256"},{"internalType":"uint256","name":"_deadline","type":"uint256"},{"internalType":"uint256","name":"_minTokenAmount","type":"uint256"},{"internalType":"address","name":"_token","type":"address"},{"internalType":"bool","name":"_toETH","type":"bool"}],"name":"unzap","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_curve","type":"address"},{"internalType":"uint256","name":"_zapAmount","type":"uint256"},{"internalType":"uint256","name":"_deadline","type":"uint256"},{"internalType":"uint256","name":"_minLPAmount","type":"uint256"},{"internalType":"address","name":"_token","type":"address"}],"name":"zap","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_curve","type":"address"},{"internalType":"uint256","name":"_deadline","type":"uint256"},{"internalType":"uint256","name":"_minLPAmount","type":"uint256"}],"name":"zapETH","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"payable","type":"function"}]