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
/
libcore.js
145 lines (125 loc) · 3.86 KB
/
libcore.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// @flow
import invariant from "invariant";
import { BigNumber } from "bignumber.js";
import { scanAccounts } from "../../../libcore/scanAccounts";
import { validateRecipient } from "../../../bridge/shared";
import type { AccountBridge, CurrencyBridge } from "../../../types/bridge";
import type { Transaction } from "../types";
import { sync } from "../../../libcore/syncAccount";
import { getAccountNetworkInfo } from "../../../libcore/getAccountNetworkInfo";
import {
AmountRequired,
FeeNotLoaded,
FeeRequired,
FeeTooHigh,
InvalidAddressBecauseDestinationIsAlsoSource,
NotEnoughSpendableBalance,
NotEnoughBalanceBecauseDestinationNotCreated
} from "@ledgerhq/errors";
import { makeLRUCache } from "../../../cache";
import type { Account } from "../../../types";
import { withLibcore } from "../../../libcore/access";
import { getCoreAccount } from "../../../libcore/getCoreAccount";
import signOperation from "../libcore-signOperation";
import broadcast from "../libcore-broadcast";
const createTransaction = () => ({
family: "ripple",
amount: BigNumber(0),
recipient: "",
tag: undefined,
fee: null,
feeCustomUnit: null,
networkInfo: null
});
const updateTransaction = (t, patch) => ({ ...t, ...patch });
const isAddressActivated = makeLRUCache(
(account: Account, addr: string) =>
withLibcore(async core => {
const { coreAccount } = await getCoreAccount(core, account);
const rippleLikeAccount = await coreAccount.asRippleLikeAccount();
return await rippleLikeAccount.isAddressActivated(addr);
}),
(a, addr) => a.id + "|" + addr
);
const getTransactionStatus = async (a, t) => {
const errors = {};
const warnings = {};
const baseReserve = t.networkInfo ? t.networkInfo.baseReserve : BigNumber(0);
const estimatedFees = t.fee || BigNumber(0);
const totalSpent = !t.useAllAmount
? t.amount.plus(estimatedFees)
: a.balance.minus(baseReserve);
const amount = t.useAllAmount ? a.balance.minus(estimatedFees) : t.amount;
if (amount.gt(0) && estimatedFees.times(10).gt(amount)) {
warnings.feeTooHigh = new FeeTooHigh();
}
if (!t.fee) {
errors.fee = new FeeNotLoaded();
} else if (t.fee.eq(0)) {
errors.fee = new FeeRequired();
totalSpent.gt(a.balance.minus(baseReserve));
} else if (totalSpent.gt(a.balance.minus(baseReserve))) {
errors.amount = new NotEnoughSpendableBalance();
} else if (
amount.lt(baseReserve) &&
!(await isAddressActivated(a, t.recipient))
) {
errors.amount = new NotEnoughBalanceBecauseDestinationNotCreated();
}
if (a.freshAddress === t.recipient) {
errors.recipient = new InvalidAddressBecauseDestinationIsAlsoSource();
} else {
let { recipientError, recipientWarning } = await validateRecipient(
a.currency,
t.recipient
);
if (recipientError) {
errors.recipient = recipientError;
}
if (recipientWarning) {
warnings.recipient = recipientWarning;
}
}
if (!errors.amount && amount.eq(0)) {
errors.amount = new AmountRequired();
}
return Promise.resolve({
errors,
warnings,
estimatedFees,
amount,
totalSpent
});
};
const prepareTransaction = async (a, t) => {
let networkInfo = t.networkInfo;
if (!networkInfo) {
const r = await getAccountNetworkInfo(a);
invariant(r.family === "ripple", "ripple getAccountNetworkInfo expected");
networkInfo = r;
}
const fee = t.fee || networkInfo.serverFee;
if (t.networkInfo !== networkInfo || t.fee !== fee) {
return {
...t,
networkInfo,
fee
};
}
return t;
};
const currencyBridge: CurrencyBridge = {
preload: () => Promise.resolve(),
hydrate: () => {},
scanAccounts
};
const accountBridge: AccountBridge<Transaction> = {
createTransaction,
updateTransaction,
prepareTransaction,
getTransactionStatus,
sync,
signOperation,
broadcast
};
export default { currencyBridge, accountBridge };