Solidity智能合约

安装Remix-ide

先排个雷,目前官方提供的remix-ide desktop版能安装,但是跑不起来,我在github release列表下了latest version,启动后就是漫长的白屏,网上给的解释是需要修改代理ping通github,但是我这边是能ping通的,多次尝试无果后我选择使用Remix-Ethereum IDE + 本地 remixd。remixd是Remix IDE提供的一个辅助工具,主要用于 Solidity 智能合约开发时,让本地文件系统和 Remix Web IDE 之间建立连接。
安装方式:npm install -g @remix-project/remixd
启动命令:remixd -s ./ –remix-ide https://remix.ethereum.org

vscode + solc

需要在vscode中安装solidity插件,另外也可安装remix-light插件用于轻量化编译、部署。使用solidity插件对sol编译的方式是右键选择Solidity:Compile Contract。如果编译器版本不匹配的话,solidity插件支持三种不同的更换版本方式:
(1)远程下载: 使用远程版本进行编译,需要更改solidity setting中的solidity.compileUsingRemoteVersion,设置为相应的版本号
(2)使用本地文件:更改solidity setting中的solidity.compileUsingLocalVersion,设置为solc文件本地路径
(3)Npm/节点安装:可以在solidity项目文件夹中本地安装solc(npm install solc)
优先顺序:首先使用npm/节点安装,然后使用本地文件,最后使用远程

Ques: 简述Solidity的编译和部署都干了什么事儿
Ans:Solidity的编译是将人类可读的智能合约代码转化成EVM(以太坊虚拟机)能理解的字节码;Solidity部署是将合约代码正式发布到区块链上,从而拥有独立的地址(合约地址),用来管理资产,支持收款与支付

区块链交易明细

  • 交易哈希(Transaction Hash):这个是在这个区块链上这笔交易的唯一ID,这个交易哈希标识了发送了0.25个以太币到我们的地址的操作。
  • 状态(Status):我们可以看到交易状态是成功的,它没有因某种情况而失败。
  • 区块(Block):我们可以看到这个交易所在的区块高度。
  • 时间戳(Timestamp):这里是时间戳,代表这个交易是何时发生的。
  • 发送者(From):我们可以看到这个交易是由谁发送的,当然我们可以在新的标签页打开它,你就能看到发送交易的账户信息。
  • 接受者(To):接受了交易的用户,这里就是我们自己。
  • 价值(Value):交易的资产价值是0.25个以太币。
  • 交易手续费(Transaction Fee):交易的手续费,付给矿工处理这笔交易的费用。
  • Gas价格(Gas Price):Gas的价格,Gas价格是交易中每个执行单元的花费(用ether和gwei做单位),Gas价格越高,被写到区块中的机会越大。

区分ETH代币和Link代币

Link代币的功能:

  • 支付数据服务费:用 LINK 代币支付获取链下数据的费用,比如价格、天气等
  • 节点质押担保:节点需要质押 LINK 作为担保,作恶会被罚掉
  • 节点奖励激励:节点提供高质量数据,获得 LINK 奖励
  • 跨链数据传输费:用于支付跨链数据通信的成本,比如在不同链之间同步信息
  • DeFi 抵押品:在一些 DeFi 协议中可以作为抵押品,借贷稳定币等
    下面是Eth的代币和Link代币的对比总结:
    特点 ETH(以太坊) LINK(Chainlink)
    项目类型 智能合约平台 去中心化预言机网络
    代币功能 支付交易费、质押、DApp 支付节点费用、质押、奖励
    技术层级 Layer 1 主链 ERC-20 代币(基于以太坊)
    市场定位 “区块链燃料”、平台代币 “数据桥梁”、预言机领域龙头
    代表场景 DeFi、NFT、DAO、Staking 数据喂价、跨链数据传输

编写FundMe代码在链上筹款

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Get funds from users
// Withdraw funds
// Set a minimum funding value in ETH

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.22;

contract FundMe {

uint256 public minimumETH = 0.01 * 1e18; // 1 * 10 ** 18

address[] public funders;
mapping(address => uint256) public addressToAmountFunded;

function fund() public payable {
// Want to be able to set a minimum fund amount in currency
// 1. How do we send ETH to this contract?
require(msg.value >= minimumETH, "Didn't send enough!");
funders.push(msg.sender);
addressToAmountFunded[msg.sender] = msg.value;
}

function withdraw() public {
/* starting index, ending index, step amount */
for (uint256 funderIndex = 0; funderIndex < funders.length; funderIndex++) {
// code
address funder = funders[funderIndex];
addressToAmountFunded[funder] = 0;
}
// resret the array
funders = new address[](0);
(bool callSuccess, ) = payable(msg.sender).call{value: address(this).balance}("");
require(callSuccess, "Call failed");
}
}

Solidity智能合约
http://example.com/2025/03/01/FundMe/
作者
Peter
发布于
2025年3月1日
许可协议