PHP WebShell
Текущая директория: /usr/lib/node_modules/bitgo/node_modules/@celo/contractkit/lib/wrappers
Просмотр файла: SortedOracles.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 __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 };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SortedOraclesWrapper = exports.MedianRelation = void 0;
var address_1 = require("@celo/base/lib/address");
var connect_1 = require("@celo/connect");
var address_2 = require("@celo/utils/lib/address");
var fixidity_1 = require("@celo/utils/lib/fixidity");
var base_1 = require("../base");
var celo_tokens_1 = require("../celo-tokens");
var BaseWrapper_1 = require("./BaseWrapper");
var MedianRelation;
(function (MedianRelation) {
MedianRelation[MedianRelation["Undefined"] = 0] = "Undefined";
MedianRelation[MedianRelation["Lesser"] = 1] = "Lesser";
MedianRelation[MedianRelation["Greater"] = 2] = "Greater";
MedianRelation[MedianRelation["Equal"] = 3] = "Equal";
})(MedianRelation = exports.MedianRelation || (exports.MedianRelation = {}));
/**
* Currency price oracle contract.
*/
var SortedOraclesWrapper = /** @class */ (function (_super) {
__extends(SortedOraclesWrapper, _super);
function SortedOraclesWrapper(connection, contract, registry) {
var _this = _super.call(this, connection, contract) || this;
_this.connection = connection;
_this.contract = contract;
_this.registry = registry;
/**
* Returns the report expiry parameter.
* @returns Current report expiry.
*/
_this.reportExpirySeconds = (0, BaseWrapper_1.proxyCall)(_this.contract.methods.reportExpirySeconds, undefined, BaseWrapper_1.valueToBigNumber);
/**
* Helper function to get the rates for StableToken, by passing the address
* of StableToken to `getRates`.
*/
_this.getStableTokenRates = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
return [2 /*return*/, this.getRates(base_1.CeloContract.StableToken)
/**
* Gets all elements from the doubly linked list.
* @param target The ReportTarget, either CeloToken or currency pair in question
* @return An unpacked list of elements from largest to smallest.
*/
];
}); }); };
return _this;
}
/**
* Gets the number of rates that have been reported for the given target
* @param target The ReportTarget, either CeloToken or currency pair
* @return The number of reported oracle rates for `token`.
*/
SortedOraclesWrapper.prototype.numRates = function (target) {
return __awaiter(this, void 0, void 0, function () {
var identifier, response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.toCurrencyPairIdentifier(target)];
case 1:
identifier = _a.sent();
return [4 /*yield*/, this.contract.methods.numRates(identifier).call()];
case 2:
response = _a.sent();
return [2 /*return*/, (0, BaseWrapper_1.valueToInt)(response)];
}
});
});
};
/**
* Returns the median rate for the given target
* @param target The ReportTarget, either CeloToken or currency pair
* @return The median exchange rate for `token`, expressed as:
* amount of that token / equivalent amount in CELO
*/
SortedOraclesWrapper.prototype.medianRate = function (target) {
return __awaiter(this, void 0, void 0, function () {
var identifier, response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.toCurrencyPairIdentifier(target)];
case 1:
identifier = _a.sent();
return [4 /*yield*/, this.contract.methods.medianRate(identifier).call()];
case 2:
response = _a.sent();
return [2 /*return*/, {
rate: (0, BaseWrapper_1.valueToFrac)(response[0], response[1]),
}];
}
});
});
};
/**
* Checks if the given address is whitelisted as an oracle for the target
* @param target The ReportTarget, either CeloToken or currency pair
* @param oracle The address that we're checking the oracle status of
* @returns boolean describing whether this account is an oracle
*/
SortedOraclesWrapper.prototype.isOracle = function (target, oracle) {
return __awaiter(this, void 0, void 0, function () {
var identifier;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.toCurrencyPairIdentifier(target)];
case 1:
identifier = _a.sent();
return [2 /*return*/, this.contract.methods.isOracle(identifier, oracle).call()];
}
});
});
};
/**
* Returns the list of whitelisted oracles for a given target
* @param target The ReportTarget, either CeloToken or currency pair
* @returns The list of whitelisted oracles for a given token.
*/
SortedOraclesWrapper.prototype.getOracles = function (target) {
return __awaiter(this, void 0, void 0, function () {
var identifier;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.toCurrencyPairIdentifier(target)];
case 1:
identifier = _a.sent();
return [2 /*return*/, this.contract.methods.getOracles(identifier).call()];
}
});
});
};
/**
* Returns the expiry for the target if exists, if not the default.
* @param target The ReportTarget, either CeloToken or currency pair
* @return The report expiry in seconds.
*/
SortedOraclesWrapper.prototype.getTokenReportExpirySeconds = function (target) {
return __awaiter(this, void 0, void 0, function () {
var identifier, response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.toCurrencyPairIdentifier(target)];
case 1:
identifier = _a.sent();
return [4 /*yield*/, this.contract.methods.getTokenReportExpirySeconds(identifier).call()];
case 2:
response = _a.sent();
return [2 /*return*/, (0, BaseWrapper_1.valueToBigNumber)(response)];
}
});
});
};
/**
* Checks if the oldest report for a given target is expired
* @param target The ReportTarget, either CeloToken or currency pair
*/
SortedOraclesWrapper.prototype.isOldestReportExpired = function (target) {
return __awaiter(this, void 0, void 0, function () {
var identifier, response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.toCurrencyPairIdentifier(target)];
case 1:
identifier = _a.sent();
return [4 /*yield*/, this.contract.methods.isOldestReportExpired(identifier).call()];
case 2:
response = _a.sent();
return [2 /*return*/, response];
}
});
});
};
/**
* Removes expired reports, if any exist
* @param target The ReportTarget, either CeloToken or currency pair
* @param numReports The upper-limit of reports to remove. For example, if there
* are 2 expired reports, and this param is 5, it will only remove the 2 that
* are expired.
*/
SortedOraclesWrapper.prototype.removeExpiredReports = function (target, numReports) {
return __awaiter(this, void 0, void 0, function () {
var identifier;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.toCurrencyPairIdentifier(target)];
case 1:
identifier = _a.sent();
if (!!numReports) return [3 /*break*/, 3];
return [4 /*yield*/, this.getReports(target)];
case 2:
numReports = (_a.sent()).length - 1;
_a.label = 3;
case 3: return [2 /*return*/, (0, connect_1.toTransactionObject)(this.connection, this.contract.methods.removeExpiredReports(identifier, numReports))];
}
});
});
};
/**
* Updates an oracle value and the median.
* @param target The ReportTarget, either CeloToken or currency pair
* @param value The amount of `token` equal to one CELO.
*/
SortedOraclesWrapper.prototype.report = function (target, value, oracleAddress) {
return __awaiter(this, void 0, void 0, function () {
var identifier, fixedValue, _a, lesserKey, greaterKey;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.toCurrencyPairIdentifier(target)];
case 1:
identifier = _b.sent();
fixedValue = (0, fixidity_1.toFixed)((0, BaseWrapper_1.valueToBigNumber)(value));
return [4 /*yield*/, this.findLesserAndGreaterKeys(target, (0, BaseWrapper_1.valueToBigNumber)(value), oracleAddress)];
case 2:
_a = _b.sent(), lesserKey = _a.lesserKey, greaterKey = _a.greaterKey;
return [2 /*return*/, (0, connect_1.toTransactionObject)(this.connection, this.contract.methods.report(identifier, fixedValue.toFixed(), lesserKey, greaterKey), { from: oracleAddress })];
}
});
});
};
/**
* Updates an oracle value and the median.
* @param value The amount of US Dollars equal to one CELO.
* @param oracleAddress The address to report as
* @param token The token to report for
*/
SortedOraclesWrapper.prototype.reportStableToken = function (value, oracleAddress, token) {
if (token === void 0) { token = celo_tokens_1.StableToken.cUSD; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.report(celo_tokens_1.stableTokenInfos[token].contract, value, oracleAddress)];
});
});
};
/**
* Returns current configuration parameters.
*/
SortedOraclesWrapper.prototype.getConfig = function () {
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = {};
return [4 /*yield*/, this.reportExpirySeconds()];
case 1: return [2 /*return*/, (_a.reportExpirySeconds = _b.sent(),
_a)];
}
});
});
};
/**
* @dev Returns human readable configuration of the sortedoracles contract
* @return SortedOraclesConfig object
*/
SortedOraclesWrapper.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*/, {
reportExpiry: (0, BaseWrapper_1.secondsToDurationString)(config.reportExpirySeconds),
}];
}
});
});
};
/**
* Gets all elements from the doubly linked list.
* @param target The ReportTarget, either CeloToken or currency pair in question
* @return An unpacked list of elements from largest to smallest.
*/
SortedOraclesWrapper.prototype.getRates = function (target) {
return __awaiter(this, void 0, void 0, function () {
var identifier, response, rates, i, medRelIndex;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.toCurrencyPairIdentifier(target)];
case 1:
identifier = _a.sent();
return [4 /*yield*/, this.contract.methods.getRates(identifier).call()];
case 2:
response = _a.sent();
rates = [];
for (i = 0; i < response[0].length; i++) {
medRelIndex = parseInt(response[2][i], 10);
rates.push({
address: response[0][i],
rate: (0, fixidity_1.fromFixed)((0, BaseWrapper_1.valueToBigNumber)(response[1][i])),
medianRelation: medRelIndex,
});
}
return [2 /*return*/, rates];
}
});
});
};
/**
* Gets all elements from the doubly linked list.
* @param target The ReportTarget, either CeloToken or currency pair in question
* @return An unpacked list of elements from largest to smallest.
*/
SortedOraclesWrapper.prototype.getTimestamps = function (target) {
return __awaiter(this, void 0, void 0, function () {
var identifier, response, timestamps, i, medRelIndex;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.toCurrencyPairIdentifier(target)];
case 1:
identifier = _a.sent();
return [4 /*yield*/, this.contract.methods.getTimestamps(identifier).call()];
case 2:
response = _a.sent();
timestamps = [];
for (i = 0; i < response[0].length; i++) {
medRelIndex = parseInt(response[2][i], 10);
timestamps.push({
address: response[0][i],
timestamp: (0, BaseWrapper_1.valueToBigNumber)(response[1][i]),
medianRelation: medRelIndex,
});
}
return [2 /*return*/, timestamps];
}
});
});
};
SortedOraclesWrapper.prototype.getReports = function (target) {
return __awaiter(this, void 0, void 0, function () {
var _a, rates, timestamps, reports, _loop_1, _i, rates_1, rate;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, Promise.all([
this.getRates(target),
this.getTimestamps(target),
])];
case 1:
_a = _b.sent(), rates = _a[0], timestamps = _a[1];
reports = [];
_loop_1 = function (rate) {
var match = timestamps.filter(function (t) { return (0, address_1.eqAddress)(t.address, rate.address); });
reports.push({ address: rate.address, rate: rate.rate, timestamp: match[0].timestamp });
};
for (_i = 0, rates_1 = rates; _i < rates_1.length; _i++) {
rate = rates_1[_i];
_loop_1(rate);
}
return [2 /*return*/, reports];
}
});
});
};
SortedOraclesWrapper.prototype.findLesserAndGreaterKeys = function (target, value, oracleAddress) {
return __awaiter(this, void 0, void 0, function () {
var currentRates, greaterKey, lesserKey, _i, currentRates_1, rate;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getRates(target)];
case 1:
currentRates = _a.sent();
greaterKey = address_1.NULL_ADDRESS;
lesserKey = address_1.NULL_ADDRESS;
// This leverages the fact that the currentRates are already sorted from
// greatest to lowest value
for (_i = 0, currentRates_1 = currentRates; _i < currentRates_1.length; _i++) {
rate = currentRates_1[_i];
if (!(0, address_1.eqAddress)(rate.address, oracleAddress)) {
if (rate.rate.isLessThanOrEqualTo(value)) {
lesserKey = rate.address;
break;
}
greaterKey = rate.address;
}
}
return [2 /*return*/, { lesserKey: lesserKey, greaterKey: greaterKey }];
}
});
});
};
SortedOraclesWrapper.prototype.toCurrencyPairIdentifier = function (target) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
if ((0, celo_tokens_1.isStableTokenContract)(target)) {
return [2 /*return*/, this.registry.addressFor(target)];
}
else if ((0, address_2.isValidAddress)(target)) {
return [2 /*return*/, target];
}
else {
throw new Error("".concat(target, " is not StableTokenContract deployed or a valid Address"));
}
return [2 /*return*/];
});
});
};
return SortedOraclesWrapper;
}(BaseWrapper_1.BaseWrapper));
exports.SortedOraclesWrapper = SortedOraclesWrapper;
//# sourceMappingURL=SortedOracles.js.mapВыполнить команду
Для локальной разработки. Не используйте в интернете!