diff --git a/src/test/java/irita/sdk/MsgsDemo.java b/src/test/java/irita/sdk/MsgsDemo.java index 066fa9c..b91aca4 100644 --- a/src/test/java/irita/sdk/MsgsDemo.java +++ b/src/test/java/irita/sdk/MsgsDemo.java @@ -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; @@ -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 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 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"; @@ -114,13 +127,15 @@ public void testSendMsgs() throws IOException { .setSender(account.getAddress()) .setRecipient(account.getAddress()) .build(); + //构建的多个nft添加到 List 类型的列表中 List 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()); diff --git a/src/test/java/irita/sdk/NftDemo.java b/src/test/java/irita/sdk/NftDemo.java index 586e4d5..2e9a22d 100644 --- a/src/test/java/irita/sdk/NftDemo.java +++ b/src/test/java/irita/sdk/NftDemo.java @@ -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()); } @@ -51,7 +55,7 @@ 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) @@ -59,7 +63,7 @@ public void testNft() throws IOException { 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()); @@ -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"; @@ -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()); @@ -89,7 +93,7 @@ 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()); @@ -97,7 +101,7 @@ public void testNft() throws IOException { assertEquals(data, nft.getData()); assertEquals(keyInfo.getAddress(), nft.getOwner()); - //transfer nft1 + //transfer nft1 交易nft1到其他链上地址 String reci = "iaa14s9hekvzhtf3y3962zn3vzv45k0ay7mguyqhrl"; TransferNFTRequest transferReq = new TransferNFTRequest() .setDenom(denomID) @@ -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 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); } }