news 2026/6/10 15:35:32

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/6/10 16:52:43

5个关键步骤解决PHP版本兼容性问题

5个关键步骤解决PHP版本兼容性问题 【免费下载链接】PHPCompatibility PHPCompatibility/PHPCompatibility: PHPCompatibility是一个针对PHP代码进行兼容性检查的Composer库,主要用于PHP版本迁移时确保现有代码能够适应新版本的PHP语言特性,避免潜在的兼…

作者头像 李华
网站建设 2026/6/10 16:07:33

一机多 Git 账号?SSH 密钥管理完全指南

你是否遇到过这样的场景? 公司用 GitLab,个人项目用 GitHub,偶尔还要提交到 Gitee;公司内部还有多个 Git 仓库(如 gitlab.dbblive.com 和 code.internal.com);每个平台要求使用不同的邮箱或独立…

作者头像 李华
网站建设 2026/6/10 16:10:01

Smithbox终极指南:7天从零精通游戏修改全流程

你是否曾经在《艾尔登法环》中面对强大对手感到束手无策?或者想在《黑暗之魂3》中创造完全属于自己的游戏体验?🤔 Smithbox游戏修改工具正是为你量身打造的强大解决方案! 【免费下载链接】Smithbox Smithbox is a modding tool fo…

作者头像 李华
网站建设 2026/6/10 16:09:34

MailView:终极Rails邮件预览工具完整指南

MailView:终极Rails邮件预览工具完整指南 【免费下载链接】mail_view Visual email testing 项目地址: https://gitcode.com/gh_mirrors/ma/mail_view 在Rails应用开发过程中,邮件模板的调试往往是最令人头疼的环节之一。每次修改后都需要发送测试…

作者头像 李华
网站建设 2026/6/10 16:07:44

终极指南:如何高效使用ReplayBook管理英雄联盟回放

终极指南:如何高效使用ReplayBook管理英雄联盟回放 【免费下载链接】ReplayBook Play, manage, and inspect League of Legends replays 项目地址: https://gitcode.com/gh_mirrors/re/ReplayBook 还在为找不到关键比赛回放而烦恼吗?ReplayBook作…

作者头像 李华
网站建设 2026/6/10 13:41:30

(请在基于Web的企业招投标管理系统的开发

基于Web的企业招投标管理系统的开发 摘要 企业招投标作为市场经济活动的重要环节,其管理效率与透明度直接影响市场公平竞争与资源配置。随着Web技术的不断进步,构建基于Web的招投标管理系统成为提升管理效能的关键途径。传统招投标管理方式存在流程繁琐、…

作者头像 李华