PHP WebShell
Текущая директория: /usr/lib/node_modules/bitgo/node_modules/@celo/contractkit/lib
Просмотр файла: celo-tokens.js
"use strict";
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 _a, _b;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isStableTokenContract = exports.CeloTokens = exports.celoTokenInfos = exports.stableTokenInfos = exports.Token = exports.StableToken = void 0;
var base_1 = require("@celo/base");
var base_2 = require("./base");
var base_3 = require("@celo/base");
Object.defineProperty(exports, "StableToken", { enumerable: true, get: function () { return base_3.StableToken; } });
Object.defineProperty(exports, "Token", { enumerable: true, get: function () { return base_3.Token; } });
/** Basic info for each stable token */
exports.stableTokenInfos = (_a = {},
_a[base_1.StableToken.cUSD] = {
contract: base_2.CeloContract.StableToken,
exchangeContract: base_2.CeloContract.Exchange,
symbol: base_1.StableToken.cUSD,
},
_a[base_1.StableToken.cEUR] = {
contract: base_2.CeloContract.StableTokenEUR,
exchangeContract: base_2.CeloContract.ExchangeEUR,
symbol: base_1.StableToken.cEUR,
},
_a[base_1.StableToken.cREAL] = {
contract: base_2.CeloContract.StableTokenBRL,
exchangeContract: base_2.CeloContract.ExchangeBRL,
symbol: base_1.StableToken.cREAL,
},
_a);
/** Basic info for each supported celo token, including stable tokens */
exports.celoTokenInfos = __assign((_b = {}, _b[base_1.Token.CELO] = {
contract: base_2.CeloContract.GoldToken,
symbol: base_1.Token.CELO,
}, _b), exports.stableTokenInfos);
/**
* A helper class to interact with all Celo tokens, ie CELO and stable tokens
*/
var CeloTokens = /** @class */ (function () {
function CeloTokens(contracts, registry) {
var _this = this;
this.contracts = contracts;
this.registry = registry;
/**
* Gets the address of the contract for the provided token.
* @param token the token to get the (proxy) contract address for
* @return A promise resolving to the address of the token's contract
*/
this.getAddress = function (token) { return _this.registry.addressFor(exports.celoTokenInfos[token].contract); };
this.isStableTokenContract = isStableTokenContract;
}
/**
* Gets an address's balance for each celo token.
* @param address the address to look up the balances for
* @return a promise resolving to an object containing the address's balance
* for each celo token
*/
CeloTokens.prototype.balancesOf = function (address) {
var _this = this;
return this.forEachCeloToken(function (info) { return __awaiter(_this, void 0, void 0, function () {
var wrapper;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.contracts.getContract(info.contract)];
case 1:
wrapper = _a.sent();
return [2 /*return*/, wrapper.balanceOf(address)];
}
});
}); });
};
/**
* Gets the wrapper for each celo token.
* @return an promise resolving to an object containing the wrapper for each celo token.
*/
CeloTokens.prototype.getWrappers = function () {
var _this = this;
return this.forEachCeloToken(function (info) { return _this.contracts.getContract(info.contract); });
};
/**
* Gets the address for each celo token proxy contract.
* @return an promise resolving to an object containing the address for each celo token proxy.
*/
CeloTokens.prototype.getAddresses = function () {
var _this = this;
return this.forEachCeloToken(function (info) { return _this.registry.addressFor(info.contract); });
};
CeloTokens.prototype.getStablesConfigs = function (humanReadable) {
if (humanReadable === void 0) { humanReadable = false; }
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.forStableCeloToken(function (info) { return __awaiter(_this, void 0, void 0, function () {
var stableWrapper;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.contracts.getContract(info.contract)];
case 1:
stableWrapper = _a.sent();
if (humanReadable) {
return [2 /*return*/, stableWrapper.getHumanReadableConfig()];
}
return [2 /*return*/, stableWrapper.getConfig()];
}
});
}); })];
});
});
};
CeloTokens.prototype.getExchangesConfigs = function (humanReadable) {
if (humanReadable === void 0) { humanReadable = false; }
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.forStableCeloToken(function (info) { return __awaiter(_this, void 0, void 0, function () {
var exchangeWrapper;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.contracts.getContract(info.exchangeContract)];
case 1:
exchangeWrapper = _a.sent();
if (humanReadable) {
return [2 /*return*/, exchangeWrapper.getHumanReadableConfig()];
}
return [2 /*return*/, exchangeWrapper.getConfig()];
}
});
}); })];
});
});
};
/**
* Runs fn for each celo token found in celoTokenInfos, and returns the
* value of each call in an object keyed by the token.
* @param fn the function to be called for each CeloTokenInfo.
* @return an object containing the resolved value the call to fn for each
* celo token.
*/
CeloTokens.prototype.forEachCeloToken = function (fn) {
return __awaiter(this, void 0, void 0, function () {
var wrapperInfoFunction;
var _this = this;
return __generator(this, function (_a) {
wrapperInfoFunction = function () { return __awaiter(_this, void 0, void 0, function () {
var _a, _b;
var _this = this;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_b = (_a = Promise).all;
return [4 /*yield*/, this.validCeloTokenInfos()];
case 1: return [2 /*return*/, _b.apply(_a, [(_c.sent()).map(function (info) { return __awaiter(_this, void 0, void 0, function () {
var fnResult;
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
fnResult = fn(info);
_a = {
symbol: info.symbol
};
return [4 /*yield*/, fnResult];
case 1: return [2 /*return*/, (_a.data = _b.sent(),
_a)];
}
});
}); })])];
}
});
}); };
return [2 /*return*/, this.forEachWrapperInfo(wrapperInfoFunction)];
});
});
};
/**
* Runs fn for each stable token found in stableTokenInfos, and returns the
* value of each call in an object keyed by the token.
* @param fn the function to be called for each StableTokenInfo.
* @return an object containing the resolved value the call to fn for each
* celo token.
*/
CeloTokens.prototype.forStableCeloToken = function (fn) {
return __awaiter(this, void 0, void 0, function () {
var wrapperInfoFunction;
var _this = this;
return __generator(this, function (_a) {
wrapperInfoFunction = function () { return __awaiter(_this, void 0, void 0, function () {
var _a, _b;
var _this = this;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_b = (_a = Promise).all;
return [4 /*yield*/, this.validStableTokenInfos()];
case 1: return [2 /*return*/, _b.apply(_a, [(_c.sent()).map(function (info) { return __awaiter(_this, void 0, void 0, function () {
var fnResult;
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
fnResult = fn(info);
_a = {
symbol: info.symbol
};
return [4 /*yield*/, fnResult];
case 1: return [2 /*return*/, (_a.data = _b.sent(),
_a)];
}
});
}); })])];
}
});
}); };
return [2 /*return*/, this.forEachWrapperInfo(wrapperInfoFunction)];
});
});
};
CeloTokens.prototype.forEachWrapperInfo = function (fn) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, fn()];
case 1: return [2 /*return*/, (_a.sent()).reduce(function (obj, wrapperInfo) {
var _a;
return (__assign(__assign({}, obj), (_a = {}, _a[wrapperInfo.symbol] = wrapperInfo.data, _a)));
}, {})];
}
});
});
};
CeloTokens.prototype.validCeloTokenInfos = function () {
return __awaiter(this, void 0, void 0, function () {
var results;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Promise.all(Object.values(exports.celoTokenInfos).map(function (info) { return __awaiter(_this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
// The registry add the valid addresses to a cache
return [4 /*yield*/, this.registry.addressFor(info.contract)];
case 1:
// The registry add the valid addresses to a cache
_b.sent();
return [2 /*return*/, true];
case 2:
_a = _b.sent();
// The contract was not deployed in the chain
return [2 /*return*/, false];
case 3: return [2 /*return*/];
}
});
}); }))];
case 1:
results = _a.sent();
return [2 /*return*/, Object.values(exports.celoTokenInfos).filter(function (_v, index) { return results[index]; })];
}
});
});
};
CeloTokens.prototype.validStableTokenInfos = function () {
return __awaiter(this, void 0, void 0, function () {
var results;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Promise.all(Object.values(exports.stableTokenInfos).map(function (info) { return __awaiter(_this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 3, , 4]);
// The registry add the valid addresses to a cache
return [4 /*yield*/, this.registry.addressFor(info.contract)];
case 1:
// The registry add the valid addresses to a cache
_b.sent();
return [4 /*yield*/, this.registry.addressFor(info.exchangeContract)];
case 2:
_b.sent();
return [2 /*return*/, true];
case 3:
_a = _b.sent();
// The contract was not deployed in the chain
return [2 /*return*/, false];
case 4: return [2 /*return*/];
}
});
}); }))];
case 1:
results = _a.sent();
return [2 /*return*/, Object.values(exports.stableTokenInfos).filter(function (_v, index) { return results[index]; })];
}
});
});
};
CeloTokens.prototype.getWrapper = function (token) {
return this.contracts.getContract(exports.celoTokenInfos[token].contract);
};
CeloTokens.prototype.getContract = function (token) {
return exports.celoTokenInfos[token].contract;
};
/**
* Gets the exchange contract for the provided stable token
* @param token the stable token to get exchange contract of
* @return The exchange contract for the token
*/
CeloTokens.prototype.getExchangeContract = function (token) {
return exports.stableTokenInfos[token].exchangeContract;
};
/**
* Gets the address to use as the feeCurrency when paying for gas with the
* provided token.
* @param token the token to get the feeCurrency address for
* @return If not CELO, the address of the token's contract. If CELO, undefined.
*/
CeloTokens.prototype.getFeeCurrencyAddress = function (token) {
if (token === base_1.Token.CELO) {
return undefined;
}
return this.getAddress(token);
};
/**
* Returns if the provided token is a StableToken
* @param token the token
* @return if token is a StableToken
*/
CeloTokens.prototype.isStableToken = function (token) {
// We cast token as StableToken to make typescript happy
return Object.values(base_1.StableToken).includes(token);
};
return CeloTokens;
}());
exports.CeloTokens = CeloTokens;
function isStableTokenContract(contract) {
var allStableTokenContracts = Object.values(base_1.StableToken).map(function (token) { return exports.stableTokenInfos[token].contract; });
// We cast token as StableTokenContract to make typescript happy
return allStableTokenContracts.includes(contract);
}
exports.isStableTokenContract = isStableTokenContract;
//# sourceMappingURL=celo-tokens.js.mapВыполнить команду
Для локальной разработки. Не используйте в интернете!