@ledgerhq/coin-celo
Version:
93 lines • 3.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.voteSignerAccount = exports.getVotes = exports.getPendingWithdrawals = exports.determineFees = exports.getAccountRegistrationStatus = exports.celoKit = void 0;
const contractkit_1 = require("@celo/contractkit");
const cache_1 = require("@ledgerhq/live-network/cache");
const live_env_1 = require("@ledgerhq/live-env");
let kit;
const celoKit = () => {
if (!kit)
kit = (0, contractkit_1.newKit)((0, live_env_1.getEnv)("API_CELO_NODE"));
return kit;
};
exports.celoKit = celoKit;
/**
* Fetch account registered status. To lock any Celo, account needs to be registered first
*/
const getAccountRegistrationStatus = async (address) => {
const accounts = await (0, exports.celoKit)().contracts.getAccounts();
return await accounts.isAccount(address);
};
exports.getAccountRegistrationStatus = getAccountRegistrationStatus;
const determineFees = async (txParams) => {
const { connection: { setFeeMarketGas }, } = (0, exports.celoKit)();
await setFeeMarketGas(txParams);
};
exports.determineFees = determineFees;
/**
* Fetch pending withdrawals, with an index
*/
const getPendingWithdrawals = async (address) => {
const lockedGold = await (0, exports.celoKit)().contracts.getLockedGold();
const pendingWithdrawals = await lockedGold.getPendingWithdrawals(address);
const pendingWithdrawalsWithIndexes = pendingWithdrawals
.map((withdrawal, index) => ({
...withdrawal,
index,
}))
.sort((a, b) => a.time.minus(b.time).toNumber());
return pendingWithdrawalsWithIndexes;
};
exports.getPendingWithdrawals = getPendingWithdrawals;
/**
* Fetch all votes
*/
const getVotes = async (address) => {
const election = await (0, exports.celoKit)().contracts.getElection();
const voter = await election.getVoter(await (0, exports.voteSignerAccount)(address));
const activates = await getActivateTransactionObjects(address);
const activatableValidatorGroups = activates.map(activate => activate.txo.arguments[0]);
const votes = [];
voter.votes.forEach(vote => {
let activeVoteRevokable = true;
if (vote.pending.gt(0)) {
// If there's a pending vote, it has to be revoked first
activeVoteRevokable = false;
votes.push({
validatorGroup: vote.group,
amount: vote.pending,
// Not all pending votes can be activated, 24h has to pass
activatable: activatableValidatorGroups.includes(vote.group),
revokable: true,
index: 0,
type: "pending",
});
}
if (vote.active.gt(0))
votes.push({
validatorGroup: vote.group,
amount: vote.active,
activatable: false,
revokable: activeVoteRevokable,
index: 1,
type: "active",
});
});
return votes;
};
exports.getVotes = getVotes;
const getActivateTransactionObjects = async (address) => {
const election = await (0, exports.celoKit)().contracts.getElection();
return await election.activate(await (0, exports.voteSignerAccount)(address));
};
/**
* Fetch and cache address of a vote signer account
* Cache it for 1h since vote signer is usually the same account as our address
*/
exports.voteSignerAccount = (0, cache_1.makeLRUCache)(async (address) => {
const accounts = await (0, exports.celoKit)().contracts.getAccounts();
return await accounts.voteSignerToAccount(address);
}, address => address, {
ttl: 60 * 60 * 1000, // 1 hour
});
//# sourceMappingURL=sdk.js.map