news 2026/9/2 7:13:51

Solidity-learning(4)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Solidity-learning(4)

1-Libraries

Libraries(库)与智能合约类似,但是不能声明任何静态变量,也不能发送ETH。

Library | Solidity by Example | 0.8.26

如何创建一个库?

建立文件PriceConverter.sol,回到FundMe.sol文件中,复制最后三个函数直接放入PriceConverter.sol中。

那么现在PriceConverter.sol,现在只关注getConversionRate函数就可以:

//SPDX-License-Identifier: MIT pragma solidity ^0.8.30; import "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol"; //直接导入 library PriceConverter { // 所有库中的函数都必须是internal,让库中的不同函数都可以被uint256调用 function getPrice() internal view returns(uint256) { // conver msg.value to USD // need two thing: ABI Address(0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43) // AggregatorV3Interface(0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43).version(); AggregatorV3Interface priceFeed = AggregatorV3Interface(0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43); (,int256 price,,,) = priceFeed.latestRoundData(); //price:BTC in terms of USD // 9033.148958846, remenber: priceFeed 返回的值中有八个是在小数点之后的(from function decimal:AggregatorV3Interface.sol) return uint256(price * 1e10); // 1e10 == 1**10 == 10000000000 } function getVersion() internal view returns (uint256) { AggregatorV3Interface priceFeed = AggregatorV3Interface(0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43); return priceFeed.version(); } function getConversionRate(uint256 ethAmount) internal view returns (uint256) { uint256 ethPrice = getPrice(); uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1e18; return ethAmountInUsd; } }

FundMe.sol改为如下样式:
 

// Get funds from users // Withdraw funds // Set a minimum funding value in USD // SPDX-License-Identifier: MIT pragma solidity ^0.8.30; import "./PriceConverter.sol"; //直接导入 contract FundMe { using PriceConverter for uint256; // uint256 public number; uint256 public minimumUsd = 50 * 1e18; //最小USD金额为美金计算 address[] public funders; //记录每个捐款人 mapping (address => uint256) public addressToAmountFunded; //记录每个地址发送资金的数量 function fund() public payable { // want to be able to set a minimum fund amount in USD // 1. How do we send ETH to this contract? // 如果要求至少发送 1 ether ,关键词 require 会检查 msg.value 是否大于 1 // require(msg.value > 1e18 , "Didn't send enough!"); //1e18 == 1*10**18 == 1000000000000000000 wei == 1 ether,value单位为ETH require(msg.value.getConversionRate() >= minimumUsd , "Didn't send enough!"); //如何将ether转换为usd?这就是oracles的作用 //msg.value:18 decimals funders.push(msg.sender); addressToAmountFunded[msg.sender] = msg.value; // What is reverting? // undo any action before, and send remaining gas back // number = 5; // 如果fund函数运行成功,那么number = 5,运行失败则整个函数回滚,number = 0, 消耗的gas也会原
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/2 21:23:54

15分钟攻克实时说话人区分:Sortformer场景化部署全攻略

15分钟攻克实时说话人区分:Sortformer场景化部署全攻略 【免费下载链接】WhisperLiveKit Real-time, Fully Local Speech-to-Text and Speaker Diarization. FastAPI Server & Web Interface 项目地址: https://gitcode.com/GitHub_Trending/wh/WhisperLiveKi…

作者头像 李华
网站建设 2026/9/3 0:04:39

Linux 下自定义命令的参数补全

你在使用 git、kubectl 或 docker 时&#xff0c;是否曾惊叹于它们强大的命令行补全功能&#xff1f;输入 git checkout <Tab><Tab>&#xff0c;就能列出所有分支&#xff1b;输入 kubectl get pod -n <Tab>&#xff0c;就能自动补全命名空间。这种体验不仅提…

作者头像 李华
网站建设 2026/9/2 22:09:36

修复 Nginx 反向代理后 URL 暴露后端端口的问题

你是否遇到过这样的问题&#xff1f; 你用 Nginx 将 https://api.dbblive.com 代理到内网 http://127.0.0.1:8080&#xff1b;正常访问时 URL 显示正常&#xff1b;但一旦点击浏览器刷新&#xff08;F5&#xff09;&#xff0c;地址栏突然变成 https://api.dbblive.com:8080/so…

作者头像 李华
网站建设 2026/9/2 8:25:56

基于Web的求职招聘平台的设计与实现任务书

广州航海学院毕业设计任务书学院名称&#xff1a; 计算机学院 专 业&#xff1a; 计算机科学与技术 学生姓名&#xff1a; 李 炜 学 号&#xff1a; 指导教师&#xff1a; 王晓狄 …

作者头像 李华
网站建设 2026/9/2 23:04:34

中国AI大模型盘点:科技巨头与新兴力量

百度文心一言&#xff08;ERNIE&#xff09; 百度推出的知识增强大模型&#xff0c;在中文语义理解方面表现突出&#xff0c;尤其擅长金融风控和方言识别&#xff08;准确率可达92%&#xff09;。其开源版本适配国产芯片&#xff0c;训练成本降低62%&#xff0c;已广泛应用于度…

作者头像 李华
网站建设 2026/9/3 1:08:13

2025年MIFARE Classic Tool终极指南:从零开始玩转NFC标签

2025年MIFARE Classic Tool终极指南&#xff1a;从零开始玩转NFC标签 【免费下载链接】MifareClassicTool An Android NFC app for reading, writing, analyzing, etc. MIFARE Classic RFID tags. 项目地址: https://gitcode.com/gh_mirrors/mi/MifareClassicTool 还在为…

作者头像 李华