首页 > 其他分享 >【truffle】快速入门

【truffle】快速入门

时间:2024-05-26 15:04:29浏览次数:24  
标签:const 入门 快速 await metaCoinInstance amount truffle MetaCoin

安装truffle

npm install -g truffle

创建truffle项目

mkdir truffle-quickstart
truffle init

查看目录结构 

编写合约

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

library ConvertLib{
	function convert(uint amount, uint conversionRate) public pure returns (uint convertedAmount)
	{
		return amount * conversionRate;
	}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import "./ConvertLib.sol";

contract MetaCoin {
	mapping (address => uint) balances;

	event Transfer(address indexed _from, address indexed _to, uint256 _value);

	constructor() {
		balances[tx.origin] = 10000;
	}

	function sendCoin(address receiver, uint amount) public returns(bool sufficient) {
		if (balances[msg.sender] < amount) return false;
		balances[msg.sender] -= amount;
		balances[receiver] += amount;
		emit Transfer(msg.sender, receiver, amount);
		return true;
	}

	function getBalanceInEth(address addr) public view returns(uint){
		return ConvertLib.convert(getBalance(addr),2);
	}

	function getBalance(address addr) public view returns(uint) {
		return balances[addr];
	}
}

 编译合约

truffle compile

编译完后会多一个build目录 

部署合约

部署脚本migrations/1_deploy_contracts.js

const ConvertLib = artifacts.require("ConvertLib");
const MetaCoin = artifacts.require("MetaCoin");

module.exports = function(deployer) {
  // 部署库ConvertLib,然后将ConvertLib链接到合约MetaCoin,然后部署合约MetaCoin
  
  deployer.deploy(ConvertLib);
  // 将已部署的库链接到合约或多个合约
  deployer.link(ConvertLib, MetaCoin);
  deployer.deploy(MetaCoin);
};

 修改配置truffle-config.js

下载ganache,Ganache - Truffle Suite

truffle migrate

测试合约

测试脚本solidity-TestMetaCoin.sol

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

// These files are dynamically created at test time
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/MetaCoin.sol";

contract TestMetaCoin {

  function testInitialBalanceUsingDeployedContract() public {
    MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin());

    uint expected = 10000;

    Assert.equal(meta.getBalance(tx.origin), expected, "Owner should have 10000 MetaCoin initially");
  }

  function testInitialBalanceWithNewMetaCoin() public {
    MetaCoin meta = new MetaCoin();

    uint expected = 10000;

    Assert.equal(meta.getBalance(tx.origin), expected, "Owner should have 10000 MetaCoin initially");
  }

}

测试脚本js类型-metacoin.js

const MetaCoin = artifacts.require("MetaCoin");

contract('MetaCoin', (accounts) => {
  it('should put 10000 MetaCoin in the first account', async () => {
    const metaCoinInstance = await MetaCoin.deployed();
    const balance = await metaCoinInstance.getBalance.call(accounts[0]);

    assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account");
  });
  it('should call a function that depends on a linked library', async () => {
    const metaCoinInstance = await MetaCoin.deployed();
    const metaCoinBalance = (await metaCoinInstance.getBalance.call(accounts[0])).toNumber();
    const metaCoinEthBalance = (await metaCoinInstance.getBalanceInEth.call(accounts[0])).toNumber();

    assert.equal(metaCoinEthBalance, 2 * metaCoinBalance, 'Library function returned unexpected function, linkage may be broken');
  });
  it('should send coin correctly', async () => {
    const metaCoinInstance = await MetaCoin.deployed();

    // Setup 2 accounts.
    const accountOne = accounts[0];
    const accountTwo = accounts[1];

    // Get initial balances of first and second account.
    const accountOneStartingBalance = (await metaCoinInstance.getBalance.call(accountOne)).toNumber();
    const accountTwoStartingBalance = (await metaCoinInstance.getBalance.call(accountTwo)).toNumber();

    // Make transaction from first account to second.
    const amount = 10;
    await metaCoinInstance.sendCoin(accountTwo, amount, { from: accountOne });

    // Get balances of first and second account after the transactions.
    const accountOneEndingBalance = (await metaCoinInstance.getBalance.call(accountOne)).toNumber();
    const accountTwoEndingBalance = (await metaCoinInstance.getBalance.call(accountTwo)).toNumber();

    assert.equal(accountOneEndingBalance, accountOneStartingBalance - amount, "Amount wasn't correctly taken from the sender");
    assert.equal(accountTwoEndingBalance, accountTwoStartingBalance + amount, "Amount wasn't correctly sent to the receiver");
  });
});
truffle test

标签:const,入门,快速,await,metaCoinInstance,amount,truffle,MetaCoin
From: https://blog.csdn.net/qq2942713658/article/details/139213596

相关文章

  • ctfshow web入门之web259
    web259题目描述1.题目源码很短:<?phphighlight_file(__FILE__);$vip=unserialize($_GET['vip']);//vipcangetflagonekey$vip->getFlag();2.题目在提示给出了flag.php的内容:$xff=explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);array......
  • 指针的入门
    听课笔记#include<stdio.h>intmain(){ intnum=1; //定义了一个指针变量,指针 //说明 //1:int*表示类型为指针类型 //2:名称ptr,ptr就是一个int*类型 //3:ptr指向了一个int类型的变量的地址 int*ptr=&num; //num的地址是多少 //说明1:如果要输出一个变量的地址,使用......
  • 剪辑赚钱靠谱吗?剪辑赚钱软件、自媒体怎么入门、自媒体如何赚钱、写作赚钱的平台,软件。
     我试过8种,现在还在搞的只有2个。副业收入每月5k+,算下来平均每天100多,好的时候一天能赚两三百。但是现在搞副业,坑多水深的,太多人栽跟头了,丢几百块都能算少的。我摸索了半年,被骗了2k 元子才开始走对路子。现在收益稳定,而且所有的投入都回本了。今天分享的都是我亲测......
  • Ae 从入门到精通之九:表达式
    与使用关键帧相比,Ae的表达式 Expression也用于控制属性值。只不过使用表达式能简化操作,大大提高工作效率,使复杂动画的制作变得更轻松。◆  ◆  ◆表达式语法基础表达式基于标准的JavaScript语言。如果熟悉脚本语言编程,在Ae中使用表达式会更加容易。不过,不会J......
  • 【QGIS入门实战精品教程】10.7: 基于DEM的地形因子分析(坡度、坡向、粗糙度、山体阴影、
    文章目录一、加载dem二、山体阴影三、坡度四、坡向五、地形耐用指数六、地形位置指数七、地表粗糙度一、加载dem二、山体阴影方法一:符号系统利用符号系统中的山体阴影,渲染出阴影效果。方法二:山体阴影工具该算法计算输入中的数字化地形模型的山体阴......
  • QShop商城-快速开始-后端
    QShop商城-快速开始-后端下载代码QShop目前默认SDK版本为.Net6,推荐使用此版本(默认使用VS2022开发)下载地址为:https://gitee.com/qiushuochina/QShop目录说明后端代码结构初始化数据库用MySql创建一个数据库,例如:qshop使用8_数据库脚本内的MySql初始化初始化数......
  • MySQL入门——增删查改(下)
    数据库约束约束类型NOTNULL-指示某列不能存储NULL值。UNIQUE-保证某列的每行必须有唯一的值。DEFAULT-规定没有给列赋值时的默认值。PRIMARYKEY-NOTNULL和UNIQUE的结合。确保某列(或两个列多个列的结合)有唯一标识,有助于更容易更快速地找到表中的一个特定的......
  • git 入门学习(持续学习ing)
    目录为什么要学习使用Git?什么是Git?Git的下载和安装1.进入Git官网,并跳转到Windows版本下载界面2.下载64位Windows操作系统(也可以根据自己电脑实际信息进行选择)3.下载完成后,双击下载程序,一直点击下一步即可完成安装(一路回车法)4.安装完成后,在桌面空白处点击右键,会发现多了两个菜......
  • springcloudalibaba入门(一)
    1、父工程pom文件<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apac......
  • Python小白必备!清华大牛整理的《Django零基础入门到精通》手册
    Django是Python社区的两大最受欢迎的Web框架之一(另一个是Flask)。凭借功能强大的脚手架和诸多开箱即用的组件,可以使你能够以最小的代价构建和维护高质量的Web应用。从好的方面来看,Web开发激动人心且富于创造性;从另一面来看,它却是份繁琐而令人生厌的工作。通过减少重复......