PHP WebShell
Текущая директория: /usr/lib/node_modules/bitgo/node_modules/@celo/contractkit/lib/wrappers
Просмотр файла: Exchange.js
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExchangeWrapper = void 0;
var bignumber_js_1 = __importDefault(require("bignumber.js"));
var BaseWrapper_1 = require("./BaseWrapper");
/**
* Contract that allows to exchange StableToken for GoldToken and vice versa
* using a Constant Product Market Maker Model aka Mento
*/
var ExchangeWrapper = /** @class */ (function (_super) {
__extends(ExchangeWrapper, _super);
function ExchangeWrapper() {
var _this = _super !== null && _super.apply(this, arguments) || this;
/**
* Query spread parameter
* @returns Current spread charged on exchanges
*/
_this.spread = (0, BaseWrapper_1.proxyCall)(_this.contract.methods.spread, undefined, BaseWrapper_1.fixidityValueToBigNumber);
/**
* Query reserve fraction parameter
* @returns Current fraction to commit to the gold bucket
*/
_this.reserveFraction = (0, BaseWrapper_1.proxyCall)(_this.contract.methods.reserveFraction, undefined, BaseWrapper_1.fixidityValueToBigNumber);
/**
* Query update frequency parameter
* @returns The time period that needs to elapse between bucket
* updates
*/
_this.updateFrequency = (0, BaseWrapper_1.proxyCall)(_this.contract.methods.updateFrequency, undefined, BaseWrapper_1.valueToBigNumber);
/**
* Query minimum reports parameter
* @returns The minimum number of fresh reports that need to be
* present in the oracle to update buckets
* commit to the gold bucket
*/
_this.minimumReports = (0, BaseWrapper_1.proxyCall)(_this.contract.methods.minimumReports, undefined, BaseWrapper_1.valueToBigNumber);
/**
* Query last bucket update
* @returns The timestamp of the last time exchange buckets were updated.
*/
_this.lastBucketUpdate = (0, BaseWrapper_1.proxyCall)(_this.contract.methods.lastBucketUpdate, undefined, BaseWrapper_1.valueToBigNumber);
/**
* DEPRECATED: use function sell
* Exchanges sellAmount of sellToken in exchange for at least minBuyAmount of buyToken
* Requires the sellAmount to have been approved to the exchange
* @param sellAmount The amount of sellToken the user is selling to the exchange
* @param minBuyAmount The minimum amount of buyToken the user has to receive for this
* transaction to succeed
* @param sellGold `true` if gold is the sell token
* @return The amount of buyToken that was transfered
*/
_this.exchange = (0, BaseWrapper_1.proxySend)(_this.connection, _this.contract.methods.exchange, (0, BaseWrapper_1.tupleParser)(BaseWrapper_1.valueToString, BaseWrapper_1.valueToString, BaseWrapper_1.identity));
/**
* Sells sellAmount of sellToken in exchange for at least minBuyAmount of buyToken
* Requires the sellAmount to have been approved to the exchange
* @param sellAmount The amount of sellToken the user is selling to the exchange
* @param minBuyAmount The minimum amount of buyToken the user has to receive for this
* transaction to succeed
* @param sellGold `true` if gold is the sell token
* @return The amount of buyToken that was transfered
*/
_this.sell = (0, BaseWrapper_1.proxySend)(_this.connection, _this.contract.methods.sell, (0, BaseWrapper_1.tupleParser)(BaseWrapper_1.valueToString, BaseWrapper_1.valueToString, BaseWrapper_1.identity));
/**
* Sells sellAmount of sellToken in exchange for at least minBuyAmount of buyToken
* Requires the sellAmount to have been approved to the exchange
* @param buyAmount The amount of sellToken the user is selling to the exchange
* @param maxSellAmount The maximum amount of sellToken the user will sell for this
* transaction to succeed
* @param buyGold `true` if gold is the buy token
* @return The amount of buyToken that was transfered
*/
_this.buy = (0, BaseWrapper_1.proxySend)(_this.connection, _this.contract.methods.buy, (0, BaseWrapper_1.tupleParser)(BaseWrapper_1.valueToString, BaseWrapper_1.valueToString, BaseWrapper_1.identity));
/**
* Returns the buy token and sell token bucket sizes, in order. The ratio of
* the two also represents the exchange rate between the two.
* @param sellGold `true` if gold is the sell token
* @return [buyTokenBucket, sellTokenBucket]
*/
_this.getBuyAndSellBuckets = (0, BaseWrapper_1.proxyCall)(_this.contract.methods.getBuyAndSellBuckets, undefined, function (callRes) {
return [(0, BaseWrapper_1.valueToBigNumber)(callRes[0]), (0, BaseWrapper_1.valueToBigNumber)(callRes[1])];
});
/**
* Sell amount of CELO in exchange for at least minStableAmount of the stable token
* Requires the amount to have been approved to the exchange
* @param amount The amount of CELO the user is selling to the exchange
* @param minStableAmount The minimum amount of the stable token the user has to receive for this
* transaction to succeed
*/
_this.sellGold = function (amount, minStableAmount) {
return _this.sell(amount, minStableAmount, true);
};
/**
* Sell amount of the stable token in exchange for at least minGoldAmount of CELO
* Requires the amount to have been approved to the exchange
* @param amount The amount of the stable token the user is selling to the exchange
* @param minGoldAmount The minimum amount of CELO the user has to receive for this
* transaction to succeed
*/
_this.sellStable = function (amount, minGoldAmount) {
return _this.sell(amount, minGoldAmount, false);
};
/**
* Deprecated alias of sellStable.
* Sell amount of the stable token in exchange for at least minGoldAmount of CELO
* Requires the amount to have been approved to the exchange
* @deprecated use sellStable instead
* @param amount The amount of the stable token the user is selling to the exchange
* @param minGoldAmount The minimum amount of CELO the user has to receive for this
* transaction to succeed
*/
_this.sellDollar = _this.sellStable;
/**
* Buy amount of CELO in exchange for at most maxStableAmount of the stable token
* Requires the amount to have been approved to the exchange
* @param amount The amount of CELO the user is buying from the exchange
* @param maxStableAmount The maximum amount of the stable token the user will pay for this
* transaction to succeed
*/
_this.buyGold = function (amount, maxStableAmount) {
return _this.buy(amount, maxStableAmount, true);
};
/**
* Buy amount of the stable token in exchange for at least minGoldAmount of CELO
* Requires the amount to have been approved to the exchange
* @param amount The amount of the stable token the user is selling to the exchange
* @param maxGoldAmount The maximum amount of CELO the user will pay for this
* transaction to succeed
*/
_this.buyStable = function (amount, maxGoldAmount) {
return _this.buy(amount, maxGoldAmount, false);
};
/**
* Deprecated alias of buyStable.
* Buy amount of the stable token in exchange for at least minGoldAmount of CELO
* Requires the amount to have been approved to the exchange
* @deprecated use buyStable instead
* @param amount The amount of the stable token the user is selling to the exchange
* @param maxGoldAmount The maximum amount of CELO the user will pay for this
* transaction to succeed
*/
_this.buyDollar = _this.buyStable;
/**
* Returns the amount of CELO a user would get for sellAmount of the stable token
* @param sellAmount The amount of the stable token the user is selling to the exchange
* @return The corresponding CELO amount.
*/
_this.quoteStableSell = function (sellAmount) { return _this.getBuyTokenAmount(sellAmount, false); };
/**
* Deprecated alias of quoteStableSell.
* Returns the amount of CELO a user would get for sellAmount of the stable token
* @deprecated Use quoteStableSell instead
* @param sellAmount The amount of the stable token the user is selling to the exchange
* @return The corresponding CELO amount.
*/
_this.quoteUsdSell = _this.quoteStableSell;
/**
* Returns the amount of the stable token a user would get for sellAmount of CELO
* @param sellAmount The amount of CELO the user is selling to the exchange
* @return The corresponding stable token amount.
*/
_this.quoteGoldSell = function (sellAmount) { return _this.getBuyTokenAmount(sellAmount, true); };
/**
* Returns the amount of CELO a user would need to exchange to receive buyAmount of
* the stable token.
* @param buyAmount The amount of the stable token the user would like to purchase.
* @return The corresponding CELO amount.
*/
_this.quoteStableBuy = function (buyAmount) { return _this.getSellTokenAmount(buyAmount, false); };
/**
* Deprecated alias of quoteStableBuy.
* Returns the amount of CELO a user would need to exchange to receive buyAmount of
* the stable token.
* @deprecated Use quoteStableBuy instead
* @param buyAmount The amount of the stable token the user would like to purchase.
* @return The corresponding CELO amount.
*/
_this.quoteUsdBuy = _this.quoteStableBuy;
/**
* Returns the amount of the stable token a user would need to exchange to receive buyAmount of
* CELO.
* @param buyAmount The amount of CELO the user would like to purchase.
* @return The corresponding stable token amount.
*/
_this.quoteGoldBuy = function (buyAmount) { return _this.getSellTokenAmount(buyAmount, true); };
/**
* Returns the exchange rate for the stable token estimated at the buyAmount
* @param buyAmount The amount of the stable token in wei to estimate the exchange rate at
* @return The exchange rate (number of CELO received for one stable token)
*/
_this.getStableExchangeRate = function (buyAmount) { return _this.getExchangeRate(buyAmount, false); };
/**
* Deprecated alias of getStableExchangeRate.
* Returns the exchange rate for the stable token estimated at the buyAmount
* @deprecated Use getStableExchangeRate instead
* @param buyAmount The amount of the stable token in wei to estimate the exchange rate at
* @return The exchange rate (number of CELO received for one stable token)
*/
_this.getUsdExchangeRate = _this.getStableExchangeRate;
/**
* Returns the exchange rate for CELO estimated at the buyAmount
* @param buyAmount The amount of CELO in wei to estimate the exchange rate at
* @return The exchange rate (number of stable tokens received for one CELO)
*/
_this.getGoldExchangeRate = function (buyAmount) { return _this.getExchangeRate(buyAmount, true); };
return _this;
}
/**
* @dev Returns the amount of buyToken a user would get for sellAmount of sellToken
* @param sellAmount The amount of sellToken the user is selling to the exchange
* @param sellGold `true` if gold is the sell token
* @return The corresponding buyToken amount.
*/
ExchangeWrapper.prototype.getBuyTokenAmount = function (sellAmount, sellGold) {
return __awaiter(this, void 0, void 0, function () {
var sell, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
sell = (0, BaseWrapper_1.valueToString)(sellAmount);
if (new bignumber_js_1.default(sell).eq(0)) {
return [2 /*return*/, new bignumber_js_1.default(0)];
}
return [4 /*yield*/, this.contract.methods.getBuyTokenAmount(sell, sellGold).call()];
case 1:
res = _a.sent();
return [2 /*return*/, (0, BaseWrapper_1.valueToBigNumber)(res)];
}
});
});
};
/**
* Returns the amount of sellToken a user would need to exchange to receive buyAmount of
* buyToken.
* @param buyAmount The amount of buyToken the user would like to purchase.
* @param sellGold `true` if gold is the sell token
* @return The corresponding sellToken amount.
*/
ExchangeWrapper.prototype.getSellTokenAmount = function (buyAmount, sellGold) {
return __awaiter(this, void 0, void 0, function () {
var buy, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
buy = (0, BaseWrapper_1.valueToString)(buyAmount);
if (new bignumber_js_1.default(buy).eq(0)) {
return [2 /*return*/, new bignumber_js_1.default(0)];
}
return [4 /*yield*/, this.contract.methods.getSellTokenAmount(buy, sellGold).call()];
case 1:
res = _a.sent();
return [2 /*return*/, (0, BaseWrapper_1.valueToBigNumber)(res)];
}
});
});
};
/**
* @dev Returns the current configuration of the exchange contract
* @return ExchangeConfig object
*/
ExchangeWrapper.prototype.getConfig = function () {
return __awaiter(this, void 0, void 0, function () {
var res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Promise.all([
this.spread(),
this.reserveFraction(),
this.updateFrequency(),
this.minimumReports(),
this.lastBucketUpdate(),
])];
case 1:
res = _a.sent();
return [2 /*return*/, {
spread: res[0],
reserveFraction: res[1],
updateFrequency: res[2],
minimumReports: res[3],
lastBucketUpdate: res[4],
}];
}
});
});
};
/**
* @dev Returns human readable configuration of the exchange contract
* @return ExchangeConfig object
*/
ExchangeWrapper.prototype.getHumanReadableConfig = function () {
return __awaiter(this, void 0, void 0, function () {
var config;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getConfig()];
case 1:
config = _a.sent();
return [2 /*return*/, __assign(__assign({}, config), { updateFrequency: (0, BaseWrapper_1.secondsToDurationString)(config.updateFrequency), lastBucketUpdate: (0, BaseWrapper_1.unixSecondsTimestampToDateString)(config.lastBucketUpdate) })];
}
});
});
};
/**
* Returns the exchange rate estimated at buyAmount.
* @param buyAmount The amount of buyToken in wei to estimate the exchange rate at
* @param sellGold `true` if gold is the sell token
* @return The exchange rate (number of sellTokens received for one buyToken).
*/
ExchangeWrapper.prototype.getExchangeRate = function (buyAmount, sellGold) {
return __awaiter(this, void 0, void 0, function () {
var takerAmount;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getBuyTokenAmount(buyAmount, sellGold)];
case 1:
takerAmount = _a.sent();
return [2 /*return*/, (0, BaseWrapper_1.valueToFrac)(buyAmount, takerAmount)]; // Number of sellTokens received for one buyToken
}
});
});
};
return ExchangeWrapper;
}(BaseWrapper_1.BaseWrapper));
exports.ExchangeWrapper = ExchangeWrapper;
//# sourceMappingURL=Exchange.js.mapВыполнить команду
Для локальной разработки. Не используйте в интернете!