-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpair_amount.js
More file actions
38 lines (34 loc) · 1.18 KB
/
Copy pathpair_amount.js
File metadata and controls
38 lines (34 loc) · 1.18 KB
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
export function createAmountFromMojo(tokenMojo, xchMojo) {
return {
token_amount: Number.isNaN(tokenMojo) ? 0 : tokenMojo / 1000,
token_amount_mojo: Number.isNaN(tokenMojo) ? 0 : tokenMojo,
xch_amount: Number.isNaN(xchMojo) ? 0 : xchMojo / 10 ** 12,
xch_amount_mojo: Number.isNaN(xchMojo) ? 0 : xchMojo,
};
}
export function getPairAmount(record) {
const pairAmount = createAmountFromMojo(0, 0);
for (const field in record) {
if (field === "xch") {
pairAmount.xch_amount = record.xch / 10.0 ** 12;
pairAmount.xch_amount_mojo = record.xch;
} else {
// the token asset_id is the field name
pairAmount.token_amount = record[field] / 1000.0;
pairAmount.token_amount_mojo = record[field];
}
}
return pairAmount;
}
export function negate(amount) {
return createAmountFromMojo(
-amount.token_amount_mojo,
-amount.xch_amount_mojo,
);
}
export function addAmounts(amount1, amount2) {
return createAmountFromMojo(
amount1.token_amount_mojo + amount2.token_amount_mojo,
amount1.xch_amount_mojo + amount2.xch_amount_mojo,
);
}