Skip to content

Commit

Permalink
Merge pull request #28 from wanghao-bianjie/master
Browse files Browse the repository at this point in the history
R4R: add msgs demo
  • Loading branch information
kaifei Hu authored Jan 7, 2022
2 parents 8455b9d + b41055f commit 20472ce
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 15 deletions.
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
128 changes: 128 additions & 0 deletions src/test/java/irita/sdk/MsgsDemo.java
Original file line number Diff line number Diff line change
@@ -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<QueryDenomResp> 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<GeneratedMessageV3> 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<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);
assertNotNull(resultTx);
QueryCollectionResp collection = client.getNftClient().queryCollection(denomID, null);
assertNotNull(collection);
assertEquals(2, collection.getNfts().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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/<your projectID>/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
Expand Down

0 comments on commit 20472ce

Please sign in to comment.