当前位置: 首页 > news >正文

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也会原
http://www.cnnetsun.cn/news/19825.html

相关文章:

  • 大模型微调:不冻结参数 vs 冻结主干
  • 30亿参数改写AI效率范式:Qwen3-30B-A3B如何让企业AI成本降60%?
  • ppInk:Windows平台上的终极协作写作与在线文档编辑指南
  • 3步彻底解决PDFMathTranslate中文乱码:从新手到专家的终极指南
  • React Stripe.js 终极指南:快速构建安全支付系统
  • 新手专属!BurpSuite 零基础到实战全攻略 —— 渗透测试核心工具配置与精通教程
  • 基于web的农产品溯源系统选题表
  • 基于Web的企业招投标管理系统的开发中期报告
  • ABB RobotWare软件资源下载与配置完全指南
  • Unity角色移动系统终极指南:打造《原神》级流畅体验
  • 终极指南:使用APK Icon Editor轻松定制Android应用
  • 基于Java的学贷通智慧管理系统的设计与实现全方位解析:附毕设论文+源代码
  • 字节跳动开源90亿学术大模型:轻量化英文场景解决方案来了
  • 如何获取119,376个英语单词发音MP3音频资源?实用工具完整指南
  • GitHub下载加速革命:告别龟速访问的终极解决方案
  • cohesive 单元设置粘性(VISCOSITY)
  • AI 内容洞察决策系统:4 大功能让科技企业创新效率提升 50%,敏捷破局信息困局
  • DeepPCB数据集终极指南:PCB缺陷检测实战手册
  • 7-Zip ZS:六种压缩算法如何彻底改变你的文件处理体验
  • Chrome DevTools Protocol终极指南:解锁浏览器自动化的无限可能
  • ET框架客户端性能优化终极指南:从15秒到3秒的快速启动实战
  • 4种有效方法:如何将音乐从Mac传输到Android
  • LoRaWAN智慧物联应用:远距离、低功耗、广连接
  • KK-HF_Patch完整使用指南:轻松解锁游戏无限可能
  • AppPolice终极指南:如何快速限制Mac应用CPU占用
  • 2025 年广州服装批发市场推荐:原创与效率双驱采批标杆
  • 记录vue动态切换访问后台服务
  • stm32 adc采集光敏传感器模块的模拟输出脚A0的值
  • 银发旅游崛起:退休后的“第二人生”革命
  • 3步精通Eino框架:构建企业级Go语言AI应用