Skip to content

Commit

Permalink
update demo remark
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghao-bianjie committed Jan 7, 2022
1 parent b41055f commit 5d3f2ab
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 32 deletions.
47 changes: 31 additions & 16 deletions src/test/java/irita/sdk/MsgsDemo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package irita.sdk;

import com.google.protobuf.GeneratedMessageV3;
import irita.sdk.client.BaseClient;
import irita.sdk.client.IritaClient;
import irita.sdk.config.ClientConfig;
import irita.sdk.config.OpbConfig;
Expand Down Expand Up @@ -34,58 +35,70 @@ public class MsgsDemo {

@BeforeEach
public void init() {
//更换为自己链上地址的助记词
String mnemonic = "opera vivid pride shallow brick crew found resist decade neck expect apple chalk belt sick author know try tank detail tree impact hand best";
KeyManager km = KeyManagerFactory.createDefault();
km.recover(mnemonic);

//连接测试网(连接主网请参考README.md)
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);
//测试网为null,主网请参考README.md
OpbConfig opbConfig = null;

client = new IritaClient(clientConfig, opbConfig, km);
//判断由助记词恢复的是否为预期的链上地址
assertEquals("iaa1ytemz2xqq2s73ut3ys8mcd6zca2564a5lfhtm3", km.getCurrentKeyInfo().getAddress());
}

@Test
@Disabled
public void testSendMsgs() throws IOException {
List<QueryDenomResp> queryDenomResps = client.getNftClient().queryDenoms(null);
int size = queryDenomResps.size();
//创建 denom 的参数
String denomID = "testdenom" + new Random().nextInt(1000);
String denomName = "test_name";
String schema = "no shcema";

Account account = client.getBaseClient().queryAccount(baseTx);

/**
* issue denom (build 、sign、broadcast)
* 以 NFT 模块的 issue denom 交易为例,通过 baseClient build->sign->broadcast 发送交易
* 其他模块交易也可以以这种方式实现
*/
//build msg
BaseClient baseClient = client.getBaseClient();
//查询账户信息
Account account = baseClient.queryAccount(baseTx);
//build msg 构建msg
Tx.MsgIssueDenom msg = Tx.MsgIssueDenom
.newBuilder()
.setId(denomID)
.setName(denomName)
.setSchema(schema)
.setSender(account.getAddress())
.setSender(account.getAddress())//这种形式下的所有交易都需要指定sender
.build();
List<GeneratedMessageV3> issueDenomMsgs = Collections.singletonList(msg);
TxOuterClass.TxBody txBody = client.getBaseClient().getTxEngine().buildTxBodyWithMemo(issueDenomMsgs, "example memo");
//memo 为交易备注,如不需要交易备注可以直接使用 buildTxBody()
//TxOuterClass.TxBody txBody = client.getBaseClient().getTxEngine().buildTxBody(issueDenomMsgs);
TxOuterClass.TxBody txBody = baseClient.getTxEngine().buildTxBodyWithMemo(issueDenomMsgs, "example memo");
byte[] bytes = txBody.toByteArray();
//sign
//sign 签名
TxOuterClass.TxBody txBodyFromBytes = TxOuterClass.TxBody.parseFrom(bytes);
TxOuterClass.Tx signTx = client.getBaseClient().getTxEngine().signTx(txBodyFromBytes, baseTx, account);
TxOuterClass.Tx signTx = baseClient.getTxEngine().signTx(txBodyFromBytes, baseTx, account);
byte[] signTxBytes = signTx.toByteArray();
//broadcast
ResultTx resultTx = client.getBaseClient().getRpcClient().broadcastTx(signTxBytes, baseTx.getMode());
//broadcast 广播签名后的交易
ResultTx resultTx = baseClient.getRpcClient().broadcastTx(signTxBytes, baseTx.getMode());
assertNotNull(resultTx);
queryDenomResps = client.getNftClient().queryDenoms(null);
assertEquals(size + 1, queryDenomResps.size());

//查询denom验证
QueryDenomResp denom = client.getNftClient().queryDenom(denomID);
assertNotNull(denom);
assertEquals(denomID, denom.getId());

/**
* mint nfts in one tx
* 在一笔交易中可以有多个msg,下面的例子是在一笔交易中 mint 多个 nft
*/
String nftID1 = "test1";
String nftID2 = "test2";
Expand Down Expand Up @@ -114,13 +127,15 @@ public void testSendMsgs() throws IOException {
.setSender(account.getAddress())
.setRecipient(account.getAddress())
.build();
//构建的多个nft添加到 List<GeneratedMessageV3> 类型的列表中
List<GeneratedMessageV3> mintNFTMsgs = new ArrayList<>();
mintNFTMsgs.add(msg1);
mintNFTMsgs.add(msg2);
//mintNFTMsgs.add(msgN)
account = client.getBaseClient().queryAccount(baseTx);
resultTx = client.getBaseClient().buildAndSend(mintNFTMsgs, baseTx, account);
//使用 baseClient 每一笔交易前都需要查询一次account(因为每发一笔交易,该地址account的sequence会自增)
account = baseClient.queryAccount(baseTx);
resultTx = baseClient.buildAndSend(mintNFTMsgs, baseTx, account);
assertNotNull(resultTx);
//通过denomID查询collection,即该denom和其nft列表信息
QueryCollectionResp collection = client.getNftClient().queryCollection(denomID, null);
assertNotNull(collection);
assertEquals(2, collection.getNfts().size());
Expand Down
51 changes: 35 additions & 16 deletions src/test/java/irita/sdk/NftDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,22 @@ public class NftDemo {

@BeforeEach
public void init() {
//更换为自己链上地址的助记词
String mnemonic = "opera vivid pride shallow brick crew found resist decade neck expect apple chalk belt sick author know try tank detail tree impact hand best";
km = KeyManagerFactory.createDefault();
km.recover(mnemonic);

//连接测试网(连接主网请参考README.md)
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);
//测试网为null,主网请参考README.md
OpbConfig opbConfig = null;

IritaClient client = new IritaClient(clientConfig, opbConfig, km);
nftClient = client.getNftClient();
//判断由助记词恢复的是否为预期的链上地址
assertEquals("iaa1ytemz2xqq2s73ut3ys8mcd6zca2564a5lfhtm3", km.getCurrentKeyInfo().getAddress());
}

Expand All @@ -51,15 +55,15 @@ public void testNft() throws IOException {
String denomName = "test_name";
String schema = "no shcema";

//issue denom
//issue denom 创建 denom
IssueDenomRequest req = new IssueDenomRequest()
.setId(denomID)
.setName(denomName)
.setSchema(schema);
ResultTx resultTx = nftClient.issueDenom(req, baseTx);
assertNotNull(resultTx.getResult().getHash());

//query denom
//query denom 通过denomID查询denom信息
QueryDenomResp denom = nftClient.queryDenom(denomID);
assertEquals(denomID, denom.getId());
assertEquals(denomName, denom.getName());
Expand All @@ -68,7 +72,7 @@ public void testNft() throws IOException {
KeyInfo keyInfo = km.getCurrentKeyInfo();
assertEquals(keyInfo.getAddress(), denom.getCreator());

//mint nft
//mint nft 在上面创建的denom下发行2个nft
String nftID1 = "test1" + new Random().nextInt(1000);
String nftID2 = "test2" + new Random().nextInt(1000);
String nftName = "test_name";
Expand All @@ -80,7 +84,7 @@ public void testNft() throws IOException {
.setName(nftName)
.setUri(uri)
.setData(data)
.setRecipient(keyInfo.getAddress());
.setRecipient(keyInfo.getAddress());//nft的接收地址
//mint nft1
resultTx = nftClient.mintNft(mintReq, baseTx);
assertNotNull(resultTx.getResult().getHash());
Expand All @@ -89,15 +93,15 @@ public void testNft() throws IOException {
resultTx = nftClient.mintNft(mintReq, baseTx);
assertNotNull(resultTx.getResult().getHash());

//query nft1
//query nft1 通过denomID和nftID查询nft信息
QueryNFTResp nft = nftClient.queryNFT(denomID, nftID1);
assertEquals(nftID1, nft.getId());
assertEquals(nftName, nft.getName());
assertEquals(uri, nft.getUri());
assertEquals(data, nft.getData());
assertEquals(keyInfo.getAddress(), nft.getOwner());

//transfer nft1
//transfer nft1 交易nft1到其他链上地址
String reci = "iaa14s9hekvzhtf3y3962zn3vzv45k0ay7mguyqhrl";
TransferNFTRequest transferReq = new TransferNFTRequest()
.setDenom(denomID)
Expand All @@ -106,37 +110,52 @@ public void testNft() throws IOException {
resultTx = nftClient.transferNFt(transferReq, baseTx);
assertNotNull(resultTx.getResult().getHash());

//query nft1
//query nft1 查询nft1的信息 判断nft的owner地址是否已改变
nft = nftClient.queryNFT(denomID, nftID1);
assertEquals(reci, nft.getOwner());

//query collection
//edit nft2 通过denomID和nftID修改nft的name/uri/data
String newNftName = "test_name_new";
String newUri = "https://www.baidu.com/new";
String newData = "any data new";
EditNFTRequest editReq = new EditNFTRequest()
.setDenom(denomID)
.setId(nftID2)
.setName(newNftName)
.setUri(newUri)
.setData(newData);
resultTx = nftClient.editNft(editReq, baseTx);
assertNotNull(resultTx.getResult().getHash());

//query collection 通过denomID查询集合 即该denom信息和其nft列表信息
QueryCollectionResp queryCollectionResp = nftClient.queryCollection(denomID, null);
assertNotNull(queryCollectionResp);
assertEquals(queryCollectionResp.getNfts().size(), 2);
assertEquals(2, queryCollectionResp.getNfts().size());

//burn nft2
//burn nft2 通过denomID和nftID销毁nft2
BurnNFTRequest burnNFTReq = new BurnNFTRequest()
.setDenom(denomID)
.setId(nftID2);
resultTx = nftClient.burnNft(burnNFTReq, baseTx);
assertNotNull(resultTx.getResult().getHash());

//query collection
//query collection 查询集合,nft列表的数量减少1个
queryCollectionResp = nftClient.queryCollection(denomID, null);
assertNotNull(queryCollectionResp);
assertEquals(queryCollectionResp.getNfts().size(), 1);
assertEquals(1, queryCollectionResp.getNfts().size());

//query denoms
//query denoms 查询所有denom列表
List<QueryDenomResp> queryDenomResps = nftClient.queryDenoms(null);
assertNotNull(queryDenomResps);
//query owner

//query owner 通过地址查询该地址拥有的nft列表 denomID可以指定也可以传空
QueryOwnerResp queryOwnerResp = nftClient.queryOwner(denomID, reci);
assertNotNull(queryOwnerResp);
queryOwnerResp = nftClient.queryOwner("", reci);
assertNotNull(queryOwnerResp);
//query supply

//query supply 查询该地址在该denom下拥有的nft数量
long supply = nftClient.querySupply(denomID, reci);
assertEquals(supply, 1);
assertEquals(1, supply);
}
}

0 comments on commit 5d3f2ab

Please sign in to comment.