智能合约自学笔记一之初始化

叫嚣着学只能合约好几个月了,不管怎么样今天必须把简单东西过一遍,军令状!!!!
这种贴代码的我实在是不想贴在我的笔记上,就放在CSDN上吧。
分为三部分,首先是创建一个智能合约,这个合约只有个名字
智能合约用的就是solidity

// the file is for creating smart contract
pragma solidity ^0.5.0;

contract Marketplace {
    string public name;

    constructor() public {
        name = "Dapp University Marketplace";
    }
}

接着要把合约部署到ganache中,也就是上链,当然,上链只能有一次,这应该知道吧。这里用的就是js了

//the file is for deploy smart contract to ganache
const Marketplace = artifacts.require("Marketplace");

module.exports = function(deployer){
	deployer.deploy(Marketplace );
};

最后尝试着调用这个合约。调用的时候要检查这个合约的地址,因为我们的ganache是通过网络挂在PC上的,还检查了这个合约的名字,这里也是JS

//it's for calling smartcontract
const Marketplace = artifacts.require('./Marketplace.sol') //import the contract into the test file, of course the smartcontact is defined in this file

contract('Marketplace', (accounts) =>{
	let marketplace

	before(async () => {
		marketplace = await Marketplace.deployed()//deployed这个函数是异步的,await关键字为了将异步变为同步
	}) 

	describe('deployment', async() => { //mocha which is a framework of js
		it('deploys successfully', async()=> {
			const address = await marketplace.address
			assert.notEqual(address, 0x0) //chai for assert
			assert.notEqual(address, null)
			assert.notEqual(address, '')
			assert.notEqual(address, undefined)
		})

		it('has a name', async() => {
			const name = await marketplace.name()
			assert.equal(name, 'Dapp University Marketplace')
		})

	})
})

注意这个await关键字。因为合约的部署是异步的,这很好理解,上链的什么东西都很慢的,异步肯定是最好的,但是我还想等结果怎么办,await,我理解这个await就是让异步看起来像同步
有没有巨简单

这里有个问题,调用addres应该是成员变量,或者是隐藏的成员,所以是marketplace.address,但是在调用name的时候为什么是函数的形式?明明合约中只有一个构造函数。难道带()才是成员变量吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tux~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值