-
Notifications
You must be signed in to change notification settings - Fork 54
/
CoinData.java
225 lines (199 loc) · 6.95 KB
/
CoinData.java
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* *
* * MIT License
* *
* * Copyright (c) 2017-2019 nuls.io
* *
* * Permission is hereby granted, free of charge, to any person obtaining a copy
* * of this software and associated documentation files (the "Software"), to deal
* * in the Software without restriction, including without limitation the rights
* * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* * copies of the Software, and to permit persons to whom the Software is
* * furnished to do so, subject to the following conditions:
* *
* * The above copyright notice and this permission notice shall be included in all
* * copies or substantial portions of the Software.
* *
* * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* * SOFTWARE.
*
*/
package io.nuls.base.data;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.nuls.base.basic.AddressTool;
import io.nuls.base.basic.NulsByteBuffer;
import io.nuls.base.basic.NulsOutputStreamBuffer;
import io.nuls.core.exception.NulsException;
import io.nuls.core.model.ByteArrayWrapper;
import io.nuls.core.parse.SerializeUtils;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author
*/
public class CoinData extends BaseNulsData {
private List<CoinFrom> from;
private List<CoinTo> to;
public CoinData() {
from = new ArrayList<>();
to = new ArrayList<>();
}
public CoinData(byte[] bytes) throws NulsException {
from = new ArrayList<>();
to = new ArrayList<>();
this.parse(new NulsByteBuffer(bytes));
}
/**
* serialize important field
*/
@Override
protected void serializeToStream(NulsOutputStreamBuffer stream) throws IOException {
int fromCount = from == null ? 0 : from.size();
stream.writeVarInt(fromCount);
if (null != from) {
for (CoinFrom coin : from) {
stream.writeNulsData(coin);
}
}
int toCount = to == null ? 0 : to.size();
stream.writeVarInt(toCount);
if (null != to) {
for (CoinTo coin : to) {
stream.writeNulsData(coin);
}
}
}
@Override
public void parse(NulsByteBuffer byteBuffer) throws NulsException {
int fromCount = (int) byteBuffer.readVarInt();
if (0 < fromCount) {
List<CoinFrom> from = new ArrayList<>();
for (int i = 0; i < fromCount; i++) {
from.add(byteBuffer.readNulsData(new CoinFrom()));
}
this.from = from;
}
int toCount = (int) byteBuffer.readVarInt();
if (0 < toCount) {
List<CoinTo> to = new ArrayList<>();
for (int i = 0; i < toCount; i++) {
to.add(byteBuffer.readNulsData(new CoinTo()));
}
this.to = to;
}
}
@Override
public int size() {
int size = SerializeUtils.sizeOfVarInt(from == null ? 0 : from.size());
if (null != from) {
for (CoinFrom coin : from) {
size += SerializeUtils.sizeOfNulsData(coin);
}
}
size += SerializeUtils.sizeOfVarInt(to == null ? 0 : to.size());
if (null != to) {
for (CoinTo coin : to) {
size += SerializeUtils.sizeOfNulsData(coin);
}
}
return size;
}
public List<CoinFrom> getFrom() {
return from;
}
public void setFrom(List<CoinFrom> from) {
this.from = from;
}
public List<CoinTo> getTo() {
return to;
}
public void setTo(List<CoinTo> to) {
this.to = to;
}
public void addTo(CoinTo coinTo) {
if (null == to) {
to = new ArrayList<>();
}
to.add(coinTo);
}
public void addFrom(CoinFrom coinFrom) {
if (null == from) {
from = new ArrayList<>();
}
from.add(coinFrom);
}
/**
* fromCoinDataObtain and transact related addresses from(lacktxDataThe relevant address needs to be obtained separately for the corresponding transaction)
*
* @return
*/
@JsonIgnore
public Set<byte[]> getAddresses() {
Set<ByteArrayWrapper> addressSetWrapper = new HashSet<>();
if (to != null && to.size() != 0) {
for (CoinTo coinTo : to) {
byte[] address = coinTo.getAddress();
ByteArrayWrapper baw = new ByteArrayWrapper(address);
addressSetWrapper.add(baw);
}
}
if (from != null && from.size() != 0) {
for (CoinFrom coinFrom : from) {
byte[] address = coinFrom.getAddress();
ByteArrayWrapper baw = new ByteArrayWrapper(address);
addressSetWrapper.add(baw);
}
}
Set<byte[]> addressSet = new HashSet<>();
for (ByteArrayWrapper byteArrayWrapper : addressSetWrapper) {
addressSet.add(byteArrayWrapper.getBytes());
}
return addressSet;
}
public Set<String> getFromAddressList(){
Set<String> fromAddressList = new HashSet<>();
if(from != null && !from.isEmpty()){
for (CoinFrom coinFrom:from) {
fromAddressList.add(AddressTool.getStringAddressByBytes(coinFrom.getAddress()));
}
}
return fromAddressList;
}
public int getFromAddressCount(){
Set<String> addressSet = new HashSet<>();
for (CoinFrom coinFrom:from) {
addressSet.add(AddressTool.getStringAddressByBytes(coinFrom.getAddress()));
}
return addressSet.size();
}
/**
* Calculate designated asset handling fees
* @param assetChainId Designated asset chainID
* @param assetId Designated assetsID
* @return Handling fee size
* */
public BigInteger getFeeByAsset(int assetChainId, int assetId){
BigInteger fromAmount = BigInteger.ZERO;
BigInteger toAmount = BigInteger.ZERO;
for (CoinFrom coinFrom : from) {
if (coinFrom.getAssetsChainId() == assetChainId && coinFrom.getAssetsId() == assetId) {
fromAmount = fromAmount.add(coinFrom.getAmount());
}
}
for (CoinTo coinTo : to) {
if (coinTo.getAssetsChainId() == assetChainId && coinTo.getAssetsId() == assetId) {
toAmount = toAmount.add(coinTo.getAmount());
}
}
return fromAmount.subtract(toAmount);
}
}