@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
174 lines • 6.59 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const bignumber_js_1 = require("bignumber.js");
const errors_1 = require("@ledgerhq/errors");
const account_1 = require("../../../account");
const mockHelpers_1 = require("../../../bridge/mockHelpers");
const jsHelpers_1 = require("@ledgerhq/ledger-wallet-framework/bridge/jsHelpers");
const staking_1 = require("../staking");
const validateAddress_1 = require("../../../bridge/validateAddress");
const isAccountBalanceSignificant = (a) => a.balance.gt(100);
const receive = (0, mockHelpers_1.makeAccountBridgeReceive)();
const estimateGasLimitAndStorage = () => {
const storage = new bignumber_js_1.BigNumber(257);
const gasLimit = new bignumber_js_1.BigNumber(10600);
return {
storage,
gasLimit,
};
};
const defaultGetFees = (a, t) => (t.fees || new bignumber_js_1.BigNumber(0)).times(t.gasLimit || new bignumber_js_1.BigNumber(0));
const estimateMaxSpendable = ({ account, parentAccount, transaction }) => {
const mainAccount = (0, account_1.getMainAccount)(account, parentAccount);
const estimatedFees = transaction ? defaultGetFees(mainAccount, transaction) : new bignumber_js_1.BigNumber(10);
return Promise.resolve(bignumber_js_1.BigNumber.max(0, account.balance.minus(estimatedFees)));
};
const createTransaction = () => ({
family: "tezos",
mode: "send",
amount: new bignumber_js_1.BigNumber(0),
fees: null,
gasLimit: null,
storageLimit: null,
recipient: "",
networkInfo: null,
useAllAmount: false,
taquitoError: null,
estimatedFees: null,
});
const getTransactionStatus = (a, t) => {
const errors = {};
const warnings = {};
const subAcc = !t.subAccountId
? null
: a.subAccounts && a.subAccounts.find(ta => ta.id === t.subAccountId);
const account = subAcc || a;
if (t.mode !== "undelegate") {
if (account.freshAddress === t.recipient) {
errors.recipient = new errors_1.InvalidAddressBecauseDestinationIsAlsoSource();
}
else {
if (!t.recipient) {
// Fill up recipient errors...
errors.recipient = new errors_1.RecipientRequired("");
}
else if ((0, mockHelpers_1.isInvalidRecipient)(t.recipient)) {
errors.recipient = new errors_1.InvalidAddress("");
}
else if (t.recipient.startsWith("KT")) {
errors.recipient = new errors_1.NotSupportedLegacyAddress();
}
}
}
let amount = t.amount;
// FIXME: maybe we need this
// if (!t.fees) {
// errors.fees = new FeeNotLoaded();
// } else if (!errors.recipient) {
// estimatedFees = defaultGetFees(a, t);
// }
const estimatedFees = defaultGetFees(a, t);
const useAllAmount = !!t.useAllAmount;
let totalSpent = useAllAmount
? account.balance
: subAcc
? new bignumber_js_1.BigNumber(t.amount)
: new bignumber_js_1.BigNumber(t.amount).plus(estimatedFees);
amount = useAllAmount
? subAcc
? new bignumber_js_1.BigNumber(t.amount)
: account.balance.minus(estimatedFees)
: new bignumber_js_1.BigNumber(t.amount);
if (amount.gt(0) && estimatedFees.times(10).gt(amount)) {
warnings.feeTooHigh = new errors_1.FeeTooHigh();
}
if (!errors.amount && subAcc && estimatedFees.gt(a.balance)) {
errors.amount = new errors_1.NotEnoughBalanceInParentAccount();
}
if (!errors.recipient && !errors.amount && (amount.lt(0) || totalSpent.gt(account.balance))) {
errors.amount = new errors_1.NotEnoughBalance();
totalSpent = new bignumber_js_1.BigNumber(0);
amount = new bignumber_js_1.BigNumber(0);
}
if (t.mode === "send") {
if (!errors.amount && amount.eq(0)) {
errors.amount = new errors_1.AmountRequired();
}
else if (amount.gt(0) && estimatedFees.times(10).gt(amount)) {
warnings.feeTooHigh = new errors_1.FeeTooHigh();
}
const thresholdWarning = 0.5 * 10 ** a.currency.units[0].magnitude;
if (!subAcc && !errors.amount && account.balance.minus(totalSpent).lt(thresholdWarning)) {
if ((0, staking_1.isAccountDelegating)(account)) {
warnings.amount = new errors_1.RecommendUndelegation();
}
else if ((a.subAccounts || []).some(isAccountBalanceSignificant)) {
warnings.amount = new errors_1.RecommendSubAccountsToEmpty();
}
}
}
else {
// delegation case, we remap NotEnoughBalance to a more precise error
if (errors.amount instanceof errors_1.NotEnoughBalance) {
errors.amount = new errors_1.NotEnoughBalanceToDelegate();
}
}
return Promise.resolve({
errors,
warnings,
estimatedFees,
amount,
totalSpent,
});
};
const prepareTransaction = async (a, t) => {
let networkInfo = t.networkInfo;
if (!networkInfo) {
const ni = {
family: "tezos",
fees: t.fees || new bignumber_js_1.BigNumber(0),
};
networkInfo = ni;
}
let gasLimit = t.gasLimit;
let storageLimit = t.storageLimit;
if (!gasLimit || storageLimit) {
if (t.mode === "undelegate" || (0, mockHelpers_1.isInvalidRecipient)(t.recipient)) {
const r = estimateGasLimitAndStorage();
gasLimit = r.gasLimit;
storageLimit = r.storage;
}
}
const fees = t.fees || networkInfo.fees;
if (t.networkInfo !== networkInfo ||
t.gasLimit !== gasLimit ||
t.storageLimit !== storageLimit ||
t.fees !== fees) {
return { ...t, networkInfo, storageLimit, gasLimit, fees };
}
return t;
};
const accountBridge = {
createTransaction,
updateTransaction: jsHelpers_1.updateTransaction,
getTransactionStatus,
estimateMaxSpendable,
prepareTransaction,
sync: mockHelpers_1.sync,
receive,
signOperation: mockHelpers_1.signOperation,
signRawOperation: mockHelpers_1.signRawOperation,
broadcast: mockHelpers_1.broadcast,
getSerializedAddressParameters: jsHelpers_1.getSerializedAddressParameters,
validateAddress: validateAddress_1.validateAddress,
};
const currencyBridge = {
preload: () => Promise.resolve({}),
hydrate: () => { },
scanAccounts: mockHelpers_1.scanAccounts,
};
exports.default = {
currencyBridge,
accountBridge,
};
//# sourceMappingURL=mock.js.map