-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathActionTest.java
176 lines (148 loc) · 5.59 KB
/
ActionTest.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
package mercury;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.google.gson.Gson;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashSet;
import mercury.constant.AddressWithKeyHolder;
import mercury.constant.CkbNodeFactory;
import mercury.constant.MercuryApiFactory;
import mercury.constant.UdtHolder;
import org.junit.jupiter.api.Test;
import org.nervos.ckb.type.transaction.Transaction;
import org.nervos.mercury.model.TransferPayloadBuilder;
import org.nervos.mercury.model.req.Action;
import org.nervos.mercury.model.req.FromKeyAddresses;
import org.nervos.mercury.model.req.Source;
import org.nervos.mercury.model.req.ToKeyAddress;
import org.nervos.mercury.model.resp.TransactionCompletionResponse;
public class ActionTest {
Gson g = new Gson();
@Test
void transferCompletionCkbWithPayByFrom() {
TransferPayloadBuilder builder = new TransferPayloadBuilder();
builder.from(
new FromKeyAddresses(
new HashSet<>(Arrays.asList(AddressWithKeyHolder.testAddress0())),
Source.unconstrained));
builder.addItem(
new ToKeyAddress(AddressWithKeyHolder.testAddress4(), Action.pay_by_from),
new BigInteger("100")); // unit: CKB, 1 CKB = 10^8 Shannon
System.out.println(new Gson().toJson(builder.build()));
try {
TransactionCompletionResponse s =
MercuryApiFactory.getApi().buildTransferTransaction(builder.build());
System.out.println(g.toJson(s));
Transaction tx = sign(s);
String result = CkbNodeFactory.getApi().sendTransaction(tx);
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
void transferCompletionSudtWithPayByFrom() {
TransferPayloadBuilder builder = new TransferPayloadBuilder();
builder.udtHash(UdtHolder.UDT_HASH);
builder.from(
new FromKeyAddresses(
new HashSet<>(Arrays.asList(AddressWithKeyHolder.testAddress1())),
Source.unconstrained));
builder.addItem(
new ToKeyAddress(AddressWithKeyHolder.testAddress2(), Action.pay_by_from),
new BigInteger("100"));
try {
TransactionCompletionResponse s =
MercuryApiFactory.getApi().buildTransferTransaction(builder.build());
Transaction tx = sign(s);
String result = CkbNodeFactory.getApi().sendTransaction(tx);
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
void transferCompletionCkbWithLendByFrom() {
TransferPayloadBuilder builder = new TransferPayloadBuilder();
builder.from(
new FromKeyAddresses(
new HashSet<>(Arrays.asList(AddressWithKeyHolder.testAddress1())),
Source.unconstrained));
builder.addItem(
new ToKeyAddress(AddressWithKeyHolder.testAddress2(), Action.lend_by_from),
new BigInteger("100"));
try {
TransactionCompletionResponse s =
MercuryApiFactory.getApi().buildTransferTransaction(builder.build());
} catch (Exception e) {
assertEquals("The transaction does not support ckb", e.getMessage());
}
}
@Test
void transferCompletionSudtWithLendByFrom() {
TransferPayloadBuilder builder = new TransferPayloadBuilder();
builder.udtHash(UdtHolder.UDT_HASH);
builder.from(
new FromKeyAddresses(
new HashSet<>(Arrays.asList(AddressWithKeyHolder.testAddress1())),
Source.unconstrained));
builder.addItem(
new ToKeyAddress(AddressWithKeyHolder.testAddress2(), Action.lend_by_from),
new BigInteger("100"));
try {
TransactionCompletionResponse s =
MercuryApiFactory.getApi().buildTransferTransaction(builder.build());
Transaction tx = sign(s);
System.out.println(g.toJson(s.txView));
String result = CkbNodeFactory.getApi().sendTransaction(tx);
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
void transferCompletionCkbWithPayByTo() {
TransferPayloadBuilder builder = new TransferPayloadBuilder();
builder.from(
new FromKeyAddresses(
new HashSet<>(Arrays.asList(AddressWithKeyHolder.testAddress1())),
Source.unconstrained));
builder.addItem(
new ToKeyAddress(AddressWithKeyHolder.testAddress1(), Action.pay_by_to),
new BigInteger("100"));
try {
TransactionCompletionResponse s =
MercuryApiFactory.getApi().buildTransferTransaction(builder.build());
} catch (Exception e) {
assertEquals("The transaction does not support ckb", e.getMessage());
}
}
@Test
void transferCompletionSudtWithPayByTo() {
TransferPayloadBuilder builder = new TransferPayloadBuilder();
builder.udtHash(UdtHolder.UDT_HASH);
builder.from(
new FromKeyAddresses(
new HashSet<>(Arrays.asList(AddressWithKeyHolder.testAddress1())),
Source.unconstrained));
builder.addItem(
new ToKeyAddress(AddressWithKeyHolder.testAddress4(), Action.pay_by_to),
new BigInteger("100"));
System.out.println(g.toJson(builder.build()));
try {
TransactionCompletionResponse s =
MercuryApiFactory.getApi().buildTransferTransaction(builder.build());
Transaction tx = sign(s);
System.out.println(g.toJson(s.txView));
String result = CkbNodeFactory.getApi().sendTransaction(tx);
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
private Transaction sign(TransactionCompletionResponse s) throws IOException {
Transaction tx = SignUtils.sign(s);
return tx;
}
}