@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
124 lines • 4.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addNotCreatedRippleMockAddress = addNotCreatedRippleMockAddress;
const bignumber_js_1 = require("bignumber.js");
const errors_1 = require("@ledgerhq/errors");
const index_1 = require("@ledgerhq/ledger-wallet-framework/account/index");
const jsHelpers_1 = require("@ledgerhq/ledger-wallet-framework/bridge/jsHelpers");
const index_2 = require("@ledgerhq/coin-framework/currencies/index");
const mockHelpers_1 = require("../../../bridge/mockHelpers");
const validateAddress_1 = require("../../../bridge/validateAddress");
const receive = (0, mockHelpers_1.makeAccountBridgeReceive)();
const notCreatedAddresses = [];
function addNotCreatedRippleMockAddress(addr) {
notCreatedAddresses.push(addr);
}
const defaultGetFees = (a, t) => t.fees;
const createTransaction = () => ({
family: "xrp",
amount: new bignumber_js_1.BigNumber(0),
recipient: "",
fees: new bignumber_js_1.BigNumber(10),
tag: undefined,
networkInfo: null,
useAllAmount: false,
});
const estimateMaxSpendable = ({ account, parentAccount, transaction }) => {
const mainAccount = (0, index_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 getTransactionStatus = (a, t) => {
const minimalBaseAmount = 10 ** a.currency.units[0].magnitude * 20;
const errors = {};
const warnings = {};
const useAllAmount = !!t.useAllAmount;
const estimatedFees = defaultGetFees(a, t);
const totalSpent = useAllAmount ? a.balance : new bignumber_js_1.BigNumber(t.amount).plus(estimatedFees);
const amount = useAllAmount ? a.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 (totalSpent.gt(a.balance)) {
errors.amount = new errors_1.NotEnoughSpendableBalance("", {
minimumAmount: (0, index_2.formatCurrencyUnit)(a.currency.units[0], new bignumber_js_1.BigNumber(minimalBaseAmount), {
disableRounding: true,
useGrouping: false,
showCode: true,
}),
});
}
else if (minimalBaseAmount && a.balance.minus(totalSpent).lt(minimalBaseAmount)) {
errors.amount = new errors_1.NotEnoughSpendableBalance("", {
minimumAmount: (0, index_2.formatCurrencyUnit)(a.currency.units[0], new bignumber_js_1.BigNumber(minimalBaseAmount), {
disableRounding: true,
useGrouping: false,
showCode: true,
}),
});
}
else if (minimalBaseAmount &&
(t.recipient.includes("new") || notCreatedAddresses.includes(t.recipient)) &&
amount.lt(minimalBaseAmount)) {
// mimic XRP base minimal for new addresses
errors.amount = new errors_1.NotEnoughBalanceBecauseDestinationNotCreated("", {
minimalAmount: `XRP Minimum reserve`,
});
}
if (!t.recipient) {
errors.recipient = new errors_1.RecipientRequired("");
}
else if ((0, mockHelpers_1.isInvalidRecipient)(t.recipient)) {
errors.recipient = new errors_1.InvalidAddress("");
}
else if (a.freshAddress === t.recipient) {
errors.recipient = new errors_1.InvalidAddressBecauseDestinationIsAlsoSource();
}
if (!errors.amount && amount.eq(0)) {
errors.amount = new errors_1.AmountRequired();
}
return Promise.resolve({
errors,
warnings,
estimatedFees,
amount,
totalSpent,
});
};
const prepareTransaction = async (a, t) => {
// TODO it needs to set the fee if not in t as well
if (!t.networkInfo) {
return {
...t,
networkInfo: {
family: "xrp",
serverFee: new bignumber_js_1.BigNumber(10),
},
};
}
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