以太坊数据上链和展示智能合约

以太坊数据上链和展示智能合约,第1张

以太坊数据上链和展示智能合约 1. 首先定义上链数据的基本结构(推荐使用结构体) 2. 定义全局变量和判定的一些变量 3. 定义接受数据和展示数据的函数 4. 将数据传入,使用memory定义接受变量,保证数据上链 5. 展示函数将展示数据直接返回
pragma solidity ^0.4.13;



contract TimeProject {
	enum ProjectStatus { Open, Examing, Finshed }
	//enum ProductCondition { New, Used }

	uint public projectIndex;
	mapping (address => mapping(uint => Project)) examer;
	mapping (uint => address) projectIdInStore;
	
	
	//项目信息结构
	struct Project {
		uint id;
		string name;
		string category;
		//string imageLink;
		string when;
		string where;
		string description;
		uint price;
		uint demandNum;
		
		ProjectStatus status;
		//ProductCondition condition;
		
	}
	


	constructor() public {
		projectIndex = 0;
	}
	//添加项目信息上链
	function addProject(string _name, string _category,string _when, string _where, string _description, uint _price, uint _demandNum) public {
		require(_price>0,"The price is too low!");
		projectIndex += 1;
		Project memory project = Project(projectIndex, _name, _category, _when, _where, _description,_price,_demandNum, ProjectStatus.Open);
		examer[msg.sender][projectIndex] = project;
		projectIdInStore[projectIndex] = msg.sender;
	}
	//查询展示链上项目信息
	function getProject(uint _projectId) view public returns (uint, string, string, string, string, string, uint, uint, ProjectStatus) {
		Project memory project = examer[projectIdInStore[_projectId]][_projectId];
		return (project.id, project.name, project.category, project.when, project.where, project.description, project.price, project.demandNum, project.status);
	}
	

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/928001.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-17
下一篇 2022-05-17

发表评论

登录后才能评论

评论列表(0条)

保存