@ledgerhq/coin-celo
Version:
141 lines • 5.97 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getValidatorGroups = exports.getAccountDetails = void 0;
const network_1 = __importDefault(require("@ledgerhq/live-network/network"));
const bignumber_js_1 = require("bignumber.js");
const live_env_1 = require("@ledgerhq/live-env");
const operation_1 = require("@ledgerhq/coin-framework/operation");
const logic_1 = require("../logic");
const sdk_1 = require("../network/sdk");
const DEFAULT_TRANSACTIONS_LIMIT = 200;
const getUrl = (route) => `${(0, live_env_1.getEnv)("API_CELO_INDEXER")}${route || ""}`;
// Indexer returns both account data and transactions in one call.
// Transactions are just limited, there's no block height offset
const fetchAccountDetails = async (address, transactionsLimit = DEFAULT_TRANSACTIONS_LIMIT) => {
const { data } = await (0, network_1.default)({
method: "GET",
url: getUrl(`/account_details/${address}?limit=${transactionsLimit}`),
});
return data;
};
const fetchStatus = async () => {
const { data } = await (0, network_1.default)({
method: "GET",
url: getUrl(`/status`),
});
return data;
};
const fetchValidatorGroups = async () => {
const { data } = await (0, network_1.default)({
method: "GET",
url: getUrl(`/validator_groups`),
});
return data.items;
};
const getOperationType = (type) => {
switch (type) {
case "InternalTransferSent":
return "OUT";
case "InternalTransferReceived":
return "IN";
case "GoldLocked":
return "LOCK";
case "GoldUnlocked":
return "UNLOCK";
case "GoldWithdrawn":
return "WITHDRAW";
case "ValidatorGroupVoteCastSent":
return "VOTE";
case "ValidatorGroupActiveVoteRevokedSent":
return "REVOKE";
case "ValidatorGroupPendingVoteRevokedSent":
return "REVOKE";
case "ValidatorGroupVoteActivatedSent":
return "ACTIVATE";
case "AccountCreated":
return "REGISTER";
case "AccountSlashed":
return "SLASH";
default:
return "NONE";
}
};
const transactionToOperation = (accountId, transaction) => {
const type = getOperationType(transaction.kind);
const hasFailed = transaction.data?.success ? !transaction.data?.success : false;
const data = transaction.data;
const sender = data?.Account || data?.from;
const recipient = data?.Group || data?.to || data?.Raw?.address;
const fee = new bignumber_js_1.BigNumber(transaction.data?.gas_used || 0).times(new bignumber_js_1.BigNumber(transaction.data?.gas_price || 0));
const value = ["LOCK", "UNLOCK", "ACTIVATE", "VOTE", "REVOKE", "REGISTER"].includes(type)
? new bignumber_js_1.BigNumber(fee)
: new bignumber_js_1.BigNumber(transaction.amount);
return {
id: (0, operation_1.encodeOperationId)(accountId, transaction.transaction_hash, type),
hash: transaction.transaction_hash,
accountId,
fee,
value,
type,
blockHeight: transaction.height,
date: new Date(transaction.time),
senders: sender ? [sender] : [],
recipients: recipient ? [recipient] : [],
hasFailed,
blockHash: null,
extra: {
celoOperationValue: new bignumber_js_1.BigNumber(transaction.amount),
...(["ACTIVATE", "VOTE", "REVOKE"].includes(type)
? {
celoSourceValidator: recipient ? recipient : "",
}
: {}),
},
};
};
const getAccountDetails = async (address, accountId) => {
const accountDetails = await fetchAccountDetails(address);
const spendableBalance = new bignumber_js_1.BigNumber(accountDetails.gold_balance);
const lockedBalance = new bignumber_js_1.BigNumber(accountDetails.total_locked_gold);
const nonvotingLockedBalance = new bignumber_js_1.BigNumber(accountDetails.total_nonvoting_locked_gold);
const balance = spendableBalance.plus(lockedBalance);
const indexerStatus = await fetchStatus();
const kit = (0, sdk_1.celoKit)();
const lockedGold = await kit.contracts.getLockedGold();
const allTransactions = accountDetails.internal_transfers
.filter((transfer) => transfer.data?.to != lockedGold.address && transfer.data?.from != lockedGold.address)
.concat(accountDetails.transactions);
const operations = allTransactions.map((transaction) => transactionToOperation(accountId, transaction));
return {
blockHeight: indexerStatus.last_indexed_height,
balance,
spendableBalance,
operations,
lockedBalance,
nonvotingLockedBalance,
};
};
exports.getAccountDetails = getAccountDetails;
const getValidatorGroups = async () => {
const validatorGroups = await fetchValidatorGroups();
const result = validatorGroups.map((validatorGroup) => ({
address: validatorGroup.address,
name: validatorGroup.name || validatorGroup.address,
votes: new bignumber_js_1.BigNumber(validatorGroup.active_votes).plus(new bignumber_js_1.BigNumber(validatorGroup.pending_votes)),
}));
return customValidatorGroupsOrder(result);
};
exports.getValidatorGroups = getValidatorGroups;
const customValidatorGroupsOrder = (validatorGroups) => {
const defaultValidatorGroup = validatorGroups.find(logic_1.isDefaultValidatorGroup);
const sortedValidatorGroups = [...validatorGroups]
.sort((a, b) => b.votes.minus(a.votes))
.filter(group => !(0, logic_1.isDefaultValidatorGroup)(group));
return defaultValidatorGroup
? [defaultValidatorGroup, ...sortedValidatorGroups]
: sortedValidatorGroups;
};
//# sourceMappingURL=hubble.js.map