From e2d56955b49d35faf2a73e51da29446b900b4ae0 Mon Sep 17 00:00:00 2001 From: wanghao <1019686375@qq.com> Date: Fri, 7 Jan 2022 10:39:10 +0800 Subject: [PATCH 1/2] add msgs demo --- src/test/java/irita/sdk/MsgsDemo.java | 128 ++++++++++++++++++ .../sdk/{NftDemoTest.java => NftDemo.java} | 20 +-- 2 files changed, 135 insertions(+), 13 deletions(-) create mode 100644 src/test/java/irita/sdk/MsgsDemo.java rename src/test/java/irita/sdk/{NftDemoTest.java => NftDemo.java} (83%) diff --git a/src/test/java/irita/sdk/MsgsDemo.java b/src/test/java/irita/sdk/MsgsDemo.java new file mode 100644 index 0000000..066fa9c --- /dev/null +++ b/src/test/java/irita/sdk/MsgsDemo.java @@ -0,0 +1,128 @@ +package irita.sdk; + +import com.google.protobuf.GeneratedMessageV3; +import irita.sdk.client.IritaClient; +import irita.sdk.config.ClientConfig; +import irita.sdk.config.OpbConfig; +import irita.sdk.constant.enums.BroadcastMode; +import irita.sdk.key.KeyManager; +import irita.sdk.key.KeyManagerFactory; +import irita.sdk.model.Account; +import irita.sdk.model.BaseTx; +import irita.sdk.model.Fee; +import irita.sdk.model.ResultTx; +import irita.sdk.module.nft.QueryCollectionResp; +import irita.sdk.module.nft.QueryDenomResp; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import proto.cosmos.tx.v1beta1.TxOuterClass; +import proto.nft.Tx; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Random; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class MsgsDemo { + private IritaClient client; + private BaseTx baseTx = new BaseTx(200000, new Fee("200000", "uirita"), BroadcastMode.Commit); + + @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); + + 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); + 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(); + 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) + */ + //build msg + Tx.MsgIssueDenom msg = Tx.MsgIssueDenom + .newBuilder() + .setId(denomID) + .setName(denomName) + .setSchema(schema) + .setSender(account.getAddress()) + .build(); + List issueDenomMsgs = Collections.singletonList(msg); + TxOuterClass.TxBody txBody = client.getBaseClient().getTxEngine().buildTxBodyWithMemo(issueDenomMsgs, "example memo"); + byte[] bytes = txBody.toByteArray(); + //sign + TxOuterClass.TxBody txBodyFromBytes = TxOuterClass.TxBody.parseFrom(bytes); + TxOuterClass.Tx signTx = client.getBaseClient().getTxEngine().signTx(txBodyFromBytes, baseTx, account); + byte[] signTxBytes = signTx.toByteArray(); + //broadcast + ResultTx resultTx = client.getBaseClient().getRpcClient().broadcastTx(signTxBytes, baseTx.getMode()); + assertNotNull(resultTx); + queryDenomResps = client.getNftClient().queryDenoms(null); + assertEquals(size + 1, queryDenomResps.size()); + + /** + * mint nfts in one tx + */ + String nftID1 = "test1"; + String nftID2 = "test2"; + String nftName = "test_name"; + String uri = "https://www.baidu.com"; + String data = "any data"; + //nft1 + Tx.MsgMintNFT msg1 = Tx.MsgMintNFT + .newBuilder() + .setDenomId(denomID) + .setId(nftID1) + .setName(nftName) + .setUri(uri) + .setData(data) + .setSender(account.getAddress()) + .setRecipient(account.getAddress()) + .build(); + //nft2 + Tx.MsgMintNFT msg2 = Tx.MsgMintNFT + .newBuilder() + .setDenomId(denomID) + .setId(nftID2) + .setName(nftName) + .setUri(uri) + .setData(data) + .setSender(account.getAddress()) + .setRecipient(account.getAddress()) + .build(); + 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); + assertNotNull(resultTx); + QueryCollectionResp collection = client.getNftClient().queryCollection(denomID, null); + assertNotNull(collection); + assertEquals(2, collection.getNfts().size()); + } +} diff --git a/src/test/java/irita/sdk/NftDemoTest.java b/src/test/java/irita/sdk/NftDemo.java similarity index 83% rename from src/test/java/irita/sdk/NftDemoTest.java rename to src/test/java/irita/sdk/NftDemo.java index 0632fac..586e4d5 100644 --- a/src/test/java/irita/sdk/NftDemoTest.java +++ b/src/test/java/irita/sdk/NftDemo.java @@ -22,32 +22,26 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -public class NftDemoTest { +public class NftDemo { private KeyManager km; private NftClient nftClient; - //baseTx 的 denom 可能会需要根据不同环境的链进行修改 - private BaseTx baseTx = new BaseTx(200000, new Fee("300000", "upoint"), BroadcastMode.Commit); + private BaseTx baseTx = new BaseTx(200000, new Fee("200000", "uirita"), BroadcastMode.Commit); @BeforeEach public void init() { - //换成自己链上地址的助记词 - String mnemonic = "code tattoo laundry ice chuckle priority immune rebuild dream prevent sibling inspire banner black shock page person brush oxygen sorry dilemma raccoon estate funny"; + 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); - String nodeUri = "http://101.132.67.8:26657"; - String grpcAddr = "101.132.67.8:9090"; -// String nodeUri = "https://opbningxia.bsngate.com:18602/api//rpc"; -// String grpcAddr = "opbningxia.bsngate.com:18603"; - String chainId = "wenchangchain"; + 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); OpbConfig opbConfig = null; - //测试环境 opbConfig = null,正式环境需要使用自己的 projectID 和 projectKey -// OpbConfig opbConfig = new OpbConfig("your projectID", "your projectKey", ""); IritaClient client = new IritaClient(clientConfig, opbConfig, km); nftClient = client.getNftClient(); - assertEquals("iaa18e23vvukxgatgzm4fgqkdggxecurkf39ytw7ue", km.getCurrentKeyInfo().getAddress()); + assertEquals("iaa1ytemz2xqq2s73ut3ys8mcd6zca2564a5lfhtm3", km.getCurrentKeyInfo().getAddress()); } @Test From b41055f00ef5587982f453fff9647ecd70852960 Mon Sep 17 00:00:00 2001 From: wanghao <1019686375@qq.com> Date: Fri, 7 Jan 2022 11:07:40 +0800 Subject: [PATCH 2/2] update README.md --- README.md | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b65a965..a349910 100644 --- a/README.md +++ b/README.md @@ -110,10 +110,10 @@ https://mvnrepository.com/artifact/io.github.bianjieai String grpcAddr = "opbningxia.bsngate.com:18603"; String chainId = "wenchangchain"; ClientConfig clientConfig = new ClientConfig(nodeUri, grpcAddr, chainId); - OpbConfig opbConfig = new OpbConfig(${projectID}, ${projectKey}, km.getAddr()); // 如果没有 projectKey 传null + OpbConfig opbConfig = new OpbConfig(${projectID}, ${projectKey}, km.getCurrentKeyInfo().getAddress()); // 如果没有 projectKey 传null OpbClient client = new OpbClient(clientConfig, opbConfig, km); - assertEquals("iaa1ytemz2xqq2s73ut3ys8mcd6zca2564a5lfhtm3", km.getAddr()); + assertEquals("iaa1ytemz2xqq2s73ut3ys8mcd6zca2564a5lfhtm3", km.getCurrentKeyInfo().getAddress()); ``` ### 2 构造 txBodyBz (construct txBodyBz) @@ -290,3 +290,32 @@ https://mvnrepository.com/artifact/io.github.bianjieai ### 4. perm模块的其他查询和交易 (other query and tx in this module) 查看 permClient 下的方法 (see method in perClient.java) + +## 单元测试 (Unit Test Demo) +[MsgsDemo.java](./src/test/java/irita/sdk/MsgsDemo.java) + +[NftDemo.java](./src/test/java/irita/sdk/NftDemo.java) + +[ClientTest.java](./src/test/java/irita/sdk/ClientTest.java) + +[ComGovContractTest.java](./src/test/java/irita/sdk/ComGovContractTest.java) + +[HttpUtilsTest.java](./src/test/java/irita/sdk/HttpUtilsTest.java) + +[IdentityClientTest.java](./src/test/java/irita/sdk/IdentityClientTest.java) + +[IOUtilTest.java](./src/test/java/irita/sdk/IOUtilTest.java) + +[KeyManagerTest.java](./src/test/java/irita/sdk/KeyManagerTest.java) + +[MsgsTest.java](./src/test/java/irita/sdk/MsgsTest.java) + +[NftTest.java](./src/test/java/irita/sdk/NftTest.java) + +[PermTest.java](./src/test/java/irita/sdk/PermTest.java) + +[RecordTest.java](./src/test/java/irita/sdk/RecordTest.java) + +[TibcTest.java](./src/test/java/irita/sdk/TibcTest.java) + +[WasmTest.java](./src/test/java/irita/sdk/WasmTest.java) \ No newline at end of file