OSDN Git Service

refactor
[bytom/bytom-java-sdk.git] / java-sdk / src / main / java / io / bytom / api / UnspentOutput.java
1 package io.bytom.api;
2
3 import com.google.gson.annotations.SerializedName;
4 import io.bytom.common.ParameterizedTypeImpl;
5 import io.bytom.common.Utils;
6 import io.bytom.exception.BytomException;
7 import io.bytom.http.Client;
8 import org.apache.log4j.Logger;
9
10 import java.lang.reflect.Type;
11 import java.util.List;
12
13 public class UnspentOutput {
14
15     /**
16      * The id of the asset being controlled.
17      */
18     @SerializedName("asset_id")
19     public String assetId;
20
21     /**
22      * The alias of the asset being controlled.
23      */
24     @SerializedName("asset_alias")
25     public String assetAlias;
26
27     /**
28      * The number of units of the asset being controlled.
29      */
30     public long amount;
31
32     /**
33      * address of account
34      */
35     public String address;
36
37     /**
38      * whether the account address is change
39      */
40     public boolean change;
41
42     /**
43      * The ID of the output.
44      */
45     @SerializedName("id")
46     public String id;
47
48     /**
49      * The control program which must be satisfied to transfer this output.
50      */
51     @SerializedName("program")
52     public String program;
53
54     @SerializedName("control_program_index")
55     public String controlProgramIndex;
56
57     /**
58      * source unspent output id
59      */
60     @SerializedName("source_id")
61     public String sourceId;
62
63     /**
64      * position of source unspent output id in block
65      */
66     @SerializedName("source_pos")
67     public int sourcePos;
68
69     /**
70      * The definition of the asset being controlled (possibly null).
71      */
72     @SerializedName("valid_height")
73     public int validHeight;
74
75     private static Logger logger = Logger.getLogger(UnspentOutput.class);
76
77     /**
78      * Serializes the Address into a form that is safe to transfer over the wire.
79      *
80      * @return the JSON-serialized representation of the Receiver object
81      */
82     public String toJson() {
83         return Utils.serializer.toJson(this);
84     }
85
86     public static class QueryBuilder {
87
88         /**
89          * id of unspent output.
90          */
91         public String id;
92
93         /**
94          * The id of the account controlling this output (possibly null if a control program
95          * is specified).
96          */
97         @SerializedName("account_id")
98         public String accountId;
99
100         /**
101          * The alias of the account controlling this output (possibly null if a control
102          * program is specified).
103          */
104         @SerializedName("account_alias")
105         public String accountAlias;
106
107         public Boolean unconfirmed;
108
109         @SerializedName("smart_contract")
110         public Boolean smartContract;
111
112         public Integer from;
113
114         public Integer count;
115
116         public QueryBuilder setId(String id) {
117             this.id = id;
118             return this;
119         }
120
121         public QueryBuilder setAccountId(String accountId) {
122             this.accountId = accountId;
123             return this;
124         }
125
126         public QueryBuilder setAccountAlias(String accountAlias) {
127             this.accountAlias = accountAlias;
128             return this;
129         }
130
131         public QueryBuilder setSmartContract(boolean smartContract) {
132             this.smartContract = smartContract;
133             return this;
134         }
135
136         public QueryBuilder setUnconfirmed(boolean unconfirmed) {
137             this.unconfirmed = unconfirmed;
138             return this;
139         }
140
141         public QueryBuilder setFrom(Integer from) {
142             this.from = from;
143             return this;
144         }
145
146         public QueryBuilder setCount(Integer count) {
147             this.count = count;
148             return this;
149         }
150
151         /**
152          * call list-unspent-outputs api
153          *
154          * @param client client object that makes requests to the core
155          * @return
156          * @throws BytomException BytomException
157          */
158         public List<UnspentOutput> list(Client client) throws BytomException {
159
160             Type listType = new ParameterizedTypeImpl(List.class, new Class[]{UnspentOutput.class});
161             List<UnspentOutput> unspentOutputList = client.request("list-unspent-outputs", this, listType);
162             logger.info("list-unspent-outputs:");
163             logger.info("size of unspentOutputList:" + unspentOutputList.size());
164             for (UnspentOutput UTXO : unspentOutputList) {
165                 logger.info(UTXO.toJson());
166             }
167
168             return unspentOutputList;
169         }
170
171     }
172 }