@ledgerhq/coin-celo
Version:
84 lines • 3.25 kB
JavaScript
import { isCeloOperationExtra, isCeloOperationExtraRaw, } from "../types";
import { BigNumber } from "bignumber.js";
export function toCeloResourcesRaw(r) {
const { registrationStatus, lockedBalance, nonvotingLockedBalance, pendingWithdrawals, votes, electionAddress, lockedGoldAddress, maxNumGroupsVotedFor, } = r ?? {};
return {
registrationStatus,
lockedBalance: lockedBalance?.toString(),
nonvotingLockedBalance: nonvotingLockedBalance?.toString(),
pendingWithdrawals: pendingWithdrawals?.map(withdrawal => ({
value: withdrawal.value.toString(),
time: withdrawal.time.toString(),
index: withdrawal.index.toString(),
})),
votes: votes?.map(vote => ({
validatorGroup: vote.validatorGroup.toString(),
amount: vote.amount.toString(),
activatable: vote.activatable,
revokable: vote.revokable,
type: vote.type,
index: vote.index,
})),
electionAddress,
lockedGoldAddress,
maxNumGroupsVotedFor: maxNumGroupsVotedFor?.toString(),
};
}
export function fromCeloResourcesRaw(r) {
return {
registrationStatus: r.registrationStatus,
lockedBalance: new BigNumber(r.lockedBalance),
nonvotingLockedBalance: new BigNumber(r.nonvotingLockedBalance),
pendingWithdrawals: r.pendingWithdrawals?.map(u => ({
value: new BigNumber(u.value),
time: new BigNumber(u.time),
index: Number(u.index),
})),
votes: r.votes?.map(vote => ({
validatorGroup: vote.validatorGroup,
amount: new BigNumber(vote.amount),
activatable: vote.activatable,
revokable: vote.revokable,
type: vote.type,
index: vote.index,
})),
electionAddress: r.electionAddress,
lockedGoldAddress: r.lockedGoldAddress,
maxNumGroupsVotedFor: new BigNumber(r.maxNumGroupsVotedFor),
};
}
export function assignToAccountRaw(account, accountRaw) {
const celoAccount = account;
if (celoAccount.celoResources)
accountRaw.celoResources = toCeloResourcesRaw(celoAccount.celoResources);
}
export function assignFromAccountRaw(accountRaw, account) {
const celoResourcesRaw = accountRaw.celoResources;
if (celoResourcesRaw)
account.celoResources = fromCeloResourcesRaw(celoResourcesRaw);
}
export function fromOperationExtraRaw(extraRaw) {
if (!isCeloOperationExtraRaw(extraRaw)) {
throw new Error("Unsupported OperationExtra");
}
const extra = {
celoOperationValue: new BigNumber(extraRaw.celoOperationValue),
};
if (extraRaw.celoSourceValidator) {
extra.celoSourceValidator = extraRaw.celoSourceValidator;
}
return extra;
}
export function toOperationExtraRaw(extra) {
if (!isCeloOperationExtra(extra)) {
throw new Error("Unsupported OperationExtra");
}
const extraRaw = {
celoOperationValue: extra.celoOperationValue.toString(),
};
if (extra.celoSourceValidator) {
extraRaw.celoSourceValidator = extra.celoSourceValidator;
}
return extraRaw;
}
//# sourceMappingURL=serialization.js.map