@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
102 lines • 3.22 kB
JavaScript
import { BigNumber } from "bignumber.js";
import { NotEnoughBalance, RecipientRequired, InvalidAddress } from "@ledgerhq/errors";
import { getSerializedAddressParameters } from "@ledgerhq/ledger-wallet-framework/bridge/jsHelpers";
import { scanAccounts, signOperation, signRawOperation, broadcast, sync, isInvalidRecipient, makeAccountBridgeReceive, } from "../../../bridge/mockHelpers";
import { validateAddress } from "../../../bridge/validateAddress";
const receive = makeAccountBridgeReceive();
const createTransaction = () => ({
family: "tron",
amount: new BigNumber(0),
useAllAmount: false,
mode: "send",
duration: 3,
recipient: "",
networkInfo: null,
resource: null,
votes: [],
});
const updateTransaction = (t, patch) => ({ ...t, ...patch });
const estimateMaxSpendable = ({ account }) => {
return account.balance;
};
const getTransactionStatus = (a, t) => {
const errors = {};
const warnings = {};
const tokenAccount = !t.subAccountId
? null
: a.subAccounts && a.subAccounts.find(ta => ta.id === t.subAccountId);
const account = tokenAccount || a;
const useAllAmount = !!t.useAllAmount;
const estimatedFees = BigNumber(0);
const totalSpent = useAllAmount
? account.balance
: tokenAccount
? new BigNumber(t.amount)
: new BigNumber(t.amount).plus(estimatedFees);
const amount = useAllAmount
? tokenAccount
? new BigNumber(t.amount)
: account.balance.minus(estimatedFees)
: new BigNumber(t.amount);
// Fill up transaction errors...
if (totalSpent.gt(account.balance)) {
errors.amount = new NotEnoughBalance();
}
// Fill up recipient errors...
if (!t.recipient) {
errors.recipient = new RecipientRequired("");
}
else if (isInvalidRecipient(t.recipient)) {
errors.recipient = new InvalidAddress("");
}
return Promise.resolve({
errors,
warnings,
estimatedFees,
amount,
totalSpent,
});
};
const prepareTransaction = async (a, t) => {
let res = t;
if (!res.networkInfo) {
const freeNetUsed = 0, freeNetLimit = 0, NetUsed = 0, NetLimit = 0, EnergyUsed = 0, EnergyLimit = 0;
res = {
...res,
networkInfo: {
family: "tron",
freeNetUsed: new BigNumber(freeNetUsed),
freeNetLimit: new BigNumber(freeNetLimit),
netUsed: new BigNumber(NetUsed),
netLimit: new BigNumber(NetLimit),
energyUsed: new BigNumber(EnergyUsed),
energyLimit: new BigNumber(EnergyLimit),
},
};
}
return res;
};
const accountBridge = {
createTransaction,
updateTransaction,
getTransactionStatus,
estimateMaxSpendable,
prepareTransaction,
sync,
receive,
signOperation,
signRawOperation,
broadcast,
getSerializedAddressParameters,
validateAddress,
};
const currencyBridge = {
preload: () => Promise.resolve({}),
hydrate: () => { },
scanAccounts,
};
export default {
currencyBridge,
accountBridge,
};
//# sourceMappingURL=mock.js.map