在本教程中,您将学习如何使用Web3j Java库部署智能合约,以及如何与智能合约的功能进行交互。 作为先决条件,您应该熟悉帐户管理和智能合约java wrapper生成,如本系列前一篇文章中所...
本文标题使用Java与以太坊智能合约交互,作者:知世,本文有3419个文字,大小约为13KB,预计阅读时间9分钟,请您欣赏。知世金融网众多优秀文章,如果想要浏览更多相关文章,请使用网站导航的搜索进行搜索。本站虽然不乏优秀之作,但仅作为投资者学习参考。
在本教程中,您将学习如何使用Web3j Java库部署智能合约,以及如何与智能合约的功能进行交互。
作为先决条件,您应该熟悉帐户管理和智能合约java wrapper生成,如本系列前一篇文章中所述。为了保持连续性,我们将部署相同的DocumentRegistry智能合约。区块链研究实验室|从您的智能合同生成Java封装器。
DocumentRegistry.sol
pragma solidity ^0.5.6;
/**
* @dev Smart Contract resposible to notarize documents on the Ethereum Blockchain
*/
contract DocumentRegistry {
struct Document {
address signer; // Notary
uint date; // Date of notarization
string hash; // Document Hash
}
/**
* @dev Storage space used to record all documents notarized with metadata
*/
mapping(bytes32 => Document) registry;
/**
* @dev Notarize a document identified by the hash of the document hash, the sender and date in the registry
* @dev Emit an event Notarized in case of success
* @param _documentHash Document hash
*/
function notarizeDocument(string calldata _documentHash) external returns (bool) {
bytes32 id = keccak256(abi.encodePacked(_documentHash));
registry[id].signer = msg.sender;
registry[id].date = now;
registry[id].hash = _documentHash;
emit Notarized(msg.sender, _documentHash);
return true;
}
/**
* @dev Verify a document identified by its has was noterized in the registry previsouly.
* @param _documentHash Document hash
* @return bool if document was noterized previsouly in the registry
*/
function isNotarized(string calldata _documentHash) external view returns (bool) {
return registry[keccak256(abi.encodePacked(_documentHash))].signer != address(0);
}
/**
* @dev Definition of the event triggered when a document is successfully notarized in the registry
*/
event Notarized(address indexed _signer, string _documentHash);
}
Mining与Gas的简介
Mining
与以太坊网络的任何更新EVM状态的交互必须由广播到区块链的交易触发。一些示例交互包括将以太发送到另一个帐户、部署智能合约和一些智能合约函数调用。
矿工是通过不断尝试计算复杂数学难题(一种称为工作证明共识的机制)的答案来确保以太坊网络安全的实体。
矿工的工作是(从mempool)收集一组挂起的事务,并创建一个包含这些事务的区块。一旦一个事务包含在一个挖掘的区块中,它就被认为是已执行的,并且任何相关的状态更改都将被应用。
Gas
以太币,以太坊的原生加密货币,由交易发送方支付给在一个区块内包含交易的矿工,这是激励矿工的方法之一。
gas是以太坊网络中计算工作的一个单位,执行交易时支付的以太数量取决于消耗的gas量,以及gasPrice交易属性,该属性定义了发送方每消耗一个gas单位将支付多少以太。重要的是要了解不同的交易将需要不同数量的gas,这取决于操作,每笔交易至少需要21,000个gas。
还可以通过指定gaslimit属性来定义事务发送方为了执行事务而愿意消耗的绝对最大gas量。
部署
部署永久存在的智能合约的能力是以太坊的秘密!智能合约是一段代码,其功能可由任何相关方执行。它们在网络中以字节码的形式存在,但通常使用Solidity或Vyper等语言编写,然后进行编码和部署。
到目前为止,部署DocumentRegistry智能合约的最简单方法是使用由Web3j生成的wrapper。此wrapper提供智能合约的本机java类表示,提供了两种(非弃用的)deploy方法,可用于将代码部署到以太坊网络:
public static RemoteCall<DocumentRegistry> deploy(Web3j web3j,
Credentials credentials, ContractGasProvider contractGasProvider)
public static RemoteCall<DocumentRegistry> deploy(Web3j web3j,
TransactionManager transactionManager, ContractGasProvider contractGasProvider)
后者允许指定TransactionManager; 控制Web3j如何连接到以太坊客户端的对象。我们很高兴在此示例中使用默认的RawTransactionManager,因此我们将使用前一种方法,该方法将钱包凭据作为参数。我们还必须创建一个ContractGasProvider,它为交易提供gas价格和gas限制; 间接指定合同在Ether中部署的成本。
DocumentRegistry部署代码
//Create credentials from private key
Credentials creds = Credentials.create("
0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63");
DocumentRegistry registryContract = DocumentRegistry.deploy(web3j,
creds, new DefaultGasProvider()).send();
String contractAddress = registryContract.getContractAddress();
deploy方法返回RemoteCall对象。在RemoteCall上同时调用send()会将智能合约部署到以太坊网络,并返回与此部署代码链接的DocumentRegistry实例。每个部署的智能合约都有一个与之关联的唯一以太坊地址,并且可以通过在部署后调用合同包装器上的getContractAddress()方法来访问此地址。
在此片段中,凭据由硬编码的私钥构成(对于地址0xfe3b557e8fb62b89f4916b721be55ceb828dbd73)。这适用于测试和演示目的,但生产实现永远不应对私钥进行硬编码,因为攻击者将能够控制您的帐户。解决此问题的一种方法是将密钥设置为服务器上的环境变量,并将其加载到代码中。
在此示例中使用提供的DefaultGasProvider,它将gas价格和限制设置为硬编码值,但可以通过实现以下接口来构建自定义版本:
public interface ContractGasProvider {
BigInteger getGasPrice(String contractFunc);
@Deprecated
BigInteger getGasPrice();
BigInteger getGasLimit(String contractFunc);
@Deprecated
BigInteger getGasLimit();
}
为已经部署的合约创建wrapper实例
通常情况下,您要与之交互的智能合约将已部署到以太坊网络。在这种情况下,可以使用静态加载(..)方法:
public static DocumentRegistry load(String contractAddress, Web3j web3j,
Credentials credentials, ContractGasProvider contractGasProvider)
public static DocumentRegistry load(String contractAddress, Web3j web3j,
TransactionManager transactionManager, ContractGasProvider contractGasProvider)
例如,如果我们的DocumentRegistry部署地址为0x10c7dc2b84b6c8e6df5a749655830e70adca3a2b,我们可以获得已部署合同的java wrapper,如下所示:
//Create credentials from private key
Credentials creds = Credentials.create("
0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63");
DocumentRegistry registryContract = DocumentRegistry.load(web3j,
creds, new DefaultGasProvider());
调用智能合约功能
本文相关推荐: 以太网交换机、IndusCloud工业私有云方案和大网管软件等可用于透明工厂
以上便是知世金融网给大家分享的关于使用Java与以太坊智能合约交互/qkl/26939.html的相关信息了,希望能帮助到大家,更多金融相关信息,敬请关注知世金融网!
网站内容均来自互联网,如侵害您的利益联系客服进行删除!
下一篇:巨蟹刘嘉陵:彪悍的人生不只需要花不完的钱,还需要理想的努力变现
本文标题:使用Java与以太坊智能合约交互
本文地址:/index.php?s=article&c=search&keyword=%E4%BB%A5%E5%A4%AA