【MoonBeam】Solidity开发工具以及对应使用示例

本文将介绍Solidity开发中的两种工具:Remix IDE和Hardhat

Remix

  • Solidity在线编译器
  • 无需安装可快速开发、部署和测试Solidity智能合约
  • 支持插件,包括OpenZeppelin,Oraclize和Solium等
  • 可以本地安装,使用命令行操作,完全开源

Remix部署ERC-721合约

进入Remix,创建OpenZeppelinERC721.sol

在这里插入图片描述

插入以下代码

pragma solidity ^0.8.0;

import "https://github.com/OpenZeppelin/OpenZeppelin-contracts/blob/master/contracts/token/ERC721/presets/ERC721PresetMinterPauserAutoId.sol";

修改要部署的合约,注意必须部署主合约。若选择部署接口合约系统会报错。

在这里插入图片描述
在这里插入图片描述

点击部署,确认交易,合约部署成功后即可对已部署的合约进行交互

在这里插入图片描述

当成功调用mint函数时,成功铸造一个NFT,此时totalSupply字段由0变为1

在这里插入图片描述

Chainlink预言机的介绍及其喂价演示

什么是预言机

  • 是一种抽象电脑,用来回答决定性问题
  • 区块链预言机是为链上智能合约提供链下信息的服务
  • 为区块链网络提供直接获取链外的信息的途径

Chainlink

  • 去中心化区块链预言机协议
  • 最初基于Ethereum,现在已经有12+条L1和L2链有原生部署
  • 最主要的应用场景之一是为各类DeFi智能合约提供喂价

智能合约喂价基本原理

  • 预言机节点向Aggregator合约发布价格数据,并获得奖励
  • Aggregator合约从预言机网络定期接受最新数据更新,并将数据聚合并存储上链
  • 终端用户通过Aggregator接口或通过代理合约的Consumer接口使用只读操作检索喂价

Remix演示

创建AggregatorInterface.sol,写入一下代码

pragma solidity ^0.8.0;

interface AggregatorV3Interface {
    /**
     * Returns the decimals to offset on the getLatestPrice call
     */
    function decimals() external view returns (uint8);

    /**
     * Returns the description of the underlying price feed aggregator
     */
    function description() external view returns (string memory);

    /**
     * Returns the version number representing the type of aggregator the proxy points to
     */
    function version() external view returns (uint256);

    /**
     * Returns price data about a specific round
     */
    function getRoundData(uint80 _roundId) external view returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);

    /**
     * Returns price data from the latest round
     */
    function latestRoundData() external view returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
}

以下是喂价地址

#Moonbase Alpha

BTC to USD	
0xCf88A8d7fc1A687895fC8ffAad567f303926B094
ETH to USD	
0x3669da30c33D27A6A579548fCfc345fE5dEdda6e
DOT to USD	
0xA873F6b30aD79fCAF9b03A0A883d6D1f18D661d7
KSM to USD	
0x0C515E77897b2A7181C875c88FaF9BC8E5661E3b
AAVE to USD	
0x64B22D2B8c3CA311a0C2de34bf799f8101c89362
ALGO to USD	
0x9fc3b0BF1156868085AFC1cFf4Bf6D85ea301371
BAND to USD	
0xC5aeD933FEb49794A8Bf2FB0e73D9c958c8a07ba
LINK to USD	
0x446b93236B4d34642732B8dcbeB3cb4f4FA03C70
SUSHI to USD	
0x4a6Cf10C0f5c4D4e7cf7385bFfecDAec0778357C
UNI to USD	
0x326997c21451DaB916F9f01684991B6169dAf3E5

如图,decimals为小数点在answer的位数,即20605.82224916

在这里插入图片描述

因此,使用Chainlink预言机可以实时获取链下数据实现链上链下协同。

HardHat

  • 是一个编译,部署和测试Solidity智能合约和应用的开发环境
  • 自带一个本地EVM虚拟机运行环境 - Hardhat Network,类似于Ganache
  • 可以自定义任务来简化开发工作流程
  • 基于JavaScript,也支持TypeScript
  • 支持各种插件,包括Etherscan插件,Waffle插件,Ganache插件等等

安装Hardhat

# 安装和初始化命令

# 创建目录
mkdir hardhat && cd hardhat

# 初始化Node.js项目
npm init -y

# 安装Hardhat
npm install hardhat

# 创建项目
npx hardhat

# 安装ethers插件
npm install @nomiclabs/hardhat-ethers ethers

# 安装OpenZeppelin依赖
npm install @openzeppelin/contracts

## 发现安装进程卡住不动,可以配置npm代理(设置淘宝镜像)来提高下载速度
npm config set registry https://registry.npm.taobao.org

Hardhat部署和交互ERC-20合约

## 部署合约

# 编译合约
npx hardhat compile

# 部署合约
npx hardhat run --network moonbase scripts/deploy.js

在这里插入图片描述

## 交互合约

# 开启Hardhat Console

npx hardhat console --network moonbase

# 获得合约ABI示例
const MyToken = await ethers.getContractFactory('MyToken');

# 关联到之前部署的合约地址
const mytoken = await MyToken.attach('your-deployed-contract-address');

console.log(mytoken)

await mytoken.name()

await mytoken.totalSupply()

# 查询余额
await mytoken.balanceOf("the-deployer-wallet-address")

# 转账
await mytoken.transfer("recipient-wallet-address", 1000000)

# 查询余额
await mytoken.balanceOf("the-recipient-wallet-address")

在这里插入图片描述
在这里插入图片描述

在Moonbase α的testnet上也能查询到相关Transaction Details

在这里插入图片描述

Remin IDE vs. Hardhat

HardhatRemix IDE
运行模式本线命令行/CLI线上/浏览器
可纳入其他项目依赖YN
包含编辑器NY
支持任务和自动化流程YN
支持单元测试通过Waffle插件有限
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值