web3j教程:java使用web3j开发以太坊智能合约交易

资讯 2024-07-10 阅读:50 评论:0
2019独角兽企业重金招聘Python工程师标准>>> 2019 Unicorn Enterprise Heavy Recruiting Engineer Python > > 从...
美化布局示例

欧易(OKX)最新版本

【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   全球官网 大陆官网

币安(Binance)最新版本

币安交易所app【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

火币HTX最新版本

火币老牌交易所【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

从广义上讲,有web3j支持三种类型的以太坊交易:

Broadly speaking, xff0c; web3j supports three types of xff1a transactions;

  • 1.以太币从一方交易到另一方
  • 2.创建一个智能合约
  • 3.与智能合约交易

为了进行这些交易,必须有以太币(以太坊区块链的代币)存在于交易发生的以太坊账户中。这是为了支付gas成本,这是为支付参与交易的以太坊客户端的交易执行成本,支付了这个成本就能将结果提交到以太坊区块链上。获得以太币的说明下文会说到。

In order to make these transactions xff0c; there must be an Ether currency (in a token of the #xff0c chain) in which the transaction took place. This is to pay gasgas< #xfff0c; this is the cost of implementing the transaction on the #xff0c end of the thongs involved in the transaction ; this cost is paid to submit the results to the Ethio chain.

此外,我们还可以查询智能合约的状态。

xff0c; we can also check the status of the smart contract.

如何获得以太币Ether

要想获得以太币Ether你有两种途径可以选择:

There are two ways you can choose xff1a if you want to get to Ether in TT;

  • 1.自己开采挖矿
  • 2.从别人那里获取以太币

在私有链中自己挖矿,或者公共测试链()是非常简单直接的。但是,在主要的公有链()中,它需要很多很明显的专用GPU时间,除非你已经拥有多个专用GPU的矿机,否则基本上不太可行。如果你希望使用私有链,则在这个官方文档中有一些指导。

Self-mining in private chains xff0c; or public testing chains () is very simple and straightforward. But xff0c; in the main public chain () xff0c; it takes a lot of distinctly dedicated GPU time xff0c; unless you already have several dedicated GPU mine machines xff0c; it is hardly feasible. If you want to use private chains xff0c; there is some guidance in this official document .

要购买以太币Ether,你需要通过交易所。由于不同的地区有不同的交易所,你还需要研究自己去哪儿合适。官方文档中包含多个交易所,是一个很好的参考。

以太坊测试链(testnets)

针对Ethereum以太坊有许多专用测试网络或者叫测试链,他们由各种客户端支持。

There are many dedicated testing networks for Etherum or the Test Chain #xff0c; they are supported by a variety of clients.

  • 1.Rinkeby:只支持geth客户端。
  • 2.Kovan:只支持Parity客户端。
  • 3.Ropsten:支持geth和Parity客户端。

对于开发,建议你使用或测试链。这是因为他们使用的工作量证明共识机制,确保交易和块能够一致并及时的创建。测试链,虽然最接近公有链(),但是因为它使用的工作量证明是共识机制,过去已受到攻击,对以太坊开发人员来说往往有更多的问题。

For the development of xff0c; it is recommended that you use or test the chain. This is because they use workload proofs of consensus mechanisms xff0c; ensure that transactions and blocks are created in a consistent and timely manner. Test chains xff0c; although it is closest to the public chain () xff0c; but because it uses workloads that prove to be consensus mechanisms xff0c; have been attacked in the past xff0c; and there are often more problems for the user community developers.

你可以通过测试链的请求以太坊币,具体怎么做可以看这里https://www.rinkeby.io/

You can use #xff0c as a test chain request; see https://www.rinkeby.io/.

有关如何请求测试链的细节可以在这里找到。

Details of how to request the test chain can be found here at .

如果你需要在上的得到一些以太币,将你的钱包地址的消息发布到web3j gitter channel,然后会发送一些给你。

If you need to get some e-notes xff0c; send information about your wallet address to web3j giftchannel #xff0c; then send some to you.

在testnet测试链或者私有链上挖掘

在ethereum以太坊测试链中,挖掘难度低于公有链。这意味着你可以用普通的CPU,比如你的笔记本电脑来挖掘新的以太币。你需要做的是运行一个以太坊客户端,例如或,开始做一些储备。进一步的资料可在他们的官方网站上获得。

xff0c; digging is less difficult than public chains. This means that you can use common CPUxff0c; for example, your laptop to dig for new Ethers. What you need to do is run an Ether client xff0c; for example, xff0c; start making some reserves. Further information can be found on their official website.

一旦你开采了一些以太币,你就可以开始使用以太坊区块链了。

Once you've mined some Ethercopic #xff0c; you can start using the Etherton block chain.

然而,如上所述,使用或者测试网络更简单些。

However, xff0c; as noted above xff0c; use or test the network simpler.

gas

当在Ethereum以太坊发生交易时,必须为执行该交易的客户端支付交易成本,将该交易的输出提交到以太坊区块链Ethereum blockchain。

When a transaction takes place in Etheeum, & #xff0c; the client performing the transaction must pay transaction costs & #xff0c; the output of the transaction is submitted to Etherium block chain Etherium blockchain.

此成本是通过gas来测量的,其中gas是用于在以太坊虚拟机中执行交易指令的数量。请参阅官方文档以获取更多信息。

This cost is measured by gas & #xff0c; of which gas is the number used to execute trade instructions in Ether's virtual machine. See for more information.

当你使用以太坊客户端时,这意味着,有两个参数用来指示你希望花费多少以太来完成传输:

xff0c when you use an Etheraya client; this means xff0c; two parameters are used to show how much you want to spend on xff1a;

这两个参数共同决定了你愿意花费在交易成本上的最大量的以太币Ether。也就是说,你花费的gas不会超过。gas价格也会影响交易发生的速度,这取决于其他交易是否能为矿工提供更有利的gas价格。

Together, these two parameters determine the largest amount of ether that you would like to spend on transaction costs. That is, & #xff0c; you will not spend more on gas. Gas prices also affect the speed at which the transaction takes place & #xff0c; this depends on whether other transactions provide more favourable gas prices for miners.

你可能需要调整这些参数以确保交易能及时进行。

You may need to adjust these parameters to ensure that the transaction is conducted in a timely manner.

交易机制

当你用一些以太币Ether创建了一个有效的帐户时,你可以使用两种机制来与以太坊进行交易。

#xff0c when you create a valid account with some Ethel; you can use two mechanisms to trade with Ether.

  • 通过以太坊ethereum客户端进行认证签名交易
  • 离线交易签名认证

这两种机制都是Web3j所支持的。

Both mechanisms are supported by Web3j.

通过以太坊ethereum客户端进行认证签名交易

为了通过以太坊客户端进行交易,首先需要确保你正在使用的客户端知道你的钱包地址。最好是运行自己的以太坊客户端,比如/,以便可以更方便的做到这一点。一旦你有一个客户端运行,你可以创建一个以太坊钱包,通过:

In order to trade xff0c through the Taiwan client; first of all to ensure that the client you are using knows your wallet address. It is best to run your own xff0c; e.g. xff0c; so that this can be done more easily. Once you have a client running xff0c; you can create a xff0c; via xff1a;

  • geth Wiki包含了geth支持的良好运行的不同机制,例如导入私有密钥文件,并通过控制台创建新的以太坊帐户。
  • 或者,你可以通过客户端使用JSON-RPC管理命令,例如用为/创建新以太坊账户。

通过创建你的钱包文件,你可以通过web3j打开帐户,首先创建支持/管理命令的web3j实例:

By creating your wallet file xff0c; you can open the account xff0c through web3j; first you create the web3j example xff1a that supports/managing the command;

 
 

然后,你可以解锁帐户,并如果是成功的,就可以发送一个交易:

xff0c; you can unlock accounts xff0c; and if xff0c is successful; you can send a transaction xff1a;

 
 

以这种方式发送的交易应该通过EthSendTransaction创建,使用Transaction类型:

Transactions sent in this way should be created by >EthSendTransalaction; using type

 
 

其中值获得方式,下文会提到。 有关此交易工作流的详细信息,请参阅 DeployContractITScenario

See , for more information on this trade stream, see .

web3j支持的各种管理命令的进一步细节在Management APIs中。

Further details of the various management orders supported by web3j are available at .

离线交易签名认证Offline transaction signing

如果你不想管理自己的以太坊客户端,或者不想向以太坊客户端提供诸如密码之类的钱包详细信息,那么就通过离线交易认证签名。

If you do not want to manage your Etherm client & #xff0c; or do not want to provide Ether's client with details of wallets such as passwords & #xff0c; then the signature is authenticated by offline transactions.

离线交易签名认证允许你在web3j中使用你的以太坊钱包签署交易,允许你完全控制你的私有凭据。然后,离线创建的交易可以被发送到网络上的任何以太坊客户端,只要它是一个有效的交易,它会将交易传播到其他节点。

The offline transaction signature certification allows you to sign the transaction xff0c using your taupulega wallet in Web3j; allows you to fully control your private credentials. Then xff0c; offline created transactions can be sent to any #xff0c user on the network; as long as it is a valid transaction xff0c; it spreads the transaction to other nodes.

如果需要,还可以执行进程外交易签名认证。这可以通过重写ECKeyPair的方法来实现。

This can be done by rewriting ECKeyPair.

创建和使用钱包文件Ethereum wallet file

为了离线脱机交易,你需要有你的钱包文件或与私密钱包/账户相关的公共和私人密钥。

To offline transactions xff0c; you need your wallet file or public and private keys associated with a private wallet/account.

web3j能够为你生成一个新的安全的以太坊钱包文件Ethereum wallet file,或者与也可以通过私钥来和现有的钱包文件一起工作。

Web3j can generate a new secure Etherium wallet file for you; or work with existing wallet files that can also be used through private keys.

创建新的钱包文件:

Create new wallet file:

 
 

加载凭据从钱包文件:

Loading of supporting documents from wallet file:

 
 

然后这些凭据会被用来签署交易,请参阅Web3安全存储定义钱包文件规范Web3 Secret Storage Definition

These papers will then be used to sign the transaction & #xff0c; see Web3 security storage definition wallet file instruction RawTransaction类型。类似于前面提到的类型,但是它不需要通过具体的账号地址来请求,因为可以从签名中推断出来。

For offline signature transactions to be signed xff0c; a type of RawTransaaction is required. Similar to the above-mentioned type xff0c; however, it does not need to request xff0c through a specific account address; it can be extrapolated from the signature.

为了创建和签署原生交易,交易的顺序如下:

To create and sign the original transaction xff0c; the order of the transaction is as follows xff1a;

  • 1.确定交易发起者帐户的下一个可用随机数
  • 2.创建对象
  • 3.使用递归长度前缀编码(RLP即Recursive Length Prefix)对对象进行编码
  • 4.签署对象
  • 5.将对象发送到节点进行处理

是一个不断增长的数值,用来唯一地标识交易。一个只能使用一次,直到交易被挖掘完成,可以以相同的随机数发送交易的多个版本,但是一旦其中一个被挖掘完成,其他后续提交的都将被拒绝。

is a growing value xff0c; used to identify transactions only. One can only use xff0c once; until the transaction is excavated xff0c; multiple versions of the transaction xff0c can be sent at the same random number; but once one of them is excavated xff0c; other subsequent submissions will be rejected.

一旦获得下一个可用的,该值就可以用来创建对象:

Once the next available xff0c is available; this value can be used to create objects xff1a;

 
 

然后可以对交易进行签名和编码:

The transaction can then be signed and coded xff1a;

 
 

其中凭据是根据创建和使用钱包文件加载的。

These documents are based on the creation and use of wallet files based on

然后使用eth_SendRawTransaction发送交易:

eth_SendRawTransaction to send the transaction #xff1a;

 
 

有关创建和发送原始事务的完整示例,请参阅 CreateRawTransactionIT

Full examples of the creation and sending of the original service & #xff0c; see

交易随机数nonce

是一个不断增长的数值,用来唯一地标识交易。一个只能使用一次,直到交易被挖掘完成,可以以相同的随机数发送交易的多个版本,但是一旦其中一个被挖掘完成,其他后续提交的都将被拒绝。

is a growing value xff0c; used to identify transactions only. One can only use xff0c once; until the transaction is excavated xff0c; multiple versions of the transaction xff0c can be sent at the same random number; but once one of them is excavated xff0c; other subsequent submissions will be rejected.

可以通过方法获得下一个可用的:

The next available xff1a can be obtained by means;

 
 

然后可以使用创建你的交易对象:

Then you can use the object that created you & #xff1a;

 
 

交易类型

web3j中的不同类型的交易都使用和对象。关键的区别是交易对象必须始终有一个地址,以便处理请求的以太坊客户端知道要使用哪个钱包来代表消息发送者并发送该交易。如上所述,对于离线签名认证签署的原始交易而言,这不是必须的。

The key difference is that the subject must always have an address & #xff0c; in order to process the request, the user knows which wallet to use to represent the sender and send the transaction. As noted above & #xff0c; xff0c for offline signature authentication of the original transaction; this is not necessary.

接下来的部分概述了不同交易类型所需的关键交易属性。下列属性对所有人都是不变:

The following section outlines the key transaction properties required for different transaction types. The following attributes are constant for everyone xff1a;

  • Gas price 天然气气体价格
  • Gas limit 天然气气体限制
  • Nonce 随机数
  • from 发送地址

和对象在所有后续示例中都可互换使用。

and objects can be used interchangeably in all subsequent examples.

以太币从一方交易到另一方

在双方之间发送以太币Ether需要交易对象的最少量的信息:

Sending the smallest amount of information between the parties to whom Ether needs to deal xff1a;

  • to :目的地钱包地址
  • value:价值,希望发送到目的地的以太币数量
 
 

但是,建议你使用TransferClass来发送以太币Ether,它负责对管理和通过不断的轮询为你提供响应:

xff0c; however, it is recommended that you use

 
 

使用智能合约打包器smart contract wrappers

当使用下面列出的智能合约打包器时,将不得不手动执行从Solidity到本机Java类型的所有转换。使用Solidity smart contract wrappers是非常有效的,它负责所有的代码生成和转换。

When using the smart contract packager listed below & #xff0c; you will have to manually execute all conversions from Solidity to the current Java type. Use

创建一个智能合约

要部署新的智能合约,需要提供以下属性:

To deploy a new smart contract xff0c; the following properties are required xff1a;

  • value :在智能合约中希望存放的以太坊Ether量(如果没有提供默认为零)
  • data :十六进制格式化、编译的智能合约创建代码
 
 

如果智能合约包含构造函数,则必须对关联的构造函数字段值进行编码,并将其附加到编译的智能合约代码中:

If an intelligent contract contains a construction function & #xff0c; the associated construction letter numerical segment value & #xff0c must be encoded and attached to the compiled smart contract code & #xff1a;

 
 

与智能合约交易

要与现有的智能合约进行交易,需要提供以下属性:

To trade with existing smart contracts xff0c; the following properties are required xff1a;

  • to:智能合同地址
  • value:在智能合约中你希望存放的以太币Ether量(如果智能合约接受以太币Ether的话)
  • data: 已编码的函数选择器和自变量参数

web3j负责函数编码,有关实现的进一步细节,请参阅应用程序二进制接口部分Application Binary Interface

Web3j is responsible for function coding & #xff0c; further details on the achievement & #xff0c; see application binary interface section .

 
 

无论消息签名的返回类型如何,都不可能从事务性函数调用返回值。但是,使用过滤器捕获函数返回的值是可能的。详情请参阅过滤器和事件部分。

No matter the type of return of the message signature xff0c; it is not possible to call the return value from the transactional function. But xff0c; values returned from the capture function using the filter are possible. See the filter and the event section for details.

查询智能合约状态

这种功能是由eth_call通过调用来实现的。

This function is performed by eth_call.

eth_call允许你调用智能合约上的方法来查询某个值。此函数没有关联交易成本,这是因为它不改变任何智能合约方法的状态,它只返回它们的值:

Eth_call allows you to call the smart contract to search for a value. This function does not relate to transaction costs xff0c; it does not change the status of any smart contract method xff0c; it only returns their value xff1a;

 
 

注意:如果一个无效的函数调用被执行,或者得到一个空null返回结果时,返回值将是一个Collections.emptyList实例。

Note : if an invalid function calls xff0c executed; or xff0c when an empty null returns results; return value will be an example of Collactions.emptyList.

  • web3j教程,主要是针对java和android程序员进行区块链以太坊开发的web3j开发详解。
  • 以太坊教程,主要介绍智能合约与dapp应用开发,适合入门。
  • 以太坊开发,主要是介绍使用node.js、mongodb、区块链、ipfs实现去中心化电商DApp实战,适合进阶。

汇智网原创翻译,转载请标明出处。原文访问其官方博客

Original translator & #xff0c; reprinted from

美化布局示例

欧易(OKX)最新版本

【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   全球官网 大陆官网

币安(Binance)最新版本

币安交易所app【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

火币HTX最新版本

火币老牌交易所【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址
文字格式和图片示例

注册有任何问题请添加 微信:MVIP619 拉你进入群

弹窗与图片大小一致 文章转载注明

分享:

扫一扫在手机阅读、分享本文

发表评论
平台列表
美化布局示例

欧易(OKX)

  全球官网 大陆官网

币安(Binance)

  官网

火币(HTX)

  官网

Gate.io

  官网

Bitget

  官网

deepcoin

  官网
热门文章
  • 0.00015693个比特币等于多少人民币/美金

    0.00015693个比特币等于多少人民币/美金
    0.000 15693比特币等于多少人民币?根据比特币对人民币的最新汇率,0.000 15693比特币等于10.6 1678529美元/76.86554996人民币。比特币(BTC)【比特币价格翻倍】美元(USDT)人民币(CNY)0.000/克洛克-0/5693【数字货币矿机】10.6 167852976.8655254996比特币对人民币的最新汇率为:489,807.72 CNY(1比特币= 489,807.72人民币)(1美元=7.24人民币)(0.00015693 U...
  • 0.00003374个比特币等于多少人民币/美金

    0.00003374个比特币等于多少人民币/美金
    0.00003374比特币等于多少人民币?根据比特币对人民币的最新汇率,0.00003374比特币等于2.2826 1222美元/16.5261124728人民币。比特币(BTC)美元(USDT)人民币(CNY)0.00003374克洛克-0/22216.5261124728比特币对人民币的最新汇率为:489807.72 CNY(1比特币=489807.72人民币)(1美元=7.24人民币)(0.00003374USDT=0.0002442776 CNY)。汇率更新于2024...
  • 2000年美国GDP占世界的304%,中国GDP仅占35%,现在呢?

    2000年美国GDP占世界的304%,中国GDP仅占35%,现在呢?
    GDP作为全球公认的实力基准,就像是一个大国实力的代言人,它是布雷顿森林体系下全球团结的声音。它不仅仅是数字的累积,更是大国综合实力的人格化,默默诉说着每个国家的辉煌与荣耀。虽然GDP不是衡量一个国家综合实力的唯一标准,但无疑是最关键的指标之一。作为一面镜子,它反映了国家的经济实力和发展水平,是国家综合实力的重要体现,不容忽视。2000年,中国GDP迈过/克洛克-0/万亿美元的重要门槛,达到/克洛克-0/。2/克洛克-0/万亿美元(折合人民币7。7万亿元)。然而,在全球经济的...
  • A股利好!证监会连开三场座谈会!五部门发文 事关元宇宙…周末影响一周市场的十大消息(新股+点评)

    A股利好!证监会连开三场座谈会!五部门发文 事关元宇宙…周末影响一周市场的十大消息(新股+点评)
    1   证监会连开三场座谈会!将研究出台更多务实、管用的政策举措 The SEC will hold three talks! More practical and practical policy initiatives will be developed.   近日,中国证监会就活跃资本市场、提振投资者信心召开三场座谈会,分别听取专家学者和投资者意...
  • 1929经济大萧条或许即将重演?

    1929经济大萧条或许即将重演?
    人类似乎陷入了一个历史悖论,即我们总是重复同样的错误,无法真正从过去的错误中吸取教训。近年来,我们对世界各地接连不断的挑战和危机深感不安。20 19年突如其来的疫情,乌克兰的战火硝烟,欧洲的天然气供应危机以及全球少数国家的饥荒,所有这些问题都像洪水一样,一个接一个地涌来。如果你今天感到心情沉重,不要失去希望,因为明天可能会带来更严峻的挑战。首先,让我们深入讨论名为1929大萧条的时期。这场大萧条实际上是指从1929到1933的一场影响深远的经济危机。这场危机首先起源于美国,然...
标签列表