web3学习系列——合约的部署与交互

资讯 2024-07-10 阅读:23 评论:0
美化布局示例

欧易(OKX)最新版本

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

APP下载   全球官网 大陆官网

币安(Binance)最新版本

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

APP下载   官网地址

火币HTX最新版本

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

APP下载   官网地址

经过一段时间的学习,我写了一些简单的solidity代码,然后我决定把它们部署到区块链上

After a while of learning, I wrote some simple solidity codes, and I decided to deploy them to the block chain.

这里我们选择的环境是

Here's where we're going to choose.

image.png

会发现我们的钱包被自动注入了,然后点击确认部署合约

We'll find our wallet automatically injected and then click to confirm the deployment contract.

image.png

我理解的Injected Provider 就是从本地的 VM测试环境 切换到 网络的测试环境

Injected Provider, as I understand it, is the testing environment from the local VM testing environment to the network.

image.png

部署成功咯

It's a successful deployment.

image.png

这没什么难的

It's not that hard.

image.png

这个我理解的就是当我们在某个合约中引入另一个合约,并且这个实例化这个合约,那么我们在部署当前合约时就相当于一块部署引入的合约

What I understand is that when we introduce another contract in one contract, and this is an example of the contract, when we deploy the current contract, we're equivalent to a deployment-initiated contract.

部署好合约后,未初始化引入的合约实例时,可以看到,simpleStorage的实例全是0

When a contract is deployed and the case of an introduced contract is not initialized, it can be seen that the example of simpleStorage is all zero.

image.png

初始化后

After initialization

image.png

那我们就得到一个实际的地址

Then we'll get a real address.

对于一个程序员来说,这真的没啥哈哈,就是函数互相调用

For a programmer, it's really nothing. It's a function calling each other.

image.png

关注一下写法,和两个关键字,可以被修改的标识是virtual,覆盖的标识是override

Focus on writing, and two keywords, the markings that can be modified are virtual, and the markings are override.

image.png

通过一个简单的交易合约,我们再来学习一些新的内容

With a simple deal, we'll learn something new.

image.png

  1. payable

    是一个函数修饰符,用于指示函数可以接收以太币。在Solidity中,如果你想让一个函数接收以太币(即在交易中有ether被发送到这个函数),那么这个函数需要被标记为

    is a function modifier used to indicate that the function can be received in too much currency. In Solidity, if you want a function to be received in too much currency (i.e. where there is another sent to this function in the transaction), then this function needs to be marked as

  2. require

    函数是一种用于条件检查的控制结构,常用于验证函数调用的前提条件。如果的条件返回,则当前函数执行会立即终止,所有状态更改都会回滚,且未花费的Gas会被退回。此外,函数可以接受第二个参数作为错误信息,当条件不满足时,这个错误信息会被返回,便于调试。

    Function is a control structure used for condition check, often used as a prerequisite for validating a call for the function. If the condition returns, the current function will be terminated immediately, all state changes will roll back and the unspent Gas will be returned. In addition, the function can accept the second parameter as a bug message, which will be returned when the condition is not met, so that the error information can be debuggered.

  3. 的含义

    Meaning

    在Solidity中,每个交易都有一个全局变量,它包含了交易的一些重要信息。是其中的一个字段,表示随交易发送的以太币(ETH)数量,以wei为单位(1 ETH=10^18 wei)。常用于处理接收以太币的函数中,以确保正确的金额被发送。

    In Solidity, each transaction has a global variable that contains some important information about the transaction. It is a field in which the number of ETHs sent with the transaction is in wei (1 ETH=10.618 wei). It is often used to process the receipt of TT functions to ensure that the correct amount is sent.

先来学一个新的概念

Let's start with a new concept.

来自GPT

From GPT

在区块链和智能合约的上下文中,"data feed"通常指的是从区块链外部世界(off-chain)到区块链内部(on-chain)的数据传输机制。因为智能合约运行在封闭的、去中心化的区块链网络上,它们无法直接访问或验证外部世界的数据(如股票价格、天气信息、货币汇率等)。数据馈送提供了一种解决方案,使得智能合约能够依据外部世界的真实信息来执行逻辑。

In the context of block chains and smart contracts, "data feed" usually refers to a data transfer mechanism from the outside world of the block chain (off-chain) to the inside of the block chain (on-chain). Because smart contracts operate on a closed, decentralized network of block chains, they cannot directly access or validate data from the outside world (e.g. stock prices, weather information, currency exchange rates, etc.). The data feed provides a solution that allows smart contracts to follow logic based on real information from the outside world.

docs.chain.link/data-feeds/…

image.png

我们在Remix里面打开一下这段代码,这是最新的BTC和USD的转化,没找到ETH

We opened this code in Remix. It's the latest BTC and USD transformation. There's no ETH.

image.png

image.png

看起来没问题,Solidity不支持浮点数或实数类型,这里的小数位数应该是8位

Solidity doesn't support floating points or real-digit types. The decimal numbers here should be eight.

区块链是一个确定的系统,如果我们想要一些实时的、随机的东西,就需要预言机,chainLink是预言机网络

image.png 像我们刚刚实现的就是data Feed 获取外部数据

image.png

来自GPT

From GPT

Chainlink允许智能合约安全地访问链下(off-chain)数据,包括通过HTTP请求从外部API获取数据。由于智能合约本身无法直接进行HTTP请求或访问外部服务器,Chainlink通过预言机节点(Oracles)提供了一种机制,使智能合约能够间接地发送HTTP请求并接收响应。这一功能极大地扩展了智能合约的应用场景,使其能够利用外部的世界数据,如天气信息、股票价格、飞行数据等。

Chainlink allows smart contracts to safely access data under the chain (off-chain), including requests for data from external APIs through HTTP. Because smart contracts themselves cannot directly request HTTPs or access external servers, Chainlink provides a mechanism by oracles that allows smart contracts to send HTTP requests indirectly and receive responses. This function greatly expands the application of smart contracts to enable them to access external world data, such as weather information, stock prices, flight data, etc.

Single Word Response | Chainlink Documentation

我们找到这个合约,然后在remix里面打开。这段Solidity代码定义了一个名为的智能合约,利用Chainlink网络从外部API(例如CryptoCompare)获取数据。

We found this contract and opened it inside the remix. The Solidity code defines a smart contract called the "Intelligent Contract ", using the Chainlink network to get data from external API (e.g. CryptoCompare).

在部署这个合约之前,先学习一下Link和ETH之间的区别

Before this contract is deployed, learn the difference between Link and ETH.

LINK:LINK是Chainlink网络的原生代币。Chainlink是一个去中心化的预言机网络,它允许智能合约安全地访问链下数据、支付API调用和执行链下计算。LINK代币用于支付Chainlink网络中的节点操作员,这些操作员为智能合约提供外部数据和计算服务。

LINK: LINK is the original ingenuity of the Chainlink network. Chainlink is a decentralised prophesy network that allows smart contracts to access data under the chain safely, pay for API calls and calculations under the enforcement chain. LINK is used to pay node operators in the Chainlink network, which provide external data and computing services for intelligent contracts.

ETH:ETH是以太坊区块链的原生货币。以太坊是一个广泛使用的智能合约平台,允许开发者部署去中心化应用(DApps)和智能合约。ETH主要用于支付交易费用和计算服务费(称为gas),这是在以太坊网络上执行操作(如执行智能合约)的必要成本。

ETH: Ether is the original currency of the Tether block chain. Ether is a widely used smart contract platform that allows developers to deploy decentralised applications (DApps) and smart contracts.

然后我们去Sepolia网络上获取一些link

Then we go to Sepolia and get some link.

image.png 我们需要去找到这个地址,然后导入我们的钱包中,这样就可以使用刚刚获取到的link币

image.png

image.png

拿到了

Got it.

image.png 然后如果要运行这个合约,需要从钱包给这个合约发送一点link币,

image.png"


我们给部署好的合约发送0.2 Link image.png

We send 0.2 Link image.png

调用API查看一下,发现有结果了

Call API and check it out. We've got results.

image.png

是一个公共函数,任何人都可以调用它来触发一个Chainlink请求,从指定的URL获取ETH的24小时交易量。

is a public function that anyone can call to trigger a Chainlink request for a 24-hour transaction of ETH from a specified URL.

直接粘贴版

获取ABI代码 github.com/smartcontra…

Get ABI code

获取部署在当前网络的合约的地址

Get the address of the contract deployed on the current network

image.png

切换到测试网部署运行函数,成功了

Switching to test network deployment running function. Success.

image.png

GitHub引入版

image.png



我们设定了需要返回的最小美元数是50,1ETH大概是3000美元,50/3000差不多等于0.016是我们所需要的eth数,然后再转化成Wei。为了保证交易顺利进行,我们在这里设定value值为20000000000000000,也就是0.021e18

We've set the minimum dollar figure for return to be 50,1ETH, which is probably $3,000, 50,3000 equals the number of eths we need, and then to Wei. In order to ensure the smooth running of the deal, we've set the value here at 20000.000000000000, or 0.021e18.

image.png

image.png

在Solidity中,语句是一种特殊的语法,它允许你将库中的函数附加到任何类型的变量上。这意味着,一旦声明了这种使用方式,类型的每个实例就能调用库中为它定义的函数,就好像这些函数是它自己的方法一样。这使得库的函数可以写得像是作用于这个类型的实例方法。

In Solidity, the statement is a special syntax that allows you to attach the functions in the library to any type of variable. This means that, once this usage is stated, each example of the type can call the functions defined for it in the library, as if they were its own method. This allows the library function to be written as an example of how it works in this type.

我们新建一个priceConverter的合约,将前面写的函数迁移过来,封装成一个库。

We create a new priceConverter contract to move the previously written function over and encapsulate it into a library.

image.png

msg.valueuint256msg.valuegetConversionRate

image.png

image.png 这两段代码展示了在Solidity中将以太币(Ether)从智能合约转移给调用者()的两种不同方法:和。

image.png

  • 安全性:和都通过限制gas的使用来提高安全性,防止在资金转移操作中可能发生的重入攻击。
  • 错误处理:在失败时自动回滚,不需要额外的错误处理;而在失败时返回,需要开发者检查这个返回值并手动处理失败情况。
  • 选择使用哪一个:选择还是取决于你是否想在资金发送失败时让整个交易回滚(使用),还是想手动处理这种失败情况(使用)。

image.png 使用来发送以太币是Solidity 0.6.x版本之后推荐的做法,因为它相较于和提供了更高的灵活性。

image.png

在Solidity中,修饰器(Modifiers)是一种特殊的声明,用于修改智能合约函数的行为。通过预先定义一些条件,修饰器可以在函数执行前或执行后添加自定义逻辑,例如验证条件、修改状态或事件记录等。这使得代码更加模块化、可重用,同时提高了智能合约的安全性。

In Solidity, Modifiers is a special statement used to modify the behaviour of an intelligent contract function. By predefined conditions, the modifier can add a custom logic, such as authentication conditions, modification status, or event logs, before or after the function is executed. This makes the code more modular, reusable, and enhances the security of the smart contract.

image.png

美化布局示例

欧易(OKX)最新版本

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

APP下载   全球官网 大陆官网

币安(Binance)最新版本

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

APP下载   官网地址

火币HTX最新版本

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

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

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

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

分享:

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

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

欧易(OKX)

  全球官网 大陆官网

币安(Binance)

  官网

火币(HTX)

  官网

Gate.io

  官网

Bitget

  官网

deepcoin

  官网
热门文章
  • 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...
  • 0.00006694个比特币等于多少人民币/美金

    0.00006694个比特币等于多少人民币/美金
    0.00006694比特币等于多少人民币?根据比特币对人民币的最新汇率,0.00006694比特币等于4.53424784美元/32.5436 16人民币。比特币(BTC)美元(USDT)人民币(CNY)0.000066944.53424784【比特币密码】32.82795436 16比特币对人民币的最新汇率为:490408.64 CNY(1比特币=490408.64人民币)(1美元=7.24人民币)(0.00006694USDT=0.0004846456 CNY)汇率更新时...
  • 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...
  • 币圈院士:5.20比特币(BTC)以太坊(ETH)行情分析

    币圈院士:5.20比特币(BTC)以太坊(ETH)行情分析
    利空出尽?华尔街多头坚信美股将摆脱泥潭 经济衰退风险被夸大A lot of people on Wall Street believe that beauty will escape the quagmire; the risk of recession is exaggerated. 从目前美国经济的情况加上美股先有的走势来判断,确信通胀已经或即将见顶,这为价格压力回落铺平了道路,这最终将使美联储得以放缓...
  • 2000年美国GDP占世界的304%,中国GDP仅占35%,现在呢?

    2000年美国GDP占世界的304%,中国GDP仅占35%,现在呢?
    GDP作为全球公认的实力基准,就像是一个大国实力的代言人,它是布雷顿森林体系下全球团结的声音。它不仅仅是数字的累积,更是大国综合实力的人格化,默默诉说着每个国家的辉煌与荣耀。虽然GDP不是衡量一个国家综合实力的唯一标准,但无疑是最关键的指标之一。作为一面镜子,它反映了国家的经济实力和发展水平,是国家综合实力的重要体现,不容忽视。2000年,中国GDP迈过/克洛克-0/万亿美元的重要门槛,达到/克洛克-0/。2/克洛克-0/万亿美元(折合人民币7。7万亿元)。然而,在全球经济的...
标签列表