This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 174
/
mock.js
104 lines (89 loc) · 2.31 KB
/
mock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// @flow
import { BigNumber } from "bignumber.js";
import {
NotEnoughBalance,
RecipientRequired,
InvalidAddress,
FeeTooHigh
} from "@ledgerhq/errors";
import type { Transaction } from "../types";
import type { AccountBridge, CurrencyBridge } from "../../../types";
import { getFeeItems } from "../../../api/FeesBitcoin";
import {
scanAccounts,
signOperation,
broadcast,
sync,
isInvalidRecipient
} from "../../../bridge/mockHelpers";
const defaultGetFees = (a, t: *) => (t.feePerByte || BigNumber(0)).times(250);
const createTransaction = (): Transaction => ({
family: "bitcoin",
amount: BigNumber(0),
recipient: "",
feePerByte: BigNumber(10),
networkInfo: null,
useAllAmount: false
});
const updateTransaction = (t, patch) => ({ ...t, ...patch });
const getTransactionStatus = (account, t) => {
const errors = {};
const warnings = {};
const useAllAmount = !!t.useAllAmount;
const estimatedFees = defaultGetFees(account, t);
const totalSpent = useAllAmount
? account.balance
: BigNumber(t.amount).plus(estimatedFees);
const amount = useAllAmount
? account.balance.minus(estimatedFees)
: BigNumber(t.amount);
if (amount.gt(0) && estimatedFees.times(10).gt(amount)) {
warnings.feeTooHigh = new FeeTooHigh();
}
// 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) => {
// TODO it needs to set the fee if not in t as well
if (!t.networkInfo) {
const feeItems = await getFeeItems(a.currency);
return {
...t,
networkInfo: {
family: "bitcoin",
feeItems
}
};
}
return t;
};
const accountBridge: AccountBridge<Transaction> = {
createTransaction,
updateTransaction,
getTransactionStatus,
prepareTransaction,
sync,
signOperation,
broadcast
};
const currencyBridge: CurrencyBridge = {
scanAccounts,
preload: () => Promise.resolve(),
hydrate: () => {}
};
export default { currencyBridge, accountBridge };