Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

42 Commits

Repository files navigation

众安链SDK

目前SDK支持智能合约的部署、调用和查询功能。

准备

编译solidity合约请自行完成
(可以通过https://remix.ethereum.org/在线编译或使用solc本地编译
生成byteCode)

  • 编译 test.sol
solc test.sol

使用说明(示例)

初始化节点服务

NodeSrvnodeSrv = newNodeSrv("http://${HOST}:${PORT}/");

生成账户私钥和地址(支持PrivateKeyECDSA)

PrivateKeyprivKey = PrivateKeyECDSA.Create();
StringprivKeyStr = privKey.toHexString();
Stringaddress = privKey.getAddress();

从已有私钥16进制字符初始化私钥对象

PrivateKeyprivKey = newPrivateKeyECDSA("0x6d79264403d667f75caf2f1dca6412c6922b15433b515850c5c00a2aa12010e9");
Stringaddress = privKey.getAddress();

查询nonce

PrivateKeyprivKey;
Stringaddress = privKey.getAddress();
intnonce = nodeSrv.queryNonce(address);

查询pending nonce

PrivateKeyprivKey;
Stringaddress = privKey.getAddress();
intnonce = nodeSrv.queryPendingNonce(address);

部署合约

部署之前编译生成的byteCode到节点

PrivateKeyprivKey = newPrivateKeyECDSA(${privKeyStr});
StringcontractAddr = nodeSrv.deployContract(${byteCode}, Arrays.asList(), privKey, BigInteger.valueOf(nonce));

调用合约(默认同步调用)

// event 定义EventDEPOSIT = newEvent("Deposit",
Arrays.asList(),
Arrays.asList(newTypeReference<Address>() {
}, newTypeReference<Address>() {
}, newTypeReference<Utf8String>() {
}));
FunctionfunctionDef = newFunction("deposit", // function we're callingArrays.asList(), // Parameters to pass as Solidity TypesArrays.asList()
);
Stringresp = nodeSrv.CallContract(BigInteger.valueOf(nonce),
"0xee463ba03224a14706728cbede6abcd76ed4e7cc",
functionDef, //函数接口定义privKey,
newDemoEventCallBack(DEPOSIT));

异步调用合约

 // 在调用callContract方法时指定isSync=false
String resp = nodeSrv.CallContract(BigInteger.valueOf(nonce),
"0xee463ba03224a14706728cbede6abcd76ed4e7cc",
functionDef, //函数接口定义
privKey,
new DemoEventCallBack(DEPOSIT), false);

编写event 处理逻辑

EventCallBack通过轮询方式监听EVM events

importcom.rendez.api.EventCallBack;
importlombok.extern.slf4j.Slf4j;
importorg.web3j.abi.datatypes.Event;
importorg.web3j.abi.datatypes.Type;
importjava.util.List;
@Slf4jpublicclassDemoEventCallBackextendsEventCallBack {
/** * * @param pollTime 轮询时间 * @param event 监听事件的格式 */publicDemoEventCallBack(intpollTime, Eventevent) {
super(pollTime, event);
}
publicDemoEventCallBack(Eventevent) {
super(event);
}
/** * 业务处理 * @param decodeResult */@OverridepublicvoidhandleEvent(List<Type> decodeResult) {
log.info("decoded result" + decodeResult);
log.info("start to Process Data");
}
}

查询合约

PrivateKeyprivKey = newPrivateKeyECDSA(${privKeyStr});
Stringaddress = privKey.getAddress();
intnonce = nodeSrv.queryNonce(address);
log.info("nonce {}" , nonce);
FunctionfunctionDef = newFunction("queryInfo", Arrays.asList(), Arrays.asList(newTypeReference<Utf8String>() {
}));
List<Type> resp = nodeSrv.queryContract(BigInteger.valueOf(nonce), contractAddress, functionDef, privKey);
log.info("resp {}", resp);

根据块高度查询合约

PrivateKeyprivKey = newPrivateKeyECDSA(${privKeyStr});
Stringaddress = privKey.getAddress();
intnonce = nodeSrv.queryNonce(address);
intheight = 100;
log.info("nonce {}" , nonce);
FunctionfunctionDef = newFunction("queryInfo", Arrays.asList(), Arrays.asList(newTypeReference<Utf8String>() {
}));
List<Type> resp = nodeSrv.queryContractByHeight(BigInteger.valueOf(nonce), contractAddress, functionDef, privKey, BigInteger.valueOf(height));
log.info("resp {}", resp);

根据块高度查询交易hash列表

intheight = 100;
String[] resp = nodeSrv.queryTransactionHashsByHeight(BigInteger.valueOf(height));
log.info("resp {}", resp);

根据交易hash查询交易数据

Stringtxhash = "";
RawTransactionDataresp = nodeSrv.queryRawTransactionByHash(txhash);
log.info("resp {}", resp);

TransactionUtil类

decodeTxMsg

staticSignedRawTransactiondecodeTxMsg(StringtxMsg)

decode链上交易, 返回交易详细信息

NodeSrv类

queryNonce

IntegerqueryNonce(Stringaddress)

根据账户地址查询nonce

queryRawTransactionByHash

RawTransactionDataqueryRawTransactionByHash(Stringtxhash)

根据交易hash查询交易数据

queryTransactionHashsByHeight

String[] queryTransactionHashsByHeight(BigIntegerheight)

根据块高度查询交易hash列表

deployContract

StringdeployContract(StringbinaryCode, List<Type> constructorParameters, PrivateKeyprivKey, BigIntegernonce)

部署合约,返回合约地址

callContractEvm

StringcallContractEvm(BigIntegernonce, StringcontractAddress, Functionfunction, PrivateKeyprivKey, EventCallBackcallBack, booleanisSyncCall)

调用evm 合约

queryContract

List<Type> queryContract(BigIntegernonce, StringcontractAddress, Functionfunction, PrivateKeyprivKey)

查询合约

queryContractByHeight

List<Type> queryContractByHeight(BigIntegernonce, StringcontractAddress, Functionfunction, PrivateKeyprivKey, BigIntegerheight)

指定区块高度查询合约

queryContractWithSig

queryContractWithSig(BigIntegernonce, StringcontractAddress, Functionfunction, Signaturesig)

签名查询合约

queryReceiptRaw

TransactionReceiptqueryReceiptRaw(StringtxHash)

查询交易执行receipt

queryReceipt

List<LogInfo> queryReceipt(StringtxHash)

查询交易执行event log

贡献代码

如果你有任何问题,可以提交issue.
如果你想贡献代码, 可以fork本仓库, 提交 pull request ,维护者将会细心review 你的代码,若一切ok,则会合并到主仓库

About

AnnChain java sdk

Resources

Stars

11 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages