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
119 lines (103 loc) · 2.81 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// @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 { getEstimatedFees } from "../../../api/Fees"; // FIXME drop. not stable.
import {
scanAccounts,
signOperation,
broadcast,
sync,
isInvalidRecipient
} from "../../../bridge/mockHelpers";
import { getGasLimit } from "../transaction";
const defaultGetFees = (a, t: *) =>
(t.gasPrice || BigNumber(0)).times(getGasLimit(t));
const createTransaction = (account): Transaction => ({
family: "ethereum",
amount: BigNumber(0),
recipient: "",
gasPrice: BigNumber(10000000000),
userGasLimit: BigNumber(21000),
estimatedGasLimit: BigNumber(21000),
feeCustomUnit: account.currency.units[1],
networkInfo: null,
useAllAmount: false,
subAccountId: null
});
const updateTransaction = (t, patch) => ({ ...t, ...patch });
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 = defaultGetFees(a, t);
const totalSpent = useAllAmount
? account.balance
: tokenAccount
? BigNumber(t.amount)
: BigNumber(t.amount).plus(estimatedFees);
const amount = useAllAmount
? tokenAccount
? BigNumber(t.amount)
: 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 { gas_price } = await getEstimatedFees(a.currency);
return {
...t,
networkInfo: {
family: "ethereum",
gasPrice: BigNumber(gas_price)
}
};
}
return t;
};
const accountBridge: AccountBridge<Transaction> = {
createTransaction,
updateTransaction,
getTransactionStatus,
prepareTransaction,
sync,
signOperation,
broadcast
};
const currencyBridge: CurrencyBridge = {
preload: () => Promise.resolve(),
hydrate: () => {},
scanAccounts
};
export default { currencyBridge, accountBridge };