-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.ts
180 lines (162 loc) · 4.52 KB
/
index.ts
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
export function getHistory(
deltas: IDelta[],
baseCurrency = "RVN"
): IHistoryItem[] {
const deltasByTransactionId = getDeltasMappedToTransactionId(deltas);
const history = Array.from(deltasByTransactionId.values()).map((obj) =>
getListItem(obj, baseCurrency)
);
history.sort((h1, h2) => {
//Sort on blockheight AND transaction, you can send multiple transaction in the same block
const value1 = h1.blockHeight + "_" + h1.transactionId;
const value2 = h2.blockHeight + "_" + h2.transactionId;
if (value1 > value2) {
return -1;
}
if (value2 < value1) {
return 1;
}
return 0;
});
return history;
}
/**
*
* @param deltas Address deltas from the same transaction
*/
function getListItem(deltas: IDelta[], baseCurrency = "RVN"): IHistoryItem {
//Very simple if only one delta, like you received two LEMONADE tokens
if (deltas.length === 1) {
const delta = deltas[0];
const item: IHistoryItem = {
isSent: delta.satoshis < 0,
fee: 0,
assets: [
{
assetName: delta.assetName,
satoshis: delta.satoshis,
value: delta.satoshis / 1e8,
},
],
blockHeight: delta.height,
transactionId: delta.txid,
};
return item;
} else {
const balanceByAsset = {};
deltas.map((delta) => {
balanceByAsset[delta.assetName] = balanceByAsset[delta.assetName] || 0;
balanceByAsset[delta.assetName] += delta.satoshis;
});
const fee = getBaseCurrencyFee(deltas, baseCurrency);
if (fee > 0) {
balanceByAsset[baseCurrency] -= fee;
}
let isSent = false;
let assets: INeedABetterName[] = Object.keys(balanceByAsset).map((name) => {
//If any of the values are negative, it means we have sent
if (balanceByAsset[name] < 0) {
isSent = true;
}
const obj = {
assetName: name,
satoshis: balanceByAsset[name],
value: balanceByAsset[name] / 1e8,
};
return obj;
});
//Did we transfer asset (not RVN)
const containsAssets = !!assets.find(
(asset) => asset.assetName !== baseCurrency
);
const hasSentAssets = isSent && containsAssets === true;
//OK we have transfered assets
//If we find RVN transferes less than 5 RVN, assume it is the miners fee
//Sure, technically you can send 4 RVN and 1 LEMONADE in the same transaction but that is exceptional
//@ts-ignore
if (hasSentAssets === true) {
assets = assets.filter((asset) => {
if (asset.assetName === baseCurrency && asset.value < 5) {
return false;
}
return true;
});
}
const listItem: IHistoryItem = {
assets,
blockHeight: deltas[0].height,
transactionId: deltas[0].txid,
isSent,
fee: fee,
};
return listItem;
}
}
function getDeltasMappedToTransactionId(deltas: IDelta[]) {
if (!deltas) {
throw Error("Argument deltas is mandatory and cannot be nullish");
}
const map: Map<string, IDelta[]> = new Map();
deltas.map((delta) => {
const arr: IDelta[] = map.get(delta.txid) || [];
arr.push(delta);
map.set(delta.txid, arr);
});
return map;
}
export interface IDelta {
assetName: string;
satoshis: number;
txid: string;
index: number;
blockindex: number;
height: number;
address: string;
}
interface INeedABetterName {
assetName: string;
value: number;
satoshis: number;
}
export interface IHistoryItem {
isSent: boolean;
assets: INeedABetterName[];
blockHeight: number;
transactionId: string;
fee: number;
}
export default {
getHistory,
};
function getBaseCurrencyFee(deltas: IDelta[], baseCurrency = "RVN"): number {
//We currently do not support calculation of fee.
//Why? because we need to get the full transaction to get the fee
return 0;
/*
//Check all inputed RVN and match with outputted RVN
//The diff is the tansaction fee.
//this only applies to SENT transactions
let inputted = 0;
let outputted = 0;
//It is sent if we have a RVN transfer that is negative
const isSent = !!deltas.find(
(delta) => delta.assetName === "RVN" && delta.satoshis < 0
);
if (isSent === true) {
console.log("Think that ", deltas[0].txid, "is sent");
}
if (isSent === false) {
return 0;
}
for (let delta of deltas) {
if (delta.assetName === "RVN") {
if (delta.satoshis < 0) {
inputted = inputted + delta.satoshis;
} else if (delta.satoshis > 0) {
outputted = outputted + delta.satoshis;
}
}
}
const fee = inputted - outputted;
return fee;*/
}