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
124 lines (108 loc) · 3.03 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
120
121
122
123
124
// @flow
import { BigNumber } from "bignumber.js";
import {
NotEnoughSpendableBalance,
NotEnoughBalanceBecauseDestinationNotCreated,
InvalidAddressBecauseDestinationIsAlsoSource,
InvalidAddress,
RecipientRequired,
FeeTooHigh
} from "@ledgerhq/errors";
import type { Transaction } from "../types";
import type { Account, AccountBridge, CurrencyBridge } from "../../../types";
import { getCryptoCurrencyById } from "../../../data/cryptocurrencies";
import {
scanAccounts,
signOperation,
broadcast,
sync,
isInvalidRecipient
} from "../../../bridge/mockHelpers";
const defaultGetFees = (a: Account, t: *) => t.fee || BigNumber(0);
const createTransaction = (): Transaction => ({
family: "ripple",
amount: BigNumber(0),
recipient: "",
fee: BigNumber(10),
feeCustomUnit: getCryptoCurrencyById("ethereum").units[1],
tag: undefined,
networkInfo: null,
useAllAmount: false
});
const updateTransaction = (t, patch) => ({ ...t, ...patch });
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
: BigNumber(t.amount).plus(estimatedFees);
const amount = useAllAmount
? a.balance.minus(estimatedFees)
: BigNumber(t.amount);
if (amount.gt(0) && estimatedFees.times(10).gt(amount)) {
warnings.feeTooHigh = new FeeTooHigh();
}
if (totalSpent.gt(a.balance)) {
errors.amount = new NotEnoughSpendableBalance();
} else if (
minimalBaseAmount &&
a.balance.minus(totalSpent).lt(minimalBaseAmount)
) {
errors.amount = new NotEnoughSpendableBalance();
} else if (
minimalBaseAmount &&
t.recipient.includes("new") &&
amount.lt(minimalBaseAmount)
) {
// mimic XRP base minimal for new addresses
errors.amount = new NotEnoughBalanceBecauseDestinationNotCreated(null, {
minimalAmount: `XRP Minimum reserve`
});
}
if (!t.recipient) {
errors.recipient = new RecipientRequired("");
} else if (isInvalidRecipient(t.recipient)) {
errors.recipient = new InvalidAddress("");
} else if (a.freshAddress === t.recipient) {
errors.recipient = new InvalidAddressBecauseDestinationIsAlsoSource();
}
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: "ripple",
serverFee: BigNumber(10),
baseReserve: BigNumber(20)
}
};
}
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 };