本文将介绍Solidity开发中的两种工具:Remix IDE和Hardhat
进入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
创建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预言机可以实时获取链下数据实现链上链下协同。
# 安装和初始化命令
# 创建目录
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
## 部署合约
# 编译合约
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
Hardhat | Remix IDE | |
---|---|---|
运行模式 | 本线命令行/CLI | 线上/浏览器 |
可纳入其他项目依赖 | Y | N |
包含编辑器 | N | Y |
支持任务和自动化流程 | Y | N |
支持单元测试 | 通过Waffle插件 | 有限 |