Skip to content

Commit

Permalink
Merge pull request #5 from cocoleecoco/0.4.0
Browse files Browse the repository at this point in the history
'client-sdk-java'
  • Loading branch information
danielgogo authored Feb 28, 2019
2 parents 6dcedbd + d96dfc1 commit 639ca38
Show file tree
Hide file tree
Showing 12 changed files with 1,837 additions and 1,191 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
<dependency>
<groupId>com.platon.client</groupId>
<artifactId>core</artifactId>
<version>0.3.0</version>
<version>0.4.0</version>
</dependency>
```

or

```
compile "com.platon.client:core:0.3.0"
compile "com.platon.client:core:0.4.0"
```

* use in project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,9 @@ public class CandidateContractTest {

private Logger logger = LoggerFactory.getLogger(CandidateContractTest.class);

private static final Credentials CREDENTIALS = loadCredentials("sophia/contracts/build/admin.json");

private static final Credentials OWNER_CREDENTIALS = loadCredentials("sophia/contracts/build/owner.json");
private static final Credentials CREDENTIALS = loadCredentials("wallet/admin.json");

private CandidateContract contract;

private CandidateContract ownerContract;


private static Credentials loadCredentials(String walletPath) {
Credentials credentials = null;
Expand All @@ -63,12 +58,6 @@ public void init() {
CREDENTIALS,
new DefaultWasmGasProvider()
);

ownerContract = CandidateContract.load(
web3j,
OWNER_CREDENTIALS,
new DefaultWasmGasProvider()
);
}

/**
Expand Down Expand Up @@ -191,7 +180,7 @@ public void SetCandidateExtra() throws Exception {
//节点名称
extra.put("nodeName", "xxxx-noedeName");
//节点logo
extra.put("nodePortrait", "group2/M00/00/12/wKgJVlw0XSyAY78cAAH3BKJzz9Y83.jpeg");
extra.put("nodePortrait", "1");
//机构简介
extra.put("nodeDiscription", "xxxx-nodeDiscription1");
//机构名称
Expand All @@ -201,7 +190,7 @@ public void SetCandidateExtra() throws Exception {


//调用接口
TransactionReceipt receipt = ownerContract.SetCandidateExtra(nodeId,extra.toJSONString()).send();
TransactionReceipt receipt = contract.SetCandidateExtra(nodeId,extra.toJSONString()).send();
logger.debug("TransactionReceipt:{}", JSON.toJSONString(receipt));

//查看返回event
Expand Down Expand Up @@ -244,14 +233,14 @@ public void CandidateApplyWithdraw() throws Exception {
String nodeId = "0xaafbc9c699270bd33c77f1b2a5c3653eaf756f1860891327dfd8c29960a51c9aebb6c081cbfe2499db71e9f4c19e609f44cbd9514e59b6066e5e895b8b592abf";
//退款金额, 单位 wei
//4400000000000000000000
BigInteger value = new BigInteger("10 000000000000000000".replaceAll(" ", ""));
BigInteger value = new BigInteger("3000000000002000000000002".replaceAll(" ", ""));

//调用接口
TransactionReceipt receipt = ownerContract.CandidateApplyWithdraw(nodeId,value).send();
TransactionReceipt receipt = contract.CandidateApplyWithdraw(nodeId,value).send();
logger.debug("TransactionReceipt:{}", JSON.toJSONString(receipt));

//查看返回event
List<CandidateApplyWithdrawEventEventResponse> events = ownerContract.getCandidateApplyWithdrawEventEvents(receipt);
List<CandidateApplyWithdrawEventEventResponse> events = contract.getCandidateApplyWithdrawEventEvents(receipt);
for (CandidateApplyWithdrawEventEventResponse event : events) {
logger.debug("event:{}", JSON.toJSONString(event.param1));
}
Expand All @@ -268,23 +257,27 @@ public void CandidateDeposit() throws Exception {
//节点id
String nodeId = "0xaafbc9c699270bd33c77f1b2a5c3653eaf756f1860891327dfd8c29960a51c9aebb6c081cbfe2499db71e9f4c19e609f44cbd9514e59b6066e5e895b8b592abf";
//质押金退款地址
String owner = "0xf8f3978c14f585c920718c27853e2380d6f5db36";
String owner = "0x493301712671ada506ba6ca7891f436d29185821";
//出块奖励佣金比,以10000为基数(eg:5%,则fee=500)
BigInteger fee = BigInteger.valueOf(500L);
//节点IP
String host = "192.168.9.76";
//节点P2P端口号
String port = "38789";

nodeId = "0xe0b6af6cc2e10b2b74540b87098083d48343805a3ff09c655eab0b20dba2b2851aea79ee75b6e150bde58ead0be03ee4a8619ea1dfaf529cbb8ff55ca23531ed";
port = "26789";

//附加数据
JSONObject extra = new JSONObject();
//节点名称
extra.put("nodeName", "xxxx-noedeName");
extra.put("nodeName", "chendai-noedeName");
//节点logo
extra.put("nodePortrait", "http://192.168.9.86:8082/group2/M00/00/00/wKgJVlr0KDyAGSddAAYKKe2rswE261.png");
extra.put("nodePortrait", "1");
//机构简介
extra.put("nodeDiscription", "xxxx-nodeDiscription中国人");
extra.put("nodeDiscription", "chendai-nodeDiscription");
//机构名称
extra.put("nodeDepartment", "xxxx-nodeDepartment");
extra.put("nodeDepartment", "chendai-nodeDepartment");
//官网
extra.put("officialWebsite", "https://www.platon.network/");

Expand Down
190 changes: 99 additions & 91 deletions codegen/src/test/java/org/web3j/contract/test/TicketContractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,35 @@
import java.net.URL;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.web3j.crypto.CipherException;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.platon.contracts.TicketContract;
import org.web3j.platon.contracts.TicketContract.VoteTicketEventEventResponse;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.ReadonlyTransactionManager;
import org.web3j.tx.gas.DefaultWasmGasProvider;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class TicketContractTest {

private Logger logger = LoggerFactory.getLogger(TicketContractTest.class);

private static final Credentials CREDENTIALS = loadCredentials();

private TicketContract contract;

private static Credentials loadCredentials() {
Credentials credentials = null;
try {
URL url = TicketContractTest.class.getClassLoader().getResource("sophia/contracts/build/admin.json");
URL url = TicketContractTest.class.getClassLoader().getResource("wallet/admin.json");
String path = url.getPath();
credentials = WalletUtils.loadCredentials("88888888",path);
} catch (IOException e) {
Expand All @@ -39,133 +44,136 @@ private static Credentials loadCredentials() {
return credentials;
}

private Web3j web3j = Web3j.build(new HttpService("http://10.10.8.209:6789"));

@Test
public void getPrice() throws Exception {
TicketContract contract = TicketContract.load(
private Web3j web3j = Web3j.build(new HttpService("http://192.168.9.76:8789"));
// private Web3j web3j = Web3j.build(new HttpService("http://10.10.8.209:6789"));


@Before
public void init() {
contract = TicketContract.load(
web3j,
new ReadonlyTransactionManager(web3j,TicketContract.CONTRACT_ADDRESS),
CREDENTIALS,
new DefaultWasmGasProvider()
);
}

@Test
public void getPrice() throws Exception {
String price = contract.GetTicketPrice().send();
logger.debug("Ticket price: {}",price);
}



@Test
public void VoteTicket() throws Exception {
TicketContract contract = TicketContract.load(
web3j,
CREDENTIALS,
new DefaultWasmGasProvider()
);

TransactionReceipt receipt = contract
.VoteTicket(BigInteger.TEN,BigInteger.ONE,"0x1f3a8672348ff6b789e416762ad53e69063138b8eb4d8780101658f24b2369f1a8e09499226b467d8bc0c4e03e1dc903df857eeb3c67733d21b6aaee2840e429")
.send();

List<TicketContract.VoteTicketEventEventResponse> eventEvents = contract.getVoteTicketEventEvents(receipt);
eventEvents.forEach(e->logger.debug("param1:{}",e.param1));

/*String a = receipt.getLogs().get(0).getData();
RlpList rl = RlpDecoder.decode(Hex.decode(a.replace("0x","")));
List<RlpType> rlpTypes = rl.getValues();
RlpList rlpList1 = (RlpList) rlpTypes.get(0);
RlpString rlpString = (RlpString) rlpList1.getValues().get(0);
String typecode = Hex.toHexString(rlpString.getBytes());
byte[] hexByte = Numeric.hexStringToByteArray(typecode);
Type result = PlatOnTypeDecoder.decode(hexByte, Utf8String.class);
logger.debug("{}",result.getValue());*/

Assert.assertEquals("0x1",receipt.getStatus());
public void GetPoolRemainder() throws Exception {
String detail = contract.GetPoolRemainder().send();
logger.debug("{}",detail);
}


@Test
public void GetCandidateTicketIds() throws Exception {
TicketContract contract = TicketContract.load(
web3j,
new ReadonlyTransactionManager(web3j,TicketContract.CONTRACT_ADDRESS),
new DefaultWasmGasProvider()
);
public void VoteTicket() throws Exception {
//选票单价
String priceStr = contract.GetTicketPrice().send();
BigInteger price = new BigInteger(priceStr);
//购票数量
BigInteger count = BigInteger.valueOf(5L);
//候选人节点Id
String nodeId = "0x4f6c8fd10bfb512793f81a3594120c76b6991d3d06c0cc652035cbfae3fcd7cdc3f3d7a82021dfdb9ea99f014755ec1a640d832a0362b47be688bb31d504f62d";

//调用接口
TransactionReceipt receipt = contract.VoteTicket(count, price, nodeId).send();
logger.debug("TicketContract TransactionReceipt:{}", JSON.toJSONString(receipt));

//查看返回event
List<VoteTicketEventEventResponse> events = contract.getVoteTicketEventEvents(receipt);
JSONObject result = null;
for (VoteTicketEventEventResponse event : events) {
result = JSON.parseObject(event.param1);
logger.debug("TicketContract event:{}", result);
}

//获得票id
String txHash = receipt.getTransactionHash();
int tickets = result.getIntValue("Data");

List<String> ticketList = contract.VoteTicketIds(tickets, txHash);
logger.debug("TicketContract tickets:{}", ticketList);

// 20:09:43.610 [main] DEBUG org.web3j.contract.test.TicketContractTest - TicketContract TransactionReceipt:{"blockHash":"0x85a49bf683c123b4e650721338acad3c6bd3ff71e0f20de8baaf16e5e3e90f67","blockNumber":15548,"blockNumberRaw":"0x3cbc","cumulativeGasUsed":34832,"cumulativeGasUsedRaw":"0x8810","from":"0x493301712671ada506ba6ca7891f436d29185821","gasUsed":34832,"gasUsedRaw":"0x8810","logs":[{"address":"0x1000000000000000000000000000000000000002","blockHash":"0x85a49bf683c123b4e650721338acad3c6bd3ff71e0f20de8baaf16e5e3e90f67","blockNumber":15548,"blockNumberRaw":"0x3cbc","data":"0xebaa7b22526574223a747275652c2244617461223a2235222c224572724d7367223a2273756363657373227d","logIndex":0,"logIndexRaw":"0x0","removed":false,"topics":["0xee64e28ecf7347fbf257cb1ced716cf9d51a1e24f06e0bbe2c3d4f40924049c9"],"transactionHash":"0x7afe907caf45da59bef88b6b708fe45958143a41baa51f6c4410f87d1338e49f","transactionIndex":0,"transactionIndexRaw":"0x0"}],"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000001000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000200000000000000","status":"0x1","statusOK":true,"to":"0x1000000000000000000000000000000000000002","transactionHash":"0x7afe907caf45da59bef88b6b708fe45958143a41baa51f6c4410f87d1338e49f","transactionIndex":0,"transactionIndexRaw":"0x0"}
// 20:09:43.629 [main] DEBUG org.web3j.contract.test.TicketContractTest - TicketContract event:{"Ret":true,"ErrMsg":"success","Data":"5"}
// 20:09:43.633 [main] DEBUG org.web3j.contract.test.TicketContractTest - TicketContract tickets:[0x6bf2236d95a98c798abf760e43d8a1a0f375ce095f6f286198053800262988c5, 0x7f3d95634ebdbf0121a7de207b00cf2d2b4846000ec41b4a8a88d1e019701a5e, 0x57d1a6fcd7932a9615f593dbc6d6999912e170286ed1be0c0b31f6f14087c3b4, 0x3ad918e72305b6666efe2abd232554c34bab3f7cd5aa679334cb3ca3a14851ce, 0xe4f0b42906eed5f5e77cb233bd11d30e664f32b5602bda7f84d155462d496cbb]

String ids = contract
.GetBatchCandidateTicketIds("0abaf3219f454f3d07b6cbcf3c10b6b4ccf605202868e2043b6f5db12b745df0604ef01ef4cb523adc6d9e14b83a76dd09f862e3fe77205d8ac83df707969b47")
.send();
logger.debug("CandidateTicketIds: {}",ids);
}

@Test
public void GetBatchCandidateTicketIds() throws Exception {
TicketContract contract = TicketContract.load(
web3j,
new ReadonlyTransactionManager(web3j,TicketContract.CONTRACT_ADDRESS),
new DefaultWasmGasProvider()
);
public void GetTicketDetail() throws Exception {
//票id
String ticketId = "0x6bf2236d95a98c798abf760e43d8a1a0f375ce095f6f286198053800262988c5";

String detail = contract.GetBatchCandidateTicketIds("0abaf3219f454f3d07b6cbcf3c10b6b4ccf605202868e2043b6f5db12b745df0604ef01ef4cb523adc6d9e14b83a76dd09f862e3fe77205d8ac83df707969b47").send();
String detail = contract.GetTicketDetail(ticketId).send();

logger.debug("{}",detail);

}

@Test
public void GetTicketDetail() throws Exception {
TicketContract contract = TicketContract.load(
web3j,
new ReadonlyTransactionManager(web3j,TicketContract.CONTRACT_ADDRESS),
new DefaultWasmGasProvider()
);
public void GetBatchTicketDetail() throws Exception {
StringBuilder stringBuilder = new StringBuilder();
//票id
stringBuilder.append("0x6bf2236d95a98c798abf760e43d8a1a0f375ce095f6f286198053800262988c5");
//分割
stringBuilder.append(":");
//票id
stringBuilder.append("0x7f3d95634ebdbf0121a7de207b00cf2d2b4846000ec41b4a8a88d1e019701a5e");

String detail = contract.GetTicketDetail("0x34bdecd0fa6b8d85b1fa436eac6066aca2f51cc5e84fec278bff7df781310982").send();
String detail = contract.GetBatchTicketDetail(stringBuilder.toString()).send();

logger.debug("{}",detail);

}

@Test
public void GetBatchTicketDetail() throws Exception {
String ticketIds = "0x34bdecd0fa6b8d85b1fa436eac6066aca2f51cc5e84fec278bff7df781310982:0x29d376241e85cba191406c7a698cb4aa322ec7971c89e9c7f7123c81b64fc7ba:0x8156589c8f71e981a3835362542ebf10bfbf93bc01cebc3b2c7185416f209508";
TicketContract contract = TicketContract.load(
web3j,
new ReadonlyTransactionManager(web3j,TicketContract.CONTRACT_ADDRESS),
new DefaultWasmGasProvider()
);
public void GetCandidateTicketIds() throws Exception {
//节点id
String nodeId = "0x4f6c8fd10bfb512793f81a3594120c76b6991d3d06c0cc652035cbfae3fcd7cdc3f3d7a82021dfdb9ea99f014755ec1a640d832a0362b47be688bb31d504f62d";

String ids = contract.GetCandidateTicketIds(nodeId).send();

logger.debug("CandidateTicketIds: {}",ids);
}


@Test
public void GetBatchCandidateTicketIds() throws Exception {

String detail = contract.GetBatchTicketDetail(ticketIds).send();
StringBuilder stringBuilder = new StringBuilder();
//候选人id
stringBuilder.append("0x4f6c8fd10bfb512793f81a3594120c76b6991d3d06c0cc652035cbfae3fcd7cdc3f3d7a82021dfdb9ea99f014755ec1a640d832a0362b47be688bb31d504f62d");
//分割
stringBuilder.append(":");
//候选人id
stringBuilder.append("0x01d033b5b07407e377a3eb268bdc3f07033774fb845b7826a6b741430c5e6b719bda5c4877514e8052fa5dbc2f20fb111a576f6696b6a16ca765de49e11e0541");


String detail = contract.GetBatchCandidateTicketIds(stringBuilder.toString()).send();

logger.debug("{}",detail);

}



@Test
public void GetCandidateEpoch() throws Exception {
TicketContract contract = TicketContract.load(
web3j,
new ReadonlyTransactionManager(web3j,TicketContract.CONTRACT_ADDRESS),
new DefaultWasmGasProvider()
);

String detail = contract.GetCandidateEpoch("0abaf3219f454f3d07b6cbcf3c10b6b4ccf605202868e2043b6f5db12b745df0604ef01ef4cb523adc6d9e14b83a76dd09f862e3fe77205d8ac83df707969b47").send();
//节点id
String nodeId = "0x4f6c8fd10bfb512793f81a3594120c76b6991d3d06c0cc652035cbfae3fcd7cdc3f3d7a82021dfdb9ea99f014755ec1a640d832a0362b47be688bb31d504f62d";

String detail = contract.GetCandidateEpoch(nodeId).send();

logger.debug("{}",detail);

}

@Test
public void GetPoolRemainder() throws Exception {
TicketContract contract = TicketContract.load(
web3j,
new ReadonlyTransactionManager(web3j,TicketContract.CONTRACT_ADDRESS),
new DefaultWasmGasProvider()
);

String detail = contract.GetPoolRemainder().send();

logger.debug("{}",detail);

}

}
Loading

0 comments on commit 639ca38

Please sign in to comment.