-
Notifications
You must be signed in to change notification settings - Fork 16
/
FeeGrantTest.java
143 lines (121 loc) · 6.02 KB
/
FeeGrantTest.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
package irita.sdk;
import irita.sdk.client.BaseClient;
import irita.sdk.client.IritaClient;
import irita.sdk.config.ClientConfig;
import irita.sdk.constant.enums.BroadcastMode;
import irita.sdk.key.AlgoEnum;
import irita.sdk.key.KeyManager;
import irita.sdk.key.KeyManagerFactory;
import irita.sdk.model.*;
import irita.sdk.module.bank.BankClient;
import irita.sdk.module.feegrant.FeeGrantClient;
import irita.sdk.module.nft.MintNFTRequest;
import irita.sdk.module.nft.NftClient;
import org.junit.jupiter.api.*;
import proto.cosmos.base.query.v1beta1.Pagination;
import proto.cosmos.feegrant.v1beta1.QueryOuterClass;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class FeeGrantTest extends ConfigTest {
private IritaClient iritaClient;
private KeyManager km;
private final String tokenDenom = "ugas";
private final BaseTx baseTx = new BaseTx(200000, new Fee("200000", tokenDenom), BroadcastMode.Commit);
@BeforeEach
public void init() {
iritaClient = getTestClient();
km = iritaClient.getBaseClient().getKm();
}
@Test
@Disabled
@Tag("首先调用该方法给某一地址开通代付token的功能")
@Order(1)
public void testGrantAllowance() throws IOException {
// 注意这里需要使用有余额的地址(iaa1ytemz2xqq2s73ut3ys8mcd6zca2564a5lfhtm3)初始化 client
BaseClient baseClient = iritaClient.getBaseClient();
String garenter = km.getCurrentKeyInfo().getAddress(); // 有余额的地址
String garentee = "iaa106lcg5m8h3cdawun7c2rrwpj7q3ncfr9k3wxwx"; // 给没有余额的地址授予代付功能
FeeGrantClient feeGrantClient = new FeeGrantClient(baseClient);
ResultTx resultTx = feeGrantClient.GrantAllowance(garenter, garentee, baseTx);
System.out.println(resultTx.getResult().getHash());
assertNotNull(resultTx);
}
@Test
@Disabled
@Tag("使用被赋予代付功能的地址发送交易")
@Order(2)
public void testFeeGrant() throws IOException {
// 注意这个需要使用被赋予代付的地址(iaa106lcg5m8h3cdawun7c2rrwpj7q3ncfr9k3wxwx)的助记词初始化 client
String mnemonic = "require trip wage figure garage reunion neutral cave because scorpion inject hover blouse dress skin oven garlic virus exist purpose boy convince become inform";
km = KeyManagerFactory.createKeyManager(AlgoEnum.SM2);
km.recover(mnemonic);
// 新的一个客户端
String nodeUri = "http://47.100.192.234:26657";
String grpcAddr = "47.100.192.234:9090";
String chainId = "testing";
ClientConfig clientConfig = new ClientConfig(nodeUri, grpcAddr, chainId);
IritaClient iritaClient2 = new IritaClient(clientConfig, null, km);
assertEquals("iaa106lcg5m8h3cdawun7c2rrwpj7q3ncfr9k3wxwx", km.getCurrentKeyInfo().getAddress());
NftClient nftClient = iritaClient2.getNftClient();
MintNFTRequest req = new MintNFTRequest();
req.setDenom("denomid669");
req.setId("hahhahaid" + new Random().nextInt(1000));
req.setData("");
req.setName("my_name");
req.setRecipient(km.getCurrentKeyInfo().getAddress());
// 设置FeeGrant 该FeeGrant账户须是已对本次交易发送方账户进行过feegrant操作
baseTx.setFeeGranter("iaa1ytemz2xqq2s73ut3ys8mcd6zca2564a5lfhtm3");
String originalToken = testQueryAccount("iaa1ytemz2xqq2s73ut3ys8mcd6zca2564a5lfhtm3", tokenDenom);
ResultTx resultTx = nftClient.mintNft(req, baseTx);
System.out.println(resultTx.getResult().getHash());
// 最后验证是否代付人的token变少
String afterTxToken = testQueryAccount("iaa1ytemz2xqq2s73ut3ys8mcd6zca2564a5lfhtm3", tokenDenom);
assertEquals(new BigInteger(originalToken).subtract(new BigInteger("200000")), new BigInteger(afterTxToken));
}
@Test
@Disabled
@Tag("撤销默一地址的代付功能")
@Order(3)
public void testRevokeAllowance() throws IOException {
FeeGrantClient feeGrantClient = iritaClient.getFeeGrantClient();
String garenter = km.getCurrentKeyInfo().getAddress();
String garentee = "iaa106lcg5m8h3cdawun7c2rrwpj7q3ncfr9k3wxwx"; // 要撤销的地址
ResultTx resultTx = feeGrantClient.RevokeAllowance(garenter, garentee, baseTx);
System.out.println(resultTx.getResult().getHash());
}
private String testQueryAccount(String address, String tokenDenom) {
BankClient bankClient = iritaClient.getBankClient();
BaseAccount account = bankClient.queryAccount(address);
List<Coin> coins = account.getCoins();
Optional<Coin> iritaCoin = coins.stream().filter(x -> x.getDenom().equals(tokenDenom)).findFirst();
return iritaCoin.isPresent() ? iritaCoin.get().getAmount() : "0";
}
@Test
@Disabled
@Tag("根据授权者和被授权者地址查询是否授权")
public void testAllowance(){
FeeGrantClient feeGrantClient = iritaClient.getFeeGrantClient();
String garenter = km.getCurrentKeyInfo().getAddress();
String garentee = "iaa106lcg5m8h3cdawun7c2rrwpj7q3ncfr9k3wxwx";
QueryOuterClass.QueryAllowanceResponse response = feeGrantClient.Allowance(garenter,garentee);
System.out.println(response.hasAllowance());
}
@Test
@Disabled
@Tag("根据被授权者地址查询授权者信息")
public void testAllowances(){
FeeGrantClient feeGrantClient = iritaClient.getFeeGrantClient();
String garentee = "iaa106lcg5m8h3cdawun7c2rrwpj7q3ncfr9k3wxwx";
Pagination.PageRequest page = Pagination.PageRequest.newBuilder()
.setOffset(0)
.setLimit(10)
.build();
QueryOuterClass.QueryAllowancesResponse response = feeGrantClient.Allowances(garentee,page);
System.out.println(response.getAllowancesCount());
}
}